Wednesday, January 14, 2015

How to change Button text color in Android

Button text color XML Java
activity_main.xml code snippet

<!--set initial text color is #4169E1 RoyalBlue-->
<Button
 android:id ="@+id/push_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Set Button New Text Color"
 android:textColor="#4169E1"
 android:padding="15dp"
 android:onClick="perform_action"
 />
MainActivity.java code snippet

public void perform_action(View v)
{
 Button btn = (Button) findViewById(R.id.push_button);

 // #FF4500 equal to OrangeRed color
 int textColor = Color.parseColor("#FF4500");

 // set the button text color
 // setTextColor() method require to pass an int color
 btn.setTextColor(textColor);

 // this line also set the button text color
 //btn.setTextColor(Color.RED);
}
Imported Class

import android.view.View;
import android.widget.Button;
import android.graphics.Color;
More android examples