Pass touch events from parent view to a specific subview - android

I have a android view layout structure as follows
<RelativeLayout>
<TextView>
<EditText>
</RelativeLayout>
The EditText, due to my limited layout skills, is sized as wrap_content and is significantly smaller than the parent RelativeLayout.
But when user touches on RelativeLayout, I would like the UI to effectively behave as if user just focused on the EditText instead.
It doesn't matter to me if cursor starts in the front, middle, or end. (Preferably at the end).
Is this something that i can achieve in the code or on the layout?
Thanks!

What you can do is to call the editText's onClick in the onClick method of the relative layout. This way all clicks on the relativeLayout go to the editText.
This should work see answer:
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusableInTouchMode(true);
editText.requestFocus();
//needed for some older devices.
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
I would do this programatically (in code), because it does not handle how the views look, but how they behave. Therefore, say in MVC, it belongs in the controller.

Related

How does WindowSoftInputMode work?

My layout requires itself to adapt to the available space when the keyboard appears, so I have this set in my Manifest:
android:windowSoftInputMode="stateVisible|adjustResize"
How does WindowSoftInputMode work, though? How does it choose what to adjust (padding, margins, image size, etc.)?
Is it possible to not resize a specific view (like an ImageView) and instead take away more padding? Can I choose what it resizes (besides just adjustResize and adjustPan)?
Completely impractical to handle resizing UI elements yourself and I understand it is discouraged. You can use the below code to explicitly handle soft input:
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
Check out THIS on how to handle the keyboard...

ClickListener in ScrollView intercepts scrolling

I have two rows of imageViews in a ScrollView. The imageViews all have onClickListener and when i want to scroll in this area it doesn't work. So i guess the Click listeners intercept the scrolling of the ScrollView. What's the best way to change this behaviour ?
My View hierarchy is like this:
<ScrollView>
<RelativeLayout>
<FrameLayout>
</FrameLayout>
<RelativeLayout>
</ScrollView>
in the FrameLayout i put a Fragment which has a LinearLayout in which i inflate other LinearLayouts like this:
productHolder.productLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
}
});
Maybe you could use an OnTouchListener instead of an OnClickListener.
In the OnTouchListener you can check if the action of the motion event was a click and consume the event (return true). Otherwise you can leave the event for other listeners (return false) to consume.
Note, that you might need to add some additional implementation for figuring out which image view was clicked, however without seeing your layout, it's hard to give you a hint how to do it.

Temporary EditText and keyboard on bottom of screen

I'm trying to create EditText on bottom of screen and open keyboard (then this EditText will move above keyboard with RelativeLayout + ALIGN_PARENT_BOTTOM), it works fine with SetContentView but with addContentView the RelativeLayout + ALIGN_PARENT_BOTTOM doesn't work for me and the keyboard overlap the EditText.
In some way I want to generate this temporary EditText independently the layout being displayed on screen, so If I press any button on any layout I call this function to create a temporary EditText on bottom of screen and bring the keyboard.
I can't change the whole main layout because this layout is generated by Unity3D. I need a way to generate this EditText and act independently the layout is being displayed on screen.
My code:
public void openInput()
{
runOnUiThread(new Runnable()
{
EditText inputName = new EditText(context);
inputName.setInputType(InputType.TYPE_CLASS_TEXT);
inputName.setFilters(new InputFilter[] { new InputFilter.LengthFilter(40)} );
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(600, RelativeLayout.LayoutParams.WRAP_CONTENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.addView(inputName, editTextParams);
addContentView(relativeLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
inputName.requestFocus();
InputMethodManager msg = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
msg.showSoftInput(inputName, InputMethodManager.SHOW_IMPLICIT);
});
}
Any idea how to solve this issue?
Thanks
put your whole layout in a scroll view, (as your set contentview)
and then when you do addContentView the layout will scroll up to the focused view, rather than push the view up
I'm using a pop-up dialog window to edit long list of parameters one by one. That's also helps to add more helpful information on the parameter being edited, like instructions, hints, etc...

How to use whole screen as button in android?

How can i get an onClick response when the user touches any part of the screen?
Place a Button inside the layout manager.
and specify
fill_parent
for width and height.
and remove the border of the button.
this will work.
For Example you have ah RelativeLayout means,
RelativeLayout mLyout=(RelativeLayout)findViewById(R.id.layout01);
relativeclic1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// Here do your Task
}
});
You can cut off the touch events before they are ever dispatched by overriding dispatchTouchEvent:
https://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)
You can get the statistics of the touch -- first, repeated, etc -- from the MotionEvent passed.

