mcprilla79
mcprilla79

Reputation: 99

Making recyclerView items fit the screen

i have a recyclerView which displays icons horizontal beneath the activity like a horizontal navigation bar.

RecyclerView Adapter and Activity

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realuser_int);
    Intent intent= new Intent(this,ListenKeyword.class);
    startService(intent);

    horizontal_recycler_view= (RecyclerView) findViewById(R.id.horizontal_recycler_view);
    Horizontalicons = new ArrayList<>();
    Horizontalicons.add(R.drawable.home);
    Horizontalicons.add(R.drawable.clock);
    Horizontalicons.add(R.drawable.records);
    Horizontalicons.add(R.drawable.menu);
    horizontalAdapter=new HorizontalAdapter(Horizontalicons);


    LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(realuserIntActivity.this, LinearLayoutManager.HORIZONTAL, false);
    horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
    horizontal_recycler_view.setAdapter(horizontalAdapter);

    user=(TextView) findViewById(R.id.textUser);

}

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder>  {
    String[]iconnames={"Home", "History", "History", "Menu"};
    private List<Integer> Horizontalicons;
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgIcons;

        public MyViewHolder(View view) {
            super(view);
            imgIcons = (ImageView) view.findViewById(R.id.imgrecycler);

        }
    }


    public HorizontalAdapter(List<Integer> Horizontalicons) {
        this.Horizontalicons = Horizontalicons;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.iconrecycler, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.imgIcons.setImageResource(Horizontalicons.get(position));

        holder.imgIcons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(realuserIntActivity.this, iconnames[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return Horizontalicons.size();
    }
}

XML FILE

 <android.support.v7.widget.RecyclerView
    android:id="@+id/horizontal_recycler_view"

    android:background="#263238"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

CUSTOM XML TO BE INFLATED IN RECYCLERVIEW

  <ImageView
    android:layout_width="63dp"
    android:layout_height="63dp"
    android:id="@+id/imgrecycler"
    android:layout_marginTop="9dp"
    android:layout_marginRight="97dp"
    android:paddingLeft="30dp"
    />

I've tried running on it on three different devices.The problem i'm facing is that, on one device with a smaller screen(phone) only three icons show, to see the last icon you will have to scroll to the left to see the last item. On a bigger screen(tablet) all icons or the items are shown without the need to scroll. Is there a way to make all the items fit the screen irrespective the size of the screen being used.

Upvotes: 2

Views: 3565

Answers (1)

Yupi
Yupi

Reputation: 4470

You can manipulate with your imageview width and height, margins and so on. You will have to create the context and pass it to the constructor of your adapter class so you can getResources like this:

 Context context;

passing to the constructor:

public HorizontalAdapter(Context context, List<Integer> Horizontalicons) {
    this.Horizontalicons = Horizontalicons;
    this.context = context;
}

Than inside MyViewHolder you can do something like this:

public MyViewHolder(View view) {
        super(view);
        imgIcons = (ImageView) view.findViewById(R.id.imgrecycler);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)imgIcons.getLayoutParams();

    int screenSize = context.getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK;

    switch(screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
        //do stuff
            break;

        case Configuration.SCREENLAYOUT_SIZE_LARGE:
           // do stuff
            break;

        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
         //do stuff
            break;

        case Configuration.SCREENLAYOUT_SIZE_SMALL:
         //for example here

                lp.height = 20;
                lp.width = 20;
                lp.leftMargin = 20;
                lp.rightMargin = 20;
                imgIcons.setLayoutParams(lp);
            break;

        default:
       }
    }

I used this pattern to setup text size in my app and cardviews height.

Upvotes: 1

Related Questions