Setting view background with selector - android

In my app I have ImageView that is added programmatically in onLayout method of its parent. I want it to have different background when it is focused different when pressed and different when on normal state. I defined some colors in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cream">#FBF9F5</color>
<color name="green">#93D932</color>
<color name="blue">#0095FF</color>
</resources>
Then I defined selector in drawables folder named table_background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/green" />:
<item android:state_pressed="true" android:drawable="#color/blue" />:
<item android:drawable="#color/cream" />:
</selector>
Then i onLayout method of its parent after I initialized it I try to set background with:
v.setBackground(getContext().getResources().getDrawable(R.drawable.table_background_selector));
And that's when IntelliJ shows me problem:
"Cannot resolve method 'setBackground(android.graphics.drawable.Drawable)'"
And I fail to understand why because View class has method
setBackground(Drawable background)
Here is .png I set with setImageResource:
http://imgur.com/Skwzd2z
Center of it is transparent and now it shows whatever background is set to app but I want it to display different colors when pressed or focused
I would be thankful for any help

Check that you not importing the android R file by mistake instead of your local one, it certainly looks so.
UPDATE:
and for setting ImageView background do this:
image.setImageResource(R.drawable.your_drawable_resource);

first clean and builder you project still you getting same error then
Please check in import area whether you included this statement
import android.R;
if yes then you need to use your package name with include R like my.package.R.

Related

Android make check in or check out button with style

I need to make a style like following screen shot
PLZ NEED HELP !!!!
I have tried using make custom style
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/buttion_inner_icon" android:state_checked="true"/>
<item android:drawable="#drawable/buttion_inner_icon" android:state_checked="false"/>
</selector>
But its not as I want
Please help me to make view same as screen shot
You have setted the same drawable when state_checked = true and state_checked = false.
You must have two differents drawables.
You can use a ToggleButton for this. In your xml file, set the text to an empty string, otherwise it will show 'on' and 'off'.
android:textOff=""
android:textOn=""
Then you have to add two drawables: one with the handle on the left side (so that 'check in' is visible, this is the unchecked state, name it for example 'check_in_unchecked'), and one with handle on the right side (so that 'check out' is visible, this is the checked state, name it for example 'check_in_checked'). After that, add a selector as background of your ToggleButton:
android:background="#drawable/selector_check_in"
selector_check_in will look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check_in_checked" android:state_checked="true"/>
<item android:drawable="#drawable/check_in_unchecked" android:state_checked="false"/>
</selector>

setShouldDisableView is not working.

Created a preference file for settings in android. When I uncheck a CheckBoxPreference I want its dependent preferences to turn out to grey color (i.e to potray). I had tried using, setShouldDisableView() but its not working. Suggest me the idea to greyout the dependent Checkboxpreference's (title and checkbox), but in default only the color of checkbox is changing.
Create separate xml file in drawable folder and paste the below code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#D3D3D3" android:state_checked="true"/>
<item android:drawable="#ffffff" android:state_checked="false"/>
</selector>
This xml file name set as android:background="#drawable/filename" in your check box tag xml. Hope this will help you!
In your theme use selectors for primary/secondary colors instead of using "pure" colors. See also: Android: change color of disabled text using Theme/Style?

Programatically make TextView background color change while pressed

I have some TextViews which are dinamically added to a LinearLayout. These TextViews are clickable and have an onLongClickListener (I also intend to add onClickListener later)
Here is the thing, I want these TextView to change their background color when pressed and I read that you can use selectors to do such thing.
So I made this xml file in res/drawable/text_view_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000"/>
<item android:state_pressed="false"
android:color="#FFFFFF"/>
</selector>
I tried to create a TextView and using this xml file likes this:
TextView t = new TextView(this);
t.setBackgroundColor(R.drawable.text_view_pressed);
But when I do this, it will give this error in t.setBackgroundColor: "Should pass resolved color instead of resource id here: getResources().getColor(R.color.text_view_pressed)" but it doesn't work as intended if I use getResources().getColor(R.color.text_view_pressed).
Anyone got an idea how to do it?
You are on the right track. However there is an important detail.
There are two types of resources that can be affected by states: ColorStateList and StateListDrawable.
A color state list can only be used in certain contexts, for example in TextView.setTextColor(). As far as I can see, you cannot use a color state list as parameter of setBackgroundColor() if you want to change the background of a View when it's pressed. You need a state list drawable for that. And in a state list drawable, the android:drawable attribute is mandatory.
So, to sum up:
The xml file should be placed in res\drawable,
Its structure should be slightly different (i.e. state list, not color list), and
You need to use setBackgroundResource() instead of setBackgroundColor().
Example file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white" />
<item android:drawable="#android:color/black"/>
</selector>
If you want to use custom colors instead of white and black, you simply need to define them as resources in res\values and reference them from here.

android colorize custom button

I've created a custom buttom with a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_np"
android:state_enabled="false"/>
<item android:drawable="#drawable/button_np_pressed"
android:state_pressed="true"/>
<item android:drawable="#drawable/button_np"
android:state_focused="true"/>
<item android:drawable="#drawable/button_np"/>
</selector>
I would like to be able to colorize this button by using a gray button as image and then colorize it with a color I've defined.
Is that possible?
Thanks
It sure is possible (at least in code), you set a color filter.
import android.graphics.PorterDuff;
Button.getBackground().setColorFilter(0xFF00FF00,PorterDuff.Mode.MULTIPLY); // Green
Button.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY); // Red
Just choose your colors and put them in the setColorFilter parameters.
yes it works very well, i use it in mine, simply create a 9Patch button and put the name of the image in where you have "#drawable/button_np" the "button_pn" is the name of your button image

Change checkMark or size of CheckedTextView

I'm working on an application where I'm using a checkedTextView, it all works great. But I really don't like that layout of the "checkbox" within the checkedTextView, it's simply to big. Is there any way to resize it or change the layout to something custom made?
I've tried the android:checkMark attribute, but that resulted in it being marked all the time, and thus showing all the time.
Instead of using a single drawable you should write a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/drawable_checked"
android:state_checked="true" />
<item
android:drawable="#drawable/drawable_unchecked"
android:state_checked="false" />
</selector>
And then set it to the android:checkMark attribute.

Categories

Resources