Create custom flat switch android - android

I want to make one custom switch as shown below.
I have created two drawable resource for that one is switch_bg and another is tracker_bg.
switch_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/thumb_on" android:state_pressed="true" />
<item android:drawable="#drawable/thumb_off" android:state_enabled="false" />
<item android:drawable="#drawable/thumb_on" android:state_checked="true" />
<item android:drawable="#drawable/thumb_off" />
</selector>
tracker_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_tracker" android:state_enabled="false" />
<item android:drawable="#drawable/ic_tracker" android:state_pressed="true" />
<item android:drawable="#drawable/ic_tracker" android:state_checked="true" />
<item android:drawable="#drawable/ic_tracker" />
</selector>
and in main xml I put following code.
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginRight="20dp"
android:thumb="#drawable/switch_bg"
android:track="#drawable/track_bg"/>
Please guide me What I am doing wrong or What things are missing from my side.

Try this sample it may help you.Click here

Related

Button pressed and two images xml android

I have two images. In one is a number, the other is shadow. I need to show this two images as can be seen in the second photo.
This is my code. Right now it only shows or the number (when is not pressed), or the shadow(when is pressed).
How is possible to show both images when the user press the button?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_select" android:state_pressed="true"
android:alpha="0.75"/>
<item android:drawable="#drawable/number_1" android:state_enabled="true" />
<item android:drawable="#drawable/number_1" android:state_pressed="false"
/>
</selector>
this are the two images
this is the goal
Also I was trying using layerDrawable in this way, but didn't work:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_select" android:state_pressed="true"
android:alpha="0.75"/>
<item android:drawable="#drawable/number_1" android:state_enabled="true" />
</selector>
</item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/number_1" android:state_pressed="false"
/>
</selector>
</layer-list>
You should use two different images...
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_image" android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image" android:state_pressed="false"/>
</selector>
You can use Relative Layout for that
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iv_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_select"/>
<ImageView
android:id="#+id/iv_img"
android:layout_width="wrap_content"
android:padding="5dp"
android:layout_height="wrap_content"
android:background="#drawable/number_1"/>
</RelativeLayout>
Just change image of iv_img id GOOD LUCK
please change the drawable selector file with this code..
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:drawable="#drawable/number_1" />
<item android:bottom="#dimen/margin_5" android:left="#dimen/margin_5" android:right="#dimen/margin_5" android:top="#dimen/margin_5">
<bitmap android:src="#drawable/button_select" />
</item>
</layer-list>
</item>
<item android:drawable="#drawable/number_1" android:state_pressed="false"/>
Limitation ::
It can use for single image of number ..for multiple numbers you have to create different selector.xml for each one..
Please tell if need more explanation or help..

Why is this ripple effect is not working as expected?

I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />

Effect for ImageButton in android

I want to create effects for ImageButton. For example, it will change color when clicked...How I do it? I want to do this in .xml file. Can you help me! Thank you!
I tried to create the state.xml file as follwing:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_0" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_ac" />
</selector>
However, I can't set background for ImageButton. The error like this:
All you have to do it to add the "android:background" attribut to your ImageButton and set a drawable.
Your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_btn"
android:background="#drawable/btn_drawable"/>
btn_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/blue"
/>
<item android:state_focused="true"
android:drawable="#drawable/white"
/>
<item android:drawable="#drawable/green" />
</selector>
In that code above you set a different drawable when your ImageButton is pressed (state_pressed), focused (state_focus) or when is normal (not pressed and not focused).
Here you can find with more detail.
Create a drawable like below and name it as btn_drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/btn_disable" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_click" />
</selector>
This is for an example,Like this you can add the <item/> according to your needs and state of the image button.
Then set the drawable as image button background.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_drawable"/>

Android ratingbar doesn't update

I've made a ratingbar in my app which uses a custom style, using my own stars.
My ratingbar is defined as followed:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="15dp"
android:id="#+id/ratingBar"
android:numStars="10"
android:stepSize="1"
android:rating="10"
android:layout_marginTop="5dp"
android:layout_below="#id/feedbackTable"
android:layout_toRightOf="#id/ratingText"
android:layout_marginLeft="5dp"
style="#style/starBar" />
The style that I use in styles.xml is:
<style name="starBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
With ratingstars.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/ratingstars_full_empty" />
<item android:id="#+id/secondaryProgress" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#+id/progress" android:drawable="#drawable/ratingstars_full_filled" />
</layer-list>
When I adjust the android:rating it shows the correct amount of stars filled and being empty in the preview. However, using my emulator or real device the bar stays completely filled with stars (10/10). I've tried adjusting it programmatically by using the following piece of code:
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.d("rating", rating+"");
ratingbar.setRating(rating);
}
});
Now the log from logcat shows actually that rating IS changed, but the bar does not visually update itself with e.g. 6 stars being full, and 4 empty and I have no clue why.
The drawables are correct as the preview also shows it correct, so what can cause this?
Edit:
This is ratingstars_full_filled.xml
<?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/star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star" />
<item android:drawable="#drawable/star" />
</selector>
And here is ratingstars_full_empty.xml
<?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/star_empty" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_empty" />
<item android:drawable="#drawable/star_empty" />
</selector>
You should change the id attribute in ratingstars.xml from "#+id/..." to "#android:id/...", since the resource is part of the android system, and already defined in the default android progress drawable.
<?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/ratingstars_full_empty" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/ratingstars_full_empty" />
<item android:id="#android:id/progress" android:drawable="#drawable/ratingstars_full_filled" />
</layer-list>
It looks like it's not a problem with the listener being called, but rather just the custom android:progressDrawable style attribute you're applying to it. My guess is that the drawable you're using is doing unexpected things. Try removing style="#style/starBar" from your RatingBar and see if things are working as expected. If so, that's your problem

Problem with selector for a ListView in Android

I have created a ListView and applied a selector to it as follows
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/my_btn_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/my_btn_focussed" />
<item android:drawable="#drawable/my_btn_normal" />
</selector>
When focussed or pressed, the background of the ListView item comes as specified in the selector. But the default background is never applied, can you tell me what is wrong?
By the way, this is the customised row xml I've used:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="20sp" >
</TextView>
Thanks,
Kiki
The solution was presented as an answer to this question.
Try using this code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_focused="false" android:drawable="#drawable/my_btn_pressed" />
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/my_btn_focussed" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/my_btn_normal" />
</selector>
In your Textview, you should try to change this...
android:background="#drawable/<b>my_btn_normal</b>"
to this...
android:background="#drawable/<b>your selector's name</b>"

Categories

Resources