Wednesday, January 14, 2015

How to convert Hex color to int color in Android

activity_main.xml code snippet

<Button
 android:id ="@+id/push_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Button Text and Background Color"
 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);

 // #808000 equal to Olive color
 int deepColor = Color.parseColor("#808000");

 // #FFF5EE equal to SeaShell color
 int lightColor = Color.parseColor("#FFF5EE");

 // set the button background color
 // setBackgroundColor() method require to pass an int color
 btn.setBackgroundColor(deepColor);

 // set the button text color
 // setTextColor() method require to pass an int color
 btn.setTextColor(lightColor);
}
Imported Class

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