Android - Struggiling To Layout My Application When Virtual Keyboard Is Visible
I'm struggling in a very annoying problem for my Android App. I created an Activity for registering to my App. This is the Activity xml file:
Solution 1:
What I usually do is using ScrollView
:
Define the ScroolView
in your Layout
like below (or just copy paste the code):
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.igor.test.MainActivity"><android.support.v7.widget.Toolbarandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="?attr/colorPrimary"android:theme="?attr/actionBarTheme"android:minHeight="?attr/actionBarSize"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:id="@+id/appTitleBar"android:elevation="4dp"><TextViewandroid:text="@string/app_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/toolbarText"android:textAppearance="@style/TextAppearance.AppCompat.Title"android:textColor="?android:attr/textColorPrimaryInverse" /></android.support.v7.widget.Toolbar><android.support.v7.widget.Toolbarandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="?attr/colorPrimary"android:theme="?attr/actionBarTheme"android:minHeight="?attr/actionBarSize"android:id="@+id/activityTitleBar"android:layout_below="@+id/appTitleBar"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:elevation="4dp"><TextViewandroid:text="signinActivityTitle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="@style/TextAppearance.AppCompat.Title"android:textColor="?android:attr/textColorPrimaryInverse" /></android.support.v7.widget.Toolbar><ScrollViewandroid:id="@+id/scroolView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/activityTitleBar"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"
><TextViewandroid:text="Welcome. bla bla bla bla bla bla bla bla bla bla"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="57dp"android:id="@+id/textView3"android:textAppearance="@style/TextAppearance.AppCompat.Large"android:textAlignment="center" /><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPassword"android:ems="10"android:layout_below="@+id/textView3"android:layout_centerHorizontal="true"android:layout_marginTop="96dp"android:id="@+id/editText3"android:hint="password" /><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPassword"android:ems="10"android:layout_below="@+id/editText3"android:layout_alignRight="@+id/editText3"android:layout_alignEnd="@+id/editText3"android:id="@+id/editText4"android:hint="confirm" /><Buttonandroid:text="SIGN IN"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:id="@+id/button2"android:layout_below="@+id/editText4"android:layout_centerHorizontal="true" /></RelativeLayout></ScrollView></RelativeLayout>
In your Activity
set the referance of the ScrollView
and the main RelativeLayout
Also set ViewTreeObserver.OnGlobalLayoutListener
on your main RelativeLayout
and implements the onGlobalLayout()
method.
Take a look at the example below:
publicclassMainActivityextendsAppCompatActivityimplementsViewTreeObserver.OnGlobalLayoutListener {
private ScrollView scrollView;
private RelativeLayout activity_main_RL;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroolView);
activity_main_RL = (RelativeLayout) findViewById(R.id.activity_main);
activity_main_RL.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
@OverridepublicvoidonGlobalLayout() {
Handlerhandler=newHandler();
handler.postDelayed(newRunnable() {
@Overridepublicvoidrun() {
intheightDiff= activity_main_RL.getRootView().getHeight() - activity_main_RL.getHeight();
if (heightDiff > 100) {
scrollView.scrollBy(0, scrollView.getBottom());
}
}
}, 500);
}
}
Solution 2:
Try this
<activityandroid:windowSoftInputMode="adjustPan"/>
Post a Comment for "Android - Struggiling To Layout My Application When Virtual Keyboard Is Visible"