Saturday, 21 July 2012

Android Textbox Tutorial


Introduction of Android Textbox Control



Android provides textbox Control for user interface. Textbox control user can enter information and submit. It is like webpage textbox that can be used multiple purposes.

How to define textbox control

Android use seperate file for layout. This file stored in android res/layout directory.

syntex :
 <TextView
        android:id="@+id/txtname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show text" />        
 
Above syntex define textbox with following properties
  • android:id a unique id provided.
  • android:layout_width this property is used to define width.
  • android:layout_heightthis property is used to define height.
set OnClickListener for button. We need to set it manually.
How to set OnClickListener to button
  • First identify your button.
     Syntex :
     
     button = (Button) findViewById(R.id.button1);
     
  • Then set event to this button control
     button.setOnClickListener(new OnClickListener() {
     
    			@Override
    			public void onClick(View arg0) {
                    write your code
    			}
     
    		});
     
  • Then get textbox content and display it
     TextView txtname = (TextView) findViewById(R.id.txtname);
     String name=txtname.getText().toString()
     Toast.makeText(this,name,Toast.LENGTH_LONG).show();
     
Example
  1. Make layout file(main.xml) which contains a button. If we click on button it display a message.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
     
     <TextView
            android:id="@+id/txtname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
     <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="show text" />  
     
    </LinearLayout>
    
  2. Then apply onclick event

    public class MyAndroidAppActivity extends Activity {
     
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
     
    		addListenerOnButton();
     
    	}
     
    	public void addListenerOnButton() {
     
    		Button button = (Button) findViewById(R.id.button1);
     
    		button.setOnClickListener(new OnClickListener() {
     
    			@Override
    			public void onClick(View arg0) {
     
                 TextView txtname = (TextView) findViewById(R.id.txtname);
                 String name=txtname.getText().toString()
                 Toast.makeText(this,name,Toast.LENGTH_LONG).show();
    			}
     
    		});
     
    	}
     
    }
    
    
  3. Output : 


     

No comments:

Post a Comment