AndyAndroid
AndyAndroid

Reputation: 4069

Android: Fragment layout issue

For large screens I have two fragments, they shall be either top/bottom or left/right. The problem is that one fragment is taking (almost) all space is taking up by one fragment. For my activity I have two main.xml (one for portrait one for landscape). But they are basically similar (apart from orientation).

main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" android:id="@+id/frag_cont" >
 </LinearLayout>

In onCreate I add my fragments to that layout:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    OverviewFragment of = new OverviewFragment();
    FragmentTransaction tof = getFragmentManager().beginTransaction();
    tof.add(R.id.frag_cont, of);
    DetailFragmentInitial df = new DetailFragmentInitial();
    tof.add(R.id.frag_cont, df);
    tof.commit();

}

The two fragments look kind of similar in terms of onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{   
    return inflater.inflate(R.layout.overview, container, false);

}

Finally the two xml files for these fragments look like this, first the smaller fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1">


<ImageView
    android:id="@+id/imageLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo"
    android:layout_centerInParent="true" />

    <TextView
    android:id="@+id/textB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="text"
    android:layout_above="@id/imageLogo" 
    android:layout_centerHorizontal="true"/>
</RelativeLayout>

And now the greedy one taking the space:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"  
android:layout_weight="10000" >

 <ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
 </ListView>

</LinearLayout>

I assumed that when I set both layout_weight to 1 then both will take the same amount of space, but even with the setting 1000:1 the bigger one takes about 2/3 of the space. Leaving the weight away will give the bigger fragment all the screen and the other one is not visiable at all. Any ideas?

Edit:

I followed the approach given below. Much better now in terms of space. On screen rotate the overview fragment (basically list view) is empty, the other fragment is still okay.

In OverviewFragment.java I repopulate the fragment in onActivityCreated():

public void onSaveInstanceState(Bundle bundle)
{
    bundle.putSerializable("list", mList);
    super.onSaveInstanceState(bundle);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
     super.onActivityCreated(savedInstanceState);

    if(savedInstanceState != null)
    {
        mList = (ArrayList<myObject>) savedInstanceState.getSerializable("list");
    }
    else
    {
        mAppList = new ArrayList<myObject>();
        new getApps().execute();
    }

    ListView lv = (ListView) getActivity().findViewById(R.id.ListView01);
    lv.setOnItemClickListener(new TableItemSelected());
    mItemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(), mList);
    lv.setAdapter(mItemAdapter);
}

when rotating the screen the onSaveInstanceState() is called first and then onActivityCreated() twice. The first call the saveInstanceState is not null, the second time it is. Anyway in both cases I should see my list, either build up from scratch or the saved list. Why isn't it and why is that method called twice?

Upvotes: 0

Views: 1144

Answers (1)

Alex Curran
Alex Curran

Reputation: 8828

Not entirely sure whether this will be causing your issue (I suspect it does), but using FragmentTransaction.add(..) doesn't add the Fragment into the container, it essentially replaces the container with that Fragment. The issue here is that you're adding both Fragments to the same container so they will take up the same space and cause issues.

A better way to do this would be to add two container views into your frag_cont LinearLayout (e.g. FrameLayouts), and give them two separate ids (e.g. frag_one and frag_two). Then in your onCreate(..), change it to read:

FragmentTransaction tof = getFragmentManager().beginTransaction();
tof.add(R.id.frag_one, of);
DetailFragmentInitial df = new DetailFragmentInitial();
tof.add(R.id.frag_two, df);
tof.commit();

This will make it so that the layout with id frag_one is filled by the OverviewFragment and the one with id frag_two replaced by the DetailFragmentInitial. You can then use the views in main.xml to tweak the sizes of the fragments as you see fit.

Upvotes: 1

Related Questions