简体   繁体   中英

How to change the Textcolor of a textview according to the color chosen alert dialog android

I want to change the Text color of a textview, for that I want to use a alert dialog. If I click on the textview It should show an alert dialog that must contain list of colors If I choose Red the Textview color must change to red.

Can Anyone help thanks in Advance.....

try this open source projects that might help you

https://github.com/QuadFlask/colorpicker

Set onClickListener of textView using this color picker.I think this will solve your purpose.

you are looking something this

TextView.setTextColor() takes an int representing the color (eg. 0xFFF5DC49) and not the resource ID from the xml file. In an activity, you can do something like:

   textView1.setTextColor(getResources().getColor(R.color.mycolor))

outside of an activity you'll need a Context eg.

   textView1.setTextColor(context.getResources().getColor(R.color.mycolor))

Here is a project on GitHub which I found good for me.

HOW TO USE IT Adding the library to your project

The aar artifact is available at the jcenter repository. Declare the repository and the dependency in your build.gradle.

(root)

repositories {
    jcenter()
}

(module)

dependencies {
    compile 'com.pes.materialcolorpicker:library:1.0.+'
}

Use the library

Import the class

import com.pes.androidmaterialcolorpickerdialog.ColorPicker;

Create a color picker dialog object

final ColorPicker cp = new ColorPicker(MainActivity.this, defaultColorR, defaultColorG, defaultColorB);

defaultColorR, defaultColorG, defaultColorB are 3 integer ( value 0-255) for the initialization of the color picker with your custom color value. If you don't want to start with a color set them to 0 or use only the first argument

Then show the dialog (when & where you want) and save the selected color

/* Show color picker dialog */
cp.show();

/* On Click listener for the dialog, when the user select the color */
Button okColor = (Button)cp.findViewById(R.id.okColorButton);
    okColor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            /* You can get single channel (value 0-255) */
            selectedColorR = cp.getRed();
            selectedColorG = cp.getGreen();
            selectedColorB = cp.getBlue();

            /* Or the android RGB Color (see the android Color class reference) */
            selectedColorRGB = cp.getColor();

            cp.dismiss();
        }
    });

That's all :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM