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

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

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
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:fitsSystemWindows="true">
  7. <ScrollView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:fillViewport="true">
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/parent_group">
  15. <!-- EditText and other views are defined here -->
  16. <!-- Omitted for brevity -->
  17. <Button
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:id="@+id/add_details"
  21. android:text="जानकारी जोडें"
  22. android:layout_below="@id/gbaddr"
  23. android:layout_margin="8dp"
  24. android:textSize="22sp"
  25. android:backgroundTint="@android:color/holo_orange_dark"/>
  26. </RelativeLayout>
  27. </ScrollView>
  28. </RelativeLayout>
  1. MainActivity.java
  1. // Your Java code for handling Firebase Realtime Database and UI interactions goes here.
  2. // 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

  1. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;
  6. android:fitsSystemWindows=&quot;true&quot;&gt;
  7. &lt;ScrollView
  8. android:layout_width=&quot;match_parent&quot;
  9. android:layout_height=&quot;match_parent&quot;
  10. android:fillViewport=&quot;true&quot;&gt;
  11. &lt;RelativeLayout
  12. android:layout_width=&quot;match_parent&quot;
  13. android:layout_height=&quot;wrap_content&quot;
  14. android:id=&quot;@+id/parent_group&quot;&gt;
  15. &lt;TextView
  16. android:layout_width=&quot;match_parent&quot;
  17. android:layout_height=&quot;wrap_content&quot;
  18. android:text=&quot;।। श्री गणेशाय नम:&quot;
  19. android:textSize=&quot;22sp&quot;
  20. android:gravity=&quot;center&quot;
  21. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  22. android:padding=&quot;8dp&quot;
  23. android:layout_marginTop=&quot;8dp&quot;
  24. android:id=&quot;@+id/start_txt&quot;/&gt;
  25. &lt;EditText
  26. android:layout_width=&quot;match_parent&quot;
  27. android:layout_height=&quot;wrap_content&quot;
  28. android:id=&quot;@+id/gbname&quot;
  29. android:hint=&quot;वर / वधु का नाम &quot;
  30. android:textSize=&quot;22sp&quot;
  31. android:padding=&quot;8dp&quot;
  32. android:layout_marginStart=&quot;8dp&quot;
  33. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  34. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  35. android:layout_below=&quot;@+id/start_txt&quot;/&gt;
  36. &lt;EditText
  37. android:layout_width=&quot;match_parent&quot;
  38. android:layout_height=&quot;wrap_content&quot;
  39. android:id=&quot;@+id/gbpname&quot;
  40. android:hint=&quot;पिता का नाम &quot;
  41. android:textSize=&quot;22sp&quot;
  42. android:padding=&quot;8dp&quot;
  43. android:layout_marginStart=&quot;8dp&quot;
  44. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  45. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  46. android:layout_below=&quot;@+id/gbname&quot; /&gt;
  47. &lt;RadioGroup
  48. android:layout_width=&quot;match_parent&quot;
  49. android:layout_height=&quot;match_parent&quot;
  50. android:id=&quot;@+id/genderrg&quot;
  51. android:orientation=&quot;horizontal&quot;
  52. android:layout_below=&quot;@+id/gbpname&quot;&gt;
  53. &lt;RadioButton
  54. android:layout_width=&quot;wrap_content&quot;
  55. android:layout_height=&quot;wrap_content&quot;
  56. android:id=&quot;@+id/rbmale&quot;
  57. android:text=&quot;वर&quot;
  58. android:layout_marginStart=&quot;10dp&quot;
  59. android:textSize=&quot;22sp&quot;
  60. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  61. android:layout_weight=&quot;1&quot;/&gt;
  62. &lt;RadioButton
  63. android:layout_width=&quot;wrap_content&quot;
  64. android:layout_height=&quot;wrap_content&quot;
  65. android:id=&quot;@+id/rbfmale&quot;
  66. android:text=&quot;वधु&quot;
  67. android:layout_marginEnd=&quot;10dp&quot;
  68. android:textSize=&quot;22sp&quot;
  69. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  70. android:layout_weight=&quot;1&quot;/&gt;
  71. &lt;/RadioGroup&gt;
  72. &lt;EditText
  73. android:layout_width=&quot;match_parent&quot;
  74. android:layout_height=&quot;wrap_content&quot;
  75. android:id=&quot;@+id/gbgotra&quot;
  76. android:hint=&quot;गोत्र &quot;
  77. android:textSize=&quot;22sp&quot;
  78. android:padding=&quot;8dp&quot;
  79. android:layout_marginStart=&quot;8dp&quot;
  80. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  81. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  82. android:layout_below=&quot;@+id/genderrg&quot;/&gt;
  83. &lt;EditText
  84. android:layout_width=&quot;match_parent&quot;
  85. android:layout_height=&quot;wrap_content&quot;
  86. android:id=&quot;@+id/gbdob&quot;
  87. android:hint=&quot;जन्‍म तिथि DD-MM-YYYY &quot;
  88. android:textSize=&quot;22sp&quot;
  89. android:padding=&quot;8dp&quot;
  90. android:layout_marginStart=&quot;8dp&quot;
  91. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  92. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  93. android:layout_below=&quot;@+id/gbgotra&quot;/&gt;
  94. &lt;EditText
  95. android:layout_width=&quot;match_parent&quot;
  96. android:layout_height=&quot;wrap_content&quot;
  97. android:id=&quot;@+id/gbheight&quot;
  98. android:hint=&quot;ऊंचाई&quot;
  99. android:textSize=&quot;22sp&quot;
  100. android:padding=&quot;8dp&quot;
  101. android:layout_marginStart=&quot;8dp&quot;
  102. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  103. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  104. android:layout_below=&quot;@+id/gbdob&quot;/&gt;
  105. &lt;EditText
  106. android:layout_width=&quot;match_parent&quot;
  107. android:layout_height=&quot;wrap_content&quot;
  108. android:id=&quot;@+id/gbcolor&quot;
  109. android:hint=&quot;रंग&quot;
  110. android:textSize=&quot;22sp&quot;
  111. android:padding=&quot;8dp&quot;
  112. android:layout_marginStart=&quot;8dp&quot;
  113. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  114. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  115. android:layout_below=&quot;@+id/gbheight&quot;/&gt;
  116. &lt;EditText
  117. android:layout_width=&quot;match_parent&quot;
  118. android:layout_height=&quot;wrap_content&quot;
  119. android:id=&quot;@+id/gbedu&quot;
  120. android:hint=&quot;शिक्षा &quot;
  121. android:textSize=&quot;22sp&quot;
  122. android:padding=&quot;8dp&quot;
  123. android:layout_marginStart=&quot;8dp&quot;
  124. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  125. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  126. android:layout_below=&quot;@+id/gbcolor&quot;/&gt;
  127. &lt;EditText
  128. android:layout_width=&quot;match_parent&quot;
  129. android:layout_height=&quot;wrap_content&quot;
  130. android:id=&quot;@+id/gbjob&quot;
  131. android:hint=&quot;व्‍यवसाय&quot;
  132. android:textSize=&quot;22sp&quot;
  133. android:padding=&quot;8dp&quot;
  134. android:layout_marginStart=&quot;8dp&quot;
  135. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  136. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  137. android:layout_below=&quot;@+id/gbedu&quot;/&gt;
  138. &lt;EditText
  139. android:layout_width=&quot;match_parent&quot;
  140. android:layout_height=&quot;wrap_content&quot;
  141. android:id=&quot;@+id/gbhobby&quot;
  142. android:hint=&quot;रूचि के विषय&quot;
  143. android:textSize=&quot;22sp&quot;
  144. android:padding=&quot;8dp&quot;
  145. android:layout_marginStart=&quot;8dp&quot;
  146. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  147. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  148. android:layout_below=&quot;@+id/gbjob&quot;/&gt;
  149. &lt;EditText
  150. android:layout_width=&quot;match_parent&quot;
  151. android:layout_height=&quot;wrap_content&quot;
  152. android:id=&quot;@+id/gbmail&quot;
  153. android:hint=&quot;ई-मेल&quot;
  154. android:textSize=&quot;22sp&quot;
  155. android:padding=&quot;8dp&quot;
  156. android:layout_marginStart=&quot;8dp&quot;
  157. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  158. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  159. android:layout_below=&quot;@+id/gbhobby&quot;/&gt;
  160. &lt;EditText
  161. android:layout_width=&quot;match_parent&quot;
  162. android:layout_height=&quot;wrap_content&quot;
  163. android:id=&quot;@+id/gbphone&quot;
  164. android:hint=&quot;मोबाईल नंबर&quot;
  165. android:textSize=&quot;22sp&quot;
  166. android:padding=&quot;8dp&quot;
  167. android:layout_marginStart=&quot;8dp&quot;
  168. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  169. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  170. android:layout_below=&quot;@+id/gbmail&quot;/&gt;
  171. &lt;EditText
  172. android:layout_width=&quot;match_parent&quot;
  173. android:layout_height=&quot;wrap_content&quot;
  174. android:id=&quot;@+id/gbaddr&quot;
  175. android:hint=&quot;स्‍थायी पता&quot;
  176. android:textSize=&quot;22sp&quot;
  177. android:padding=&quot;8dp&quot;
  178. android:layout_marginStart=&quot;8dp&quot;
  179. android:textColor=&quot;@android:color/holo_orange_dark&quot;
  180. android:textColorHint=&quot;@android:color/holo_orange_dark&quot;
  181. android:layout_below=&quot;@+id/gbphone&quot;/&gt;
  182. &lt;Button
  183. android:layout_width=&quot;match_parent&quot;
  184. android:layout_height=&quot;wrap_content&quot;
  185. android:id=&quot;@+id/add_details&quot;
  186. android:text=&quot;जानकारी जोडें&quot;
  187. android:layout_below=&quot;@id/gbaddr&quot;
  188. android:layout_margin=&quot;8dp&quot;
  189. android:textSize=&quot;22sp&quot;
  190. android:backgroundTint=&quot;@android:color/holo_orange_dark&quot;/&gt;

