Can a View be focused and invisible at the same time? - android

I want to have an EditText in focus so it is edited, but also invisible. In other words, all the user will see is the keyboard.
Is that possible?

AFAIK, an invisibility Edittext cannot have a view( I have never tried) so the workaround for you is
You can set a transaparent background for edittext to achieve this.
youredittext.setBackgroundResource(android.R.color.transparent);
or you can directly set the attribute in XML like this
android:background="#null"

I know I might be late, but recently I had to face a similar problem.
The solution could also be this one (use in styles.xml or in the layout):
android:layout_width = 0dp
android:layout_height = 0dp
That will also let your EditText be still focusable.

My two alternate approaches:
Approach 1: Fade it out
view.animate().alpha(0.0f).setDuration(0);
and fade it in when you want it back
view.animate().alpha(1.0f).setDuration(0);
Setting duration to 0 ensures it happens instantaneously.
Approach 2: Set the view outside the screen
LayoutParams params = view.getLayoutParameters();
int offset = -80; // offset from the screen border, choose your own
params.setMargins(0, 0, offset, 0); // left, top, right, bottom
view.setLayoutParams(params);
and do
params.setMargins(0, 0, 0, 0); // left, top, right, bottom
view.setLayoutParams(params);
when you want it back.

Related

How to hardcode TextView aligment distance from start?

I have a layout with white background and big textViews with darkblue background and white text.
I want to give some space to the text from the start of the darkblue background rectangle.
I already tried but only can set start, center, end, top, etc; and I want to specify it numerically.
How I can meet that goal?
I think what you are looking for can be achieved by specifying the padding value on the TextView. Look for android:padding attribute. If you want to give some space from the start, you can use android:paddingStart attribute on the TextView.
Hope this helps.
Probably setting margin to your layout might help
TextView tv = (TextView)findViewById(R.id.my_text_view);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.setMargins(10, 0, 0, 0); //move 10 px to right (increase left margin)substitute parameters for left, top, right, bottom
tv.setLayoutParams(params);//tv.setMargins(left, top, right, bottom);
im not sure if this is excatly what you are looking for

showAsDropDown Gravity parameter not working

