Color Picker how to change TextView - android

I have a problem.
How to use ColorPickerDialog ?
How do I set ColorPickerDialog so as to change my text color?

Please have a look over this existing code..
http://www.yougli.net/android/a-photoshop-like-color-picker-for-your-android-application/

You can use this project instead of a demo app
// initialColor is the initially-selected color to be shown in the rectangle on the left of the arrow.
// for example, 0xff000000 is black, 0xff0000ff is blue. Please be aware of the initial 0xff which is the alpha.
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, initialColor, new OnAmbilWarnaListener() {
#Override
public void onOk(AmbilWarnaDialog dialog, int color) {
// color is the color selected by the user.
}
#Override
public void onCancel(AmbilWarnaDialog dialog) {
// cancel was selected by the user
}
});
dialog.show();
Be careful to this (I never used it myself):
You need to link against Android 3.0 or higher since there is an XML
attribute layerType in use. This doesn't mean you can use this only
for applications requiring Android 3.0 to operate. It works even in
Android 1.6. You just need to select Android 3.0 (API level 11) on the
project settings.

Related

Apply similar looking styles to AlertDialog box for different Android versions

I want to make the AlertDialog box for my app look like the ones on Android > 6.0.0 ( as depicted in the following screenshots) for all lower android version till 4.4.4. I have applied a custom style for the dialog box.
Following is the style in styles.xml for AlertDialog:
<style
name="AlertDialogCustom" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:textColor">#color/blue</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:background">#color/dark_grey</item>
<item name="android:backgroundDimAmount">0.3</item>
</style>
Following is how I applied the style in java:
new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom))
.setMessage("Why is DialogBox Style Inconsistent through different android versions?")
// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Do Something
}
})
.show();
As a get around I managed to at least change the text color to white on the dark background by using:
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1)
style = android.R.style.Theme_Material_Dialog;
else
style = R.style.AlertDialogCustom;
So far I have tried making seperate style for lower Android version and apply them via java by detecting Android version. But, I am unable to replicate the style of AlertDialog box on Android 6+ on lower android versions till 4.4.4. And I am unable to find a solution on the internet.
There is an anomaly though; if I apply my custom style on Rate this app provided by kobakei the resulting RateThisApp dialog looks the same on all the android versions. So I believe styling the AlertDialog in a particular manner should fix the issue.
This is what I want - To make the AlertDialog box on Android 4.4.4 and 5.1.0 to look like the one in Android 6.0.0 and higher by using suitable styles. And why does the textColor and textColorPrimary attributes does not get applied on lower android versions?
If the resource provided in my question is insufficient please let me know in the comment, I will share more details.
Here are the screenshots of AlertDialog box on different Android versions with the same style applied.
Android >= 6.0.0
Android = 5.1.0
Android 4.4.4
For more flexibility, you can just create your custom dialog class with a custom layout.
It will give you better control over the way the layout looks.
You can create very complicated UI in your dialog without writing tons of code
You can simply call your dialog like this :
CustomDialog customDialog = new CustomDialog();
customDialog.show();
And you can manage click listeners and more events inside your dialog class instead of adding those code lined inside your activity and make it super big and hard to understand.
Here is an example:
public class CustomDialog extends Dialog {
private ImageView imageView;
private Context dialogContext;
public CustomDialog (#NonNull Context context) {
super(context);
setContentView(R.layout.full_size_image_dialog); .. //your layout
dialogContext = context;
imageView = findViewById(R.id.full_size_image); //controll your views (use any view, the image view is only an example)
}
public void someMethod() {
}
}
}

Android compare ConstantState does not work on 4.4 KitKat