</RelativeLayout>
</ScrollView>

</RelativeLayout>

4.MainActivity.java

  1. package com.nilesh.rbndj;
  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import com.google.android.gms.ads.AdRequest;
  5. import com.google.android.gms.ads.AdView;
  6. import com.google.android.material.snackbar.Snackbar;
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import android.view.View;
  11. import androidx.core.view.WindowCompat;
  12. import androidx.navigation.NavController;
  13. import androidx.navigation.Navigation;
  14. import androidx.navigation.ui.AppBarConfiguration;
  15. import androidx.navigation.ui.NavigationUI;
  16. import com.google.firebase.database.ChildEventListener;
  17. import com.google.firebase.database.DataSnapshot;
  18. import com.google.firebase.database.DatabaseError;
  19. import com.google.firebase.database.DatabaseReference;
  20. import com.google.firebase.database.FirebaseDatabase;
  21. import com.nilesh.rbndj.databinding.ActivityMainBinding;
  22. import android.view.Menu;
  23. import android.view.MenuItem;
  24. import android.view.ViewGroup;
  25. import android.widget.Button;
  26. import android.widget.EditText;
  27. import android.widget.RadioButton;
  28. public class MainActivity extends AppCompatActivity {
  29. private static final String TAG = &quot;MainActivity&quot;;
  30. private AdView mAdView;
  31. EditText name,fname,gotra,dob,hght,colorr,edu,job,hobby,mail,phone,addr;
  32. Button addButton;
  33. RadioButton male,fmale;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38. // mAdView = findViewById(R.id.adView);
  39. // AdRequest adRequest = new AdRequest.Builder().build();
  40. // mAdView.loadAd(adRequest);
  41. name = findViewById(R.id.gbname);
  42. fname = findViewById(R.id.gbpname);
  43. gotra = findViewById(R.id.gbgotra);
  44. dob = findViewById(R.id.gbdob);
  45. hght = findViewById(R.id.gbheight);
  46. colorr = findViewById(R.id.gbcolor);
  47. edu = findViewById(R.id.gbedu);
  48. job = findViewById(R.id.gbjob);
  49. hobby = findViewById(R.id.gbhobby);
  50. mail = findViewById(R.id.gbmail);
  51. phone = findViewById(R.id.gbphone);
  52. addr = findViewById(R.id.gbaddr);
  53. addButton = findViewById(R.id.add_details);
  54. male = findViewById(R.id.rbmale);
  55. fmale = findViewById(R.id.rbfmale);
  56. addButton.setOnClickListener(new View.OnClickListener() {
  57. @Override
  58. public void onClick(View v) {
  59. Members members = new Members();
  60. DatabaseReference mDatabase;
  61. mDatabase = FirebaseDatabase.getInstance().getReference();
  62. String id, namee, fnamee, gotraa, dobb, hghtt, colorrr, eduu, jobbb, hobbyy, email, phonee, addrr, rbm, rbf;
  63. namee = name.getText().toString();
  64. fnamee = fname.getText().toString();
  65. gotraa = gotra.getText().toString();
  66. dobb = dob.getText().toString();
  67. hghtt = hght.getText().toString();
  68. colorrr = colorr.getText().toString();
  69. eduu = edu.getText().toString();
  70. jobbb = job.getText().toString();
  71. hobbyy = hobby.getText().toString();
  72. email = mail.getText().toString();
  73. phonee = phone.getText().toString();
  74. addrr = addr.getText().toString();
  75. rbm = male.getText().toString();
  76. rbf = fmale.getText().toString();
  77. id = phonee;
  78. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;name&quot;).setValue(namee);
  79. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;father&quot;).setValue(fnamee);
  80. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;gotra&quot;).setValue(gotraa);
  81. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;dob&quot;).setValue(dobb);
  82. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;height&quot;).setValue(hghtt);
  83. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;color&quot;).setValue(colorrr);
  84. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;education&quot;).setValue(eduu);
  85. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;job&quot;).setValue(jobbb);
  86. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;hobby&quot;).setValue(hobbyy);
  87. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;email&quot;).setValue(email);
  88. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;phone&quot;).setValue(phonee);
  89. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;address&quot;).setValue(addrr);
  90. if (male.isChecked()) {
  91. members.setGender(rbm);
  92. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;gender&quot;).setValue(rbm);
  93. } else {
  94. members.setGender(rbf);
  95. mDatabase.child(&quot;biodata&quot;).child(id).child(&quot;gender&quot;).setValue(rbf);
  96. }
  97. mDatabase.addChildEventListener(new ChildEventListener() {
  98. @Override
  99. public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
  100. // if (snapshot.hasChild(&quot;message&quot;)){
  101. // clearForm();
  102. // }
  103. }
  104. @Override
  105. public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
  106. }
  107. @Override
  108. public void onChildRemoved(@NonNull DataSnapshot snapshot) {
  109. }
  110. @Override
  111. public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
  112. }
  113. @Override
  114. public void onCancelled(@NonNull DatabaseError error) {
  115. }
  116. });
  117. }
  118. });
  119. }
  120. // public void clearForm() {
  121. //
  122. // ViewGroup group = (ViewGroup) findViewById(R.id.parent_group);
  123. // for (int i = 0, count = group.getChildCount(); i &lt; count; ++i) {
  124. // View view = group.getChildAt(i);
  125. // if (view instanceof EditText) {
  126. // ((EditText) view).setText(null);
  127. // }
  128. // }
  129. }

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:

确定