Text Change On Button Click Using Android Studio



In this tutorial we are going to learn how to change the text on button click. To learn this follow the steps.
  1. Open your Android Studio
  2. Click on new Project and select an Empty Activity.Design your XML activity by Using the code given below.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="100sp"
android:gravity="center"
android:text="Hello World"
android:layout_marginTop="200dp"
android:paddingLeft="60dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="30dp"
android:textAlignment="center"
android:layout_gravity="center"
android:textAllCaps="true"
android:textStyle="bold|italic"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="239dp" />
<Button
android:id="@+id/btnChange"
android:layout_width="170dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginTop="10dp"
android:background="@drawable/button_background"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>

After designing your layout,it's time to design another xml file for the button shape.
To do this go to your draw-able folder, right click on it,goto new option and select 
"Drawable resource file"
and paste the code.

button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#47A891"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
Our design is ready,it's time to write some java code.Goto your mainActivity.java
  1. Define a textView and Button
  2. do casting of these
write code for changing the text of textView on button click.

Mainactivity.java
package com.example.sohailmustafa.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button btnChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
btnChange=(Button)findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText("Hello Android");
}
});
}
}

If you have any difficulty in this lecture don't hesitate to ask.God bless you all,
have a good day.

Please like and comment to make the things better for you.

Comments