Question

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.

enter image description here

Was it helpful?

Solution

This is how i would do it:

1.) Store the currently edited EditText in your adapter class, plus implement a View.OnFocusChangeListener for your EditTexts. This can detect if edit mode is entered/exited.

Add this to your adapter class:

private EditText current = null;

public EditText getCurrentEditText() {return current;}

And set this OnFocusChangeListener to all your editTexts:

View.OnFocusChangeListener listener = new View.OnFocusChangeListener()
{
    @Override
    public void onFocusChange(View view, boolean hasFocus)
    {
        if(hasFocus)
            current = (EditText) view;
    }
};

And then add it to your TextViews in the getView() method of your adapter class like:

editText.setOnFocusChangeListener(listener);

2.) Now in your Buttons OnClickListener you can access the currently edited EditText. For example for button '5':

private View.OnClickListener click = new View.OnClickListener()
{
    public void onClick(View v)
    {
        Integer value = (Integer) v.getTag();    

        EditText current = adapter.getCurrentEditText();
        if(current != null) 
            current.setText(current.getText().toString()+value);
    }
}

EDIT:

It may have been a bit confusing, how these things all come together.

In your Activity, you probably create an instance from your Adapter class. The Adapter we just created now has a method called getCurrentEditText() what will return you the reference to the currently edited EditText. So you don't need to have the references of all your EditTexts in your activity class, you can access them through your Adapter.

So in your Activity you do something like this:

Create an instance of your adapter.

adapter = new TextAdapter(this);

Setup the buttons:

button1 = (Button) findViewById(...);
button2 = (Button) findViewById(...);
button3 = (Button) findViewById(...);
...

To distinguish your buttons, you can do this:

button1.setTag(1);
button2.setTag(2);
button3.setTag(3);
...

Then you can simply add the OnClickListener, we just created to all your buttons.

button1.setOnClickListener(click);
button2.setOnClickListener(click);
button3.setOnClickListener(click);
...

Note that the buttons can see the adapter, because they are all members of your Activity, and the Adapter can return the current EditText.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top