How to customize TabLayout tabs separately? - java

This is my TabLayout xml code:
<android.support.design.widget.TabLayout
app:tabBackground="#drawable/profile_tab_selector"
android:id="#+id/profileTabLayout"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorPrimary"
app:tabTextColor="#color/white">
</android.support.design.widget.TabLayout>
And this one is for tabBackground="#drawable/profile_tab_selector":
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<stroke android:width="1dp" android:color="#color/white"/>
<solid android:color="#color/white"/>
</shape>
</item>
<item>
<shape>
<stroke android:width="1dp" android:color="#color/white"/>
<solid android:color="#color/blue"/>
</shape>
</item>
</selector>
I want to design it like this, with rounded cornenrs:
The problem is that in selector I can not detect the first and the last tabs to modify them. And this is the result:

You have given the shape in the drawable file , but you didn't give corner radius.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
<stroke
android:width="10dp"
android:color="#android:color/transparent"></stroke>
<solid android:color="#23cf5a" />
</shape>
And you can design something like this too
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="16dp"
android:bottomRightRadius="16dp"
android:radius="32dp"
android:topLeftRadius="16dp"
android:topRightRadius="16dp" />
<solid android:color="#color/tab_color" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:height="32dp" />
</shape>

Your Solution is here
Don't Use : app:tabBackground="#drawable/profile_tab_selector"
Use : android:background="#drawable/profile_tab_selector"
You also try it.

Related

adding corners to my tabs background in android coding

I was implementing a drawable file to add a background to my tabs but i want it to have corners . I've used a shape and corners tags all embedded into a selector tag but it has no effects to my background corners . May I please get your help ? here's the code i've implemented :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/bleue_sky" android:state_selected="true">
<shape android:shape="rectangle">
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
/>
</shape>
</item>
<item android:drawable="#color/blackTransparent"/>
</selector>
Try This
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#drawable/main_tab_bg"
android:paddingHorizontal="10dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="#drawable/tab_layout_background"
app:tabIndicator="#drawable/tab_layout_indicator"
app:tabIndicatorColor="#null"
app:tabIndicatorFullWidth="true"
app:tabIndicatorGravity="center"
app:tabMode="auto"
app:tabRippleColor="#null"
app:tabSelectedTextColor="#FFFFFF"
app:tabTextColor="#000000" />
res/drawable/main_tab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#DDDDDD"/>
</shape>
</item>
<item android:top="1dp" android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
res/drawable/tab_layout_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="9dp"
android:bottom="9dp">
<shape android:shape="rectangle">
<solid android:color="#color/transparent" />
<corners android:radius="15dp" />
<size android:height="30dp"/>
</shape>
</item>
</layer-list>
res/drawable/tab_layout_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="9dp"
android:bottom="9dp">
<shape android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="#000000" />
<size android:height="30dp"/>
</shape>
</item>
</layer-list>
output like this

How to give gradient on view in at top , bottom and transparent center?

I want to give gradient to this view like around 10% of imageview at top , transparent center with 70% and 30% gradient at the bottom.
How i can achieve this
Instead of just using a shape with a gradient use a layer-list with 2 gradients with specified height for each
API 21
The android:bottom , android:top attributes represent the padding for the layer list, change these to change the percentage of the gradient to show
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="690dp"
android:gravity="top">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#android:color/transparent"
android:startColor="#000000" />
</shape>
</item>
<item
android:top="610dp"
android:gravity="bottom">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:type="linear"
android:endColor="#000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
If your minSDK is 23+ then it's more easier to specify the height for each layer type
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="40dp"
android:gravity="top">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#android:color/transparent"
android:startColor="#000000"
android:type="linear" />
</shape>
</item>
<item
android:height="70dp"
android:gravity="bottom">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#000000"
android:startColor="#android:color/transparent"
android:type="linear" />
</shape>
</item>
Then just apply this as the background of your ViewPager
Try this out, hope this solves your issue
top_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:centerColor="#android:color/transparent"
android:centerY="0.1"
android:startColor="#000000" />
</shape>
bottom_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="#android:color/transparent"
android:centerY="0.3"
android:startColor="#000000" />
</shape>
background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg" />
<item android:drawable="#drawable/bg2" />
</layer-list>
gradient.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#00000000"
android:centerColor="#00000000"
android:endColor="#color/primaryTextColor"
android:angle="270"
android:centerY="0.7"
android:dither="true"
android:type="linear"
/>
</shape>
layout.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bottomConstraint"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/gradient">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvTitle"
style="#style/TextStyleNormal.Large.Bold.White"
android:paddingHorizontal="#dimen/padding_large"
android:paddingTop="#dimen/padding_medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/bottomConstraint"
app:layout_constraintTop_toTopOf="#+id/bottomConstraint"
tools:text="System Solution to Online Education:
Look To Northeast communal." />
</androidx.constraintlayout.widget.ConstraintLayout>
This will have transparent view but in bottom the gradient would be dark .
You can achieve it like this
gradient_top.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2B3135"
android:centerColor="#2B3135"
android:endColor="#android:color/transparent"
android:angle="270"
android:centerY="0.1"
/>
</shape>
gradient_bottom.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2B3135"
android:centerColor="#2B3135"
android:endColor="#android:color/transparent"
android:angle="90"
android:centerY="0.3"
/>
</shape>
layout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/ib"/>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:background="#drawable/gradient_top"/>
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="#drawable/gradient_bottom"/>
</RelativeLayout>

