Android learning part 5 - Button click many ways

 JAVA Part : 


package first.learn.buttonclick;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btnOne, btnThree ;
TextView tvOne ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnOne = (Button) findViewById(R.id.btnOne);
btnThree = (Button)findViewById(R.id.btnThree);

btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTextOnTextView("This is Button One");
}
});

tvOne = (TextView) findViewById(R.id.tvOne);

Button btnTwo = (Button) findViewById(R.id.btnTwo);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);



Button btnFour = (Button) findViewById(R.id.btnFour);
btnFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTextOnTextView("This is Button Four");
changeColorOfButtonOne();
}
});


}


void changeColorOfButtonOne (){
btnOne.setTextColor(Color.RED);
}

void setTextOnTextView ( String text ){

tvOne.setText(text);

btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

btnOne.setTextColor(Color.WHITE);
setTextOnTextView("This is Button One");
}
});

}


@Override
public void onClick(View v) {

int id = v.getId();

if(id == R.id.btnTwo){
setTextOnTextView( "this is Clicked Button Two ");
}

if (id == R.id.btnThree){
setTextOnTextView("This is Button Three");
}



}

public void btnFiveClick(View view) {
setTextOnTextView ("Button Five Click");
}
}


XML part : 


<?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"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:id="@+id/tvOne"
android:layout_margin="20dp"
android:textSize="25dp"
android:textColor="@color/purple_700"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
/>

<Button
android:id="@+id/btnOne"
android:layout_margin="20dp"
android:text="Button One"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>


<Button
android:id="@+id/btnTwo"
android:layout_margin="20dp"
android:text="Button Two"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>


<Button
android:id="@+id/btnThree"
android:layout_margin="20dp"
android:text="Button Three"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



<Button
android:id="@+id/btnFour"
android:layout_margin="20dp"
android:text="Button Four"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



<Button
android:id="@+id/btnFive"
android:layout_margin="20dp"
android:text="Button Five"
android:onClick="btnFiveClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>




No comments:

Post a Comment