How to remove drawableleft - android

I have a button and a text-view, text-view has a drawable-left.
After click on button the drawable-left should be removed and a plain text should be set to Text-view, but I don’t know how to remove drawable-left from code.
Thanks in advance.

The drawableLeft (or any of the similar attributes) XML attribute can be modified (removing a drawable in your case) via code using something like this:
yourTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
yourTextView.setText("The Text You Need In There");
The constructor for the method is in this order:
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Read more about the method setCompoundDrawablesWithIntrinsicBounds here

We can sets the Drawables (if any) to appear to the left of, above, to the
right of, and below the text.
Use 0 or null if you do not want a Drawable there.
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
The Drawables' bounds will be set to their intrinsic bounds.
Calling this method will overwrite any Drawables previously set using
{#link #setCompoundDrawablesRelative} or related methods.
And if we wants to set Drawables then we can use :
textView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);

The drawables of a TextView can be set programatically via the setCompoundDrawables method.
So you could try this:
textView.setCompoundDrawables(null, null, null, null);
Or
textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

Add a Kotlin Extension
If you are going to be doing this frequently, adding an extension makes your code more readable
fun TextView.clearDrawables() {
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
}
To use the extension, simply call
view.clearDrawables()

How to do this and apply to any of the four drawables with DataBinding
#BindingAdapter(value = [
"leftDrawable",
"topDrawable",
"rightDrawable",
"bottomDrawable"], requireAll = false)
fun setCompoundDrawables(textView: TextView,
#DrawableRes left: Int?,
#DrawableRes top: Int?,
#DrawableRes right: Int?,
#DrawableRes bottom: Int?) {
textView.setCompoundDrawablesWithIntrinsicBounds(
left ?: 0,
top ?: 0,
right ?: 0,
bottom ?: 0
)}

Set All Position is Zero Your Drawable item is remove
Its Working Using This setOnFocusChangeListener method
et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
et_feedback.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
//TODO using this Drawable icon remomve onclick
et_feedback.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
et_feedback.setTextColor(Color.BLACK);
}
});

Related

How to set a Custom View (not an image) at the end of a multiline TextView in Android?

I am trying to add a custom view (the portion with red boundary will be replaced with my custom view having a 3 dots loader animation) at the end of a multi-line TextView in my Android app.
I know how to specifically add an ImageView using a SpannableString like this:-
fun addImageToEndOfTheString(text: String, drawableResourceId : Int ,context: Context) : SpannableStringBuilder {
val drawable = ContextCompat.getDrawable(context, drawableResourceId)!!
drawable.setBounds(0, 0, 98, 50)
val rocketImageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
val ssBuilder = SpannableStringBuilder(text)
ssBuilder.setSpan(
rocketImageSpan,
text.length-1,
text.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
return ssBuilder
}
But the problem is I have a custom view which I want to place at the end of my TextView. How can I achieve that?
Note : My custom view is a subclass of View.
simply: you can't.
in posted example you aren't adding ImageView, you are adding ImageSpan with just drawable, thats very not the same...
Views can't hold/host other Views, you have to use ViewGroup or some subclass
easiest way would be to make some LinearLayout with vertical orientation and two Views: TextView and your custom View. second one may have android:visibility="gone" and you can show it only when needed

How to remove custom margins / padding and set it back to reading what was defined in the XML Layout

In my XML layout I define margins / padding as normal:
android:layout_marginBottom="54dp"
I then click a button, and I programatically override this by doing this:
param.setMargins(0, 0, 0, 0)
textInputLayout.layoutParams = param
Now, I need to click a button again, and it should go back to just reading the margin value that I defined in the XML layout. How can I clear / remove the custom margin?
I would expect there to be something like
param.clear()
Is there something like this? Or do I from this point forwards always need to override it programatically?
You have to set margins again after second time button click. Below code might help you.
// Instance variable
private var isSecondTime = false
// I have used FrameLayout as my parent, Replace with your parent layout. E.g LinearLayout
val params = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
And handle button click as per your requirement. Like:
button.setOnClickListener {
if (!isSecondTime) {
isSecondTime = true
params.setMargins(0, 0, 0, 0)
} else {
// setting margin again to 54 dp
//setMargins(left, top, right, bottom)
params.setMargins(0, 0, 0, convertDPToPx(54))
}
textView.layoutParams = params
}
Converting DP to Pixel:
private fun convertDPToPx(dip: Int): Int {
val r: Resources = resources
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dip.toFloat(),
r.displayMetrics
).toInt()
}

