Last active
February 5, 2017 14:08
-
-
Save menuka94/0a50e7cce6e89d6a0a2d37de50016c37 to your computer and use it in GitHub Desktop.
Android Fragments - 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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