Skip to content

Instantly share code, notes, and snippets.

@menuka94
Last active February 5, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menuka94/0a50e7cce6e89d6a0a2d37de50016c37 to your computer and use it in GitHub Desktop.
Save menuka94/0a50e7cce6e89d6a0a2d37de50016c37 to your computer and use it in GitHub Desktop.
Android Fragments - 1
package com.android.menuka.androidfragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by menuka on 2/5/17.
*/
public class FragmentLandscape extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.landscape_fragment, container, false);
}
}
package com.android.menuka.androidfragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by menuka on 2/5/17.
*/
public class FragmentPortrait extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.portrait_fragment, container, false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_landscape_fragment"
android:textSize="30dp"/>
</LinearLayout>
package com.android.menuka.androidfragments;
import android.content.res.Configuration;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Configuration configInfo = getResources().getConfiguration();
if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
FragmentLandscape fragmentLandscape = new FragmentLandscape();
fragmentTransaction.replace(android.R.id.content, fragmentLandscape);
}else{
FragmentPortrait fragmentPortrait = new FragmentPortrait();
fragmentTransaction.replace(android.R.id.content, fragmentPortrait);
}
fragmentTransaction.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In Portrait Fragment"
android:textSize="30dp"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment