How to set a gradient background to a Material Button?

To use a custom drawable background with the MaterialButton you can use the android:background attribute: <MaterialButton app:backgroundTint=”@null” android:background=”@drawable/bkg_button_gradient” … /> NOTE: It requires at least the version 1.2.0-alpha06 Currently it is very important to add app:backgroundTint=”@null” to avoid that the custom background doesn’t get tinted by default with the backgroundTint color. With lower versions of … Read more

How do I set a different color for the pressed state of the button?

create xml file using the button image like this with mybutton.xml in drawable folder <?xml version=”1.0″ encoding=”utf-8″?> <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:state_pressed=”true” android:drawable=”@color/blue” /> <item android:state_focused=”true” android:drawable=”@color/gold” /> <item android:drawable=”@color/grey” /> </selector> and use this in button xml code android:background=”@drawable/mybutton” add those color codes in the resource–>values–>colors.xml like this <?xml version=”1.0″ encoding=”utf-8″?> <resources> <color name=”blue”>#0066cc</color> <color … Read more

Rounded corners on material button

With the Material Components Library:. Add the dependency to your build.gradle: dependencies { implementation ‘com.google.android.material:material:1.3.0’ } In this case you can use a MaterialButton in your layout file: <com.google.android.material.button.MaterialButton …. style=”@style/Widget.MaterialComponents.Button” app:cornerRadius=”..” app:strokeColor=”@color/colorPrimary”/> Use app:cornerRadius attribute to change the size of corner radius. This will round off the corners with specified dimensions. You can also … Read more

How is using OnClickListener interface different via XML and Java code? [duplicate]

These are exactly the same. android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your DoIt method. Here is what using a android:onClick=”DoIt” does internally: Button button= (Button) findViewById(R.id.buttonId); button.setOnClickListener(new View.OnClickListener() { … Read more

Is any difference between a MaterialButton and a simple Button?

If you are using a MaterialComponents Theme there is no difference between <Button /> and <com.google.android.material.button.MaterialButton />. There is an auto-inflation enabled which will replace <Button with <com.google.android.material.button.MaterialButton at runtime. The MaterialComponentsViewInflater replaces some framework widgets with Material Components ones at inflation time, provided if a MaterialComponents theme is in use. Something similar happens also … Read more

Android – make an arrow shape with xml

What you need is to create a shape xml file in your project’s drawable-xxx folder and then use this shape as background for a button. Here is the shape file called arrow_shape.xml: <?xml version=”1.0″ encoding=”UTF-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android” > <!– Colored rectangle–> <item> <shape android:shape=”rectangle”> <size android:width=”100dp” android:height=”40dp” /> <solid android:color=”#5EB888″ /> <corners android:radius=”0dp”/> </shape> </item> … Read more

How to add a button to a PreferenceScreen?

For the xml: <Preference android:title=”Acts like a button” android:key=”@string/myCoolButton” android:summary=”This is a cool button” /> Then for the java in your onCreate() Preference button = findPreference(getString(R.string.myCoolButton)); button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { //code for what you want it to do return true; } }); This will appear like a normal Preference, with … Read more

How to make a round button?

Create an xml file named roundedbutton.xml in drawable folder <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” android:shape=”rectangle”> <solid android:color=”#eeffffff” /> <corners android:bottomRightRadius=”8dp” android:bottomLeftRadius=”8dp” android:topRightRadius=”8dp” android:topLeftRadius=”8dp”/> </shape> Finally set that as background to your Button as android:background = “@drawable/roundedbutton” If you want to make it completely rounded, alter the radius and settle for something that is ok for … Read more