I am trying to apply background style on button for that I created a drawable resource file but it's unable to take its effect. It's only showing white button only.
Below is my code:
log_out.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/accent"/>
<corners
android:radius="10dp"/>
</shape>
activity_main.xml
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="LOGOUT"
android:background="#drawable/log_out"/>
How can I achieve the desired layout?
It is due to the application theme. This structure will not work if you are using a Material Design theme.
If you change the main theme with parent = "Theme.AppCompat.DayNight" as an example, it will work.
Related
I want to make a gradient button in an Android app that I'm developing in Kotlin. I have made the drawable file for the gradient background but when I do give the button the customized background it doesn't work, nothing changes. This is the code for the gradient background and below that is the XML code for the button.
<item>
<shape android:shape="rectangle">
<gradient android:startColor="#color/start_color"
android:endColor="#color/end_color"/>
<corners android:radius="10dp"/>
</shape>
</item>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo"
android:layout_marginHorizontal="26dp"
tools:layout_editor_absoluteX="16dp"
app:cornerRadius="10dp"
android:textSize="20sp"
android:text="Create an account"
android:background="#drawable/create_acc_btn"
android:fontFamily="#font/montserrat" />
You just have to fix your gradient drawable file.
remove the item tag from it and put android xml namespace declaration like this :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#color/start_color"
android:endColor="#color/end_color"/>
<corners android:radius="10dp"/>
</shape>
And add this line to your button xml code :
app:backgroundTint="#null"
without this your gradient background won't show up
Note : MaterialButton manages its own background to control elevation, shape, color and states. Consider using backgroundTint, shapeAppearance and other attributes where available. A custom background will ignore these attributes and you should consider handling interaction states such as pressed, focused and disabled
You are also going to lose ripple effect when using custom background.
I have been experimenting with button attributes and what I can do with buttons in general. I wanted to modify the way a button looks when clicked.
I created three drawable resource files, two for each button states(pressed or default) and one for state list to pass to the button in the main activity xml.
However this does not work as the button stays the same when I click it.
custom_button_pressed.xml file code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/button_color_pressed"/>
<corners android:radius="30dp"/>
</shape>
custom_button.xml file (default state file) code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="30dp"/>
<solid android:color="#color/button_color"/>
</shape>
custom_button_full.xml file code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/custom_button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/custom_button"/>
</selector>
code fragment for the button in the main xml file:
<Button
android:id="#+id/buttonAboutApp"
android:layout_width="160dp"
android:layout_height="105dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="44dp"
android:background="#drawable/custom_button_full"
android:drawableLeft="#mipmap/about_app_icon"
android:text="#string/About_app"
android:textAllCaps="false"
android:textColor="#color/app_background"
app:backgroundTint="#color/button_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
what am I doing wrong ? Thank you in advance
If you are using a version of the Material Components library less than 1.2.0, the android:background attribute is not supported by MaterialButton. If your app/activity uses a MaterialComponents theme, any <Button> tag will be automatically inflated as a MaterialButton instance, so your custom background won't work.
Since you're trying to override the button's appearance anyway, you probably want just a basic android.widget.Button instead of a MaterialButton. You can achieve this by specifying the fully-qualified name in the view tag of your layout file:
<android.widget.Button
android:background="#drawable/custom_button_full"
.../>
I'm working on an Android application where I want to create a windowBackground with a centered element and a layout also with a centered element. I want these elements to be in the exact same position, with the layout overlapping the background. The problem I'm having is that the layout and the background seem to be calculating center differently (see image). Why is this happening, and what can I do to line the elements up?
This is what I see right now. The red box is created by the background and the green box is created by the foreground. Screenshot was created with a Nexus 5X API 26 emulator.
Foreground layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:background="#color/foreground_box"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerInParent="true" />
</RelativeLayout>
Background Drawable (applied via android:windowBackground in my theme)
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/background" />
<item android:gravity="center">
<shape
android:gravity="center"
android:shape="rectangle">
<solid android:color="#color/background_box" />
<size android:width="10dp"
android:height="10dp" />
</shape>
</item>
</layer-list>
For clarity, my colors file is
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="background">#ffffff</color>
<color name="background_box">#AAFF0000</color>
<color name="foreground_box">#AA00FF00</color>
</resources>
Full source for this sample project is available at https://github.com/HofmaDresu/AndroidCenteredTest
The reason windowBackground includes both the heights 1) statusBar and 2) actionBar
Modify below line in your background.xml
<item android:gravity="center" android:top="80dp"> // 56 actionBarSize + 24 statusBarHeight
You may need to manage this programatically as statusBarHeight and actionBarSize varies based on device API/resolution.
Here is the result. For testing, have resized background size bit bigger so that overlapping between views and background become visible.
It is probably because of the extra space taken up by the ActionBar in the foreground.
To fix this, you can add a margin to your View in the foreground layout as:
android:layout_marginBottom="?android:attr/actionBarSize"
After test it in AS, I can say you that the right code for you background_drawable is this:
<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/background" />
<item android:gravity="center" android:bottom="48dp">
<shape
android:gravity="center"
android:shape="rectangle">
<solid android:color="#color/background_box" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
using android:top, the red square go more down than center. Need to use android:bottom instead to center background. By my tests results that 48dp is the right value.
I have this:
round_button.xml
<xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#dec60000"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#860000"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
My Button:
<Button
android:id="#+id/incrementBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/round_button"
android:onClick="onClick"
android:soundEffectsEnabled="true"
android:text="0"
android:textSize="50sp"
tools:ignore="HardcodedText" />
Dynamically, I want to change the background color (which is defined in the round_button xml) programmatically. Is there a way I can do this?
If you want to define certain states for your button, you could set them all in xml, without having to do it programmatically (if you do, you can indeed set a filter, but it can get messy if you have many states and conditions IMO).
I'll detail the steps here:
1) Creating a xml with the states you want
You can create a xml with a selector in your drawable folder with the defined states. As an example,
button_bkg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bkg_is_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/bkg_is_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/bkg_default"/>
</selector>
Let's call this file button_bkg.xml. In the example above, I have listed 3 states: pressed, disabled and default, which means that, when the button is pressed, it will assume the bkg_is_pressed background and, when I set the button to disabled (either in xml or programmatically through setEnabled(boolean), it will assume bkg_is_disabled background.
2) Creating the backgrounds
Now you will define what you want the background to be in the xml files you defined (bkg_is_pressed, bkg_is_default, bkg_is_pressed). In your case, in example, you would take each shape defined in your round_button.xml file and separate them into each one of the xml files you defined for the states. In my case, I defined a layer-list:
bkg_is_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/button_corner_radius"/>
<solid android:color="#color/color_alert"/>
<stroke
android:width="#dimen/universal_1_pixel"
android:color="#color/color_gray_dark"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/button_corner_radius"/>
<solid android:color="#color/color_mask_highlighted"/>
</shape>
</item>
</layer-list>
You will do that for each of the states.
It is important to note that, if you are going to build for API 21+, you can define a ripple effect by creating ANOTHER button_bkg.xml file in your drawables-v21 folder, which would be like this:
button_bkg.xml (in your drawable-v21 folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bkg_is_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/bkg_is_pressed" />
To use the ripple, you can define a color as explained below:
bkg_is_pressed.xml (in your drawable-v21 folder)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/color_mask_highlighted">
<item android:drawable="#drawable/bkg_is_default" />
</ripple>
You only have to put the button_bkg.xml and the bkg_is_pressed.xml into your drawable-v21 folder file. In my case, bkg_is_default and bkg_is_disabled.xml were the same for both 21+ and 21- APIs, so I didn't add it to my drawable-v21 folder, I just created it in the drawable folder.
I want to emphasize that you STILL need the other files in your regular drawable folder so that devices with API 21- will work properly.
3) Assigning that background to your button
Lastly, you just have to define that background to your button:
<Button
...
android:background="#drawable/button_bkg
/>
So, there you have it. This way, you don't need to set the styles programmatically, you can just define all the backgrounds (according to your states) in the xml files.
But, if you also prefer to set them all programmatically, you can do the same, just use setBackground and use the xml files you defined and apply the state logic you want to it (if button is pressed, setBackground(bkg_is_pressed) and so on)
I hope that helps, let me know if that works for you.
I solved it by setting a ColorFilter:
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
myButton.setResource(mDrawable);
You could construct the shapes from code, depending on the color you need to use, create a StateListDrawable from those and set it as your buttons background.
I'm trying to create to make this form in Android
You see the EditText has some shadows at the front. How do I create that?
I created AutoComplete Text Views for State and Place. But how to I add that line and arrow on it?
You can set background drawable to achieve.either create custom drawable with shape or use background images and put it in the layout xml
android:background="#drawable/text_bg"
create an "testing_gradient.xml" file in /res/drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/grey">
<shape
android:shape="rectangle"
android:thickness="10dp"></shape>
</item>
<item
android:drawable="#color/white"
android:left="50dp">
<shape android:shape="rectangle"></shape>
</item>
</layer-list>
apply the code in layout xml file
<EditText
android:background="#drawable/testing_gradient"
android:layout_width="match_parent"
android:textColor="#color/black"
android:text="User input"
android:paddingLeft="60dp"
android:layout_height="60dp"/>
final result
Again, you can add more layer to input more images