Can anybody tell how to add a button in android?
In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it.
Add a Button In order to do this add an XML Button element to the XML file. You need to specify the button's layout width and height. Choose "wrap_content", which means the view will be sized to exactly match the button's required content size. Also specify a layout margin and the button's text.
Check this Android Button tutorial; this simple example creates a Close Button.
All you need to do is:
1.Add Button widget to your Layout
<Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/title_close" />
2.Attach a setOnClickListener method to the button instance:
protected void onCreate(Bundle savedInstanceState) { this.setContentView(R.layout.layoutxml); this.closeButton = (Button)this.findViewById(R.id.close); this.closeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With