requestFocus doesn't work properly for EditText

A lot of time was spent to solve the problem, and it looks easy, but I'm really tired and couldn't find the solution.
I have an Activity, activity has 4 EditText components, 2 of them has popup menu (AlertDialog) which contain the list, next one - is disabled for edit, and last one - is editable, and should show the soft keyboard, when user is tapping on it.
Also, my root LinearLayout has LinearLayout which contain inside RelativeLayout. The last one is need for AdvBanner. Last LinearLayout(RelativeLayout) is aligned to the bottom of root layout.
The part of XML that describes it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="bottom">
<RelativeLayout
android:id="#+id/AdvLayoutReserveArea"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="bottom" />
</LinearLayout>
When activity is start, editable EditText has focus with GREEN border and cursor is blinking. After few seconds left, the AdvBanner is loaded and shown. When it happens, editable EditText lost focus.. from this moment, my life be like a nightmare.
Let's look step by step.
Problem 1.
If in THIS MOMENT (when Adv loaded and appears) user is editing an EditText field via the soft keyboard, focus is lost, border take a GRAY color, and if user continue to typing a text is have no result - symbols are not printed (CURSOR in EditText is too lost).
I THINK any user will be annoyed - when you typing text, and cursor is inactive, because in background some adv is loaded and it take focus for self.
To solve this, in method when Adv is loaded (is shown) I try to back focus manually to EditText by requestFocus method.
public void onAdLoaded()
{
// TODO Auto-generated method stub
// add app specific code for this event here...
// called when an ad is successfully displayed on device
CountEdit1.requestFocus();
}
Yes, the cursor is returned to EditText field, and if soft keyboard is active, user can still typing text, but border of EditText field stay GRAY...
NOTE: actually I'm not sure about the difference between GREEN and GRAY border of focused EditText.. GREEN is usually when user is tapping on it, and GRAY, probably, when we want to request a focus manually (using requestFocus() etc)
Problem 2. (As result of solvation Problem #1).
After soft keyboard was closed, if user tap on editable EditText field, it take focus and cursor appears inside, but no reaction for showing soft keyboard again! Tapping of it do not show soft keyboard, but looks like the edit field in copy mode - when user can select a text and cut/copy it to clipboard.
My goal is easy for a first look. I just want to SAVE the cursor and focus to editable EditText field (CountEdit1) while soft keyboard is shown and user typing some text.
And normal reaction when user tapping EditText - as usually, just show me the soft keyboard!
I read all issues here, I combined different methods (clearFocus, requestFocusFromTouch etc), just not enough of time and space to describe all that I tried to do to solve this. The main problems are described above.
Hope for help and solving the problem...
Thanks in advance..
The goal is solved, the workaround is an easier than I thought. Problem #2 is fixed by using onClick () method. Sufficient condition for appearing of soft keyboard that use of both clearFocus() and requestFocus() methods.
CountEdit1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
CountEdit1.clearFocus();
CountEdit1.requestFocus();
}
});
The soft keyboard appears when user is tapping on the EditText field.
Fixed.
Works for me:
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
editField.requestFocus();
mgr.showSoftInput(editField, InputMethodManager.SHOW_IMPLICIT);
userInput.post(new Runnable() {
public void run() {
userIdInput.requestFocus();
}
});
Have a go at this and tell if your problem is still unsolved.
You should request focus after view is created in fragment or activity:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
CountEdit1.requestFocus();
}
try this:
public void onAdLoaded()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
CountEdit1.requestFocus();
InputMethodManager mgr = (InputMethodManager) base.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(CountEdit1, InputMethodManager.SHOW_IMPLICIT);
CountEdit1.setSelection(CountEdit1.getText().length());
}
},0);
}

Categories

Resources