User
User

Reputation: 1236

RecyclerView item Layout issue

I am using cardview for RecyclerView item. I want the item width to be match parent while the height to be wrap content. I wrote a layout, but somehow, my layout width fits only 60% of the screen width instead of 100%. Can someone point out whats going wrong here?

Thanks

Activity Layout:

<?xml version="1.0" encoding="utf-8"?>
<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=".main.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_marginTop="56dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/videoDataView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:layout_gravity="center_horizontal" />


    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>

RecyclerView item Layout:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/videoDataView"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="1dp"
    android:layout_marginBottom="1dp"
    android:layout_marginEnd="1dp"
    android:layout_marginStart="1dp"
    card_view:cardCornerRadius="1dp"
    card_view:cardElevation="1dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_marginTop="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/quser"
            android:layout_marginTop="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="@string/quser"/>

        <TextView
            android:id="@+id/auser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="@string/auser"/>

        <FrameLayout
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp">

            <ImageView
                android:id="@+id/thumbImg"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_gravity="center" />

            <ImageView
                android:id="@+id/play"
                android:layout_width="44dp"
                android:layout_height="44dp"
                android:layout_gravity="center"
                android:src="@drawable/ic_play_arrow_24dp"/>
        </FrameLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

Adapter class:

public class VideoDataListAdapter extends RecyclerView.Adapter<VideoDataListAdapter.ViewHolder> {

    private List<KwickieData> kwickieDataList;
    private Context context;
    private final String TAG = "VideoDataListAdapter";

    public VideoDataListAdapter(Context context, List<KwickieData> kwickieDataList) {
        this.kwickieDataList = kwickieDataList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_video_data,null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final KwickieData kwickieData = kwickieDataList.get(position);
        String qUsrString = holder.qusr.getText()+": "+kwickieData.getQuestionUser().getFirstName()+ " " + kwickieData.getQuestionUser().getLastName();
        String aUsrString = holder.ausr.getText()+": "+kwickieData.getAnswerUser().getFirstName()+" "+kwickieData.getAnswerUser().getLastName();
        holder.qusr.setText(qUsrString);
        holder.ausr.setText(aUsrString);
        String thumbUrl = kwickieData.getKwickieVideo().getThumbUrl();
        String extension = thumbUrl.substring(thumbUrl.length() - 4, thumbUrl.length());
        String filePath = context.getFilesDir().getAbsolutePath() + "/" + kwickieData.getKwickieVideo().getId() + extension;
        holder.thumbImg.setImageDrawable(Drawable.createFromPath(filePath));

        holder.playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"play button pressed");
                Intent intent = new Intent(context, VideoActivity.class);
                intent.putExtra("videourl",kwickieData.getKwickieVideo().getProcessPlaylistUrl());
                context.startActivity(intent);
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        TextView qusr =  (TextView) itemView.findViewById(R.id.quser);
        TextView ausr = (TextView) itemView.findViewById(R.id.auser);
        ImageView thumbImg = (ImageView) itemView.findViewById(R.id.thumbImg);
        ImageView playButton = (ImageView) itemView.findViewById(R.id.play);


    }
}

Upvotes: 1

Views: 842

Answers (1)

User
User

Reputation: 1236

I got it fixed by making Video View to Match_parent.

Upvotes: 1

Related Questions