Hi I have created 6x6 edittexts (which cover 70% of the screen)in my application using gridview. So I used Adapter class to put that 36 edittexts in the gridview. Below that I have arranged 10 buttons(which cover remaining 30% of the screen) named as 1-9 digits & C in order to create customized keypad. So that I can enter 1-9 digits in the edittexts(I also disabled the softkeypad). But my doubt is how to get the rowid of the edittext in which the cursor is present and in that edittext, my number( on which(buttons 1-9) I press) should be taken as input. At the end of the time, I have to retrieve all the values. So, my question is about the ids of editexts and number placing. Any help would be greatly appreciated. Thanks in advance. Below is my code.
public class TextAdapter extends BaseAdapter {
private Context mContext;
int count=36;
int k=0;
public TextAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
final EditText editText;
if (convertView == null) { // if it's not recycled, initialize some attributes
editText = new EditText(mContext);
editText.setLayoutParams(new GridView.LayoutParams(54, 53));
editText.setBackgroundResource(R.drawable.edittextshape);
editText.setGravity(Gravity.CENTER);
editText.setId(k);
k++;
editText.setFilters( new InputFilter[] { new InputFilter.LengthFilter(1)});
editText.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = editText.getInputType(); // backup the input type
editText.setInputType(InputType.TYPE_NULL); // disable soft
editText.onTouchEvent(event); // call native handler
editText.setInputType(inType); // restore input type
return true; // consume touch even
}
});
// editText.setScaleType(EditText.ScaleType.CENTER_CROP);
editText.setPadding(0, 0, 0, 0);
} else {
editText = (EditText) convertView;
}
editText.setText(" ");
return editText;
}
}
Activity Program:
public class MainActivity extends Activity implements OnClickListener{
GridView gridView;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b10;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_two_frame_layout);
FrameLayout fL1 = (FrameLayout) findViewById(R.id.fLayout1);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setVerticalScrollBarEnabled(false);
gridView.setAdapter(new TextAdapter(this));
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int gridSize = display.getWidth();
int colWidth = (gridSize / 9);
gridView.setColumnWidth(colWidth);
gridView.setNumColumns(9);
b1=(Button)findViewById(R.id.keypad_1);
b2=(Button)findViewById(R.id.keypad_2);
b3=(Button)findViewById(R.id.keypad_3);
b4=(Button)findViewById(R.id.keypad_4);
b5=(Button)findViewById(R.id.keypad_5);
b6=(Button)findViewById(R.id.keypad_6);
b7=(Button)findViewById(R.id.keypad_7);
b8=(Button)findViewById(R.id.keypad_8);
b9=(Button)findViewById(R.id.keypad_9);
b10=(Button)findViewById(R.id.keypad_10);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b10.setOnClickListener(this);
}
TextAdapter textadapter=new TextAdapter(this);
public void onClick(View v)
{
EditText current = textadapter.getCurrentEditText();
System.out.println(textadapter);
System.out.println(current);
switch (v.getId())
{
case R.id.keypad_1:
current.setText("1");
break;
case R.id.keypad_2:
current.setText(current.getText().toString()+'2');
break;
case R.id.keypad_3:
if(current != null)
current.setText(current.getText().toString()+'3');
break;
case R.id.keypad_4:
if(current != null)
current.setText(current.getText().toString()+'4');
break;
case R.id.keypad_5:
if(current != null)
current.setText(current.getText().toString()+'5');
break;
case R.id.keypad_6:
if(current != null)
current.setText(current.getText().toString()+'6');
break;
case R.id.keypad_7:
if(current != null)
current.setText(current.getText().toString()+'7');
break;
case R.id.keypad_8:
if(current != null)
current.setText(current.getText().toString()+'8');
break;
case R.id.keypad_9:
if(current != null)
current.setText(current.getText().toString()+'9');
break;
case R.id.keypad_10:
if(current != null)
current.setText("");
break;
}
}
}
Below is the logcat shot.