Fragment in LinearLayout changes its height - android

I'm trying to set very simple thing in XML in android app.
I want to put fragment in top of every activity, and trying to divide screen into 2/8 weight (sum 10). But unfortunately this is changing from activity to activity. No idea why. Any help? :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top" >
<fragment
android:id="#+id/fragment_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="top"
class="com.app.TopFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:id="#+id/chart_scrollview" >
Here goes LinearLayout with other linearlayouts inside.
</ScrollView>
</LinearLayout>
Any ideas?
EDIT:
Fragment class declaration is:
class TopFragment extends Fragment implements View.OnClickListener{}
EDIT2:
Thank you guys. Funny thing is that I already tried this 0dip thing and it was not working. Now it's working, but for any other people fighting similar problem here's the hint: Do not use
android:layout_width="fill_parent"
on anything which is inside of these things in my sample code. Always use
android:layout_width="match_parent"
This was the reason why 0dip solution was not working. Thank you very much.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top" >
<fragment
android:id="#+id/fragment_top"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:layout_gravity="top"
class="com.app.TopFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:id="#+id/chart_scrollview" >
<!-- Here goes LinearLayout with other linearlayouts inside-->
</ScrollView>
</LinearLayout>

Try changing height of fragment and ScrollView from wrap_content to match_parent or 0dip if you want to control the height of the views by their weights.

then you should write something like this in your xml
<com.app.TopFragment
android:id="#+id/fragment_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="top"
class="com.app.TopFragment" />

Related

ScrollView not displaying all Contents in Homefragment

I am trying to disable scrollview and show up all contents in homepage, However till no luck scroll doesn't show up all the contents.I already tried all questions regarding this but still no luck. I would appreciate if anyone could help me.Thank You :)
Here is fragment_home.xml..
<?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"
android:background="#color/bg">
<ProgressBar
android:id="#+id/progressBar"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#+id/ContainerSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_latest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_featured"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Did you try adding to scrollview
android:fillportView="true"
You also dont need two recyclerview.. Do one with multiple view types.
With that said you also dont need that scrollview. You only need one recyclerview with multiple viewtypes.
Your layout can be handled well by using EPOXY, a library from airbnb.
Will make your life a lot easier.
https://github.com/airbnb/epoxy
Have you tried using NestedScrollView instead?
The LinearLayout should also have a height of match_parent instead of wrap_content.
Also you should only use weight for views that are children of a LinearLayout so you can remove that for the NestedScrollView.
<NestedScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

I have some problems with the XML layouts (android development).

I think I have some problems with a LinearLayout container. I do not know how to fix these problems:
I am a beginner to XML but I think the problem is in the second LinearLayout. I hope someone can help me out.
The code is here below:
<?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:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
**<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
andriod:orientation="horizontal" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>**
<Button
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
The problem I see when reading your xml file is that inside the main LinearLayout you have 3 elements with the properties about the width and height as follows:
android:layout_height="match_parent"
android:layout_width="match_parent"
which means you expect the elements to fill entirely the main LinearLayout. This is not going to work. A linear layout has ordered not overlapping elements (RelativeLayout is there for that). Since the main LinearLayout is supposed to be oriented vertically, I suppose that for these three elements, you need to set the properties to match the whole width of main LinearLayout and to be wrapped vertically, by setting these values:
android:layout_height="wrap_content"
android:layout_width="match_parent"
You should apply these to the TextView, LinearLayout and Button elements of second level.
If you have overlapping imageviews try adding weights to them and/or changing the width to not match the parent and only wrapping the content.
please change andriod to android ( Line 14 )
Your posted code has a typo. Your fix might be as simple as replacing the line:
andriod:orientation="horizontal"
with:
android:orientation="horizontal"
Judging by the image you posted, I think you may have forgotten to add weights to occupy the free space in your Linearlayouts. Try this:
<?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:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
andriod:orientation="horizontal"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>

How to set listviews in horizontalScrollView to fill up the screen?

I have three listviews in horizontalScrollView. I want to make each of them to occupy the full screen, by including them into a linearlayout which is set to (layout_width: match_parent, layout_height: match_parent). However, it doesn't work. As you see from the screenshot, the three listviews just tightly linked with one another.
Any suggestions so that the three listviews each occupies the full screen? THx a lot.
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/page_bg_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tab_login_myFriends_requestList"
android:background="#ffffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tab_login_myFriends_pendingList"
android:background="#ffffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tab_login_myFriends_confirmedList"
android:background="#ffffffff" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Use this,
http://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport
Set that to true. Also your listviews should probably have a weight attribute associated with them.
But I can't really figure out what you're trying to do. Maybe provide the xml and I'll guide you further.
I think what you're looking for is this,
http://developer.android.com/training/animation/screen-slide.html

layout_weight with fragment and linear layout

I am trying to make a two pane layout for my app, and i'm trying to use layout_weight but its not working correctly. It looks like it's reversed so that the view with more weight is smaller but when I checked it didn't even do that. Here is my xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<EditText
android:id="#+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search" >
<requestFocus />
</EditText>
<ListView
android:id="#+id/bookList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<fragment
android:id="#+id/fmtEditBook"
android:layout_weight="2"
android:layout_height="match_parent"
android:layout_width="0dp"
android:name="com.perlib.wmbg.fragments.EditBookFragment"
tools:layout="#layout/fragment_edit_book" />
</LinearLayout>
The outer LinearLayout has width wrap_content, change it to match_parent
I don't know the exact reason why this happens, but if you change the layout_width of the root element to match_parent it will work correctly.

Showing button below a fragment in an activity

I'm quite new to Android. I have a very simple layout that doesn't work yet:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout">
<fragment android:layout_width="fill_parent"
android:layout_height="match_parent"
android:name="pl.nscr.playwatch.MainActivity$WatchListFragment"
android:id="#+id/watchlist"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start service"/>
</LinearLayout>
Now my problem is that the fragment spans the whole available height in an activity. I'd like to be able to see that button below it, but I don't actually know how to layout this thing.
I tried using RelativeLayout, but I ended up with the button showing above the fragment which looks ugly. What am I doing wrong?
Update
Apparently it was a silly mistake. I changed back again to RelativeLayout but the layout now has width and height set to 'fill_parent' and everything works as expected.
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout">
<fragment android:layout_width="fill_parent"
android:layout_height="match_parent"
android:name="pl.nscr.playwatch.MainActivity$WatchListFragment"
android:id="#+id/watchlist"
android:layout_above="#+id/btnStartService"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#id/btnStartService"
android:text="Start service"/>
</RelativeLayout>
do the following
add android:orientation="vertical" attribute to <LinearLayout>
add layout_weight="1" attribute to <fragment />
Add android:orientation="vertical" to the LinearLayout..

Categories

Resources