ImageButton set drawable using @android:drawable/ programmatically

how to add icon image button in android studio
android imagebutton
android image button with text
android:drawable icon programmatically
how to set image for button in android programmatically
android drawable icons programmatically
android studio image button onclick
change image button image onclick android

I have the following image button in my XML layout

<ImageButton
    android:id="@+id/show_only_checked"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0"
    android:src="@android:drawable/arrow_down_float"
    tools:src="@android:drawable/arrow_down_float" />

Great. Works perfect. But I want to toggle it in the onClick() event to the up version of this drawable in code. Everytime the user clicks the button, I want to toggle back and forth between the up and down arrow. I appear to be too dense to figure this out. I have tried the following code, with no luck.

if (ShowOnlyChecked) {
    view.setBackground(Drawable.createFromPath("@android:drawable/arrow_up_float"));
} else {
    view.setBackground(Drawable.createFromPath("@android:drawable/arrow_down_float"));
}

Any help would be appreciated.

Part of my problem was how to reference "android.R..." to the drawable. The other was which method to call.

Your code will change the view's background image.

Here is how to set the button image:

((ImageButton) view).setImageResource(R.drawable.arrow_down_float);

Imagebutton change programmatically?, Android ImageButton set android:src=”@drawable” programmatically example. Note: Put image into drawable-hdpi folder. Download imagebutton image from below and put inside drawable-hdpi folder. Code for activity_main.xml layout file. Screenshot : Android:src=” ” attribute is used to add image inside image button but some times app developer required some spacial type of functionality in their android application. So here is the complete step by step tutorial for Android ImageButton set android:src=”@drawable” programmatically example.

try:

 imageButton.setBackgroundResource(android.R.drawable.arrow_up_float);

Android ImageButton set android:src="@drawable , An easy way to do this is with an XML drawable "selector. Android will automatically change the image based on the state of the button and� Android ImageView set image source src drawable dynamically using programming coding. Setting up image drawable source from MainActivity.java programming file is very easy because some times there are so much images and app developer need to set image path on coding file.

As you are using ImageButton component, you set things in runtime wrongly, because Drawable.createFromPath provides a path to drawable as say absolute path or the desired path. If you want to set an arrow, then try to add different resolution images of arrow based on resource folder or youId.setBackgroundResource(android:R.drawable.id) or get images from here

https://material.io/resources/icons/?style=baseline

ImageButton, How to use. getDrawable. method. in. android.widget.ImageButton Common ways to obtain ImageButton Set android shape color programmatically. //get the� On ImageButton click, we set the next drawable on the ImageView from the array. On ImageView click, we set the color filter over the ImageView using the colorFilter setter property. Also, we set a random scaleType on the ImageView from the arrayList by shuffling the array and taking the first element.

android.widget.ImageButton.getDrawable java code examples , I've got Image Buttons that I am building dynamically but I can't find a way to do android:src="@drawable/stopwatch" programmatically. March 2016 edited March 2016 in Xamarin.Android. I've got Image Buttons that I am� A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon.

How to set a ImageButton's src programmatically., In android imagebutton is a ui control which is used to display an image with button either in the XML layout file or create it in the Activity file programmatically. we defined ImageButton control and we are showing the image from drawable It is used to set the padding from left, right, top and bottom of the image button. This example demonstrates how do I programmatically set drawableleft in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Android ImageButton with Examples, We can set a color or a drawable in the background of a Button. we set the black background color of a image button programmatically means in java class. This example demonstrates how do I set background drawable programmatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Comments
  • Possible duplicate of Imagebutton change programmatically?
  • Hii There is lot of similar question already answered. stackoverflow.com/questions/14233062/…
  • Check this too stackoverflow.com/questions/16526112/…
  • Ok, both this and Ehsan were almost correct. Here is what worked: ((ImageButton) view).setImageResource(android.R.drawable.arrow_up_float);
  • Well of course you can call setImageResource() with any resource you want
  • I didn't know how to get to "android.R.drawable...", that was one hurdle that is new for me.
  • glad you sorted it out !