-1

I have a recyclerview adapter where i want to set it to portrait only. I cant seem to find any resources on this.

Is there a way to do this programatically?

can i set it in onCreateViewHolder?

public class AlarmRecyclerViewAdapter extends RecyclerView.Adapter<AlarmRecyclerViewAdapter.DataObjectHolder> {


@Override
public AlarmRecyclerViewAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                                                    int viewType) {

    AlarmRecyclerViewAdapter.DataObjectHolder dataObjectHolder = null;
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_alarms, parent, false);
    dataObjectHolder = new AlarmRecyclerViewAdapter.DataObjectHolder(view);
    return dataObjectHolder;

}
steller
  • 245
  • 3
  • 14

3 Answers3

1

Specific views cannot request orientation. Activities can request orientation.

You can achieve as needed by adding below code in manifest:

 <activity
  android:name=".YourActivityName"
  android:screenOrientation="portrait"/>
Kruti Parekh
  • 1,271
  • 9
  • 21
Anice Jahanjoo
  • 7,088
  • 3
  • 20
  • 29
0

You can fix your activity's orientation in 2 ways:

  1. Using manifest as @nice j suggested

  2. Here is how you can set your activity's orientation programatically: link

Hope it helps

mark922
  • 1,136
  • 2
  • 11
  • 20
0

You can do this in two ways:

  1. In Android Manifest File

    <activity
      android:name=".YourActivityName"
      android:screenOrientation="portrait"/>
    
  2. Do it in Activity class programatically:

     @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
     }
    
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56