Pages

Thursday 25 July 2013

How to fetch the value from the text box in android


In your XML:

            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:hint="Fetch the Value"
        />
         
            <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:text="Click" />

(@+id/username)This is the id of the text box. Read this fully to declare id in XML layout.


In your Activity:

//To declare your EditText
   EditText fetch;
   String fetchValue;
   Button clickBtn;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
     
 //binding editbox id with your activity
         fetch=(EditText)findViewById(R.id.username);
//to get the value from the textbox
         fetchValue=fetch.getText().toString();

      clickBtn=(Button)findViewById(R.id.button1);

clickBtn.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
// TODO Auto-generated method stub
 Toast.makeText(getApplicationContext(),fetchValue, Toast.LENGTH_LONG).show();
   }
});



}
Read more about Toast.

No comments:

Post a Comment