Skip to content

Instantly share code, notes, and snippets.

@MohammadSamandari
Last active March 30, 2020 20:35
Show Gist options
  • Save MohammadSamandari/6e1ad71d0ef8492bb3f86fd676ad3df5 to your computer and use it in GitHub Desktop.
Save MohammadSamandari/6e1ad71d0ef8492bb3f86fd676ad3df5 to your computer and use it in GitHub Desktop.
Support Libraries / ContextCompat / Change a color base on the resources proramatically

Support libraries

Android uses three directives to indicate how your app should behave for different API versions:

minSdkVersion: the minimum API version your app supports. compileSdkVersion: the API version your app should be compiled with. targetSdkVersion: the API version your app was designed for.

ContextCompat

get a value from the resources and changing a color base on the colors in the resources:

The ContextCompat class provides methods for compatibility with context and resource-related methods for both old and new API levels.

in this example we have a list of colors in values and same array of names that match the name of the colors in the color in the resources.

we are going to change the color of a textview randomly with the colors in the resources. see the code inside the activity.

package mohammad.samandari.hellocompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
// Text view for Hello World.
private TextView mHelloTextView;
private Button button;
// array of color names, these match the color resources in color.xml
private String[] mColorArray = {"red", "pink", "purple", "deep_purple",
"indigo", "blue", "light_blue", "cyan", "teal", "green",
"light_green", "lime", "yellow", "amber", "orange", "deep_orange",
"brown", "grey", "blue_grey", "black" };
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelloTextView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
/**
* This method handles the click of the Change Color button by
* picking a random color from a color array.
*
* @param view The view that was clicked
*/
// Get a random color name from the color array (20 colors).
Random random = new Random();
String colorName = mColorArray[random.nextInt(20)];
// Get the color identifier that matches the color name.
int colorResourceName = getResources().getIdentifier(colorName,
"color", getApplicationContext().getPackageName());
// Get the color ID from the resources.
int colorRes = ContextCompat.getColor(getApplicationContext(), colorResourceName);
// Set the text color.
mHelloTextView.setTextColor(colorRes);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment