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>
Related
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
I created a specially effect, if a button pressed. So the the xml file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/clicked"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/clicked"
/>
<item android:drawable="#drawable/shape"
/>
</selector>
shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<padding android:bottom="0dp" />
<solid android:color="#2B4881" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
In activity.xml I set the file as background for the button:
<Button android:background="#drawable/background"/>
But if i press the button, it tooks a while, until the button changes it's color. Can i remove this delay?
Use below code inside selectors
<item android:drawable="#drawable/clicked" android:state_pressed="true"></item>
<item android:drawable="#drawable/shape"></item>"
I am a newbie in Java-Android developing.
I want to add this drawable on a view like button's background
but it doesn't draw anything.
This is my background drawable_shining.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<gradient
android:endColor="#000"
android:centerColor="#111"
android:startColor="#000"
android:angle="45"
android:type="linear" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#000"
android:gradientRadius="100"
android:startColor="#555"
android:type="radial" />
</shape>
</item>
</layer-list>
[here is code and layout][1]
according to documentation layer-list is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an element inside a single element.
in your code, write each <shape> in <item> tag. Your final code will be look like this,
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<gradient
android:endColor="#000"
android:gradientRadius="100"
android:startColor="#333"
android:type="radial" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#000"
android:gradientRadius="100"
android:startColor="#333"
android:type="radial" />
</shape>
</item>
You can use same format like below code :
Reference : angrytools
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#47A891"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
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));
}