So here is my RecyclerView's xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/bikename_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And here's the layout inflated by the RecyclerView's Adapter.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/bikenames_cardview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/bikenames_imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/bikenames_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
So here I need to find the width of CardView (@id/bikenames_cardview) in my Activity or in the RecyclerView adapter. I have wrote RecyclerView adapter also.
Adapter code
public class BikeNamesAdapter extends RecyclerView.Adapter<BikeNamesViewHolder> {
BikeNames bikeNames;
Context context;
public BikeNamesAdapter(BikeNames bikeNames) {
this.bikeNames = bikeNames;
}
@Override
public int getItemCount() {
return bikeNames.getLength();
}
@Override
public BikeNamesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.cardview_bike_names, parent, false);
return new BikeNamesViewHolder(view);
}
@Override
public void onBindViewHolder(BikeNamesViewHolder holder, int position) {
holder.textViewBikeName.setText(bikeNames.getBikeNameByIndex(position));
}
}
I might have skipped some codes, to reduce length here.