Android - Button
Android - Button
Android - Button
A Button is a Push-button which can be pressed, or clicked, by the user to perform an action.
Button Attributes
Following are the important attributes related to Button control. You can check Android official documentation for
complete list of attributes and related methods which you can use to change these attributes are run time.
/
Sr.No Attribute & Description
1
android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects some common
spelling errors.
2
android:drawableBottom
This is the drawable to be drawn below the text.
3
android:drawableRight
4
android:editable
5
android:text
Attribute Description
1
android:background
2
android:contentDescription
3
android:id
This supplies an identifier name for this view.
4
android:onClick
This is the name of the method in this View's context to invoke when the view is
clicked.
5
android:visibility
Example
/
This example will take you through simple steps to show how to create your own Android application using Linear
Layout and Button.
Step Description
You will use Android studio IDE to create an Android application and name it as myapplication under a package
1
com.example.saira_000.myapplication as explained in the Hello World Example chapter.
4 No need to declare default string constants at string.xml, Android studio takes care of default string constants.
5 Run the application to launch Android emulator and verify the result of the changes done in the application.
Following is the content of the modified main activity file src/MainActivity.java. This file can include each of the
fundamental lifecycle methods.
package com.example.saira_000.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"YOUR MESSAGE",Toast.LENGTH_LONG).show();
}
});
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Control"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/imageButton"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_alignTop="@+id/editText"
android:layout_alignLeft="@+id/textView1"
android:layout_alignStart="@+id/textView1"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
</RelativeLayout>
/
Following will be the content of res/values/strings.xml to define these new constants −
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guidemo4.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your GUIDemo4 application. I assume you had created your AVD while doing environment setup.
To run the app from Android Studio, open one of your project's activity files and click Run icon from the
toolbar.Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and
application, it will display following Emulator window −
/
The following screen will appear by clicking on Button −
Exercise
/
I will recommend to try above example with different attributes of Button in Layout XML file as well at
programming time to have different look and feel of the Button. Try to make it editable, change to font color, font
family, width, textSize etc and see the result. You can also try above example with multiple Button controls in one
activity.