How to remove the delay from an android button? - java

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>"

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 change style attribute value in programmatically?

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>

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>

Button color change when button pressed

I have been trying over an hour to make a button's color change when I click the button.
Here is my "mybutton.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#c4161616"
android:endColor="#c4161616"
android:angle="270" />
<corners
android:radius="10dp" />
<stroke
android:width="5px"
android:color="#00FF00" />
</shape>
Now the Button looks like this:
And when the button gets pressed it should look like this:
<gradient
android:startColor="#c4161616"
android:endColor="#c4565656"
android:angle="270" />
I'm a beginner in Android and XML. Can you please show me how to do it?
Bro try this:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>

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