Android compare ConstantState does not work on 4.4 KitKat - android

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()))

Related

Drawable setAlpha not working on android 4.4.2

I'm using following function to enable and disable drawables...
public static void setDrawableState(Drawable d, boolean enabled)
{
if (d == null)
return;
d.mutate(); // so drawables don't share state anymore
if (enabled)
d.setAlpha(255);
else
d.setAlpha(100);
}
This worked on all phones I've tried yet, now I see it does not seem to work on android 4.4.2 (maybe it's not even version specific).
Is there another (better) way to set the alpha of a drawable? Or am I'm missing something?
Because drawables might share the same state, changing the drawable state will not have any effect. You need to mutate the drawable, for example in your code, try something like:
d.mutate().setAlpha(100);
Android developer Blogs has a great blog post explaining more on drawable state and mutations.
in case you wanna be really sure:
Drawable d2 = d.getConstantState().newDrawable().mutate();
d2.setAlpha(100)

Can't change background color of CheckBox view twice - Android

I try to change CheckBox background after user change its state to Checked. Code below doesn't work quite well. If i click unchecked checkbox its changes state and color to blue, when i click it again and uncheck, color stays blue instead changing to red. I did introduced call of invalidate() with no sucess. I confirmed that method recievies clicks and correctly sees checked status, but if statement i've made seems to work only once.
XML
<CheckBox
android:id="#+id/chkLargeIcons"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:onClick="handleCheckBoxClick" />
Code
public void handleCheckBoxClick(View view) {
CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
if(tmpChkBox.isChecked())
{
tmpChkBox.setBackgroundColor(color.blue);
}
else
{
tmpChkBox.setBackgroundColor(color.red);
}
System.out.println(view.getId() + " " + tmpChkBox.isChecked());
switch (view.getId()) {
case R.id.chkLargeIcons:
...
break;
...
}
...
}
What should i change in my code to make it work as i want?
Thanks in advance for any help and suggestions.
For it to change when checked you need to attach an OnCheckChangedListener. Then place the above code inside that.
CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
tmpChkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.setBackgroundColor(Color.BLUE);
} else {
buttonView.setBackgroundColor(Color.RED);
}
}
});
First some explanation of one unclear aspect
Thing is that example colors mentioned in my code might be misleading if someone assume that i use android Color class. While i was using my versions of colors red and blue stored in color.xml and thats why I stated them in code as color.red instead of Color.RED.
So my code as same as listener code in other answer, both are valid if you assume that color is taken from android class.
Source of my problem is some strange glitch, or not known by me android behaviour that causes following code work faulty.
public void handleCheckBoxClick(View view) {
CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
if(tmpChkBox.isChecked())
{
tmpChkBox.setBackgroundColor(color.blue);
}
else
{
tmpChkBox.setBackgroundColor(color.red);
}
}
Precisely lines like this one
tmpChkBox.setBackgroundColor([ColorFromResources]);
While execution of code gave me just one occurence of color change, then it just stayed like that, completly unresponsive. That one change was largely misleading and got me stuck on this problem long time, and even made me leave it for later fixing.
After some more research i figured that following change in code fixes my issue:
tmpChkBox.setBackgroundColor(getResources().getColor([ColorFromResources]));
Now all works like a charm. But I'm still puzzled why does call of color from resources works without getResources().getColor(...). Hopefully that answer will help someone as strangely stuck as me.

Spinner works in 2.3 but not 3.2