I developed an app and everything works great on my s7 and other devices, but I tested it on an older one running 4.4 kitkat and when the app compares the buttons' colours, in 4.4, it doesn't work.
This is the piece of code I'm using:
for (Button btn : selectorArrayList) {
final Button button = btn;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*
Check if button color is white or red
if white, turn red and activate the
selector (category) and if it's red
turn white and deactivate the selector
category)
*/
// I've set the background in the XML Layout as R.color.white
Drawable pd = (Drawable) button.getBackground();
if (pd.getConstantState().equals(ContextCompat.getDrawable(getContext(), R.color.white).getConstantState())) {
...
At this very point on a newer version of the OS it decides the states are equal, and on 4.4 (haven't tried on other versions) they're not.
Does anyone know if I'm doing something wrong here? Thank you.
Try this, should work:
if (pd.getConstantState().equals(new ColorDrawable(R.color.white).getConstantState()))

Getting Memory Out For using color picker

I am getting Memory out error in Bitmap for one of my Play Store Apps. In this app I used color Picker for choosing color by User. And that gives me the error.
My used color Picker is:
https://github.com/chiralcode/Android-Color-Picker
Its normally take 6 to 7 MB memory.
Now I can't solve this problem. Is there any better Color Picker?
Thanks in advance.
If you're looking for a great new android color picker that use material design I have forked an great project from github and made a simple-to-use android color picker dialog.
This is the project: Android Color Picker
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.1'
}
Use the library
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 :)

Programatically changing one EditText color changes EditTexts color in whole app using Android 5

I have very strange problem in Android 5. If user inputs something wrong I want to set error to edittext and change it color to red, and when user starts typing something I want to change color back to green. This is how i do it:
eText.setError(message);
eText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
eText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
eText.getBackground().setColorFilter(
ctx.getResources().getColor(R.color.dark_green), PorterDuff.Mode.SRC_ATOP);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
#Override
public void afterTextChanged(Editable s) {}
});
In lower android versions than 5, everything works perfect, but not in Lollipop. If I change one edittext color, all edittexts in all app changes it color. Is there any way to fix this strange thing? Or it is some of material design and Android 5 tricks which I don't know?
The problem is that the background Drawable is reused across many Views. To ensure the Drawable is not shared between multiple Views you should use the mutate method.
See: mutate()
Example code:
Drawable background = mainMenuButton.getBackground();
background.mutate();
background.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setBackground(background);
Android 5.0 Lollipop: setColorFilter "leaks" onto other buttons
Do Same for like spinner colour changes..
In Android Lollipop Version you have to implement code for kitkat version and lollipop version separately , please do this code for change background of spinner. its example of image background changes of images.
if (Build.VERSION.SDK_INT => Build.VERSION_CODES.KitKat)
{
//for Lollipop Vession
// do on textChangeListner code
eText.setBackgroundDrawable(Color.RED);
}
else
{
// do on textChangeListner code
eText.setBackgroundResource(Color.RED);
}
i hope it helps you, if it is useful code for then please mark me .. Thanx.. :)

Android ColorPickerDialog show black, white colors

I have this code running: Color picker with usage in android perfectly, althought it doens't show the color white, gray, black, etc..to choose.
What do I need to change in code for being able to choose that colors?
Thank you!
You can customize the one that you mentioned in your question also. You will need to do some modifications in the ColorPickerView class.
You can also try an alternative color picker available in the below link.
Download the zip file from
http://code.google.com/p/yuku-android-util/source/browse/#git%2FAmbilWarna
Extract the zip file.
Import the library project (AmbilWarna) into workspace as you would do for any other library projects like google play services or actionbarsherlock.
Reference the library in your android project
On Button Click or some action
AmbilWarnaDialog dialog = new AmbilWarnaDialog(MainActivity.this, Color.RED, new OnAmbilWarnaListener() {
#Override
public void onOk(AmbilWarnaDialog dialog, int color) {
// color is the color selected by the user.
mPaint.setColor(color); // set color here
}
#Override
public void onCancel(AmbilWarnaDialog dialog) {
// cancel was selected by the user
}
});
dialog.show();

Categories

Resources