Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with EditText and soft keyboard in a fragment

I'm currently building an android application that enables users to take pictures and write details of it. The application uses Sherlock libraries.

I've implemented a SherlockFragment to display the image and a few EditText and TextView to enable users to key in information of the image. However, when one of the EditText is on focus, all the fields are pushed up and the soft keyboard pops up.

Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/linear"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"      android:background="#FFFFFF">      <ImageView         android:id="@+id/imageView1"         android:layout_width="fill_parent"         android:layout_height="200dip"         android:layout_above="@+id/textView"         android:layout_marginTop="20dip"         android:layout_marginBottom="20dip"         android:contentDescription="desc" />      <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Title:"         android:textStyle="bold"         android:layout_above="@+id/editTextSimple"          android:layout_marginBottom="2dip"         android:textSize="15sp"/>       <EditText         android:id="@+id/editTextSimple"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_above="@+id/textView1"         android:layout_marginBottom="4dip">         <requestFocus />     </EditText>      <TextView         android:id="@+id/textView1"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Description:"         android:textStyle="bold"         android:layout_above="@+id/editTextSimple1"          android:layout_marginBottom="2dip"         android:textSize="15sp"/>      <EditText         android:id="@+id/editTextSimple1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_above="@+id/textView2"           android:layout_marginBottom="12dip">     </EditText>      <Button android:textColor="#FF000000"          android:id="@+id/submitButton"          android:text="@string/memorySubmit"          android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_marginBottom="24dip"         android:onClick = "submit"/> </RelativeLayout> 

From what I understand, in an activity, one have to place this <activity android:windowSoftInputMode="...." > in the Android Manifest to adjust the view when the soft keyboard is called upon. However, how do we set this for fragments?

like image 772
user1140240 Avatar asked Dec 26 '12 08:12

user1140240


People also ask

How do I hide soft keyboard when EditText is focused?

setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.

How do I get the EditText above my keyboard?

After research I figured out that to work in some cases parent layout has to be set android:fitsSystemWindows="true" . Then native functionality of scrolling EditText above scrollbar working like a charm. Show activity on this post. Simply setting android:fitsSystemWindows="true" in the layout xml worked for me.

How do you ensure that the user can return to the previous fragment by pressing the back button?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.


1 Answers

this worked for me in a scenario where I was using multiple layouts and a viewstub to create a messaging screen. Place this in onCreate() Hope this helps.

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
like image 176
kenodoggy Avatar answered Sep 19 '22 11:09

kenodoggy