I am trying to center horizontally a PopUpWindow on the anchored view, but the Gravity.CENTER parameter is being ignored.
This is the method I am using:
popupWindow.showAsDropDown(anchorView, 0, 0, Gravity.CENTER);
No matter what CENTER gravity value I enter (also TOP or BOTTOM don't work), the pop-up always displays at the same place: the anchor's top left corner, unless I use the START or END values.
The documentation and javaDoc does not list any restriction on the values that it accepts, so I assume that it also accepts any of the CENTER, TOP and BOTTOM values: https://developer.android.com/reference/android/widget/PopupWindow#showAsDropDown(android.view.View,%20int,%20int,%20int)
I also tested this with anchor views that are smaller, equal and larger than the pop-up window and the pop-up window is set to not fill the screen width, as intended.
It appears that this method does nothing different from the equivalent method that does not take the gravity parameter
popupWindow.showAsDropDown(anchorView, 0, 0);
Is this a bug or is this not how it is suppose to be used?
For PopupWindow.showAsDropDown(), it seems that is working as intended, tracing through its source, you can eventually see that the gravity parameter is only meant for horizontal values.
Tracking the value of gravity from PopupWindow.showAsDropDown() to PopupWindow.findDropDownPosition(), we can already see the hint #param gravity horizontal gravity specifying popup alignment
Then a step deeper to Gravity.getAbsoluteGravity() also hints at the same thing #param gravity The gravity to convert to absolute (horizontal) values.
But I believe what you are looking to achieve is at Show a PopupWindow centralized, which uses PopupWindow.showAtLocation()
PopupWindow.showAtLocation(View parent, int gravity, int x, int y)
This is a bit late, but if you want to center the pop up window with showAsDropDown(), you can do this:
target.post(() -> {
popupWindow.showAsDropDown(anchor, (anchor.getWidth() / 2), yOffset, Gravity.CENTER);
});
It is inside target.post() to ensure that the anchor is drawn when we call getWidth(), otherwise it returns 0.
What I'm doing is:
fun showPopupWindow(popupWindow : PopupWindow){
popupWindow.width = 200
popupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
popupWindow.showAsDropDown(anchor, -(popupWindow.width - anchor.width)/2, 0, Gravity.CENTER)
}
Make sure popupWindow.width and anchor.width are not 0, -1 or -2

Change edittext with equally along left and right when setting width

I have following setup.
There is an edittext with controllers on left and right for resizing.
When user resizes by touching and moving the left controller, I am calculating the distance between moved x-coordinate and the mid-point of right side of edittext.
The idea is that the right side has to remain static, where as on resizing from left, the left bound has to increase of decrease like wise. I am assigning new width and center to edittext on resize from left as follows:
ivLeftControl.setPivotX(traversedPoint.x);
editTextViewNew.setGravity(Gravity.CENTER);
editTextViewNew.setWidth((int) (traversedWidth-ivLeftControl.getWidth()*2));
editTextViewNew.setPivotX(centerX - (traversedWidth - originalWidth)/2);
But the resize is happening only along the right side of edittext, instead of happening along left side. Even when I dont change the pivot, the resize happens along right side only.
Any suggestion on how this features could be addressed the best?! Keeping in mind, the entire setup of controllers and edittext is enveloped using a parent layout.
Solution - MarginLayoutParams
you need to create MarginLayoutParams and then set height , width , margins in it and set to your view.
MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
layoutParams.width = mPixelSize;
layoutParams.height = mPixelSize;
layoutParams.setMargins(mLeftMargin, mTopMargin, 0, 0);
requestLayout();

Animate button to match_parent without replacing button's text

I have a menu consisting of 5 buttons with color background (width set to match_parent, and height to wrap_content).
I want it to fill all the screen with the background color after i click it with the button's text either staying on his place or moving slovly to center.
After a lots of searching for different animation methods i picked that one
Transition changeBounds = new ChangeBounds();
changeBounds.setDuration(1000);
TransitionManager.beginDelayedTransition(landingScreenLayout, changeBounds);
ViewGroup.LayoutParams sizeRules = mButton.getLayoutParams();
mButton.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
I chose it because animating it with "scale" stretches the button's text. But the problem is, that when i start animating it, its text immediately goes to the center, what ruins everything - obliviously
Any help appreciated ;)
Edit: oh, and i've already forgot - i tried to make this 'button'/menu item with a RelativeLayout and TextView - what worked, but it looked horribly dirty in XML and i wouldn't want to go that way

Is setPadding broken for LinearLayout?

I have a very short problem. I have a custom control that is based upon LinearLayout. I inflate it's view from xml', in which the root is set as element.
Now when I try to set the padding of this custom control by "this.setPadding(x,x,x,x)" it does not affect the TextView and ImageView I add to this control in a few lines of code later.
Currently I am bypysing it, by setting the margin of each control separately, to achieve the global padding of the whole custom control, but it would be nice to know if there is a catch in using setPadding dynamicaly, or maybe a mistake is mine.
To simplify, my case looks like that:
setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams lp = new
LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT); setLayoutParams(lp);
setPadding(0, 30, 0, 30); //I know it's in px
Afterwards I'm adding a large image, which shrinks due to it's size, but the padding of LinearLayout(which I try to set dynamicaly) does not affect it, only if I set margin on it directly.
Any tip would be greatly appriciated.
After a little of digging, found an answer through other StackOverflow question:
https://stackoverflow.com/a/13363318/905938
Basicaly if one person sets a background reasource with a selector xml given (as in my case) it overrides completely the previous padding setting. So the padding I was setting within the custom control initialization was lost as soon as it was set.
Now that I know the problem, I basicaly just intercept the call to this method in my custom control like this:
#Override
public void setBackgroundResource(int resid)
{
int paddingLeft, paddingTop, paddingRight, paddingBottom;
paddingLeft = getPaddingLeft();
paddingTop = getPaddingTop();
paddingRight = getPaddingRight();
paddingBottom = getPaddingBottom();
super.setBackgroundResource(resid);
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
That solves the problem, and I hope will solve a problem for anybody who will find this question with a similar problem.

Categories

Resources