1

I want that whent the soft key board is open the activity page will scrolled.

To do this I added scroll View in xml,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.trying.MainActivity" 
    >


    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >

        <RelativeLayout
            android:id="@+id/LayoutBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
             android:background="@drawable/background" 
           >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_world" >

              <requestFocus />
           </EditText>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

and changed the realativeLayout height in code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Display display = getWindowManager().getDefaultDisplay();
         Point size = new Point();
         display.getSize(size);
         int height = size.y;

         int x=getActionBarHeight();//in this line I get the actionBar height programicaly the function is in the end of page


         RelativeLayout l= (RelativeLayout)findViewById(R.id.LayoutBackground);
         l.getLayoutParams().height = (height-x);//the layout size = size of full screen - size of actionBar




    }

    //I saw this function in stackOverflow site. function to get the actionBar height
    public int getActionBarHeight() {

            TypedValue tv = new TypedValue();
            getApplicationContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
            int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
            return actionBarHeight;
        }

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trying"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
             android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The idia is that the layout heigt is depended of screen size (all devices), and action bar heigt too.

My problem is that when I run the app, the background is small. the it's It seems that the action bar size fell twice,

Toda
  • 403
  • 1
  • 6
  • 13

2 Answers2

0

Add the following line of code to your manifest for the particular activity

android:windowSoftInputMode="adjustResize"
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
0

You don't need to add ScrollView in your layout. Remove it from xml. Then in AndroidManifest.xml, add the following lines in your activity:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
          android:configChanges="keyboardHidden"
        android:windowSoftInputMode="adjustPan|stateAlwaysHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Yasir Tahir
  • 790
  • 1
  • 11
  • 31
  • Please keep in mind that, the page will only be scrolled up to focus the edittext, when the field is going to be behind the keyboard. If the field is placed above the keyboard size area, the view will never scroll up. – Yasir Tahir Mar 31 '16 at 10:01
  • Try to place the field at bottom and then check whether is it working or not. – Yasir Tahir Mar 31 '16 at 10:01