Java .xml creating a drawable button background

New to programming, 3 weeks beginner.
I am making a button with stacked rectangles through drawable xml.
This is my button .xml code, very straight forward:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D5A04F"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
<Button
android:id="#+id/leftnavbar"
android:layout_width="212dp"
android:layout_height="279dp"
android:layout_marginTop="16sp"
android:layout_marginBottom="500sp"
android:background="#drawable/rounded_rectangle"
/>
</android.support.design.widget.NavigationView>
</android.support.constraint.ConstraintLayout>
and this is my drawable .xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="#dimen/mr_dialog_fixed_width_minor"
android:paddingTop="#dimen/mr_dialog_fixed_width_major"
>
<item
android:top="5sp"
android:left="5sp"
android:right="5sp"
android:bottom="50sp">
<shape android:shape="rectangle">
<solid android:color="#D3AE71" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="30sp"
android:left="30sp"
android:right="30sp"
android:bottom="600sp">s
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="160sp"
android:left="30sp"
android:right="30sp"
android:bottom="470sp">
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="290sp"
android:left="30sp"
android:right="30sp"
android:bottom="340sp">
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="420sp"
android:left="30sp"
android:right="30sp"
android:bottom="210sp"
>
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="550sp"
android:left="30sp"
android:right="30sp"
android:bottom="80sp">
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
</layer-list>
The only item that is shown in drawable is the first, the rest of the rectangles fail to show.
Suggestions or pointers to the reason will be much apreciated. thank you for your time and attention...
First of all please do not use "sp" for things other than text font size.
Second, pls look into excessive size of your android:bottom, like 600sp for example - they are leaving no space for respective shapes.
Your button's height is too short,chang android:layout_height="279dp" to android:layout_height="700dp"

How to have a button with backgroundTint and corner radius

I wanna have a button with backgroundTint and corner radius :D
Here is the style I defined:
<style xmlns:tools="http://schemas.android.com/tools" name="myButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:background">#drawable/button_light</item>
<item name="android:backgroundTint" tools:targetApi="lollipop">#e0e0e0</item>
</style>
And the #drawable/button_light code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="#FAFAFA" />
</shape>
And I apply #style/myButton to the button I want and the result is I have round corners but no ripple effect (backgroundTint doesn't work indeed).
How can I solve this?
You can use layer-list to achieve this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="#FAFAFA" />
</shape>
</item>
<!-- Put any image or anything here as the drawable -->
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
Create a drawable/butt.xml file containing:
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#41ba7a" />
<stroke
android:width="2dip"
android:color="#03ae3c" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#3AA76D" />
<stroke
android:width="2dip"
android:color="#03ae3c" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
</item>
</selector>
2: Use it in button tag in any layout file in your code
<Button
android:layout_width="220dp"
android:layout_height="220dp"
android:background="#drawable/butt"
android:text="#string/btn_scan_qr"
android:id="#+id/btn_scan_qr"
android:textSize="15dp"
/>
you can use from this library :
material ripple effect
to add ripple effect to your button and adding your shape to button
like this :
<com.balysv.materialripple.MaterialRippleLayout
style="#style/RippleStyleBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
>
<Button
android:id="#+id/call_btn"
android:layout_width="60dp"
android:layout_height="25dp"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:text="call"
android:textSize="12sp"
android:background="#drawable/call_btn_bg"
android:textColor="#color/deep_orange_600"
/>
</com.balysv.materialripple.MaterialRippleLayout>

Rounded corners for TABS in android

