m_naish

Fragment01

Nov 17th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MainActivity.java
  2. ==========================
  3. package com.example.zeevm.myfragor;
  4.  
  5. import android.app.FragmentManager;
  6. import android.app.FragmentTransaction;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.widget.Toast;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         //we don't need to set view, our fragment will handle it
  17.         //setContentView(R.layout.activity_main);
  18.  
  19.         //Create instance of Fragment manager to handle our fragments
  20.         FragmentManager fm = getFragmentManager();
  21.         //create instance of Fragment transaction to handle fragment replace and animation
  22.         FragmentTransaction ft = fm.beginTransaction();
  23.  
  24.         int displayMode = getResources().getConfiguration().orientation;
  25.  
  26.         if (displayMode==1)
  27.         {
  28.             //create instance of first fragment
  29.             Fragment1 f1 = new Fragment1();
  30.             //change content of the screen to our new fragment
  31.             ft.replace(android.R.id.content,f1);
  32.         }
  33.         else
  34.         {
  35.             //create instance of second fragment
  36.             Fragment2 f2 = new Fragment2();
  37.             //change content of the screen to our new fragment
  38.             ft.replace(android.R.id.content,f2);
  39.         }
  40.         //choose animation
  41.         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  42.         //commit all changes to screen fragments
  43.         ft.commit();
  44.     }
  45. }
  46.  
  47.  
  48. Fragment1.java
  49. =====================
  50. package com.example.zeevm.myfragor;
  51.  
  52. import android.app.Fragment;
  53. import android.os.Bundle;
  54. import android.support.annotation.Nullable;
  55. import android.view.LayoutInflater;
  56. import android.view.View;
  57. import android.view.ViewGroup;
  58.  
  59. /**
  60.  * Created by zeevm on 31/10/2016.
  61.  */
  62.  
  63. public class Fragment1 extends Fragment {
  64.     @Nullable
  65.     @Override
  66.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  67.         //return my view as inflated view to the fragment
  68.         return inflater.inflate(R.layout.activity_main,container,false);
  69.     }
  70. }
  71.  
  72.  
  73. Fragment2.java
  74. =====================
  75. package com.example.zeevm.myfragor;
  76.  
  77. import android.app.Fragment;
  78. import android.os.Bundle;
  79. import android.support.annotation.Nullable;
  80. import android.view.LayoutInflater;
  81. import android.view.View;
  82. import android.view.ViewGroup;
  83.  
  84. /**
  85.  * Created by zeevm on 31/10/2016.
  86.  */
  87.  
  88. public class Fragment2 extends Fragment {
  89.     @Nullable
  90.     @Override
  91.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  92.         return inflater.inflate(R.layout.layout_horzintal,container,false);
  93.     }
  94. }
  95.  
  96.  
  97. activity_main.xml
  98. ==================
  99. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  100.     android:layout_width="match_parent"
  101.     android:layout_height="match_parent"
  102.     android:orientation="vertical">
  103.  
  104.     <TextView
  105.         android:layout_width="match_parent"
  106.         android:layout_height="match_parent"
  107.         android:text="I Am vertical Fragment"
  108.         android:gravity="center"
  109.         android:textSize="32sp"
  110.         android:id="@+id/txtFv"/>
  111.  
  112. </LinearLayout>
  113.  
  114.  
  115. layout_horzintal.xml
  116. ========================
  117. <?xml version="1.0" encoding="utf-8"?>
  118. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  119.     android:orientation="horizontal" android:layout_width="match_parent"
  120.     android:layout_height="match_parent">
  121.  
  122.     <TextView
  123.         android:layout_width="match_parent"
  124.         android:layout_height="match_parent"
  125.         android:text="I am Horzintal Layout"
  126.         android:gravity="center"
  127.         android:background="#ff9e5e"
  128.         android:textSize="32sp"
  129.         android:id="@+id/txtHr"/>
  130.  
  131. </LinearLayout>
Add Comment
Please, Sign In to add comment