Android breaks my text when using fragments

I have one fragment activity. At the top of the fragment I want to place a small advertisement. The problem is that if I define something on top of my fragment, the text inside my fragment will seem to be cut off. Look at the images (before advertising and after).

Before: Normal image After: Problematic image Pay attention to the scroll bar at the top, which means that it reduces the visibility of my button.

All I wanted to do was keep the text β€œJogar” visible when the ad is visible. Here are some of the xml and code that will help if necessary. Please help me if you have an idea how to fix this.

activity_xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00F"> <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" ads:adUnitId="..." ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR" android:background="@android:color/transparent" ads:loadAdOnCreate="true" /> <FrameLayout android:id="@+id/fragmentPlaceholder" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0F0"/> </LinearLayout> 

fragment_xml:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center"> <Button android:id="@+id/playButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dp" android:text="@string/play" style="@style/TextFont"/> <Button android:id="@+id/rankButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rank" style="@style/TextFont"/> <Button android:id="@+id/achievementsButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/achievements" style="@style/TextFont" /> <Button android:id="@+id/settingsButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/settings" style="@style/TextFont"/> <com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/sign_out_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textColor="@android:color/black" android:background="@android:drawable/btn_default" android:text="@string/logout" android:visibility="gone" /> </LinearLayout> </ScrollView> 

In the Fragment class, I inflate a call of the form:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.menu_initial, container, false); ... 

In the Activity class, I call the fragment with:

 fm = getSupportFragmentManager(); if(fm.getBackStackEntryCount() == 0){ initialMenu = new InitialMenu(); fm.beginTransaction().replace(R.id.fragmentPlaceholder, initialMenu, "initialFrag").commit(); } 
+4
source share
3 answers

Try removing the android:layout_gravity="center" LinearLayout from the LinearLayout that is wrapped by a ScrollView in the fragment layout:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> //... 

This property does not make sense, because you LinearLayout center itself in the parent object, which gives it as much space as it needs / wants (and using this property for some reason can compensate for the contents of ScrollView ).

+3
source

Consider this solution - use RelativeLayout instead of the linear and snippet below adView:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00F"> <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" ads:adUnitId="a151a1ce3f32101" ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR, CF4058453805477305A64DACC4D23425" android:background="@android:color/transparent" ads:loadAdOnCreate="true" /> <FrameLayout android:id="@+id/fragmentPlaceholder" android:layout_width="match_parent" android:layout_height="match_parent" anroid:layout_below="@+id/adView" android:background="#0F0"/> </RelativeLayout> 
0
source

Your problem is that in your FrameLayout you have match_parent height match_parent to match the device height, thus behind your AdView . Try switching it to wrap_content :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00F"> <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" ads:adUnitId="..." ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR" android:background="@android:color/transparent" ads:loadAdOnCreate="true" /> <FrameLayout android:id="@+id/fragmentPlaceholder" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0F0"/> </LinearLayout> 
0
source

Source: https://habr.com/ru/post/1485484/


All Articles