I have been successfully running an app under android 2.2 (api8) using spinners with OnItemSelectedListeners. It was built with tarketSdkVersion = 8 and minSdkVersion = 8. I am now trying to run it on a 3.2 device, but the spinners can not be selected. They do populate with the default array value however, so the adapter appears to be working. Clicking on the spinners results in no reaction. I tried building with tarketSdkVersion = 13 and minSdkVersion = 13, but the spinners are still dead. I am using slightly customized spinner versions to achieve "wrap_content" in a multiline_spinner_dropdown_item.xml file. Is there a compatibility issue with spinners since 2.2?
I tried the .setEnabled(true) but it didn't work. In frustration, I started to remove sections of code from main.java and layout.xml until a single spinner was left and working. As I added the code and controls back, I found that a ScrollView nestled inside a TabHost/LinearLayout/TabWidget/FrameLayout prevented the Spinners from responding. By removing the Scrollview, the Spinners worked in 3.2. For some reason the Scrollview worked in 2.2, but not 3.2.
I had the same problem, I used the spinner in 2.2 ,it was working but the same do not work in 3.2, The problem was with the default theme of 3.2, its holo. because of that the spinner is not shown properly,
Just create a theme in values/style, and apply it to your activity in android.manifest file.
Are you by any chance changing the visibility of the spinner?
I had a similar problem, and it was necessary to call setAdapter() every time the spinner was re-shown, otherwise it became immune to clicks.
You can see the change I made to my project that fixed this problem at https://github.com/nikclayton/android-squeezer/commit/7a148edf5f1b3eaca7718161de18254970290ce0.
Perhaps some visibility issues ?
Are you stacking the ScrollView on top of the area where the spinners' items show up ?
Try to use different states for the spinners by changing their icon or color for example and see if they get the click event. If they do, then it is probably something related with visibility. If they don't, it might something else.
Some code sample could help :)
Have you tried setting the spinner as enabled?
spinner.setEnabled(true);
Although it would be weird that it was disabled on default. Your symptoms do describe a disabled spinner though
Oddly, I tried another app of my that works great on 2.2. When I installed it on a 3.1 tablet or emulator, the onClick events won't fire. No spinners this time, but the listeners don't work either! Here's some of the code:
//In onCreate:
//setup listeners
rbSlab = (RadioButton)findViewById(R.id.rbSlab);
rbBeam = (RadioButton)findViewById(R.id.rbBeam);
rbSlab.setOnClickListener(radio_listener);
rbBeam.setOnClickListener(radio_listener);
etFpc.setOnEditorActionListener(this);
etFy.setOnEditorActionListener(this);
etBw.setOnEditorActionListener(this);
etDp.setOnEditorActionListener(this);
etMu.setOnEditorActionListener(this);
...
}//end onCreate
//In Main body of app:
//for radio buttons
private OnClickListener radio_listener = new View.OnClickListener()
{ //#Override
public void onClick(View v) {
DoCalcs();
}//onClick
};
//for edittext
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if ((actionId == EditorInfo.IME_ACTION_DONE) || //if DONE button pushed
((event.getAction()==KeyEvent.ACTION_DOWN) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))) //if ENTER button pushed
{
//do calcs
return(true);
}
else return(false);
}
I've built this with target API 8 and min API 8, and also with target API 14, min 8, and when installed on a API 8 the listeners work fine, but not with API 14!
I had a similar problem. It's because of the default theme of Android 3.2 and above versions.
The simpler solution is to use a custom spinner, or to make any background colour or picture for the same, like:
android:background="#drawable/spinner"
This link will help you for custom spinner.

Color Picker how to change TextView

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.

transparency of button is not changed using 'setAlpha'

I am developing a simple application ,in that there is set of 6 buttons.
when i clicked on one of these button then the other buttons must be partially transparent. I tried to done that By setting alpha of 5 buttons
acre_button.getBackground().mutate().setAlpha(155);
Ui of the application not changed as i expected. i got only 3 out of 5 is get transparent.when clicking on that two button it is slowly changing it's transparency
Thanks in advance
regards,
kariyachan
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.main_btn);
Drawable d = getResources().getDrawable(R.drawable.imagen);
d.setAlpha(60);
btn.setBackgroundDrawable(d);
}
This works for me :)
Find a button background in your android-sdk directory here: android-sdk\platforms\android-10\data\res\drawable-mdpi\btn_default_normal.9.png
You can modify it to make it semi transparent (Please note that this is a 9-patch and you shouldn't change the opacity of the black lines).
Once you have this changed button in your drawable directory you can add this to your code:
button.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparentImage));
to make it semi transparent and
button.setBackgroundDrawable(getResources().getDrawable(Android.R.drawable.btn_default_normal));
to change it back.
For anyone still hunting for a solution to this:
The method setBackgroundDrawable(Drawable d) is deprecated as of API 16
Assuming your button's id is buttonId and your drawable is named button_img,
handle this by using the following within the onCreate method:
((Button)(findViewById(R.id.buttonId))).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Drawable d = v.getResources().getDrawable(R.drawable.button_img);
d.setAlpha(40);
if (Build.VERSION.SDK_INT >= 16)
v.setBackground(d);
else
v.setBackgroundDrawable(d);
//Then call your next Intent or desired action.
}
});
Tested and works for me!
Try the following:
button.setBackgroundColor(android.R.color.transparent);

Categories

Resources