How to prevent that the keyboard hides my EditText?
I have a RelativeLayout at the top, then below i have a ListView in the center and finally at the bottom i have another relativeLayout with an EditText and a Button inside.
I want the ListView to resize when I click on the EditText and the IME(virtual keyboard) appears. If i put adjustResize in the manifest, the Listview is resized to leave space for the IME, but the RelativeLayout with the EditText that is below is covered by the IME and I can't see what I'm writing. If I put adjustPan, the keyboard pushs up all, the ListView is not resized and i loose the top RelativeLayout.
My Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/application_background"
android:paddingLeft="15px"
android:paddingRight="15px"
android:paddingTop="15px"
android:paddingBottom="15px">
<RelativeLayout
android:id="@+id/rlyParentPost"
android:orientation="vertical"
android:layout_width="450px"
android:layout_height="85px"
android:background="@drawable/app_gradient_color"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/imgUserOfParent"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:background="@drawable/userfeed"
android:layout_width="64px"
android:layout_height="64px"
android:layout_below="@+id/logoImg">
</ImageView>
<TextView
android:layout_below="@+id/logoImg"
android:layout_toRightOf="@+id/imgUserOfParent"
android:id="@+id/lblNameOfParent"
android:textSize="17px"
android:textStyle="bold"
android:layout_marginLeft="5dip"
android:layout_marginTop="11dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff">
</TextView>
<TextView
android:layout_below="@+id/lblNameOfParent"
android:layout_toRightOf="@+id/imgUserOfParent"
android:id="@+id/lblBodyOfParent"
android:textColor="#ffffff"
android:textSize="17px"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_below="@+id/lblBodyOfParent"
android:layout_toRightOf="@+id/imgUserOfParent"
android:id="@+id/lblDateOfParent"
android:textColor="#70ccff"
android:textSize="11px"
android:layout_marginLeft="7dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rlyParentPost"
android:id="@+id/contentLayout">
<ListView
android:isScrollContainer="true"
android:id="@+id/lsvComments"
android:layout_height="515px"
android:layout_width="450px"
android:background="#ffffff"
android:listSelector="@drawable/multi_white_color"
android:drawSelectorOnTop="false"
android:divider="#9aa5ac"
android:cacheColorHint="#00000000"
android:dividerHeight="1px">
</ListView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
开发者_C百科 android:layout_below="@+id/lsvComments"
android:background="@drawable/app_gradient_comments"
android:id="@+id/contentLayout">
<EditText
android:inputType="text"
android:id="@+id/txtComment"
android:hint="What are you working on?"
android:textSize="22px"
android:layout_marginLeft="8px"
android:layout_height="72px"
android:layout_width="344px">
</EditText>
<Button
android:layout_toRightOf="@+id/txtComment"
android:id="@+id/cmdShare"
android:layout_width="79px"
android:layout_height="65px"
android:background="@drawable/feed_comments_multi_stage"
android:layout_marginLeft="7px">
</Button>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
You should not be setting your layout_width and layout_heights with explicit pixel values. Instead, use wrap_parent or fill_parent as appropriate.
If you want somethign like a text view to be a particular width, use dpi units which will adjust correctly on different screen resolutions.
Regarding your specific virtual keyboard problem, I would try setting up the ListView so it takes up the remaining space.
--Set the heights of everything except the ListView to wrap_parent --Set the ListView to fill_parent,
The ListView should take up however much space is available, and thus shrink when you ask it to resize.
I had the same issue and I found a solution. In my case, in the Manifeste.xml, I declared a minSdkVersion=3 (Android 1.5), and my project was compiled with Android 2.2 (SDK 8).In theory, it should work... but not! I modified minsdkVersion to 4 (Android 1.6), then it's OK. It was just an compatibily issue between the different Android SDK. Good luck to you.
if you by any chance are using this:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
this can cause the problem. I originally added that line to fix a bug where the status bar pushes the app downward... but it also prevents the keyboard from pushing the app upward.
sigh.
U can add a scrollview like this :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
..........
</ScrollView>
This would cause the app to focus on the edit text and adjust it to focus on screen ! it also works inside fragments!
If EditText Field is been covered by the KeyBoard Use the following code:
EditText= findViewById(R.id.edittext)
EditText?.getParent()?.requestChildFocus(EditText,EditText)
If you want the Cursor to be in the Focused EditText than use EditText.requestFocus()
after the EditText?.getParent()?.requestChildFocus(EditText,EditText)
which helps to get the focus and Cursor in the Focused EditText.
精彩评论