I am almost done with an app where I used the bottom navigation bar to switch between different layouts (not activities, if relevant), and with some custom layouts for landscape orientation for both phone and tablet. My app behaves as expected when I run it in the emulator, but when I export an apk to test on my device (phone in this case), the app won't switch to landscape mode at all.
Let me know if you want me to paste some parts of my code you think would be relevant - I have no idea where to start regarding this problem.
EDIT: Here are the manifest and activity files:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".StoryListActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StoryDetailActivity"
android:label="@string/title_story_detail"
android:parentActivityName=".StoryListActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.compy.someappname.StoryListActivity" />
</activity>
</application>
Activity:
public class StoryListActivity extends AppCompatActivity {
private FrameLayout mainFrameLayout;
private FrameLayout shopFrameLayout;
private FrameLayout infoFrameLayout;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
private int visiblePart;
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
if (mainFrameLayout.getVisibility() == View.VISIBLE) {
savedInstanceState.putInt("visiblePart", 0);
} else if (shopFrameLayout.getVisibility() == View.VISIBLE) {
savedInstanceState.putInt("visiblePart", 1);
} else if (infoFrameLayout.getVisibility() == View.VISIBLE) {
savedInstanceState.putInt("visiblePart", 2);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
visiblePart = savedInstanceState.getInt("visiblePart");
}
setContentView(R.layout.activity_story_list);
mainFrameLayout = (FrameLayout) findViewById(R.id.mainFrameLayout);
shopFrameLayout = (FrameLayout) findViewById(R.id.shopFrameLayout);
infoFrameLayout = (FrameLayout) findViewById(R.id.infoFrameLayout);
appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
TextView titleTextView2 = (TextView) findViewById(R.id.titleTextView2);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Ovo-Regular.ttf");
titleTextView.setTypeface(typeface);
titleTextView2.setTypeface(typeface);
if (visiblePart == 0) {
mainFrameLayout.setVisibility(View.VISIBLE);
shopFrameLayout.setVisibility(View.GONE);
infoFrameLayout.setVisibility(View.GONE);
} else if (visiblePart == 1) {
mainFrameLayout.setVisibility(View.GONE);
shopFrameLayout.setVisibility(View.VISIBLE);
infoFrameLayout.setVisibility(View.GONE);
} else if (visiblePart == 2) {
mainFrameLayout.setVisibility(View.GONE);
shopFrameLayout.setVisibility(View.GONE);
infoFrameLayout.setVisibility(View.VISIBLE);
}
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_main:
mainFrameLayout.setVisibility(VISIBLE);
shopFrameLayout.setVisibility(GONE);
infoFrameLayout.setVisibility(GONE);
break;
case R.id.action_shop:
mainFrameLayout.setVisibility(GONE);
shopFrameLayout.setVisibility(VISIBLE);
infoFrameLayout.setVisibility(GONE);
break;
case R.id.action_info:
mainFrameLayout.setVisibility(GONE);
shopFrameLayout.setVisibility(GONE);
infoFrameLayout.setVisibility(VISIBLE);
break;
}
return false;
}
});
View recyclerView = findViewById(R.id.story_list);
assert recyclerView != null;
setupRecyclerView((RecyclerView) recyclerView);
if (findViewById(R.id.story_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;
}
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.story_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).story_title);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(StoryDetailFragment.ARG_ITEM_ID, holder.mItem.id);
StoryDetailFragment fragment = new StoryDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.story_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, StoryDetailActivity.class);
intent.putExtra(StoryDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Ovo-Regular.ttf");
mIdView.setTypeface(typeface);
mContentView.setTypeface(typeface);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
}
You can try this
/**
* Getting screen orientation and ON rotation if it is OFF
*/
private void rotationON() {
if (Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
}
}
/**
* Getting screen orientation and Off rotation if it is ON
*/
private void rotationOFF() {
if (Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
}
}