Reputation: 257
I have two fragments. Once I start the app First fragment opens. I open second fragment when i click on any item inside my recyclerview in the first fragment. I want to show both fragments on the screen same time when it's a tablet(Like master-detail layout). Can I do it from a fragment? I know that fragments live inside activity. But is there a way to detect that app is running on the tablet from a fragment?
I have activity_main.xml which has one Framelayout where I programmatically replace my first fragment. And I have four other activity_main.xml (w600dp,w600dp-land,w720dp,w720dp-land) layouts for tablets that have two Framelayouts.
Upvotes: 0
Views: 809
Reputation: 529
You can use master detail fragment to show two fragments in one layout and to separate tablet and phone use
if (Common.IsTablet(Context))
{
}
public static bool IsTablet(Context context)
{
return ((context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) >= ScreenLayout.SizeLarge) ||
((context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) >= ScreenLayout.SizeXlarge);
} // to detect whether device is tablet
Upvotes: 0
Reputation: 23404
in res directory create new folder values-w820dp
create dimens.xml file there inside dimens.xml write this
<?xml version="1.0"?>
-<resources>
<!-- Example customization of dimensions originally defined inres/values /dimens.xml (such as screen margins) for screens with more than 820dp of available width. This would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
<bool name="is_tablet">true</bool>
</resources>
inside your dimens.xml file in your values folder write this
<?xml version="1.0"?>
-<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<bool name="is_tablet">false</bool>
</resources>
in your activity get "is_tablet" values from dimens.xml value , if it is true it means it is a tablet , else it is not this is the example code which i used
public class FragmentsFirstActivity extends AppCompatActivity implements IFragments{
public static final String DEBUG = "Tutorials";
FragmentsFirstActivityFragment mFirstFragment;
FragmentsSecondActivityFragment mSecondFragment;
boolean is_tablet = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments_first_activity);
is_tablet = getResources().getBoolean(R.bool.is_tablet);
Log.d(DEBUG, "Is Tablet: " + is_tablet);
mFirstFragment = new FragmentsFirstActivityFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.first_fragment, mFirstFragment).commit();
if (is_tablet) {
mSecondFragment = new FragmentsSecondActivityFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.second_fragment, mSecondFragment).commit();
}
}
@Override
public void onButtonClicked(String message) {
if(is_tablet) {
} else {
startActivity(new Intent(this, FragmentsSecondActivity.class).putExtra("message", message));
}
}
}
hope this helps
Upvotes: 2