Set the background image in tab in android - android

I want to set the background (red image) on selected tab bar.
Initially I set like this
When i change in the code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_tab"
android:state_selected="true" />
</selector>
Its look like
My requirement to look like below this. Please help to achieve this.

you need to do like this way
For Tab background you need to create selector like this way
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_tab"
android:state_selected="true" />
<item android:drawable="#drawable/default_tab"/>
</selector>
now you can create Button with different layout for your tab button like this way. For this requirement Button View to set your tab icon at drawable top with text.
tab_add_photo_btn.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/tab_search"
android:background="#drawable/tab_selector"
android:drawableTop="#drawable/add_photo_icon"
android:text="ADD PHOTO"
android:textColor="#fff"
android:padding="10dp"/>

I feel its nothing to do with XML coding .. just make some design trick
you need to make two different twiky images for each tab icon .
make Image background of tab which have little top part transparent (Not selected resource )
make selected image with same size with contain selected background (Selected resource )

Related

How to change the specific item background color in bottom navigation in android?

If I select the Particular item in bottom navigation, left and right side item's background color has to change. Please help me if anyone having any idea.Thank you in advance
On your BottomNavigationView, inthe XML file, add a property called "app:itemBackground" and set a drawable.
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_nav_view_height"
android:background="#color/white"
app:itemBackground="#drawable/item_bottom_nav"
app:menu="#menu/bottom_navigation" />
Then, create the drawable, giving it a normal drawable and a selected drawable. For the selected item, give it the flag android:state_selected="true" like below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/bg_item_bottom_nav_select" />
<item android:drawable="#drawable/bg_item_bottom_nav_deselect"/>
</selector>
The last part it's just create two new drawables (the example code above i called "bg_item_bottom_nav_select" and "bg_item_bottom_nav_deselect"). One for the item selected and another for the item when it's not selected. Customize it like you want.
Hope this helps you! :)

Why does a Button loses its original behaviour after changing its color?

I am writing an Android app and now I am styling it. I am using a custom theme which is a child of Theme.Holo.Light. I really like Theme.Holo.Light because its buttons have a special effect when you click and hold it. Like the lower button in the picture below:
Not click:
click:
The upper button has been changed color. Now when I click on that button, it doesn't have that effect. I really don't understand why. Can anyone tell me why this happens and how can I get the same effect with a colored button?
And also, the colored button seems fatter.
This is because the button uses a selector to display different colors/effects/drawables based on the state of the click. You can check out the link on Color State List Resource.
To create your own you have to create a slecetor cml file and put it in your drawables folder.
For example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_btn_default_normal_gray" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/shape_btn_default_pressed_gray" android:state_pressed="true"/>
<item android:drawable="#drawable/shape_btn_default_disabled_gray"/>
</selector>
or with colors
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#color/light_green" android:state_pressed="true"/>
<item android:drawable="#color/gray"/>
</selector>
To apply this you have to set the background drawable in your layout xml like this.
<Button
android:id="#+id/my_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some text"
android:background="#drawable/selector_btn_default_gray"/>
That is the "ripple effect" of material design.You have define you own style for that effect.Link below may help you or you may search for many other answers on StackOverflow. Material effect on button with background color
It does not loses its behavior you can see after click (in your second image) the button show same scale as the above have...so by default the background is set as to show that it is button (like with padding or so) and can changes to show oncklick effect...
So when you set your desire background to button...It takes complete change on what is on presently on it and then you have to manually set onclick effect..

how can I implement non-rectangular shapes for buttons in android

hi i have to realize this layout . it has this layout.
I could try to use the icons as imagebuttons but the active state of a button is somewhat like this one !
How should i proceed with this ?
You should use selector as follows:
Prepare 2 images for button states, and put it into res/drawable folder.
button_normal_green.png – Default image button.
button_pressed_yellow.png – Display when button is pressed.
Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow" android:state_pressed="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
3.set background to button
<ImageButton
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_button" />
Have a look at Complete Example

how to change button color when it is clicked in android

By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?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/button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you

Problem setting Android tab background color

I'm adding tab icon via selector like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_cart_selected" android:state_selected="true" />
<item android:drawable="#drawable/tab_cart" />
</selector>
http://img844.imageshack.us/img844/2535/questionn.jpg
All is fine, but my icon is smaller then tab itself and I want to set background color same as icon color. But I cant seem to figure out hot to do that.
Any suggestions?
use a .png transparency.
How to Make a Transparent PNG
test with this image and you will see

Categories

Resources