Search code examples
javaandroidxmluser-interfacelayout

Layout Issue : getting layout height to match_parent only after soft keyboard present


I am trying to create a single activity app that collects data using "EditText" and saves it into a Firebase Realtime Database. I already achieved everything I needed for this project. But after setting everything and getting one successful build, a new build with some changes in activity files gives me following layout issue. It is fine showing every widget while soft keyboard is present. If I close the keyboard, it shrinks to top. You can see following screenshots for reference. Thanks for trying to help me. The code I used is as follows.

  1. Without keyboard

without keyboard

  1. While keyboard present

with keyboard

3.activity_main.xml

<RelativeLayout 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:fitsSystemWindows="true">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/parent_group">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="।। श्री गणेशाय नम:"
    android:textSize="22sp"
    android:gravity="center"
    android:textColor="@android:color/holo_orange_dark"
    android:padding="8dp"
    android:layout_marginTop="8dp"
    android:id="@+id/start_txt"/>


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbname"
    android:hint="वर / वधु का नाम "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/start_txt"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbpname"
    android:hint="पिता का नाम "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbname" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/genderrg"
                android:orientation="horizontal"
                android:layout_below="@+id/gbpname">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rbmale"
                android:text="वर"
                android:layout_marginStart="10dp"
                android:textSize="22sp"
                android:textColor="@android:color/holo_orange_dark"
                android:layout_weight="1"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rbfmale"
                android:text="वधु"
                android:layout_marginEnd="10dp"
                android:textSize="22sp"
                android:textColor="@android:color/holo_orange_dark"
                android:layout_weight="1"/>

            </RadioGroup>




<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbgotra"
    android:hint="गोत्र "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/genderrg"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbdob"
    android:hint="जन्‍म तिथि DD-MM-YYYY "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbgotra"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbheight"
    android:hint="ऊंचाई"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbdob"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbcolor"
    android:hint="रंग"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbheight"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbedu"
    android:hint="शिक्षा "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbcolor"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbjob"
    android:hint="व्‍यवसाय"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbedu"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbhobby"
    android:hint="रूचि के विषय"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbjob"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gbmail"
            android:hint="ई-मेल"
            android:textSize="22sp"
            android:padding="8dp"
            android:layout_marginStart="8dp"
            android:textColor="@android:color/holo_orange_dark"
            android:textColorHint="@android:color/holo_orange_dark"
            android:layout_below="@+id/gbhobby"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gbphone"
            android:hint="मोबाईल नंबर"
            android:textSize="22sp"
            android:padding="8dp"
            android:layout_marginStart="8dp"
            android:textColor="@android:color/holo_orange_dark"
            android:textColorHint="@android:color/holo_orange_dark"
            android:layout_below="@+id/gbmail"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbaddr"
    android:hint="स्‍थायी पता"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbphone"/>


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/add_details"
            android:text="जानकारी जोडें"
            android:layout_below="@id/gbaddr"
            android:layout_margin="8dp"
            android:textSize="22sp"
            android:backgroundTint="@android:color/holo_orange_dark"/>
    

4.MainActivity.java

package com.nilesh.rbndj;

import android.os.Bundle;
import android.os.Handler;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.core.view.WindowCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.nilesh.rbndj.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {




    private static final String TAG = "MainActivity";
    private AdView mAdView;

    EditText name,fname,gotra,dob,hght,colorr,edu,job,hobby,mail,phone,addr;
    Button addButton;
    RadioButton male,fmale;


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


//        mAdView = findViewById(R.id.adView);
//        AdRequest adRequest = new AdRequest.Builder().build();
//        mAdView.loadAd(adRequest);


        name = findViewById(R.id.gbname);
        fname = findViewById(R.id.gbpname);
        gotra = findViewById(R.id.gbgotra);
        dob = findViewById(R.id.gbdob);
        hght = findViewById(R.id.gbheight);
        colorr = findViewById(R.id.gbcolor);
        edu = findViewById(R.id.gbedu);
        job = findViewById(R.id.gbjob);
        hobby = findViewById(R.id.gbhobby);
        mail = findViewById(R.id.gbmail);
        phone = findViewById(R.id.gbphone);
        addr = findViewById(R.id.gbaddr);
        addButton = findViewById(R.id.add_details);
        male = findViewById(R.id.rbmale);
        fmale = findViewById(R.id.rbfmale);


        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Members members = new Members();
                DatabaseReference mDatabase;

                mDatabase = FirebaseDatabase.getInstance().getReference();

                String id, namee, fnamee, gotraa, dobb, hghtt, colorrr, eduu, jobbb, hobbyy, email, phonee, addrr, rbm, rbf;


                namee = name.getText().toString();
                fnamee = fname.getText().toString();
                gotraa = gotra.getText().toString();
                dobb = dob.getText().toString();
                hghtt = hght.getText().toString();
                colorrr = colorr.getText().toString();
                eduu = edu.getText().toString();
                jobbb = job.getText().toString();
                hobbyy = hobby.getText().toString();
                email = mail.getText().toString();
                phonee = phone.getText().toString();
                addrr = addr.getText().toString();
                rbm = male.getText().toString();
                rbf = fmale.getText().toString();

                id = phonee;


                mDatabase.child("biodata").child(id).child("name").setValue(namee);
                mDatabase.child("biodata").child(id).child("father").setValue(fnamee);
                mDatabase.child("biodata").child(id).child("gotra").setValue(gotraa);
                mDatabase.child("biodata").child(id).child("dob").setValue(dobb);
                mDatabase.child("biodata").child(id).child("height").setValue(hghtt);
                mDatabase.child("biodata").child(id).child("color").setValue(colorrr);
                mDatabase.child("biodata").child(id).child("education").setValue(eduu);
                mDatabase.child("biodata").child(id).child("job").setValue(jobbb);
                mDatabase.child("biodata").child(id).child("hobby").setValue(hobbyy);
                mDatabase.child("biodata").child(id).child("email").setValue(email);
                mDatabase.child("biodata").child(id).child("phone").setValue(phonee);
                mDatabase.child("biodata").child(id).child("address").setValue(addrr);

                if (male.isChecked()) {
                    members.setGender(rbm);
                    mDatabase.child("biodata").child(id).child("gender").setValue(rbm);

                } else {
                    members.setGender(rbf);
                    mDatabase.child("biodata").child(id).child("gender").setValue(rbf);
                }

                mDatabase.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//                        if (snapshot.hasChild("message")){
//                            clearForm();
//                        }
                    }

                    @Override
                    public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                    }

                    @Override
                    public void onChildRemoved(@NonNull DataSnapshot snapshot) {

                    }

                    @Override
                    public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });


            }
        });




    }

//    public  void  clearForm() {
//
//        ViewGroup group = (ViewGroup) findViewById(R.id.parent_group);
//        for (int i = 0, count = group.getChildCount(); i < count; ++i) {
//            View view = group.getChildAt(i);
//            if (view instanceof EditText) {
//                ((EditText) view).setText(null);
//            }
//        }

}

I hope this information is enough for you to help solve my issue.


Solution

  • Issue is with RadioGroup height . In your code , RadioGroup height is match_parent so replace android:layout_height="match_parent" with android:layout_height="wrap_content" for RadioGroup in xml file