I'm trying to use masterDetail layout changing as minus as possible, trying to understand their default structure.
I'm trying app on my Samsung S5.
The app work normally, but in landscape mode doesn't add the fragment in the right part of layout.
I think the problem is that the variable mTwoPane doesn't change the value, remain false, this because findViewById(R.id.pokemon_detail_container) != null evidently is ever false.
This is the code:
the block inside onCreate in MainActivity:
if (findViewById(R.id.detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
the comments are default.
The detail_conter in activity_detail.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.soissy.masterdetail.DetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
This is the code point where if mTwoPane is true, i add the fragment. This method is inside a inner class inside MainActivity:
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setImageResource(mValues.get(position).id);
holder.mNameView.setText(mValues.get(position).name);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mTwoPane) Log.d("mTwoPane", "is true");
else Log.d("mTwoPane", "is false");
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putInt(DetailFragment.ARG_ITEM_ID, holder.mItem.id);
DetailFragment fragment = new DetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(DetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
I don't understand what I've to do to add the fragment(mTwoPane=true)
I figured out the problem. When you create with Android Studio a MasterDetail "activity" at the beginning, it automatically creates a layout for two pane mode. However, i think only for tablets because the specific options of this layout are "w900dp". So I created another layout for landscape mode. Then copied and pasted from layout "with w900dp" to my new layout with landscape orientation, and now all works fine.