Can't Change Android Button Padding

I have a table of buttons and am attempting to dynamically create and set the padding around the text of each with the following function:
public static void AddChartColBtn(TableRow row, String txt, String style) {
Button note_chart_btn;
note_chart_btn = new Button(ctx);
note_chart_btn.setText(txt);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.setMargins(0, 0, 0, 0);
note_chart_btn.setLayoutParams(params);
row.addView(note_chart_btn);
}
I've tried working with LayoutParams classes to set margins and paddings, etc...but no matter what I try the button always fills the entire space of the current table cell, ignoring any padding. The Button class doesn't appear to have layout params, padding that can be manipulated directly. How can dynamically create and declare a button with 0 padding around it's text?
set Both
params.setMargins(0, 0, 0, 0);
params.setPadding(0, 0, 0, 0);
Use like this.
Button note_chart_btn;
note_chart_btn = new Button(ctx);
note_chart_btn.setText(txt);
note_chart_btn.setGravity(Gravity.CENTER);
note_chart_btn.setPadding(left, top, right, bottom);
I hope this will help you.
Try setting something like this
android:padding="10dp" for every button you have in your layout in the .xml file of your layout
setPadding should after the setBackgroundResource
Like this:
button.setBackgroundResource(R.mipmap.btn_bg)
button.setPadding(100, 0, 100, paddingBo0tom)

Reset view to default after padding applied

I have Progressbar view, in which i have applied
progressBar.setPadding(300, 0, 0, 0);
Again when i touch progressBar it should get reverted to its normal view, which was before applying padding to view.
progressBar.setPadding(0, 0, 0, 0);
tried with above padding, but view not reflected to its original size as it was before applying padding
Any suggestion how to achieve this.
You're assuming that the padding was zero on all sides before you adjusted it. It might be better to save the existing padding values before you adjust them so you can go back, i.e.:
int mLeft, mTop, mRight, mBottom;
if(isFirstPeriodEmpty) {
mLeft = smsProgressBar.getPaddingLeft();
mTop = smsProgressBar.getPaddingTop();
mRight = smsProgressBar.getPaddingRight();
mBottom = smsProgressBar.getPaddingBottom();
smsProgressBar.setPadding(300, 0, 0, 0);
} else {
//Reset padding values
smsProgessBar.setPadding(mLeft, mTop, mRight, mBottom);
}
HTH
have fun with it and make a custom progressbar called ToggleProgressBar that toggles between the two levels of padding? or just setPadding back to the old value?
You could try to call invalidate() after you change it's padding.. progressBar.invalidate()

How to set property "android:drawableTop" of a button at runtime

How to set property "android:drawableTop" of a button at runtime
Use
button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
If you use
button.setCompoundDrawables(left, top, right, bottom);
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.
Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
final Drawable drawableTop = getResources().getDrawable(R.drawable.btn_check_buttonless_on);
btnByCust.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnByCust.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
}
});
Button button = (Button) findViewById(R.id.button);
button.setCompoundDrawables(left, top, right, bottom);
I use this code for use the "Theme.Holo" button with a "Custom image" at left and change it (the image)with a function that is called from various ways.
protected void app_dibujarLogojuego() {
if(bitmaplogojuego!=null){
bitmaplogojuego.recycle();
bitmaplogojuego=null;
}
Drawable LOGO = null;
if(verjuego.equals("COSA1")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA1); }
if(verjuego.equals("COSA2")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA2); }
if(verjuego.equals("COSA3")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA3); }
if(verjuego.equals("COSA4")){ LOGO = getResources().getDrawable(R.drawable.img_logo_COSA4); }
BUTTON_DECLARED_ID.setCompoundDrawablesWithIntrinsicBounds(LOGO, null , null, null);
}
btn.setBackgroundResource(R.drawable.your_image_name_here);
If you are using Kotlin, you can use extension method to make things look elegant.
fun TextView.setDrawableTop(iconId: Int) {
val icon = this.context?.resources?.getDrawable(iconId)
this.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
}
Then you can use it like this:
// myTextView: TextView
myTextView.setDrawableTop(R.drawable.ic_happy)
Create an extension function like this and set top drawable like this
tvAccepted.setTopDrawable(R.drawable.ic_preparing_order_active)
fun TextView.setTopDrawable(icon: Int) {
this.setCompoundDrawablesRelativeWithIntrinsicBounds(0,icon,0,0)
}
where
setCompoundDrawablesRelativeWithIntrinsicBounds(left/start, top, right/end, bottom)

Categories

Resources