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 can I remove a button or make it invisible in Android?

Set button visibility to GONE (button will be completely “removed” — the buttons space will be available for another widgets) or INVISIBLE (button will became “transparent” — its space will not be available for another widgets): View b = findViewById(R.id.button); b.setVisibility(View.GONE); or in xml: <Button … android:visibility=”gone”/>

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

Android Data Binding using include tag

The problem is that the included layout isn’t being thought of as a data-bound layout. To make it act as one, you need to pass a variable: buttons.xml: <layout xmlns:andr…> <data> <variable name=”foo” type=”int”/> </data> <Button android:id=”@+id/button” ….” /> main.xml: <layout xmlns:andr… … <include layout=”@layout/buttons” android:id=”@+id/buttons” app:foo=”@{1}”/> …. Then you can access buttons indirectly through … Read more

Custom circle button

Use xml drawable like this: Save the following contents as round_button.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=”false”> <shape android:shape=”oval”> <solid android:color=”#fa09ad”/> </shape> </item> <item android:state_pressed=”true”> <shape android:shape=”oval”> <solid android:color=”#c20586″/> </shape> </item> </selector> Android Material Effect: Although FloatingActionButton is a better option, If you want to do it using xml selector, create … Read more

Android Preventing Double Click On A Button

saving a last click time when clicking will prevent this problem. i.e. private long mLastClickTime = 0; … // inside onCreate or so: findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // mis-clicking prevention, using threshold of 1000 ms if (SystemClock.elapsedRealtime() – mLastClickTime < 1000){ return; } mLastClickTime = SystemClock.elapsedRealtime(); // do your magic … Read more

Android – border for button

Step 1 : Create file named : my_button_bg.xml Step 2 : Place this file in res/drawables.xml Step 3 : Insert below code <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” android:shape=”rectangle”> <gradient android:startColor=”#FFFFFF” android:endColor=”#00FF00″ android:angle=”270″ /> <corners android:radius=”3dp” /> <stroke android:width=”5px” android:color=”#000000″ /> </shape> Step 4: Use code “android:background=”@drawable/my_button_bg” where needed eg below: <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Your Text” … Read more

Coloring Buttons in Android with Material Design and AppCompat

Officially fixed in Support Library rev.22 (Fri March 13, 2015). See relevant google code issue: https://issuetracker.google.com/issues/37008632 Usage example theme.xml: <item name=”colorButtonNormal”>@color/button_color</item> v21/theme.xml <item name=”android:colorButtonNormal”>@color/button_color</item>