3

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

Spike
  • 147
  • 3
  • 17

1 Answers1

2

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.

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • Thank you very much for your response. I request you to edit as editText.setOnFocusListener(listner) in the getView method, instead of textView. I got one more doubt while implementing this i.e., how to define onClick method in the adapter class, becuase I have created buttons in xml(main.xml) layout folder. So I got their reference by using findViewById method in the adapter class. But somehow it is always showing error. Am I doing anything wrong in this? A code snippet would be appreciated. Please suggest. Thanks in advance. – Spike Aug 30 '12 at 04:25
  • Hi, and you are wellcome :)! I changed the textView, i allways mix them accidentally. You don't need to implement the onClick() in the adapter class. I your Activity, you create an instance of your adapter class, and you find your Buttons by id from xml, correct? If so, then you can implement an OnClickListener there, and set that to your buttons, after you set them up by findViewById(). We previously added a custom method to your adapter class what can return the active EditText. I know, its confusing a bit, i try to update my answer. – Balázs Édes Aug 30 '12 at 10:46
  • Hey I edited my post with the activity program. When I run this application, I am getting display, but when I click the buttons, it is showing like"The application has stopped unexpectedly". I am getting null object in the current when I added sop to check in logcat. I think the reason is I disabled softkeypad in the TextAdapter class. So, it disables focus of the edittext too I guess. So the focus listener event doesn't fire, so the current contains null object everytime. Please suggest. – Spike Aug 30 '12 at 13:25
  • Hi bali, can you please reply? Strange I didn't get even a single response from any other one. I thought I might face this problem when using custom keypad, because a lot of members complained about this hiding soft keypad.and I revised all resources and even stackoverflow, but no use. But I want to use my own keypad in my application. – Spike Aug 30 '12 at 14:40
  • Hi! could you please post the stack trace? Now you got me curious, so i'll try to recreate the problem :) – Balázs Édes Aug 30 '12 at 17:58
  • I think i found the problem. Dont create the TextAdapter object where you declare it. Just put TextAdapter adapter; befor the onCreate() and in the onCreate() function do adapter = new TextAdapter(this); – Balázs Édes Aug 30 '12 at 18:05
  • No Bali, It didn't solve the problem. Because the problem is not with TextAdapter object, it is with current object. Because I am getting null object when I print the object. Attached are the screen shots of the logcat in my question. – Spike Aug 31 '12 at 03:33
  • Hey, I got the solution. Just I changed that method to static and called it with class name instead of object reference. – Spike Sep 01 '12 at 05:07
  • +1 for your help. The reason why I am giving now is, now I got free time somewhat. So just revising my questions once again and accepting the answers that helped solve my problems. – Spike Sep 17 '12 at 10:16