英文:
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.
- Without keyboard
- While the keyboard is present
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>
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.
- Without keyboard
- While keyboard present
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"/>
</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 = "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.
答案1
得分: 1
问题出在RadioGroup
的高度上。在你的代码中,RadioGroup
的高度是match_parent
,所以请将xml文件中的RadioGroup
的android:layout_height="match_parent"
替换为android:layout_height="wrap_content"
。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论