unknown os platform
Android code: Change button background color and text color on click | Whats-online.info
whats-online-info

Android code: Change button background color and text color on click


Android
2016:06:12 / Science and Tutorials

How to create an android application with responsive button on click that is change the button background color and text color on click.





Change button background color and text color on click


How can I change a button background color and text color on my android application?

What are the advantages of creating a responsive button on click?




Severally times, app developers tend to build Android applications with buttons unresponsive on click.

On this tutorial, I will show you step by step how to Change button background color and text color on click.

 The advantages of creating responsive buttons on click.


First, we will create a MainActivity.XML file to style up the button as shown below.


  

 <RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">             android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Change Background Color"         android:textSize="20dp"         android:background="#ff0000"         android:padding="10dp"         android:id="@+id/button"         android:layout_marginTop="77dp"         android:layout_alignParentTop="true"         android:layout_centerHorizontal="true" />             android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Change Text Color"         android:textSize="20dp"         android:background="#0000ff"         android:padding="10dp"         android:id="@+id/button2"         android:layout_centerVertical="true"         android:layout_alignLeft="@+id/button"         android:layout_alignStart="@+id/button" />          </RelativeLayout>



Then on the MainActivity.java

We call our two buttons as shown below.


//Our project package name


package com.whatsonline.changebuttonbackgroundcolorandroid;


//Then Import already existing libraries(in build functions) for Color,KeyEvent,Button as shown below. Also you can import libraries by "Hold down ALT+ENTER"

import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { //Intialize our button as shown, btn and btn2      Button btn,btn2;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main); //On our create method  , call our buttons ID on XML as defined below          btn=(Button) findViewById(R.id.button);         btn2=(Button) findViewById(R.id.button2);  //TouchListener listens for an event, if user performs an action, either on touch or on release as shown below         //Change the button background color from red to blue on touch and  blue to red on release                 btn.setOnTouchListener(new View.OnTouchListener() {             @Override             public boolean onTouch(View v, MotionEvent event) {                 // TODO Auto-generated method stub                 if (event.getAction() == KeyEvent.ACTION_DOWN) {                     btn.setBackgroundColor(Color.BLUE);                 } else if (event.getAction() == KeyEvent.ACTION_UP) {                     btn.setBackgroundColor(Color.RED);                 }                 return false;             }         });         //Change the button background color from black to red on touch and  red to black on release          btn2.setOnTouchListener(new View.OnTouchListener() {             @Override             public boolean onTouch(View v, MotionEvent event) {                 // TODO Auto-generated method stub                 if (event.getAction() == KeyEvent.ACTION_DOWN) {                     btn2.setTextColor(Color.RED);                 } else if (event.getAction() == KeyEvent.ACTION_UP) {                     btn2.setTextColor(Color.BLACK);                 }                 return false;             }         });     }     @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.menu_main, menu);         return true;     }     @Override     public boolean onOptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button, so long         // as you specify a parent activity in AndroidManifest.xml.         int id = item.getItemId();         //noinspection SimplifiableIfStatement         if (id == R.id.action_settings) {             return true;         }         return super.onOptionsItemSelected(item);     } }



Also learn how to create a custom button with an image as background. Then button background image changes on click or touch.



 







 












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment