How to set tab widget indicator's height in android? - android

I want to reduce the height of selected tab indicator, I have used 9 patch image for it. Can you please let me know if you have any idea for the same ?
Thanks.

I used this code:
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_example" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_example" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_example" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_example" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_example" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_example" />
and 9 patch images put in drawable, now it works perfectly.

I use a different way of achieving custom design on tabs , create two xml files like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1"
android:background="#drawable/tab_selector"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends"
android:textColor="#drawable/tab_text_selector"
/>
When you set the indicator while adding a tab , use setIndicator(View view) pass this view after inflating it . This way you can achieve any custom design for tabs
Create selector files for changing background or text color on the basis of state.

You need to create custom layout file for highlighted and non-highlighted tabs

Try tabIndicatorHeight attribute. e.g. app:tabIndicatorHeight="1dp". There's also a nice write-up of it here: http://panavtec.me/playing-with-the-new-support-tablayout.

Related

How to modify default color when using ?android:attr/selectableItemBackground on CardView?

Here is my code on CardView:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_home_device_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
card_view:cardCornerRadius="#dimen/cardview_default_radius"
card_view:cardElevation="#dimen/card_elevation_raised"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="#dimen/padding_card">
...
</android.support.v7.widget.CardView>
I don't have any clue to change default color, please help me.
Thank you in advance.
You can change it by defining your own selector drawable. Following are the contents of selectableItemBackground. Create a new drawable resource file and place this selector in it, and change it as per your need.
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true" android:drawable="#drawable/list_selector_background_focused" />
<item android:drawable="#color/transparent" />
</selector>
In your code,
android:foreground="?android:attr/selectableItemBackground"
takes up the default color. In case if you want modify your custom color use a custom drawable for and include it in your cardview as below.
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:foreground="#drawable/custom_bg"/>
and for the custom_bg that you can see in above code, you have to define a xml file in drawable package. So on pre-lollipop devices you will have a solid click effect and on lollipop and above devices you will have a ripple effect on the cardview with custom background specified.

Android: How to brighten up the image of a button when clicked?

I know when a button(without background) is clicked, it changes its color and then flow goes to its onClickListner. But when i set an Image to the Button, it is not the same. I want may Image in Button to brighten up a little so that it will look nice.
I know it has something to do with styling but dont know what style to use,
below is my xml code for the button.
<Button
android:id="#+id/supplier"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignLeft="#id/two_player"
android:layout_alignTop="#id/two_player"
android:layout_marginTop="75dp"
android:background="#drawable/supplier" />
Create one buttonselector in drawable folder then set it button background
btnselector.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/btn_bg_clicked" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/btn_bg" /> <!-- focused -->
<item android:drawable="#drawable/btn_bg" /> <!-- default -->
</selector>
Now set this to button background as like
<Button
android:id="#+id/supplier"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignLeft="#id/two_player"
android:layout_alignTop="#id/two_player"
android:layout_marginTop="75dp"
android:background="#drawable/btnselector" />
i think you got it ...
You have to use a Selector to achieve that.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item
android:state_pressed="true"
android:drawable="#drawable/tab_bg_pressed" />
<!-- Selected tab (using d-pad) -->
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#android:color/transparent" />
</selector>
Supposing the above file is called bg_selector, you set it as the background of the object you want to brighten, and in your case the 'Pressed tab' is what you're looking for. In my 'tab_bg_pressed' you would define another drawable where you would set the background as a brighten effect or whatever you want.

Cannot change focus/state for button png using selector element

Android Studio 0.3.7
Hello,
I have created 2 buttons png and patched using draw9patch.
The buttons will indicate whether the buttons is pressed or unpressed.
I have the following buttons_colours.xml in my values directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/rd_btn_press"/> <!-- pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/rd_btn"/> <!-- unpressed -->
<!-- Default -->
<item android:drawable="#drawable/rd_btn"/>
</selector>
In my layout for the button I have this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/rd_btn"/>
Problem 1: I get an element selector must be declared in my buttons_colours.xml
Problem 2: Not sure if this is related to problem 1, but the button never changes to my rd_btn_press state when I press it.
Many thanks for any suggestions,
you have set wrong background for the button. Replace #drawable/rd_btn with `buttons_colours :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/buttons_colours"/>
also, I don't know why you have two states in your items in the selector. Here an example of working selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rd_btn_press"
android:state_pressed="true" />
<item android:drawable="#drawable/rd_btn"
android:state_focused="true" />
<item android:drawable="#drawable/rd_btn" />
</selector>

Android ImageButton state not changing

I have an issue with my ImageButton not changing state. When I click, or rather touch, the button it stays as the same image. Here is the XML I am using as a selector.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/pushed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:drawable="#drawable/default" />
</selector>
I call this selector from my main.xml as
android:background="#drawable/imagechoice"
imagechoice.xml is the file with the selector
I don't understand why this is not working, unless I have to have some java code, but everything I've seen said this should work.
When using an ImageButton, isn't it the 'src' property you should use and not background?
Make sure that you copy the same images and the button XML into every "drawable" folders (hdpi,ldpi,mdpi).
That's how I solved this problem on my app.
Good luck :)
I have nearly the same XML and it works just fine. Are you sure you're not replacing the drawable in code somewhere?
On another note, your XML can be simplified by using the cascading nature of the state matching.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/pushed"
/>
<item android:state_focused="true"
android:drawable="#drawable/pushed"
/>
<item android:drawable="#drawable/default"
/>
</selector>
This is my xml of a button with my own custom image on it and it works great:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/btn_off" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:drawable="#drawable/btn_off" />
</selector>
Make sure you set Image button background as mentioned below.I think you are not setting the selector as background instead you are setting the image as background.
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_up_selector"
android:text="1"
android:textColor="#fffafa"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"/>

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