Search code examples
javaandroidandroid-edittext

how to prevent edit text from shrinking when keyboard opens?


I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible

This is activty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <LinearLayout
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="0dp" >

      <EditText
          android:hint="ADD ITEMS"
          android:layout_margin="10dp"
          android:id="@+id/etItems"
          android:textSize="24sp"
          android:textColor="@android:color/black"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="3" />

      <Button
          android:layout_margin="10dp"
          android:id="@+id/btnAdd"
          android:text="ADD"
          android:layout_weight="1"
          android:autoSizeTextType="uniform"
          android:layout_width="0dp"
          android:layout_height="match_parent" />
   </LinearLayout>

   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/rvItems"
       android:layout_margin="10dp"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="6" />

</LinearLayout>

This is MainActivity.Java

package com.example.todolist_09082020;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> items;
    Button btnAdd;
    EditText etItems;
    RecyclerView rvItems;

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

        btnAdd = findViewById(R.id.btnAdd);
        etItems = findViewById(R.id.etItems);
        rvItems = findViewById(R.id.rvItems);

        items = new ArrayList<>();


        final ItemAdapter adapter = new ItemAdapter(items);
        rvItems.setLayoutManager(new LinearLayoutManager(this));

        rvItems.setAdapter(adapter);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = etItems.getText().toString();
                if(!text.equals("")){
                    items.add(text);
                    adapter.notifyDataSetChanged();
                }
                etItems.setText("");
            }
        });

    }
}

I tried solutions from other posts related to this, but this could not be solved.

edit text shrinked


Solution

  • Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue