I have an activity whose ist fragment is set to normal potrait orientation but on loading the second fragment, I need to lock the orientation of the second fragment to landscape. Is it possible to do that?
This is my java fragment class
public class DeputationsFragment extends Fragment implements AdapterView.OnItemSelectedListener{
LinearLayout ll;
RecyclerView recyclerView;
SessionManager sessionManager;
CheckBox checkBox;
Spinner reportType;
String[] reportTypes,reportCodes;
String reportCode;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
return inflater.inflate(R.layout.fragment_deputations, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sessionManager=SessionManager.NewInstance(getActivity());
ll=(LinearLayout)view.findViewById(R.id.ll1);
recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
checkBox=(CheckBox)view.findViewById(R.id.sno);
reportType=(Spinner)view.findViewById(R.id.spin_report);
reportType.setOnItemSelectedListener(this);
if(Constants.isOnline())
{
getTypes();
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
reportCode=reportCodes[i];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private void getTypes()
{
AsyncHttpClient client=new AsyncHttpClient();
String url=getResources().getString(R.string.urlReportType);
Constants.showProgress(getActivity());
client.get(url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Constants.dismissProgress();
try {
int p1 = responseString.indexOf(">"); Constants.dismissProgress();
Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
int p2 = responseString.lastIndexOf("<");
String r = responseString.substring(p1 + 1, p2);
p1 = r.indexOf(">");
r = r.substring(p1 + 1, r.length());
JSONArray jsonArray = new JSONArray(r);
reportTypes = new String[jsonArray.length()];
reportCodes = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
reportTypes[i] = (jsonObject.getString("Name"));
reportCodes[i] = (jsonObject.getString("Value"));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, reportTypes);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportType.setAdapter(arrayAdapter);
reportType.setSelection(0);
reportCode = reportCodes[0];
getDeputations();
} catch (Exception e) {
Constants.dismissProgress();
Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
}
}
});
}
void getDeputations()
{
Constants.showProgress(getActivity());
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
String url=getResources().getString(R.string.urlDeputationRequest);
params.put("DistrictCode","ydRRTTxkdt6Trx91pX1+cA==");
params.put("ReportType","1");
client.get(url, params, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
try {
Constants.dismissProgress();
int p1 = responseString.indexOf(">");
int p2 = responseString.lastIndexOf("<");
String r = responseString.substring(p1 + 1, p2);
p1 = r.indexOf(">");
r = r.substring(p1 + 1, r.length());
JSONArray jsonArray = new JSONArray(r);
RecyclerDeputations recyclerDeputations=new RecyclerDeputations(jsonArray,getActivity());
ll.setVisibility(View.VISIBLE);
recyclerView.setAdapter(recyclerDeputations);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
catch (Exception e)
{
}
}
});
}
}
this is the layout file:
<?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"
android:padding="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/ll"
android:background="@color/colorAccent"
android:layout_marginTop="10dp"
android:weightSum="4">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1.7"
android:gravity="right"
android:padding="3dp"
android:text="Report Type"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="2.3"
android:gravity="left"
android:padding="3dp"
android:background="@color/white"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<Spinner
android:id="@+id/spin_report"
android:layout_width="match_parent"
android:popupBackground="@color/white"
android:gravity="left"
android:layout_height="match_parent"
android:textSize="8sp"
android:scrollbars="horizontal"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="8.5"
android:background="#386FA5"
android:id="@+id/ll1"
android:orientation="horizontal">
<CheckBox
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"
android:gravity="center_vertical"
android:layout_gravity="start"
android:text="All"
android:background="@drawable/borders"
android:id="@+id/sno"/>
<TextView
android:layout_width="0dp"
android:text="S.No."
android:gravity="start"
android:layout_gravity="start"
android:background="@drawable/borders"
android:id="@+id/serial"
android:textSize="12sp"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight=".5" />
<TextView
android:layout_width="0dp"
android:text="Name"
android:id="@+id/name"
android:gravity="start"
android:layout_gravity="start"
android:textSize="12sp"
android:background="@drawable/borders"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:id="@+id/from"
android:gravity="start"
android:textSize="12sp"
android:layout_gravity="start"
android:text="From"
android:background="@drawable/borders"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight="1.4" />
<TextView
android:layout_width="0dp"
android:id="@+id/to"
android:text="To"
android:gravity="start"
android:textSize="12sp"
android:layout_gravity="start"
android:background="@drawable/borders"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight="1.3" />
<TextView
android:layout_width="0dp"
android:id="@+id/req_date"
android:text="Request Date"
android:gravity="start"
android:textSize="12sp"
android:layout_gravity="start"
android:background="@drawable/borders"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:id="@+id/pending"
android:text="Pending With"
android:gravity="start"
android:textSize="12sp"
android:layout_gravity="start"
android:background="@drawable/borders"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:id="@+id/remarks"
android:text="Remarks"
android:gravity="start"
android:textSize="12sp"
android:layout_gravity="start"
android:background="@drawable/borders"
android:padding="3dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</LinearLayout>
I have implemented the same functionality myself. My app is locked to Portrait mode for all fragments except one. In order to achieve the correct functionality I use android:screenOrientation="nosensor"
for the activity (in the Manifest file) preventing orientation changes due to the user rotating the device.
I also use android:configChanges="keyboardHidden|orientation|screenSize"
to prevent the activity to restart when the orientation is changed. Otherwise, it's easy to end up in a loop where the activity restarts, goes into the correct fragment and changes the orientation again, repeat.
In the Fragment's onResume()
method I call getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and in it's onPause()
I call getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Hope this helps you!