Android TextView change background image when pressed - android

I wanto to change the background-image of a textview when I click it. I have red about the selectors and I did like this:
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/img1" /> <!-- pressed -->
<item android:drawable="#drawable/img2" /> <!-- default -->
</selector>
and then in the layout file:
<TextView
android:id="#+id/textUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/selector"
android:clickable="true" />
but it doesn't work. I tried to change the bg color and in this case it works. What am I doing wrong?

You should create a new drawable xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--item android:drawable="#drawable/iv_circle_checked" android:state_pressed="true" />
<item android:drawable="#drawable/iv_circle_unchecked" /-->
<item android:drawable="#drawable/iv_circle_checked" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/iv_circle_checked" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="#drawable/iv_circle_checked" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="#drawable/iv_circle_unchecked" android:state_enabled="true" android:state_selected="false"/>
<item android:drawable="#drawable/iv_circle_unchecked" android:state_enabled="true"></item>
</selector>
When you create this, you should add in your background attr this xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/addgrnbtnhov"
android:state_pressed="true"
/>
<item android:drawable="#drawable/addgrnbtn" />
</selector>
please use different images.

// try this way
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img1" android:state_checked="true" android:state_pressed="false"/>
<item android:drawable="#drawable/img2" android:state_checked="false" android:state_pressed="false"/>
<item android:drawable="#drawable/img1" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="#drawable/img2" android:state_checked="false" android:state_pressed="true"/>
</selector>
<CheckBox
android:id="#+id/chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
style="#android:style/Widget.TextView"
android:background="#drawable/selector"
android:gravity="center"
android:checked="true"
android:button="#null"/>

What you can do:
android:onClick="change"
In java:-
public void onCLick(View view){
TextView text=(TextView) findViewById(R.id.textUp);
text.setBackgroungResource(R.drawable.image);
}

Try using scaleType="fitXY"
<TextView
android:id="#+id/textUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:background="#drawable/selector"
android:clickable="true" />

Related

Android RatingBar can't set rating

I'm trying to implement a RatingBar in my Android app but I'm running into some issues when trying to set the rating. My bar is always displayed as having the maximum available rating. Below are my files:
styles.xml
<style name="ratingBarStyling" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/star_rating_bar</item>
<item name="android:indeterminateDrawable">#drawable/star_rating_bar</item>
<item name="android:minHeight">25dip</item>
<item name="android:maxHeight">27dip</item>
</style>
(same for v21)
star_rating_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/background"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#+id/secondaryProgress"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#+id/progress"
android:drawable="#drawable/star_rating_bar_full_filled" />
</layer-list>
star_rating_bar_full_empty
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star_empty" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star_empty" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star_empty" />
<item android:drawable="#drawable/ic_star_empty" />
</selector>
star_rating_bar_full_filled
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/ic_star" />
<item android:drawable="#drawable/ic_star" />
</selector>
And finally the implementation of the RatingBar on the Fragment layout:
<RatingBar
style="#style/ratingBarStyling"
android:id="#+id/venue_card_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:numStars="5"
android:stepSize="1.0"
android:rating="2.0"/>
Now for some reason I can see the filled star icons but non of the empty ones. I have also tried setting it programmatically but the results were the same. Any help would be greatly appreciated.
This is what I'm always getting: stars.png
You have made small mistake/typo
You will have to use android:id instead of providing a new id.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_rating_bar_full_empty" />
<item android:id="#android:id/progress"
android:drawable="#drawable/star_rating_bar_full_filled" />
</layer-list>

Android: how can change the text color in custom view?

I have this view called ButtonMultiline:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/ButtonMultilineCatalogTitle"
android:text="title"/>
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/ButtonMultilineCatalogSubtitle"
android:text="subtitle"/>
I apply a background on this layout to change color on press:
<selector android:exitFadeDuration="200"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/btn_large_normal" />
<item android:state_pressed="true">
<color android:color="#color/background_black_top" />
</item>
<item android:state_focused="true" android:drawable="#drawable/btn_large_pressed" />
<item android:drawable="#drawable/btn_large_pressed" android:state_hovered="true" />
<item android:state_checked="true" android:drawable="#drawable/btn_large_normal" />
<item android:drawable="#drawable/btn_large_normal" />
All works fine until here.
I want to change the both TextView color on press too, but when I add this line in both TextView, there is a crash on inflate problem on this button_text_catalog.xml:
<android:background="#drawable/button_text_catalog">
My button_text_catalog.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_pressed="true" />
<item android:color="#color/white" android:state_focused="true" />
<item android:color="#color/theme_primary"/>
Thanks guys!
Use this code:
button_text_catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:color="#color/white"/>
<!-- focused -->
<item android:state_focused="true" android:color="#color/white"/>
<!-- activated -->
<item android:state_activated="true" android:color="#color/white"/>
<!-- checked -->
<item android:state_checked="true" android:color="#color/white"/>
<!-- default -->
<item android:color="#color/theme_primary"/>
</selector>
in Style.xml
add <item name="android:textColor">#drawable/button_text_catalog</item>

Android - Navigation View item menu background color

