Change button upper text programmatically android

Change button name text on another button click event using setTextColor(color_pass) function.

Button above text are called as button name text and it is changeable through programming file MainActivity.java file using Color.parseColor(“# color code”) method. So here is the complete step by step tutorial for Change button upper text programmatically android .android-project-download-code-button

How to Change button upper text programmatically android .

Code for MainActivity.java file.

 package com.android_examples.com.buttonabovetextcolorchangeprogrammatically;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 Button b1,b2;
 
 //define your color # code in double course.
 int ButtonTextColor = Color.parseColor("#8b1502");
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 b1 = (Button)findViewById(R.id.button1);
 b2 = (Button)findViewById(R.id.button2);
 
 b2.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 // Setting up double course color code int variable on button.
 b1.setTextColor(ButtonTextColor);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.buttonabovetextcolorchangeprogrammatically.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="THIS IS SAMPLE BUTTON" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="18dp"
 android:text="CLICK HERE TO CHANGE ABOVE BUTTON TEXT COLOR ON BUTTON CLICK" />

</RelativeLayout>

Screenshot:

Change button upper text programmatically android

Click Here To Download Change button upper text programmatically android project.

Leave a Reply

Your email address will not be published. Required fields are marked *