EditText Customization

In my application , I am dynamically generating a linearlayout that contains an edittext and some Buttons .

I want that when the user presser the 'enter' on the keyboard of device the keyboard should hide

I tried it by setting the input type of the edittext , But by this when the user presses "enter" it moves onto another edittext .

How can i do that??

package com.integrated.mpr;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class Page1 extends Activity implements OnClickListener{


    static String partname;
    int pos = StaticError.n;
    int ns= StaticError.ns;
    String s = StaticError.s;
    int[][] id = new int[pos][6];
    int submit ;

    double timedataa[] = {0,0,0,0,0};
    double timedatab[] = {0,0,0,0,0};
    double timedatac[] = {0,0,0,0,0};
    double timedata[] = {0,0,0,0,0};

    double rawdataa[] = new double[22050];
    double rawdatab[] = new double[22050];
    double rawdatac[] = new double[22050];
    double rawdata[] = new double[22050];


    FeatureExtract fe = new FeatureExtract();
    WavToDat wtd = new WavToDat();
    int i;


    int a=0;
    int b=0;
    int c=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        int p =0;
        Log.d("value of ", ""+pos);
        //Creating Different ids for the elements for different layouts
        for(int i =0;i<pos;i++){
            for(int j =0;j<6;j++){
                id[i][j] = p;
                p++;
            }
        }

        ScrollView sv = new ScrollView(this);



        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        int resid = getResources().getIdentifier("background", "drawable", getPackageName());
        ll.setBackgroundResource(resid);

        Button ins = new Button(this);
        ins.setText("Instructions");
        ins.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        ins.setGravity(Gravity.RIGHT);
        ins.setId(5000);
        ins.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Context mContext = Page1.this;
                Dialog dialog = new Dialog(mContext);
                dialog.setTitle("Instructions");
                dialog.setContentView(R.layout.instructiondialog);
                dialog.show();
            }
        });

        ins.setTextColor(Color.BLUE);
        ins.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        ins.setTypeface(Typeface.SERIF);
        ll.addView(ins);


        for(i=0;i<pos;i++){



            LinearLayout llay = new LinearLayout(this);
            llay.setOrientation(LinearLayout.VERTICAL);
            llay.setId(id[i][5]);

            //EDIT TEXT
            EditText et = new EditText(this);
            et.setId(id[i][0]);
            et.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            et.setTypeface(Typeface.SERIF);



            LinearLayout lhor = new LinearLayout(this);
            lhor.setOrientation(LinearLayout.HORIZONTAL);
            lhor.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            lhor.setWeightSum(90);


            Button starta = new Button(this);
            starta.setText("Record 1");
            starta.setId(id[i][1]);
            starta.setOnClickListener(this);


            Button startb = new Button(this);
            startb.setText("Record 2");
            startb.setId(id[i][2]);

            startb.setOnClickListener(this);


            Button startc = new Button(this);
            startc.setText("Record 3");
            startc.setId(id[i][3]);

            startc.setOnClickListener(this);

            lhor.addView(starta);
            lhor.addView(startb);
            lhor.addView(startc);

            Button stop = new Button(this);
            stop.setText("Submit");
            stop.setId(id[i][4]);

            stop.setOnClickListener(this);


            TextView tv1 = new TextView(this);
            tv1.setVisibility(llay.INVISIBLE);

            llay.addView(tv);
            llay.addView(et);
            llay.addView(lhor);
            llay.addView(stop);
            llay.addView(tv1);
            ll.addView(llay);

        }



        sv.addView(ll);
        this.setContentView(sv);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub





        }





}

Answers


Try the following:

final EditText et = new EditText(this);
        et.setId(id[i][0]);
        et.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        et.setTypeface(Typeface.SERIF);

et.setOnKeyListener(new View.OnKeyListener()
        {

            @Override
            public boolean onKey(View editView, int keyCode, KeyEvent event)
            {
                Context mContext = Page1.this;
                if( keyCode == KeyEvent.KEYCODE_ENTER ){
                    et.clearFocus();
                    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(et.getWindowToken(),0);
                    return true;
                }
                return false;
            }
        })

Need Your Help

How to make Wxpython NewWindow appear to the Top

python wxpython

When right key pressed down, Popup Menu for 'edit' or 'delete' listctrl Item

Ruby array, removing everything after > within subarrays

ruby arrays

What I'm trying to do is a little more complex then what my questions asks but I can't put into words what it is I want to do and my terminology is probably wrong. Sorry. If I can edit the title of...