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(); });

Save image to sdcard from drawable resource on Android

The process of saving a file (which is image in your case) is described here: save-file-to-sd-card Saving image to sdcard from drawble resource: Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like: Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher); The path to SD Card can be … 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

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

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

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

Make an Android button change background on click through XML

To change the image by using code: public void onClick(View v) { if(v.id == R.id.button_id) { ButtonName.setImageResource(R.drawable.ImageName); } } Or, using an XML file: <?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/login_selected” /> <!– pressed –> <item android:state_focused=”true” android:drawable=”@drawable/login_mouse_over” /> <!– focused –> <item android:drawable=”@drawable/login” /> <!– default –> </selector> In OnClick, just add this … 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