Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ==========================
- package com.example.zeevm.myfragor;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //we don't need to set view, our fragment will handle it
- //setContentView(R.layout.activity_main);
- //Create instance of Fragment manager to handle our fragments
- FragmentManager fm = getFragmentManager();
- //create instance of Fragment transaction to handle fragment replace and animation
- FragmentTransaction ft = fm.beginTransaction();
- int displayMode = getResources().getConfiguration().orientation;
- if (displayMode==1)
- {
- //create instance of first fragment
- Fragment1 f1 = new Fragment1();
- //change content of the screen to our new fragment
- ft.replace(android.R.id.content,f1);
- }
- else
- {
- //create instance of second fragment
- Fragment2 f2 = new Fragment2();
- //change content of the screen to our new fragment
- ft.replace(android.R.id.content,f2);
- }
- //choose animation
- ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
- //commit all changes to screen fragments
- ft.commit();
- }
- }
- Fragment1.java
- =====================
- package com.example.zeevm.myfragor;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by zeevm on 31/10/2016.
- */
- public class Fragment1 extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //return my view as inflated view to the fragment
- return inflater.inflate(R.layout.activity_main,container,false);
- }
- }
- Fragment2.java
- =====================
- package com.example.zeevm.myfragor;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by zeevm on 31/10/2016.
- */
- public class Fragment2 extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.layout_horzintal,container,false);
- }
- }
- activity_main.xml
- ==================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="I Am vertical Fragment"
- android:gravity="center"
- android:textSize="32sp"
- android:id="@+id/txtFv"/>
- </LinearLayout>
- layout_horzintal.xml
- ========================
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="I am Horzintal Layout"
- android:gravity="center"
- android:background="#ff9e5e"
- android:textSize="32sp"
- android:id="@+id/txtHr"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement