简体   繁体   中英

Making the whole layout smaller when the keyboard appears

I made a new fragment for creating a new playlist, but when you focus the EditText the buttons below aren't visible. Setting " android:windowSoftInputMode="stateAlwaysHidden|adjustResize" " or only " adjustResize " didn't fix the problem.

That's the code of my layout for the fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/default_fullscreen_dialog"
    android:orientation="vertical"
    android:padding="24dp">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/object_create_playlist_dialog_title"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_above="@id/dialog_cp_playlistname"/>

        <EditText
            android:id="@+id/dialog_cp_playlistname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:autofillHints=""
            android:gravity="center_horizontal"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:lines="1"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textCursorDrawable="@color/white_50"
            android:textSize="35sp"
            android:textStyle="bold"
            app:backgroundTint="@color/white_50"
            android:layout_centerVertical="true"/>

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            android:stretchColumns="1,0"
            android:layout_below="@id/dialog_cp_playlistname"
            android:layout_centerVertical="true">

            <TableRow >

                <Button
                    android:id="@+id/dialog_cp_cancel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/transparent"
                    android:fontFamily="sans-serif-thin"
                    android:gravity="end|center_vertical"
                    android:paddingStart="0dp"
                    android:paddingEnd="25dp"
                    android:text="@string/object_cancel"
                    android:textColor="@color/white_70"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dialog_cp_next"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/transparent"
                    android:fontFamily="sans-serif-thin"
                    android:gravity="start|center_vertical"
                    android:paddingStart="25dp"
                    android:paddingEnd="0dp"
                    android:text="@string/object_next"
                    android:textColor="@color/accent_color"
                    android:textSize="13sp"
                    android:textStyle="bold" />

            </TableRow>
        </TableLayout>
</RelativeLayout>

That's the style that I use for the fragment:

<style name="AppTheme.Dialog">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:colorAccent">@color/accent_color</item>
    <item name="android:windowActionBar">true</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationStyle</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">#ffffff</item>
</style>

<style name="AppTheme.Dialog.CreatePlaylist">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:colorAccent">@color/accent_color</item>
    <item name="android:windowActionBar">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textSelectHandleLeft">@drawable/null_shape</item>
    <item name="android:textSelectHandleRight">@drawable/null_shape</item>
    <item name="android:textSelectHandle">@drawable/null_shape</item>
</style>

MyApp:

我的应用程序 我的应用程序与键盘

I would like to (Spotify):

我想 (Spotify) 我想用键盘(Spotify)

I would be glad about an answer.

Add below attribute for your activity that holds this fragment in the manifest file

android:windowSoftInputMode="stateUnchanged|adjustResize"

Sample

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:windowSoftInputMode="stateUnchanged|adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Please let me know if it doesn't work to think differently

EDIT

Steps:

  1. Keep using android:windowSoftInputMode="stateUnchanged|adjustResize" as mentioned before
  2. Wrap your fragment layout in ScrollView or NestedScrollView

How it looks like

Here is a demo that worked with me

MainActivity

public class MainActivity extends AppCompatActivity {

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

MainActivity Layout

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

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

    <fragment
        android:id="@+id/fragment_container"
        android:name="com.example.android.androidxtest.TempFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainFragment

public class MainFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
}

MainFragment Layout

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="24dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/object_create_playlist_dialog_title"
            android:textSize="20sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/dialog_cp_playlistname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center_horizontal"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:lines="1"
            android:maxLines="1"
            android:textSize="35sp"
            android:textStyle="bold" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal"
            android:stretchColumns="1,0">

            <TableRow>

                <Button
                    android:id="@+id/dialog_cp_cancel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/object_cancel"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dialog_cp_next"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/object_next"
                    android:textSize="13sp"
                    android:textStyle="bold" />

            </TableRow>
        </TableLayout>
    </LinearLayout>

</ScrollView>

Hope this works!

I think , maybe there is a problem in the manifest file. check whether that your manifest file has the fragment.

Now the hole answer. DON'T use windowTranslucentStatus in your style rather use windowDrawsSystemBarBackgrounds and set statusBarColor . eg:

 <style name="AppTheme.Dialog.CreatePlaylist">
        <item name="android:windowTranslucentStatus">false</item>     
        <item name="android:windowActionBar">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:statusBarColor">[some color]</item>
 </style>

By the way you needn't change your layout.

Happy Coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM