In my application I have a SeekBar. I applied a custom drawable to it using layer-list
my code is `
<?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/profilecompleteness" />
<item
android:id="#+android:id/progress"
android:drawable="#drawable/profilecomplete_fill" />
</layer-list>`
and code for SeekBar is
<SeekBar
android:id="#+id/user_profile_progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progressDrawable="#drawable/custom_seekbar"
android:thumb="#drawable/transparent" />
when use this code my seek bar displays only half image of drawable. How to can I show full image in the drawable.
Related
My Xml code:
<Button
android:id="#+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/google_btn" />
I am applying Default ripple effect
<Button
android:id="#+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground" />
but I need button background "#drawable/google_btn" with
"?android:attr/selectableItemBackground". it's means i need ripple effect with custom background.
In your drawable-v21 folder you can write code for ripple effect by your own. Make a drawable xml file and set starting tag by ripple. Like this :
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#color/colorAccentDark">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#color/button_accent_dark"
android:state_checked="false"/>
<item
android:drawable="#color/button_accent"
android:state_checked="true" />
<item
android:drawable="#color/button_accent_dark" />
</selector>
</item>
</ripple>
When the button has a background from the drawable, we can add ripple effect to the foreground parameter.. Check below code its working for my button with a different background
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:background="#drawable/shape_login_button"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:text="#string/action_button_login"
/>
Add below parameter for ripple effect
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item
android:id="#android:id/mask"
android:drawable="#drawable/btn_rectangle"/>
<item android:drawable="#drawable/btn_rect_states"/>
</ripple>
Try the above code here in drawables give your own custom drawable backgrounds as you need it.
Another option is to create a layer list drawable and set that as a background. For example:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_background"/>
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
I have an image button and I want to change the color of a button when pressed. First I show it with a transparent as a default state color as below. But it doesn't show highlighted when pressed. So I decided to use other color to highlight when pressed using state drawable. But how to keep background color through styles wise. Means by default it should be transparent and when pressed it has to show other color.
present code:
<ImageButton
android:id="#+id/imgbutton"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/ic_launcher"
android:background="#android:color/transparent"
/>
Need to be coded:
<ImageButton
android:id="#+id/imgbutton"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/ic_launcher"
android:background="#drawable/btnselector"
/>
So what should be the btnselector.xml here?
put it inside res/color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/green_text_color"/> <!-- pressed -->
<item android:state_focused="true" android:color="#color/green_text_color"/> <!-- focused -->
<item android:color="#android:color/white"/> <!-- default -->
</selector>
create a new shape, inside res/drawable
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/yourxmlfile" />
</shape>
and put it as background
or a res/drawable selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/green_botton_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/green_botton_highlited"
android:state_focused="true" />
<item android:drawable="#drawable/green_botton" />
</selector>
I customized the Toggle button by using a drawable defined by using a selector. I use this drawable as background for the Toggle button.
<ToggleButton
android:id="#+id/mailbox:toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="#drawable/toggle_background"
android:gravity="center_horizontal|center_vertical" />
The toggle_background is defined here:
<?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" />
<item
android:drawable="#drawable/img2"
android:state_checked="false" />
</selector>
The problem is that the image is always stretched. Is there a way to define an image for the two states that is not stretched?
What I need is a background that will be stretched and in the center of the button an icon that must not be stretched.
Is it possible?
This is my final solution.
In the layout:
<ToggleButton
android:id="#+id/mailbox:toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/toggle_drawable_layers"/>
in the toggle_drawable_layers.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/toggle_drawable_bkg"></item>
<item android:left="10dp" android:drawable="#drawable/toggle_drawable_left"
android:gravity="center_vertical|center_horizontal" />
</layer-list>
in the toggle_drawable_left.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<bitmap android:src="#drawable/bitmap_checked"
android:gravity="center_vertical|center_horizontal" />
</item>
<item android:state_checked="false">
<bitmap android:src="#drawable/bitmap_unchecked"
android:gravity="center_vertical|center_horizontal" />
</item>
</selector>
I did the same task this way, the button:
<ToggleButton android:id="#+id/mlAbout"
android:textOn="#string/about"
android:textOff="#string/about"
android:background="#drawable/ml_about" />
#drawable/ml_about:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/list_bg_top"></item>
<item android:left="10dp">
<bitmap android:src="#drawable/waiting_status_btn"
android:gravity="center_vertical|left" />
</item>
</layer-list>
#drawable/list_bg_top background image that will be stretched and #drawable/waiting_status_btn is the icon the will not be stretched with the widget
Just use bitmaps instead of drawables in your selector like so (no need to use layers):
<?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" />
<item
android:drawable="#drawable/img2"
android:state_checked="false" />
</selector>
Also, don't forget to declare
android:textOff=""
android:textOn=""
in your ToggleButton declaration
There is another way to overcome the issue of stretching:
Just convert the image into a Nine-Patch image, where the stretchable areas are just on all sides of the image.
Then Android will stretch the image only in invisible areas, and keep the visible image unchanged.
I have some kind ListView. All items in it are in rectangle shape, but the top one has round corners as shown in this photo. To create it I've cut the top stripe with corners and saved it as item_bg_white_top image and a stripe with 1px height saved as item_bg_white_line image. And this is how I've constructed it.
I want to make flash effect when clicking on ltest layout just like clicking on ListView item.
How I can do this?
I've tried the code below on ltest but it it didn't helped. When I tried this code on ltest_inner it just changed its background to black.
final LinearLayout ll = (LinearLayout)findViewById(R.id.ltest);
ll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ll.setBackgroundResource(android.R.drawable.list_selector_background);
}
});
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/ltest"
android:layout_width="300dp"
android:layout_height="33dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ltest_inner"
android:layout_width="300dp"
android:layout_height="8dp"
android:background="#drawable/item_bg_white_top" />
<LinearLayout
android:layout_width="300dp"
android:layout_height="25dp"
android:background="#drawable/item_bg_white_line_repeat" >
</LinearLayout>
</LinearLayout>
item_bg_white_line_repeat.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/item_bg_white_line"
android:tileMode="repeat" />
What you want to do is set up a selector and use it as the drawable for the background.
Create an xml file in your drawable folder and add a selector to it:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/disabled_pressed_image" />
<item android:state_enabled="false" android:state_focused="true" android:drawable="#drawable/enabled_focused_image" />
<item android:state_enabled="false" android:drawable="#drawable/enabled_image" />
<item android:state_focused="true" android:drawable="#drawable/focused_image" />
<item android:state_pressed="true" android:drawable="#drawable/pressed_image" />
<item android:drawable="#drawable/default_image" />
</selector>
The drawables above reference images also in your drawable folder. You don't have to implement all the states. Those are just some possible combinations.
Then attach this as the background to your Linear Layout:
<LinearLayout
android:id="#+id/ltest"
android:layout_width="300dp"
android:layout_height="33dp"
android:background="#drawable/selector_file_name"
android:orientation="vertical">
where selector file name is simply the name you gave to the selector xml file I mentioned above.
Also consider removing the inner LinearLayouts. You can do what you are trying to do with a 9-patch, then you'll only have the one LinearLayout (which you could just change to an ImageView if it is not going to host other Views).
I want to develop custom layout for seekbar below shown.
I have track,thumb and progress images . Can any body help me?
XML code
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="#drawable/seek_bar"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:thumb="#drawable/thumbler_small"
android:indeterminate="false" />
seek_bar.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+android:id/SecondaryProgress"
android:drawable="#drawable/first"/>
<item
android:id="#+android:id/progress"
android:drawable="#drawable/second"/>
</layer-list>
first drawable is empty or shown not color full area.
second drawable is fill or color full area.
this link help your more.