How to change drawableEnd property of TextView

How to change drawableEnd property of TextView

how to clear textview text in android
block textview android
an edittext object's gettext method returns a string
android set textview text programmatically
how to move textview in android studio
textview scrollto
textview and edittext in android
android textview disabled color

I have a Textview which I set the property drawableRight from my xml file, now I will like to change the drawable programatically. I want when clicked, drawable icon/image should change from let say btn_up to btn_down.

Below is what I tried so far:

Class:

requireText.SetOnClickListener {

requireText.drawableRight = resources.getDrawable(R.drawable.btn_up)

}

xml:

<TextView
  android:id="@+id/requireText"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginLeft="10dp"
  android:background="@null"
  android:clickable="true"
  android:drawableRight="@drawable/btn_down"/>

How do I solve this? Thanks in advance.


There is no .drawableRight or Left, not yet for now, you can try below code:

   class SampleActivity : AppCompatActivity() {

    var up = true
    var drawable: Drawable ?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_signup)

        userName.setOnClickListener {
            if(up){
                up = false
                drawable = resources.getDrawable(com.exolve.eros.R.drawable.sampleDownDrawable)
            }else{
                up = true
                drawable = resources.getDrawable(com.exolve.eros.R.drawable.sampleUpDrawable)
            }

            drawable!!.setBounds(0, 0, 10, 10)
            userName.setCompoundDrawables(drawable, null, null, null)
        }
    }
}

An update to the fact that you want to switch the drawable on click.

TextView, android:drawableEnd, The drawable to be drawn to the end of the text. android :freezesText, If set, the text view will include its current complete text inside of its� Our TextView has four properties that let us specify images to be set around it. These ones are: drawableLeft, drawableRight, drawableTop and drawableBottom. We also can use another one which defines the padding among the text and the images: drawablePadding. Ok then, let’s change the previous layout and get rid of LinearLayout and ImageView


Try

var isClicked = false // declare this at top               
requireText.setOnClickListener{

   requireText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 
   if(!isClicked) R.drawable.btn_up else R.drawable.btn_down, 0)
   isClicked = !isClicked
 }

Kotlin Extension for set/get compound Drawable to TextView , var TextView.drawableEnd: Drawable? get() = drawables[2]. set(value) = setDrawables(drawableStart, drawableTop, value, drawableBottom). set(value) = setDrawables(drawableStart, drawableTop, drawableEnd, value) @Deprecated ( " Consider replace with drawableStart to better support right-to-left Layout " , ReplaceWith ( " drawableStart " ))


try this code to set drawable right in kotlin class

 val my_text_view = findViewById(R.id.my_text_view)
        my_text_view.setOnClickListener(View.OnClickListener { my_text_view.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_upward_black_24dp, 0) })

set android textView drawableEnd programmatically, android change textview drawable color programmatically android add image to How to set the android:drawableEnd of TextView from the java code? the� Change gravity of Image Drawable in TextView 🌌. Contribute to lndmflngs/compound-text-view development by creating an account on GitHub.


set android textView drawableEnd programmatically, Programmatically change button background drawable onClick , Then you have to assign this drawable file to the Button background property in the layout file. Line 24-30 We have linked all our TextView declared in Line-16 of MainActivity with each TextView added in activity_main.xml(Layout File), so as to access their properties. Line-41 Since our text_view1 has no text attached to it at this point, we are adding text to it using property setText(), syntax : Textview_Name . setText(“Text String”);


The power of TextView (Part 1): Drawables, Our TextView has four properties that let us specify images to be set around it. These ones are: drawableLeft, drawableRight, drawableTop and drawableBottom. If you observe above code snippet, we are getting the TextView control which we defined in XML layout file using id property and setting the text using setText() method. Android TextView Attributes The following are some of the commonly used attributes related to TextView control in android applications.


Setting the Color of a TextView Drawable for Android, The Android TextView supports displaying a drawable beside, above, or below the text, but how do you programatically change the color of the� How to change text in textview on button click android. TextView is used to hold text and basically application developer set text on textView using activity_main.xml layout file but sometimes developer needs some different task so you can change textView text easily through MainActivity.java programming file.