I'm dynamically creating buttons. I styled them using XML first, and I'm trying to take the XML below and make it programattic.
<Button
android:id="@+id/buttonIdDoesntMatter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="buttonName"
android:drawableLeft="@drawable/imageWillChange"
android:onClick="listener"
android:layout_width="fill_parent">
</Button>
This is what I have so far. I can do everything but the drawable.
linear = (LinearLayout) findViewById(R.id.LinearView);
Button button = new Button(this);
button.setText("Button");
button.setOnClickListener(listener);
button.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
)
);
linear.addView(button);
Try this:
Might be helpful:
Following is the way to change the color of the left icon in edit text and set it in left side.
For me, it worked:
***** Kotlin Version *****
Use below snippet to add a drawable left to the button:
.
Important Point in Using Android Vector Drawable
When you are using an android vector drawable and want to have backward compatibility for API below 21, add the following codes to:
In app level
build.gradle
:In Application class: