Why does my fragment fill the screen qua height? - Android - android

I've got a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<fragment
android:id="#+id/fragment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="<headerClass>"/>
<fragment
android:id="#+id/fragment_body"
android:layout_below="#id/fragment_header"
android:layout_above="#+id/fragment_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="<bodyClass>"/>
<fragment
android:id="#id/fragment_banner"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="<BannerClass>"/>
</RelativeLayout>
Notice that the first fragment has "wrap_content" as param for the layout_height.
This fragment is defined qua layout as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/headerWrapper"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/header_background">
// stuff here...
</RelativeLayout>
As you can see the height is set to 40dp.
Yet, the header takes the full height of the screen. I am not sure why. I was looking at the layoutInflater class, but since I am unfamiliar with it I cannot see any problems with that. I use this line to inflate my fragment:
View view = inflater.inflate(R.layout.fragment_header, container, false);
Why is it showing this behavior and how can I fix it?

I've managed to fix it by putting every fragment in its own LinearLayout and giving the first a height of 40dp. I don't think this is the best solution, but it's all I could come up with.

Related

Many layouts in Scroll View in android

I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.

Android-Last item of the ListView is not visible

I have added the ListView inside ViewPager, as soon as the page is loaded the first tab is selected and the last item of the list does not show up for the first time, its becoming visible when I go to next tab and come back. I would like to see all the items of the listview when its loaded for the first time. Please help me to fix this problem.
This is the xml for the listview -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview_categories"
android:layout_width="fill_parent"
android:divider="#color/light_gray_a1"
android:dividerHeight="0.667dp"
android:background="#color/white"
android:layout_height="fill_parent" />
</RelativeLayout>
add bottom padding like this and change fill_parent to match_parent
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview_categories"
android:layout_width="match_parent"
android:divider="#color/light_gray_a1"
android:dividerHeight="0.667dp"
android:paddingBottom="40dp"
android:background="#color/white"
android:layout_height="match_parent" />
</RelativeLayout>
I had this issue. Just now I solved it. Look for android:layout_weight in all the Layouts you used and if it is set some non zero values like android:layout_weight="1" or android:layout_weight="0.91", then set them to zero like this,
android:layout_weight="0"

GridLayoutManager 3 columns in RecyclerView

I'm trying to make simple GridLayout with 3 columns but although I get 3 columns I get strange gap between rows. So, I get 3 images in row than one row that is empty (which it seems has height matching height of the row abov(that has images), then row of 3 images, than strange gap...etc. I expected not to get than blank gap. How to remove this blank gap? Trying to set wrap_content to layout_height on recycler view, and fragment didn't helped.
This is my code:
final GridLayoutManager layoutManager = new GridLayoutManager(this,3);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
This is xml of the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.adriagate.adriagateonlineandroid.activities.ImagesActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragmentImagesHeader"
android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesHeader"
tools:layout="#layout/fragment_images_header" android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragmentImagesGrid"
android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesList"
tools:layout="#layout/fragment_images_list" android:layout_width="match_parent"
android:layout_height="wrap_content" >
</fragment>
</RelativeLayout>
Notice that this layout has an important fragment called fragmentImagesGrid which has this layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.adriagate.adriagateonlineandroid.fragments.ImagesList">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_view_images_one_row"
>
</FrameLayout>
This i the layout of the one element inside recycler view:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
>
<ImageView
android:id="#+id/imageViewImagesAllOneRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
tools:src="#drawable/slika_200_200"
/>
</FrameLayout>
Looks like it happens because your images have different sizes.
Change your item to LinearLayout and set its layout height and width to match_parent. Set gravity="center" (Of the linear layout).
Another issue is android:layout_height="wrap_content". The RecyclerView doesn't work well when you set the height property to wrap_content. If you still want to use RecyclerView with wrap_content, you can ckeck this solution

How to make an ImageView overflows to the Toolbar in Android

I have a Toolbar and some fragments in FrameLayout. However in such a case I am asked to make an ImageView in a fragment which its half will be on the Toolbar. I couldn't figure it out. I considered to cut the image and make it an Options menu item and rest of it will be on the top of the fragment, but I think this won't be very effective for different devices. Can you please help me?
I don't sure what exactly u need.
But if I get it. Here is something u can do.
U just need to make your fragment on whole screen and draw toolbar over fragment.
Here is the snippet. I don't tested it:
your_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Toolbar //name possibly incorrect(change to correct one)
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"/>
</RelativeLayout>
Then inside fragment layout:
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView //or any other container for your stuff
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/toolbar_height"
android:clipToPadding="false"/>
<ImageView //your imageView that will draw halfover toolbar
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/half_toolbar_height"/>
</FrameLayout>
The snippet above possibly won't work correctly. But anyway it can be start point to solve your problem. Good luck!

Fragment's and sub-Fragment's LinearLayouts aren't stretching to the entire parent size

I'm having an issue where my Fragment view inside a LinearLayout isn't streching to the parent's height and width. But it is a little bit more complicated since I have two Fragments inside that Fragment.
Here's the Activity's view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center|fill"
android:orientation="vertical"
android:padding="8dp" >
</LinearLayout>
Here's the parent fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:background="#FF00FF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/mtg_player1_fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000FF"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/mtg_player2_fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF0000"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
And the child (player) fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mtg_player_life_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/mtg_player_life_count"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="140sp" />
</LinearLayout>
I'm adding the parent fragment like this (via clicking on a list item):
getSupportFragmentManager().beginTransaction().replace(R.id.main_content, fragment, newFragmentClass.getSimpleName()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
And the children fragment like this (on the parent's onViewCreated()):
mPlayer1Frag = (PlayerFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.player1_fragment_wrapper);
if (mPlayer1Frag == null) {
mPlayer1Frag = new PlayerFragment();
}
if (!mPlayer1Frag.isVisible()) {
((RoboSherlockFragmentActivity)getSherlockActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.player1_fragment_wrapper, mPlayer1Frag).commit();
}
mPlayer2Frag = (PlayerFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.player2_fragment_wrapper);
if (mPlayer2Frag == null) {
mPlayer2Frag = new PlayerFragment();
}
if (!mPlayer2Frag.isVisible()) {
((RoboSherlockFragmentActivity)getSherlockActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.player2_fragment_wrapper, mPlayer2Frag).commit();
}
What I get is the following:
What basically happened is:
The parent Fragment view didn't stretch vertically to fill the entire activity's view (no pink appeared);
The TextView (green) didn't stretch to cover the children fragment layout (red/blue);
What I want to happen is for the parent UI to stretch in the entire activity, for the children to cover the entire parent and for the TextViews to cover the entire child. I could change it to use a single fragment (and may well do that), but I'd prefer if I could keep it this way, since it feels more organized :)
So, the question is, what am I doing wrong? Can't this be done using two fragments inside another?
In the onCreateView() method of a Fragment always inflate the layout file using the container parameter of that method like this:
inflater.inflate(R.layout.parent_view, container, false);
This way the inflated view will be given proper LayoutParams and it will behave as designed.

Categories

Resources