Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make multi pane layout for Mobile Phone and Tablet

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 660
    Comment on it

    In this example,we will have a layout in which there are two fragments in an activity.

     

    In our mobile phone,we will show only one fragment but in tablets,we will show both fragments simuntanously.

    activity_main.xml (for mobile phone)

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:orientation="vertical">
    7. <fragment
    8. android:id="@+id/main_panel"
    9. class="com.multipaneui.fragment.MainFragment"
    10. android:layout_width="match_parent"
    11. android:layout_height="match_parent"
    12. />
    13. </RelativeLayout>

     

    Now make another layout folder with name layout-sw600dp and inside that folder,make a layout file with same name(activity_main.xml)

     

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:orientation="horizontal"
    6. android:baselineAligned="false"
    7. android:weightSum="1">
    8. <fragment
    9. android:id="@+id/side_panel"
    10. android:layout_weight="0.3"
    11. class="com.multipaneui.fragment.SideFragment"
    12. android:layout_width="wrap_content"
    13. android:layout_height="match_parent" />
    14. <fragment
    15. android:id="@+id/main_panel"
    16. class="com.multipaneui.fragment.MainFragment"
    17. android:layout_width="wrap_content"
    18. android:layout_height="match_parent"
    19. android:layout_weight="0.7" />
    20. </LinearLayout>

     

    Simple layout folder is for mobile but layout-sw600dp is for tablets of screen resolution>=600dp.

     

    Now for java file,MainActivity.java

     

    1. public class MainActivity extends AppCompatActivity {
    2. private static final String TAG = MainActivity.class.getSimpleName();
    3. @Override
    4. protected void onCreate(Bundle savedInstanceState) {
    5. super.onCreate(savedInstanceState);
    6. setContentView(R.layout.activity_main);
    7. }
    8. }

     

    That's it folks.Now u can test your app at mobile and tablet.Have fun:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: