Android Tutorial Button

Android Button is an android widget that can be pressed to perform some action.
In this android tutorial we are using a simple button example to help you understand how to use android button.

Our android button example uses a TextView, EditText and a Button. Drag and drop them from the palette in your design view.
The user enters data in the EditText field and when he clicks the button the text is displayed in the TextView. In the design view of the .xml file select the button and in properties scroll down to the onClick property and choose the method which you want to get called when the button is pressed. Or use the android:onClick=" " attribute.

Here is the .xml file code of our Android button example.
 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:showIn="@layout/activity_main">
 
 <EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"
         android:id="@+id/eText" 
 android:layout_marginTop="64dp" 
 android:layout_alignParentTop="true" 
 android:layout_alignParentStart="true" />
 
<Button 
 style="?android:attr/buttonStyleSmall" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Button" 
 android:id="@+id/button" 
 android:onClick="onButtonClick" 
 android:layout_below="@+id/eText" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="52dp" />

 <TextView 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Large Text" 
 android:id="@+id/textView" 
 android:layout_centerVertical="true" 
 android:layout_alignParentStart="true" />
</RelativeLayout>
 
Android button
 
 
Java code of our button example
 
 public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           }
    public void onButtonClick(View view){
        EditText eText = (EditText)findViewById(R.id.eText);
        String userText = eText.getText().toString();
        TextView displayEnteredText = (TextView)findViewById(R.id.textView);
        displayEnteredText.setText(userText);

    }
} 

So this was a simple android button example you can also use OnClickListener instead of using 
the XML android:onClick attribute.
 
If you want to use OnClickListener on button then see this tutorial. 
Android Tutorial  OnClickListener

0 comments: