Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to dynamically change fragment layout

I have a tablet app with an about fragment which displays my company's datas. I would like to change my layout dynamically with screen orientation while im on the page but i didn't found how yet.

If someone got the solution, thanks in advance

like image 218
Jackyto Avatar asked Jul 24 '13 08:07

Jackyto


People also ask

What are dynamic fragments in Android?

A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. A fragment is implemented as independent object -- independent of the activity that contains it. The benefit is that it can be used by multiple activities associated with the application.

What is FragmentContainerView?

FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.

What is static and dynamic fragment in Android?

A static Fragment is included in an XML file using a fragment tag within another XML layout. A dynamic Fragment isn't associated with a fragment tag and it is created in association with the FragmentManager.


2 Answers

@Yume177, i found how to do it :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.about);
    rl.removeAllViews();
    rl.addView(View.inflate(myView.getContext(), R.layout.about, null));
}

I have 2 RelativeLayout both named "about.xml", with one layout in a dir res/layout-sw720dp/about.xml with my drawable for portrait mode and res/layout-sw720dp-land/about.xml with my landscape drawable. When i rotate my tablet, my layout fits perfectly. Hope i'll help you

like image 85
Jackyto Avatar answered Oct 31 '22 13:10

Jackyto


You can try this out http://blogs.captechconsulting.com/blog/steven-byle/android-tutorial-optimizing-phones-and-tablets-fragments

If you are using ABS + viewpager it'll be kind of difficult (if you find a solution i'm interrested ! see my last question)

Here is the official doc http://developer.android.com/training/basics/fragments/fragment-ui.html

http://developer.android.com/guide/practices/tablets-and-handsets.html

http://developer.android.com/guide/practices/screens_support.html

like image 23
An-droid Avatar answered Oct 31 '22 11:10

An-droid