How to prevent keyboard from opening when activity is opened in android? - android

In my android app, in my profile edit page, when I start the activity, the first edittext field is focused (blinking cursor), and the keyboard gets displayed.
How can I keep it being focused on startup (blinking cursor) but prevent the keyboard from showing up? If that is not possible, then just not focus the edittext on startup.
Thanks.
<EditText
android:id="#+id/textinput_firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="test" />

Just add this line of code in your onCreate(...) method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

This is probably happening for EditText for most users, all you have to do is, just go to the layout file and set the layout's view groups attributes as follows:
android:focusable="true"
android:focusableInTouchMode="true"
For Example.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="41dp"
/>
</LinearLayout>
Note: you have to set the attributes for the first view in the layout
no matter what it is This will set the default focus to false;

Related

Android Custom inputs having same IDs

I've built a custom editText component by extending LinearLayout and aligning a textview and an editText. I reuse them for building multiple input programmatically.
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_input_label"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="#dimen/very_small_margin"
android:layout_weight="1"
android:textAppearance="#style/TextAppearance.MaterialComponents.Body1"
android:textColor="#color/input_label_color" />
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/text_input_edit"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginBottom="#dimen/small_margin"
android:drawablePadding="#dimen/small_margin"
android:background="#drawable/custom_input_bg"
android:paddingStart="#dimen/default_margin"
android:paddingEnd="#dimen/small_margin"
android:paddingTop="#dimen/small_margin"
android:paddingBottom="#dimen/small_margin"/>
</merge>
The problem is that with this way of rendering inputs, I endup with editText sharing the same ID and when I leave the fragment containing them and I come back, the editText all has the same values. Even setting their ID to random int does not work.
What can I do to ?
Thanks for any kind of advice or help.
I have defined the id to View.NO_ID
And it seems that the problem is now solved.

Not able to move view up with keyboard Android

I have gone through various answers but was unable to move my view up when keyboard is visible. I have a edittext in recyclerview on whose focus keyboard is visible.I want the tv_character_limit text to be moved up when keyboard is visible. I need the character limit text to be shown elevated with a z-index. I have set the windowSoftInputMode to adjustResize for the activity and for the fragment in view pager i have added the java code:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Thank you for help.Below is my layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/pink">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_post" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/tv_character_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="30dp"
android:text="#string/character_limit"
android:textColor="#color/tap_to_type"
android:textSize="15dp" />
</FrameLayout>
use,
android:windowSoftInputMode="adjustPan"
in the manifest file for your activity.
ex :
<activity
android:name=".View.Activity.yourActivity"
android:windowSoftInputMode="adjustPan"/>
Instead of FrameLayout, use RelativeLayout and when your EditText gets focus, just change the position of TextView by calling the RelativeLayout class method similar to layout_above.

AppBarLayout traps keyboard focus

I'm trying to ensure that keyboard navigation works properly on my app and all view elements are focusable when users hit the 'Tab' key or 'Up/Down' arrow keys. However, the AppBarLayout seems to trap keyboard focus. I'm running my app on an emulator with a keyboard configured. A sample layout is as below:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/search_fragment_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical"
android:scrollbars="horizontal"
android:focusable="true"
android:nextFocusDown="#id/recycler_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Sample text 1"
android:focusable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Sample text 2"
android:focusable="true"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:focusable="true"
app:layout_constraintTop_toBottomOf="#layout/content_main"
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
Hitting the Tab key or the Up/Down arrows does not switch the focus over to the recycler view and the focus is stuck within the AppBarLayout. Setting the nextFocusDown attribute on the textviews and setting the view elements to remain focusable does not work either. Here is the image. What do I need to do to make this layout accessible?
I think you need to call
findViewById(R.id.search_fragment_app_bar).setTouchscreenBlocksFocus(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
findViewById(R.id.search_fragment_app_bar).setKeyboardNavigationCluster(false);
}
setTouchscreenBlocksFocus
setKeyboardNavigationCluster
The way to release keyboard focus from AppBarLayout is to hook an actual device (NOT the emulator) to a keyboard and hit the keys Cmd+Tab (for Mac OS users) or Windows+Tab (for Windows users).

FrameLayout can not get focus with key press?

I have a view(Let's say it is MyFramLayout) extends FrameLayout, I can not get this FrameLayout get focus when press key(I am working in TV platform).
the layout is something like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.roger.MyFrameLayout
android:id="#android:id/my_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
Anyway to fix the problem?
Whenever you want your custom layout to get focus you will have to call
boolean b = yourLayout.requestFocus();
This will return a whether focus is gained or not.

Maintain keyboard state after device rotation

I have a chat fragment with a listview and a edittext and i wanna when the user rotate the device, if the keyboard was shown, it still showing.
But on my fragment, if the user rotate from landscape to portrait and the keyboard is open, it reapear open. But from portrait to landscape, it closes on rotation.
I have no idea what could be and how could i save the keyboard state to recover it.
Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<com.example.irclient2.adapter.MyEditText
android:id="#+id/chatinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:imeOptions="actionSend|flagNoExtractUi"
android:inputType="textCapSentences" />
<ListView
android:id="#+id/chatlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/chatinput"
android:background="#drawable/fundo_chat"
android:divider="#android:color/transparent"
android:dividerHeight="1.0sp"
android:padding="5dp"
android:scrollingCache="false"
android:smoothScrollbar="true"
android:transcriptMode="normal" >
</ListView>
Try removing/commenting any code you have in your fragment and activity that deals with handling focus and/or the keyboard (to isolate the problem) and try the solution to this question:
Keyboard issues arising on orientation change

Categories

Resources