i try to change color of my item Navigation View menu :
styles.xml
<item name="android:activatedBackgroundIndicator">#drawable/activated_background</item>
activated_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/White" />
<item android:state_focused="true" android:drawable="#color/White" />
<item android:state_activated="true" android:drawable="#color/White" />
<item android:drawable="#android:color/transparent" />
</selector>
drawer.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/group_1">
<item
android:id="#+id/drawer_menu_account"
android:checked="false"
android:title="#string/menu_account"
android:icon="#drawable/ic_account_grey600_24dp" />
<item
android:id="#+id/drawer_menu_my_post"
android:checked="false"
android:title="#string/menu_my_post"
android:icon="#drawable/ic_pin_grey600_24dp" />
</group>
[...]
Background color don't change..
What am i doing wrong ?
You don't set the drawable for the background of a Navigation View item in your styles.xml file. Open up your XML layout file containing your Navigation View widget, and add the following line to the widget's attributes:
app:itemBackground="#drawable/activated_background.xml"
If you're having trouble with the "app" pointer, add the following line in as well:
xmlns:app="http://schemas.android.com/apk/res-auto"
Note that this only changes the colour of the background of a selected list item. If you'd like the icon and text colour to change as well, use the app:itemTextColor attribute instead.
Below code works fine for me
my nav view:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/nav.background"
app:menu="#menu/menu_drawer"
app:itemBackground="#drawable/nav_item_background"
app:itemTextColor="#drawable/nav_item_text"/>
drawable/nav_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/nav.item.background" />
<item android:state_checked="true" android:drawable="#color/nav.item.background" />
<item android:state_focused="true" android:drawable="#color/nav.item.background" />
<item android:state_activated="true" android:drawable="#color/nav.item.background" />
<item android:drawable="#color/nav.item.background.default" />
</selector>
drawable/nav_item_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true" />
<item android:color="#bdbdbd" />
</selector>
You must use android:background="color" for back color of navigation menu
like:
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/shitstuff"
app:itemTextColor="#color/black"
app:menu="#menu/drawermenu"
android:background="#color/colorAccent"
android:layout_marginTop="-24dp"
/>
and for items color use itemTextColor="color"
To have the background display on your selected item you need to add
android:checkable="true"
on every item of your menu.xml items, then set :
app:itemBackground="#drawable/nav_view_item_background"
on your NavigationView and finally:
drawable/nav_view_item_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/primary" android:state_checked="true" />
<item android:drawable="#android:color/transparent" />
</selector>
#HaloMediaz and #HemantShori
To keep the background of the selected item in a selected state,
use android:state_checked="true" instead of android:state_pressed="true" in the color state resource.
So your activated color state resource should look like this:
activated_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#color/White" />
<item android:state_focused="true" android:drawable="#color/White" />
<item android:state_activated="true" android:drawable="#color/White" />
<item android:drawable="#android:color/transparent" />
</selector>

how to get background button with selector drawable in android?

how to get background button with selector in drawable folder?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/btn_press" android:state_pressed="true"></item>
<item android:drawable="#drawable/btn_mati" android:state_selected="false"></item>
</selector>
try like this,
Create button_selector.xml in drawable folder,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_hover" android:state_pressed="true"></item>
<item android:drawable="#drawable/btn_hover" android:state_focused="true"></item>
<item android:drawable="#drawable/btn_normal" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/btn_hover" android:state_enabled="false"></item>
</selector>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
/>
in your java class,
btn.setBackgroundResource(R.drawable.button_selector);

Change button background when pressed

I want to change background of a button when it is clicked. I tried to use a selector. But It didn't work. Here is the selector (add_grp_slctr.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp_d" />
<item android:drawable="#drawable/add_grp" />
</selector>
And the button :
<Button
android:id="#+id/addGrpBtn"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:background="#drawable/add_grp_slctr"
android:onClick="addGrpDialogOpen" />
add_grp_d and add_grp are images(png).
I tried a similar code which will be white by default, black when pressed on an onclick of a button:
//***This is the btn_selector which is to be declared in drawable folder***
<?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/black" /> <!-- pressed -->
<item android:drawable="#android:color/white" /> <!-- default -->
</selector>
and called this on the button.xml -->
android:background="#drawable/btn_selector"
Hope this would help..:)
go through the http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList once. Also state_focussed for button only works when you are focussing the button using a hardware-keyboard.
As for your case
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_selected="false" android:drawable="#drawable/button_default"/>
<item android:state_pressed="true" android:state_selected="false" android:drawable="#drawable/button_default" />
<item android:state_pressed="false" android:state_selected="true" android:drawable="#drawable/button_selected"/>
<item android:state_pressed="true" android:state_selected="true" android:drawable="#drawable/button_selected" />
<item android:drawable="#drawable/button_selected" />
</selector>
Use your selector
change your code like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp" />
<item android:drawable="#drawable/add_grp_d" />
</selector>
My Code
<item android:drawable="#drawable/shadow_design_click" android:state_pressed="true"/>
<item android:drawable="#drawable/shadow_design" android:state_focused="true"/>
<item android:drawable="#drawable/shadow_design"/>
</selector>
I think you should change your selector a bit.
Check this answer here.
Instead of passing cuastom XML file, if you just want to change the color of you button then you can try with the following way.
Button lineColorCode = (Button)findViewById(R.id.button1);
Now inside button's click event use following code.
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);

Categories

Resources