How to create Button Dynamically in android?

Create/Remove button onClick of + button and – button as below: public void onClick(View v) { switch(v.getId()){ case (R.id.plusbutton): Button myButton = new Button(this); myButton.setText(“Add Me”); LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout); LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); ll.addView(myButton, lp); break;. case (R.id.minusbutton): Button myButton = new Button(this); myButton.setText(“Remove Me”); LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout); LayoutParams lp = … 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

Rounded Button in Android

You can do a rounded corner button without resorting to an ImageView. A background selector resource, button_background.xml: <?xml version=”1.0″ encoding=”utf-8″ ?> <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <!– Non focused states –> <item android:state_focused=”false” android:state_selected=”false” android:state_pressed=”false” android:drawable=”@drawable/button_unfocused” /> <item android:state_focused=”false” android:state_selected=”true” android:state_pressed=”false” android:drawable=”@drawable/button_unfocused” /> <!– Focused states –> <item android:state_focused=”true” android:state_selected=”false” android:state_pressed=”false” android:drawable=”@drawable/button_focus” /> <item android:state_focused=”true” android:state_selected=”true” android:state_pressed=”false” android:drawable=”@drawable/button_focus” … 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

Get listview item position on button click

do you execute this btnNxt = (Button) findViewById(R.id.btnNext); btnNxt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //Here I need to get that position }); inside the getView method? if so it’s very easy btnNxt = (Button) findViewById(R.id.btnNext); btnNxt.setTag(position); btnNxt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { int position=(Integer)arg0.getTag(); });

android – How can I make a button flash?

There are several, depending on what kind of flashing you mean. You can, for example, use alpha animation and start it as your button first appears. And when the user clicks button, in your OnClickListener just do clearAnimation(). Example: public void onCreate(Bundle savedInstanceState) { final Animation animation = new AlphaAnimation(1, 0); // Change alpha from … Read more

Android Background Drawable Not Working in Button Since Android Studio 4.1

The Android Studio 4.1 new-project wizard, for many of its templates, has the project use the Material Components for Android library. And, it sets up the default theme to be based on Theme.MaterialComponents.DayNight.DarkActionBar. A side effect of this is that any <Button> elements in a layout get turned into MaterialButton widgets, not regular Button widgets. … Read more