In this post I will show the basic function of a Button, whenever a button is pressed a text message will be displayed.
Step1- First Create a Android Project and name it as Button.
Step2 - Go to res folder and then to layout and in .xml file copy the code given below
Step 3- Then go the /scr directory and in the MainActivity.java copy the code shown below.
Now upload it to your emulator or phone and you can see whenever the the button is clicked the message is displayed in the Textview.
The link for the code - https://www.dropbox.com/s/oig3o79z37648pa/Button.zip
Step1- First Create a Android Project and name it as Button.
Step2 - Go to res folder and then to layout and in .xml file copy the code given below
1: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2: xmlns:tools="http://schemas.android.com/tools"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: android:paddingBottom="@dimen/activity_vertical_margin"
6: android:paddingLeft="@dimen/activity_horizontal_margin"
7: android:paddingRight="@dimen/activity_horizontal_margin"
8: android:paddingTop="@dimen/activity_vertical_margin"
9: tools:context=".MainActivity" >
10: <Button
11: android:id="@+id/button1"
12: android:layout_width="wrap_content"
13: android:layout_height="wrap_content"
14: android:layout_alignParentBottom="true"
15: android:layout_marginBottom="179dp"
16: android:layout_marginLeft="46dp"
17: android:layout_toRightOf="@+id/textView1"
18: android:text="Button" />
19: <TextView
20: android:id="@+id/textView2"
21: android:layout_width="wrap_content"
22: android:layout_height="wrap_content"
23: android:layout_alignTop="@+id/button1"
24: android:layout_centerHorizontal="true"
25: android:layout_marginTop="108dp"
26: android:text="TextView" />
27: </RelativeLayout>
Step 3- Then go the /scr directory and in the MainActivity.java copy the code shown below.
1: package com.example.button;
2: import android.app.Activity;
3: import android.os.Bundle;
4: import android.view.View;
5: import android.widget.Button;
6: import android.widget.TextView;
7: public class MainActivity extends Activity {
8: Button Btn;
9: TextView tv;
10: @Override
11: protected void onCreate(Bundle savedInstanceState) {
12: super.onCreate(savedInstanceState);
13: setContentView(R.layout.activity_main);
14: Btn =(Button) findViewById(R.id.button1);
15: tv = (TextView) findViewById(R.id.textView2);
16: Btn.setOnClickListener(new View.OnClickListener() {
17: @Override
18: public void onClick(View v) {
19: tv.setText("Welcome to android world");
20: }
21: });
22: }
23: }
Now upload it to your emulator or phone and you can see whenever the the button is clicked the message is displayed in the Textview.
The link for the code - https://www.dropbox.com/s/oig3o79z37648pa/Button.zip
No comments:
Post a Comment