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
Related
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"
I have button and I set style from xml now on click of button I want to change style like
style="#style/ButtonNotSelected"
Place to
style="#style/ButtonSelected"
programmatically..
Please help..!!!
It will better if you use xml and a selector , to get your button change color when it is pressed, you could define an XML file called res/drawable/my_button.xml.
Set my_button.xml as background to your button.
<?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_pressed="false"
android:drawable="#drawable/button_normal" />
</selector>
#drawable/button_pressed something like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#449def"/>
<stroke android:width="1dp" android:color="#2f6699"/>
<corners android:radius="3dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
#drawable/button_normal something like this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270"/>
<stroke android:width="1dp" android:color="#2f6699"/>
<corners android:radius="4dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
try like this:
ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
Button button = new Button(newContext);
OR
btn.setBackgroundResource(R.drawable.back_button_answer);
\res\drawable\back_button_answer.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dip" />
<!-- background -->
<gradient
android:startColor="#D6D7D6"
android:centerColor="#E2E2E2"
android:centerY="0.75"
android:endColor="#D6D7D6"
android:angle="270"
/>
<stroke android:width="2dip" android:color="#fff"/>
</shape>
I want to have a pattern as a background in my RelativeLayout which is repeated.
<RelativeLayout
android:layout_width="0dp"
android:layout_height="600dp"
android:layout_weight=".20"
android:background="#drawable/timetable_background"
android:tileMode="repeat" >
But the tileMode does not work and shows only one run.
This is my pattern:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="50dp" android:height="50dp">
<shape android:shape="rectangle" >
<solid android:color="#color/timetableEntryBackground1" />
</shape>
</item>
<item android:top="50dp" android:height="50dp">
<shape android:shape="rectangle" >
<solid android:color="#color/timetableEntryBackground2" />
</shape>
</item>
</layer-list>
What do I need to change, that the background repeats?
You should use the tileMode inside the item of your layer-list, like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="50dp" android:height="50dp">
<shape android:shape="rectangle" >
<solid
android:color="#color/timetableEntryBackground1"
android:tileMode="repeat" />
</shape>
</item>
...
</layer-list>
Here, for the mixed button I have used
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/stripes"
android:tileMode="repeat"
android:dither="true"
android:antialias="true" />
</item>
</layer-list>
And for other buttons I have used
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
Now I want to add that black border around the mixed button how to do?
Use shape in layer list under item in xml file like below
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/stripes"
android:tileMode="repeat"
android:dither="true"
android:antialias="true" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
</layer-list>
It'll help you..
reference How overlay bitmap and shape in layer-list properly
You are super close, you just need to add the Shape (for your border) to the <layer-list>
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/stripes"
android:tileMode="repeat"
android:dither="true"
android:antialias="true" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
</layer-list>
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));
}