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()
Related
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()
}
I have a view that is added dynamically. Sometimes, the view is only partly visible since its bottom is off the screen. In this case, I want to move the view up. However, I don't know how to detect whether it is offscreen or not and by how much.
Edit: the context for this problem is that I have an edittext that I want to show a custom soft keyboard right next to it. Here is the code that I use to move the custom keyboard.
public void moveKeyboardNextToView(View view) {
int[] location = new int[]{0, 0};
view.getLocationInWindow(location);
Rect r = new Rect();
view.getGlobalVisibleRect(r);
int height = r.bottom - r.top;
int newTop = r.bottom - view.getHeight() / 2 - this.mKeyboardView.getHeight() / 2;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) this.mKeyboardView.getLayoutParams();
params.setMargins(0, newTop, 0, 0);
this.mKeyboardView.setLayoutParams(params);
this.mKeyboardView.invalidate();
this.mKeyboardView.post(new Runnable() {
#Override
public void run() {
int[] location = new int[2];
Rect r2 = new Rect();
mKeyboardView.getLocalVisibleRect(r2);
double abc = r2.bottom;
}
});
}
The way I would do this is, is by calculating it with the following info:
the location of the view on screen (getLocationOnScreen(int[]))
the width/height of the view (getMeasuredHeight() and getMeasuredWidth())
the width/height of the screen
If you have this information, it isn't that difficult anymore to calculate everything you need. Just make sure that you are using the same units (pixels, dpis etc) for every value.
its kind of a late answer but for future reference I created a library for this purpose.If your view is inside a scrollview you can use my library
It adds an onScrollChangedListener from ViewTreeObserver and every time scroll changes recalculates the visible percentage of the view and calls a custom listener.
You can find it here : PercentVisibleLayout
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);
}
});
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)
I am developing an application for Android and I am using a popup window when the user clicks a specific menu bar object(consisting of small images lined up horizontally) on the bottom of the screen.
On the click I want the popup window to be anchored to the top-left corner of the view that was clicked and be shown on top.
The only methods that seem to be relevant are showAsDropDown(View anchor, int xoff, int yoff) and showAtLocation(View parent, int gravity, int x, int y). The problem with showAsDropDown is that it is anchored to the bottom-left corner of the view.
Is there another way to implement this?
popupWindow.showAtLocation(...) actually shows the window absolutely positioned on the screen (not even the application). The anchor in that call is only used for its window token. The coordinates are offsets from the given gravity.
What you actually want to use is:
popupWindow.showAsDropDown(anchor, offsetX, offsetY, gravity);
This call is only available in API 19+, so in earlier versions you need to use:
popupWindow.showAsDropdown(anchor, offsetX, offsetY);
These calls show the popup window relative to the specified anchor view. Note that the default gravity (when calling without specified gravity) is Gravity.TOP|Gravity.START so if you are explicitly using Gravity.LEFT in various spots in your app you will have a bad time :)
I wrote a sample Kotlin code which will show a PopupWindow above the anchor view.
private fun showPopupWindow(anchor: View) {
PopupWindow(anchor.context).apply {
isOutsideTouchable = true
val inflater = LayoutInflater.from(anchor.context)
contentView = inflater.inflate(R.layout.popup_layout, null).apply {
measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
)
}
}.also { popupWindow ->
// Absolute location of the anchor view
val location = IntArray(2).apply {
anchor.getLocationOnScreen(this)
}
val size = Size(
popupWindow.contentView.measuredWidth,
popupWindow.contentView.measuredHeight
)
popupWindow.showAtLocation(
anchor,
Gravity.TOP or Gravity.START,
location[0] - (size.width - anchor.width) / 2,
location[1] - size.height
)
}
}
You just needed to move the popupWindow by the height of its anchor using the yoff parameter in the showAsDropDown(View anchor, int xoff, int yoff) syntax.
popupWindow.showAsDropDown(anchor, 0, -anchor.getHeight()+popupView.getHeight);
Also, be aware that if the max height allowed to anchor does not allow for the transformation, the popup might not show up properly.
popupWindow.showAtLocation(anchor, Gravity.BOTTOM, 0, anchor.getHeight());
I have this code: PopupWindow below a specific view (Gravity End) for all sdk version.
// display the popup[![enter image description here][1]][1]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mPopupWindow.showAsDropDown(v, 0, 0, Gravity.END);
} else {
mPopupWindow.showAsDropDown(v, v.getWidth() - mPopupWindow.getWidth(), 0);
}
Here View v is ImageButton Calendar.
The one you want to use is showAtLocation(...). You specify the anchor view (the one the user clicks), and position it relative to that via the gravity parameter and offsets. Think of the gravity parameter like the PopupWindow is almost like a child view and the parent view is like a container layout.
You should be able to put Gravity.LEFT | Gravity.TOP as the parameter.
you can display the popup always above the anchor by following
popupWindow.showAsDropDown(anchor, 0, -anchor.getHeight()-popupView.getHeight);
Sample example:
ScrollView scrollView = new ScrollView(context);
popupWindow.setContentView(scrollView);
scrollView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int he=scrollView.getMeasuredHeight();
popupWindow.showAsDropDown(items,0, -items.getHeight()-he);
After using so many solutions, I got one solution to display the PopupWindow above any View in Kotlin.
To display the popup window above the view, you can use the showAsDropDown() function.
popUp.showAsDropDown(v, 0, (-0.2 * v.height).roundToInt(), Gravity.CENTER)
With:
v: View(Anchor)
0: offSetX
(-0.2 * v.height).roundToInt(): offSetY
Gravity.CENTER: Gravity
For example: https://medium.com/#bhattmeet887/popupwindow-above-the-specific-view-kotlin-ab1a199581eb
Here's using the android-x solution, which should even work in case you have a floating UI using SAW permission (System Alert Window) :
#SuppressLint("InflateParams")
private fun showPopupWindow(anchor: View) {
val popupWindow = PopupWindow(anchor.context)
popupWindow.isFocusable = true
popupWindow.inputMethodMode = PopupWindow.INPUT_METHOD_NOT_NEEDED
popupWindow.contentView = LayoutInflater.from(anchor.context).inflate(R.layout.popup_layout, null)
PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.BOTTOM)
}
This is for alignment of bottom-left.
If you need bottom-center, you could use this, for example:
#SuppressLint("InflateParams")
private fun showPopupWindow(anchor: View) {
val popupWindow = PopupWindow(anchor.context)
popupWindow.isFocusable = true
popupWindow.inputMethodMode = PopupWindow.INPUT_METHOD_NOT_NEEDED
val inflater = LayoutInflater.from(anchor.context)
val contentView = inflater.inflate(R.layout.popup_layout, null)
contentView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
popupWindow.contentView = contentView
PopupWindowCompat.showAsDropDown(popupWindow, anchor, (anchor.measuredWidth - contentView.measuredWidth) / 2, 0, Gravity.BOTTOM)
}
popupwindow.showAsDropDown(anchor,0, -125);
this thing work for me