In my application I should TabLayout and ViewPager.
I want when selected Item set padding for tabIndicatorColor in tabLayout.
By default show me such as this image :
Bu t I want when selected item, show me such as this :
I want set Padding for tabIndicatorColor.
My TabLayout XML code:
<android.support.design.widget.TabLayout
android:id="#+id/serialDetail_tabLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/size50"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:tabIndicatorColor="#color/colorAccent"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextAppearance="#style/allCapsTabLayout"
app:tabTextColor="#color/white" />
How can I it? please help me. Thanks all <3
Yes it is possible without custom view and external library..
you may create one selector file
tab_indicator_line.xml add into drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp" android:bottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true" android:state_pressed="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp" android:bottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Now apply in tabLayout using
app:tabBackground="#drawable/tab_indicator_line"
app:tabIndicatorHeight="0dp"
Tell me if any query..
There are two easy ways to do this. Either you use a custom layout for tab or just add a view below the tab of same color.The second option is more easier.
Related
So i have 4 buttons, and i want to make if one button is clicked by the user it will change its color to red and stay that way until if the user pressed the other 3 buttons
enter image description here
i have read other post about this, but they showed to me to use selector state_selected and etc, but it doesnt work for me. and the other use setBackground in the java code, but i will be using 8 buttons and probably more, and its not going to be efficient to do it that way, is there any more efficient way to do this?
this is the code i have on the drawable xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<solid android:color="#color/red_maroon"/>
<stroke android:color="#color/red_maroon" android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle" >
<solid android:color="#color/red_maroon"/>
<stroke android:color="#color/red_maroon" android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/white"/>
<stroke android:color="#color/red_maroon" android:width="1dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
</selector>
If selector state_selected does not work, check if your button style has this attribute: <item name="android:selectable">true</item>
The style:
<style name="AppButtonOutlineSelectable" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearance">#style/AppButtonShape</item>
<item name="strokeColor">#color/color_selectable_button_stoker_selector</item>
<item name="android:selectable">true</item>
<item name="strokeWidth">1dp</item>
<item name="backgroundTint">#color/color_selectable_button_bg</item>
</style>
<style name="AppButtonShape" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerSize">8dp</item>
</style>
And use color selector under color directory, not drawable selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red" android:state_selected="true" />
<item android:color="#color/white" />
</selector>
The button:
<com.google.android.material.button.MaterialButton
android:id="#+id/btn"
style="#style/AppButtonOutlineSelectable"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Set onClick:
btn.setOnClickListener {
btn.isSelected = btn.isSelected.not()
}
Hi peoples, I am stuck, I would like to reproduce the design of this TabLayout. Does anyone have an idea on how to do it?
Create 2 drawables for selected and unselected as below,
drawable_selected_tab.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#android:color/white"/>
</shape>
drawable_unselected_tab.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/gray"/>
</shape>
Now, You have to create selector using these 2 drawables,
tab_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- For selected tab -->
<item
android:drawable="#drawable/drawable_selected_tab"
android:state_selected="true"/>
<!-- For unselected tab -->
<item
android:drawable="#drawable/drawable_unselected_tab"
android:state_selected="false"/>
</selector>
Now give this tab_Selector.xml to your TabLayout's tabBackground like,
<com.google.android.material.tabs.TabLayout
...
app:tabBackground="#drawable/selector_tab" />
Add more properties to this above TabLayout as per your requirement.
I have a navigation drawer in my android app. Here's the code:
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
android:background="#color/white"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
app:itemTextColor="#color/drawer_item"
app:itemIconTint="#color/drawer_item"
app:itemBackground="#drawable/nav_divider"/>
I've used one the answer to this post to add dividers between the items. Here's the nav_divider drawable that I'm using as itemBackground in my NavigationView:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="#dimen/activity_horizontal_margin">
<shape android:shape="rectangle">
<solid android:color="#color/light_gray"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
Now, the question is how do I change the background color of the selected item. I know how to do that without the divider, but with the divider, it's more complicated.
UPDATE:
Here's what I've tried:
I created another drawable called nav_divider_selected which is exactly like the nav_divider above except with different colors.
Then, I created a drawer_item_background drawable like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/nav_divider_selected" android:state_selected="true"/>
<item android:drawable="#drawable/nav_divider"/>
</selector>
And, replaced the itemBackground of the NavigationView with this new selector (app:itemBackground="#drawable/drawer_item_background"). But, it didn't work. The items all look black, for some reason.
I got it. It was a simple issue. I was just using the wrong state. So, here's the solution. I created two different layer lists (to add the divider to the navigation items):
nav_divider:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="#dimen/activity_horizontal_margin">
<shape android:shape="rectangle">
<solid android:color="#color/light_gray"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
nav_divider_selected:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="#dimen/activity_horizontal_margin">
<shape android:shape="rectangle">
<solid android:color="#color/light_gray"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#color/light_blue"/>
</shape>
</item>
</layer-list>
Then, created a drawer_item_background drawable like this (state_checked should be used not state_selected):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/nav_divider_selected" android:state_checked="true"/>
<item android:drawable="#drawable/nav_divider"/>
</selector>
And, then I used this drawable as itemBackground in my NavigationView:
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
android:background="#color/white"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
app:itemTextColor="#color/drawer_item"
app:itemIconTint="#color/drawer_item"
app:itemBackground="#drawable/drawer_item_background"/>
Also if in-case you do not want to use a selector to achieve that you can do so like this
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
</group>
By wrapping the element under <group android:checkableBehavior="single"> we get a selected background.
Try the below item_background.xml in
app:itemBackground="#drawable/item_background" for NavigationView
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/rgb_red" android:state_checked="true" />
<item android:drawable="#color/rgb_red" android:state_activated="true" />
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/rgb_white" />
<item android:start="-2dp"
android:top="-2dp"
android:end="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/rgb_pale_gray"/>
</shape>
</item>
</layer-list>
</item>
</selector>
hope it will help you
need help
I have searched for my requirement but didn't found so posting my own question.
I need to show line above tabs not below as all solution is for adding the line below the tabs text.
should I use custom tabs or android supports this scenario without making custom ??
links I have viewed are as follows
https://www.google.com.pk/search?q=fabric+documentation&rlz=1C1CHBD_enPK741PK753&oq=fabric+documentation+&aqs=chrome..69i57.5669j0j7&sourceid=chrome&ie=UTF-8#safe=active&q=how+to+adding+line+above+tabs+android
no code is posted as I want to know what should be used .. ur suggestion would be helpful .. thanks
Try this create a background in res/drawable folder and set app:tabBackground="#drawable/selector"
make your like this Tablayout
<android.support.design.widget.TabLayout
android:id="#+id/detail_tabs"
android:layout_width="match_parent"
app:tabSelectedTextColor="#color/colorPrimary"
app:tabIndicatorHeight="0dp"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabBackground="#drawable/selector" />
Here selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="3dp" android:left="-5dp" android:right="-5dp" android:bottom="-5dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true" android:state_pressed="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="3dp" android:left="-5dp" android:right="-5dp" android:bottom="-5dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Hope it will works if not then add comment.
app:tabIndicatorGravity="top"
Its very simple now to show the indicator at the top with this.
I'm trying to style the divider for ListView. What I want is just 2 simple horizontal lines.
list.xml
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ListView" >
</ListView>
styles.xml
<style name="ListView" parent="android:style/Widget.ListView">
<item name="android:divider">#drawable/divider</item>
<!-- 5dp: just to make sure there's enough space -->
<item name="android:dividerHeight">5dp</item>
</style>
divider.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#00ffff" />
<size android:height="1dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#ff0000" />
<size android:height="1dp" />
</shape>
</item>
</layer-list>
The result is a horizontal line with height of 5dp(should be 2, isn't it?) and color of red(color of second item). The first item with color #00ffff does not show up at all.
Can someone please suggest the code for 2 simple horizontal lines?
I'd better answer my own question...
It seems styles.xml - style - item takes the color of the last item in layer-list as background for the entire divider. This last item is on top of other layers, and that is why other items in the layer-list don't show up.
The key is padding.
styles.xml
<style name="ListView" parent="android:style/Widget.ListView">
<item name="android:divider">#drawable/divider</item>
<item name="android:dividerHeight">10dp</item>
</style>
divider.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#0000ff" />
<padding android:top="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#ff0000" />
</shape>
</item>
</layer-list>
So in my case I just need to change 10dp/5dp to 2dp/1dp to get 2 thin horizontal lines.