I want to achieve rounded corners for the tab that I've constructed in my application. So far I was able to come up with this
I would like my rounded corners to look as so. (I've coded it in such a way that only the right and left corners appear but when the states change it looks like the above image)
Below is the code that I've written so far. How can I achieve proper rounded corners through code ?
SELECTED TAB.XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"/>
<gradient
android:startColor="#000"
android:endColor="#000"
android:gradientRadius="400"
android:angle="-270"/>
</shape>
UNSELECTED TAB.XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#8858585a"
android:endColor="#88a9a9a9"/>
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
</shape>
Thanks for your response !! :)
I think you should use 4 shapes:
for left button not selected
for left button selected
for right button not selected
for right button selected
And then write selector to use for button background, see example for the left button (for the right just the similar):
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="rectangle">
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp"/>
<gradient
android:startColor="#000"
android:endColor="#000"
android:gradientRadius="400"
android:angle="-270"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#8858585a"
android:endColor="#88a9a9a9"/>
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp"/>
</shape>
</item></selector>
In java file, put this
tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.roundedcorners);
roundedcorners.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
Created alternate shapes to solve the problem
Note: These xml files are in addition to the two mentioned.
SELECTED TAB ALTERNATE.XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
<gradient
android:startColor="#000"
android:endColor="#000"
android:gradientRadius="400"
android:angle="-270"/>
</shape>
UNSELECTED TAB ALTERNATE.XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#8858585a"
android:endColor="#88a9a9a9"/>
<corners
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"/>
</shape>
Afterwards, add this in your tab selection listener.
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
int selectedTabPosition = tab.getPosition();
View firstTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(0);
View secondTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1);
if (selectedTabPosition == 0)
{ // that means first tab
firstTab.setBackground(getDrawable(R.drawable.selected_tab));
secondTab.setBackground(getDrawable(R.drawable.unselected_tab));
} else if (selectedTabPosition == 1)
{ // that means it's a last tab
firstTab.setBackground(getDrawable(R.drawable.selected_tab_alternate));
secondTab.setBackground(getDrawable(R.drawable.unselected_tab_alternate));
}
}
For those who still looking for solution. This is what i done.Put tablayout inside MaterialCardView,
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"`enter code here`
app:strokeColor="?attr/colorAccent"
app:strokeWidth="1dp">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="27dp"
app:tabGravity="fill"
app:tabIndicatorColor="?attr/colorAccent"
app:tabIndicatorGravity="stretch"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#android:color/white"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="?attr/colorPrimary">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.card.MaterialCardView>
tabselector_backgroud.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/gradient_tab_selected"
android:state_selected="true" />
<item android:drawable="#drawable/gradient_tab_unselected"
android:state_selected="false" />
</selector>
gradient_tab_selected.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#color/lime"
android:endColor="#color/lime"
android:startColor="#color/lime"
android:type="linear" />
</shape>
gradient_tab_unselected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"`enter code here`
android:centerColor="#color/white"
android:endColor="#color/white"
android:startColor="#color/white"
android:type="linear" />
</shape>
I think this can be done with one drawable for one view. For 2 tab need only 2 drawable. This will also worked for tabs with border color.
For Left Tab:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false">
<layer-list >
<item>
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_red_dark" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:right="-10dp">
<shape>
<solid android:color="#android:color/holo_red_dark" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true">
<layer-list >
<item >
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_green_dark" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:right="-10dp">
<shape>
<solid android:color="#android:color/holo_green_dark" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Left Tab Background Preview:
For Right Tab:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false">
<layer-list >
<item>
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_red_dark" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:left="-10dp">
<shape>
<solid android:color="#android:color/holo_red_dark" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true">
<layer-list >
<item >
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_green_dark" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:left="-10dp">
<shape>
<solid android:color="#android:color/holo_green_dark" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Right Tab Background Preview:
If there is more than 2 tabs Then this if for Middle Tab:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false">
<layer-list >
<item>
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_red_dark" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:left="-10dp" android:right="-10dp">
<shape>
<solid android:color="#android:color/holo_red_dark" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true">
<layer-list >
<item >
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_green_dark" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:left="-10dp" android:right="-10dp">
<shape>
<solid android:color="#android:color/holo_green_dark" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Middle Tab Background Preview:
Drawables have both selected and unselected state
Another, completely different approach, might be to use a library like Appcelerator Titanium or PhoneGap.
Both libraries let you "program" your android in HTML5/CSS/Javascript. Where "rounded tabs" abound.
Just a thought ...
This code are for 2 tabs in tablayout:
One is selected and two is unselected mode
Drawable xml:
When selecting Left tab:
tabselectionleft:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"/>
<solid android:color="#android:color/white"/>
</shape>
notabselectionleft:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#color/colorAccent1" android:endColor="#color/colorAccent3"/>
<corners android:topRightRadius="5dp"
android:bottomRightRadius="5dp" />
</shape>
</item>
</layer-list>
When selecting right tab:
tabselectionright:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomRightRadius="5dp"
android:topRightRadius="5dp"/>
<solid android:color="#android:color/white"/>
</shape>
notabselectionright:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="true">
<corners
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp" />
<gradient
android:endColor="#color/colorAccent3"
android:startColor="#color/colorAccent1" />
</shape>
</item>
In main layout tablayout decelartion
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabMode="fixed"
app:tabGravity="fill"
android:fitsSystemWindows="true"
android:clickable="true"
app:tabIndicatorColor="#color/colorAccent2"
app:tabIndicatorHeight="3dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:tabPaddingStart="10dp"
app:tabPaddingEnd="10dp"
android:clipToPadding="true"
android:clipChildren="true"
/>
Calling this drawable should happen programmatically as shown below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
firstTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(0);
secondTab = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1);
tabLayout.setOnTabSelectedListener(this);
firstTab.setBackground(getDrawable(R.drawable.tabselectionleft));
secondTab.setBackground(getDrawable(R.drawable.notabselectionleft));
}
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
int selectedTabPosition = tab.getPosition();
if (selectedTabPosition == 0)
{ // that means first tab
firstTab.setBackground(getDrawable(R.drawable.tabselectionleft));
secondTab.setBackground(getDrawable(R.drawable.notabselectionleft));
} else if (selectedTabPosition == 1)
{ // that means it's a last tab
firstTab.setBackground(getDrawable(R.drawable.notabselectionright));
secondTab.setBackground(getDrawable(R.drawable.tabselectionright));
}

Categories

Resources