4

I make an Android application, following Google's material design guidelines. So, I've implemented the latest support libraries (my min Sdk value is 14)

Now, I face to a problem : I've made the status bar transparent, with custom ToolBar (defined in a XML file). Everything works well, except that FloatingActionButton (from https://github.com/makovkastar/FloatingActionButton) skips XML's defined layout margin. Idem, one of my app's UI uses TextView to login the user : once focused, they're hidden by the soft keyboard.

These two problems came after replacing the main style from "AppCompat.DarkActionBar" to "AppCompat.NoActionBar" (and with item "android:windowTranslucentStatus" set to "true").

I tried to use windowSoftInputMode in my manifest with "adjustPan|adjustResize", but nothing change. Adding "ScrollView" to the layout with "EditText" inside doesn't solve my problem neither.

My english is bad, and I know that pictures are better than words, so here is what I want to do and what's happening :

(link to image) https://i.stack.imgur.com/fIRCl.png

Any ideas ? Thanks for reading !


EDIT : I solved my problem with the FloatingButton, just need to add this line to the component's XML declaration : android:layout_alignParentBottom="true"

The only problem now is the keyboard that hides EditText. Here is my code :

.

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- My toolbar -->
        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar_design">
        </include>

        <!-- Fragment's place -->
        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true">
        </FrameLayout> <!-- Open drawer bug fix -->

    </LinearLayout>

    <!-- Sliding Drawer -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:clipToPadding="false"
        android:overScrollFooter="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/drawer_background"/>

</android.support.v4.widget.DrawerLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/md_blue_800"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="4dp" >
</android.support.v7.widget.Toolbar>

fragment_profile.xml (the view with EditText's)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/container_dropshadow">

        <!-- Top fragment's Image --> 
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:scaleType="centerCrop"
            android:src="@drawable/top_image"/>

    </LinearLayout>

    <!-- Scrollview in case of screen's height is too small -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:id="@+id/rlFillProfile">

            <!-- long long text ( 9 lines ) -->
            <TextView
                android:text="@string/text_edit_profile" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:id="@+id/tvEditProfile" />

            <!-- User ID EditText -->
            <com.rengwuxian.materialedittext.MaterialEditText
                android:layout_below="@id/tvEditProfile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:maxLines="1"
                android:singleLine="true"
                android:maxEms="10"
                android:hint="Your user ID"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:id="@+id/etUserID"
                android:layout_alignLeft="@+id/etUserPassword"
                android:layout_alignStart="@+id/etUserPassword" />

            <!-- User ID TextView -->
            <TextView
                android:id="@+id/tvUserID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Identifiant :"
                android:textSize="18dp"
                android:layout_marginBottom="16dp"
                android:layout_alignBottom="@id/etUserID"
                android:layout_alignRight="@+id/tvUserPassword"
                android:layout_alignEnd="@+id/tvUserPassword" />

            <!-- User password TextView -->
            <com.rengwuxian.materialedittext.MaterialEditText
                android:layout_below="@id/etUserID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:maxLines="1"
                android:singleLine="true"
                android:maxEms="10"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:inputType="textPassword"
                android:id="@+id/etUserPassword"
                android:hint="******"
                android:layout_toRightOf="@+id/tvUserPassword"
                android:layout_toEndOf="@+id/tvUserPassword" />

            <!-- User password TextView -->
            <TextView
                android:id="@+id/tvUserPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mot de passe :"
                android:textSize="18dp"
                android:layout_alignBottom="@+id/etUserPassword"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <!-- Button to get user's touch event -->
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Continuer"
                android:id="@+id/button_disconnect"
                android:layout_below="@id/etUserPassword"
                android:layout_centerHorizontal="true" />

        </RelativeLayout>
    </ScrollView>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >

    <!-- Permissions -->
    ...

    <application
        ... >

        <activity
            android:name="..."
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"
            android:windowSoftInputMode="adjustResize|stateHidden">

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

In my MainActivity, I have the DrawerLayout and some codelines not in relationship with the Toolbar / layout, except for :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    setSupportActionBar(toolbar);

    // enabling action bar app icon and behaving it as toggle button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

    ...
}


// A method to find height of the status bar
// Source : an Stackoverflow's answer
public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}
Rascafr
  • 174
  • 11

1 Answers1

1

Ok, finally found a solution. Using this code in Fragment solves everything.

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

By the way, use AppCompatActivity instead of ActionBarActivity (support V7), I'm not sure it changes something, but just in case ...

(From : this answer https://stackoverflow.com/a/28012530/5200029)

Community
  • 1
  • 1
Rascafr
  • 174
  • 11