Layout Issue: 在软键盘出现后才将布局高度设置为match_parent

huangapple go评论77阅读模式
英文:

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 the following layout issue. It is fine showing every widget while the soft keyboard is present. If I close the keyboard, it shrinks to the top. You can see the following screenshots for reference. Thanks for trying to help me. The code I used is as follows.

  1. Without keyboard

Layout Issue: 在软键盘出现后才将布局高度设置为match_parent

  1. While the keyboard is present

Layout Issue: 在软键盘出现后才将布局高度设置为match_parent

  1. 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">

            <!-- EditText and other views are defined here -->
            <!-- Omitted for brevity -->
            
            <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"/>

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>
  1. MainActivity.java
// Your Java code for handling Firebase Realtime Database and UI interactions goes here.
// Omitted for brevity.

I hope this helps you with your issue. If you have any specific questions or need further assistance, please feel free to ask.

英文:

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

Layout Issue: 在软键盘出现后才将布局高度设置为match_parent

  1. While keyboard present

Layout Issue: 在软键盘出现后才将布局高度设置为match_parent

3.activity_main.xml

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;&gt;
&lt;ScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fillViewport=&quot;true&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/parent_group&quot;&gt;
&lt;TextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;।। श्री गणेशाय नम:&quot;
android:textSize=&quot;22sp&quot;
android:gravity=&quot;center&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:padding=&quot;8dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:id=&quot;@+id/start_txt&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbname&quot;
android:hint=&quot;वर / वधु का नाम &quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/start_txt&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbpname&quot;
android:hint=&quot;पिता का नाम &quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbname&quot; /&gt;
&lt;RadioGroup
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:id=&quot;@+id/genderrg&quot;
android:orientation=&quot;horizontal&quot;
android:layout_below=&quot;@+id/gbpname&quot;&gt;
&lt;RadioButton
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/rbmale&quot;
android:text=&quot;वर&quot;
android:layout_marginStart=&quot;10dp&quot;
android:textSize=&quot;22sp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:layout_weight=&quot;1&quot;/&gt;
&lt;RadioButton
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/rbfmale&quot;
android:text=&quot;वधु&quot;
android:layout_marginEnd=&quot;10dp&quot;
android:textSize=&quot;22sp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:layout_weight=&quot;1&quot;/&gt;
&lt;/RadioGroup&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbgotra&quot;
android:hint=&quot;गोत्र &quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/genderrg&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbdob&quot;
android:hint=&quot;जन्‍म तिथि DD-MM-YYYY &quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbgotra&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbheight&quot;
android:hint=&quot;ऊंचाई&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbdob&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbcolor&quot;
android:hint=&quot;रंग&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbheight&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbedu&quot;
android:hint=&quot;शिक्षा &quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbcolor&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbjob&quot;
android:hint=&quot;व्‍यवसाय&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbedu&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbhobby&quot;
android:hint=&quot;रूचि के विषय&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbjob&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbmail&quot;
android:hint=&quot;ई-मेल&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbhobby&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbphone&quot;
android:hint=&quot;मोबाईल नंबर&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbmail&quot;/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/gbaddr&quot;
android:hint=&quot;स्‍थायी पता&quot;
android:textSize=&quot;22sp&quot;
android:padding=&quot;8dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:textColor=&quot;@android:color/holo_orange_dark&quot;
android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
android:layout_below=&quot;@+id/gbphone&quot;/&gt;
&lt;Button
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:id=&quot;@+id/add_details&quot;
android:text=&quot;जानकारी जोडें&quot;
android:layout_below=&quot;@id/gbaddr&quot;
android:layout_margin=&quot;8dp&quot;
android:textSize=&quot;22sp&quot;
android:backgroundTint=&quot;@android:color/holo_orange_dark&quot;/&gt;

</RelativeLayout>
</ScrollView>

</RelativeLayout>

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 = &quot;MainActivity&quot;;
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(&quot;biodata&quot;).child(id).child(&quot;name&quot;).setValue(namee);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;father&quot;).setValue(fnamee);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;gotra&quot;).setValue(gotraa);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;dob&quot;).setValue(dobb);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;height&quot;).setValue(hghtt);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;color&quot;).setValue(colorrr);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;education&quot;).setValue(eduu);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;job&quot;).setValue(jobbb);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;hobby&quot;).setValue(hobbyy);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;email&quot;).setValue(email);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;phone&quot;).setValue(phonee);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;address&quot;).setValue(addrr);
if (male.isChecked()) {
members.setGender(rbm);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;gender&quot;).setValue(rbm);
} else {
members.setGender(rbf);
mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;gender&quot;).setValue(rbf);
}
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//                        if (snapshot.hasChild(&quot;message&quot;)){
//                            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 &lt; 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.

答案1

得分: 1

问题出在RadioGroup的高度上。在你的代码中,RadioGroup的高度是match_parent,所以请将xml文件中的RadioGroupandroid:layout_height=&quot;match_parent&quot;替换为android:layout_height=&quot;wrap_content&quot;

英文:

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

huangapple
  • 本文由 发表于 2023年5月17日 21:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定