TableRow in TableLayout and TableRow from RecyclerView aren’t align - android

I believe TableRow from activity_main and TableRow from recycler_item_header_row have the same values of fields. But they are not aligned! They are shown like this:
enter image description here
enter image description here
Why the positions of textViews in rows from the activity_main and from the RecyclerView aren’t aligned?
activity_main structure:
<TableLayout>
<TableRow ><\TableRow>
<RecyclerView> <\RecyclerView>
<\Table Layout>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_column="0"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="2"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</TableLayout>
Two types of item for Recycler View:
recycler_item_header_row:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_column="0"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="2"
android:layout_gravity="center"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
recycler_item_regular_row:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
>
<TextView
android:id="#+id/recycler_item_regular_cell_ID"
android:layout_column="0"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="ID"
android:textSize="16dp" />
<TextView
android:id="#+id/cell_name"
android:layout_column="1"
android:layout_weight="0.4"
android:width="0dp"
android:gravity="center"
android:text="Name"
android:textSize="16dp" />
<TextView
android:id="#+id/cell_payment"
android:layout_column="2"
android:layout_weight="0.3"
android:width="0dp"
android:gravity="center"
android:text="Payment"
android:textSize="16dp" />
</TableRow>
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<PaymentModel> paymentData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paymentData = new ArrayList<>();
add10TestItems(paymentData);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, paymentData);
recyclerView.setAdapter(adapter);
}
private void add10TestItems(ArrayList<PaymentModel> paymentData) {
for (int i = 0; i < 10; i++) {
paymentData.add(new PaymentModel("A" + i, "Name",
String.valueOf(5 * i)));
}
paymentData.add(new PaymentModel("IDDDDDDDDDDDDDDDDDD", "dddddddddddddddd", "dsfdfdf"));
paymentData.add(new PaymentModel("ID", "Name", "Payment"));
}
}
Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int HEADER_ROW_TYPE = 0;
public static final int REGULAR_ROW_TYPE = 1;
private Context context;
private List<PaymentModel> paymentModelList;
public RecyclerViewAdapter(Context context, List<PaymentModel> paymentModelList) {
this.context = context;
this.paymentModelList = paymentModelList;
}
#Override
public int getItemViewType(int position) {
if (0 == position) {
return HEADER_ROW_TYPE;
} else {
return REGULAR_ROW_TYPE;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder viewHolder;
if (viewType == HEADER_ROW_TYPE) {
view = LayoutInflater.from(context).inflate(R.layout.recycler_item_header_row,
parent, false);
viewHolder = new ViewHolderHeaderRow(view);
} else {
view = LayoutInflater.from(context).inflate(R.layout.recycler_item_regular_row,
parent, false);
viewHolder = new ViewHolderRegularRow(view);
}
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (position == 0) {
ViewHolderHeaderRow headerAdapter = (ViewHolderHeaderRow) holder;
} else {
ViewHolderRegularRow regularAdapter = (ViewHolderRegularRow) holder;
regularAdapter.setData(paymentModelList.get(position - 1));
}
}
#Override
public int getItemCount() {
return (paymentModelList.size() + 1);
}
public class ViewHolderRegularRow extends RecyclerView.ViewHolder {
private TextView cellID;
private TextView cellName;
private TextView cellPayment;
public ViewHolderRegularRow(#NonNull View itemView) {
super(itemView);
cellID = itemView.findViewById(R.id.recycler_item_regular_cell_ID);
cellName = itemView.findViewById(R.id.cell_name);
cellPayment = itemView.findViewById(R.id.cell_payment);
}
public void setData(PaymentModel paymentModel) {
cellID.setText(paymentModel.getId());
cellName.setText(paymentModel.getName());
cellPayment.setText(paymentModel.getPayment());
}
}
public class ViewHolderHeaderRow extends RecyclerView.ViewHolder {
public ViewHolderHeaderRow(#NonNull View itemView) {
super(itemView);
}
}
}
Model:
public class PaymentModel {
private String id;
private String name;
private String payment;
public PaymentModel(String id, String name, String payment) {
this.id = id;
this.name = name;
this.payment = payment;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPayment() {
return payment;
}
}

I would not use TableLayout and TableRow as TableLayout extra logic to resize any child that is a TableRow BUT as you only have one TableRow as a child of the TableLayout that logic is redundant and is less efficient.
The TableRow's that are inside the RecyclerView are the children of the RecyclerView not children of the TableLayout and as the Docs says
If a TableRow's parent is not a TableLayout, the TableRow will behave as an horizontal LinearLayout
And thus won't do any of their normal resizing of the table columns
So in practice you have actually got a structure like:-
<TableLayout>
<TableRow ><\TableRow>
<\TableLayout>
<RecyclerView>
<\LinearLayout>
<\LinearLayout>
.....
<\RecyclerView>
So as the layout weights are based on content and how much each is allowed to grow if need to, the long content RecyclerView rows grow the size of the long content cells differently to the items in the unrelated TableLayout Rows which don't need to grow.
I've designed a similar type layout but used a ConstraintLayout with a layout_constraintWidth_percent value to achieve wider Name column you are trying to achieve with the weights. This works as this is based on the percentage of the fixed width of the parent NOT the variable width of the content and thus the items in the RecyclerView behave the same are the Header line as they both have the same parent size.
Below are the new layout files need
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="#+id/HeaderID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/HeaderName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"/>
<TextView
android:id="#+id/HeaderName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderID"
app:layout_constraintEnd_toStartOf="#+id/HeaderPayment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderID"
app:layout_constraintWidth_percent="0.4"/>
<TextView
android:id="#+id/HeaderPayment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderID"
app:layout_constraintWidth_percent="0.3"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/HeaderID"/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item_header_row
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">
<TextView
android:id="#+id/HeaderRowID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ID"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/HeaderRowName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"/>
<TextView
android:id="#+id/HeaderRowName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderRowID"
app:layout_constraintEnd_toStartOf="#+id/HeaderRowPayment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderRowID"
app:layout_constraintWidth_percent="0.4"/>
<TextView
android:id="#+id/HeaderRowPayment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Payment"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/HeaderRowName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/HeaderRowID"
app:layout_constraintWidth_percent="0.3"/>
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item_regular_row
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">
<TextView
android:id="#+id/recycler_item_regular_cell_ID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintEnd_toStartOf="#+id/cell_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"
tools:text="ID" />
<TextView
android:id="#+id/cell_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintStart_toEndOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintEnd_toStartOf="#+id/cell_payment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintWidth_percent="0.4"
tools:text="Name" />
<TextView
android:id="#+id/cell_payment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="#+id/recycler_item_regular_cell_ID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/cell_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.3"
tools:text="Payment" />
</androidx.constraintlayout.widget.ConstraintLayout>
This produces the following result with all the columns lined up.

Related

Constraintlayout does not behave well as recyclerview 's item

I have a recyclerview whose items look like this:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="horizontal"
android:padding="4dp">
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
I'm trying to convert this into a flat hierarchy using Constraintlayout.
Here's how it looks:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toBottomOf="#+id/food_title_homepage" />
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:cv_color="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here's where my Recyclerview is inserted:
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.homepage.HomepageFragment">
<ProgressBar
android:id="#+id/homepage_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
<include
android:id="#+id/homepage_toolbar"
layout="#layout/fragment_homepage_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here's how each item should be: https://i.stack.imgur.com/AcfAm.png
But I don't get the same results in the Recyclerview: https://i.stack.imgur.com/jpUG4.png
I didn't have the problem before I converted the view into Constraintlayout!! Everything was working just fine until now that I converted them. They all become left-aligned in the Recyclerview!!
Does anyone know what the problem is?? Thanks in advance
-- Updated --
How it looks like on an android emulator or any other devices: https://i.stack.imgur.com/tvGz4.png
When I scroll down to the bottom (I update the list after each scroll) some items become right-aligned (they somehow get fixed) but the rest stays the same (still left aligned)
-- Java Code --
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
viewModel = new ViewModelProvider(this).get(HomepageViewModel.class);
getData(0);
setRecyclerView(linearLayoutManager);
EndlessRecyclerViewScrollListener scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
getData(page);
}
};
recyclerView.addOnScrollListener(scrollListener);
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private List<Food> foods;
FoodAdapter(List<Food> foods) {
this.foods = foods;
}
static class FoodViewHolder extends RecyclerView.ViewHolder {
CircleView foodImage;
TextView foodTitle, ingredients;
CardView foodItem;
FoodViewHolder(View itemView) {
super(itemView);
foodImage = itemView.findViewById(R.id.food_image);
foodTitle = itemView.findViewById(R.id.food_title_homepage);
ingredients = itemView.findViewById(R.id.ingredients_homepage);
foodItem = itemView.findViewById(R.id.card_food_item);
}
}
#NonNull
#Override
public FoodViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_homepage_food_item, parent, false);
return new FoodViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull FoodViewHolder holder, int position) {
final Food currentFood = foods.get(position);
StringBuilder food_ingredient_str = new StringBuilder();
int ingredient_position = 0;
int ingredients_size = currentFood.getFood_ingredients().size();
for (Food.FoodIngredient foodIngredient : currentFood.getFood_ingredients()) {
food_ingredient_str.append(foodIngredient.getIngredient());
if (ingredient_position != ingredients_size / 3)
food_ingredient_str.append(", ");
if (ingredient_position == ingredients_size / 3) {
food_ingredient_str.append(" و ...");
break;
}
ingredient_position += 1;
}
holder.foodTitle.setText(currentFood.getName());
holder.ingredients.setText(food_ingredient_str);
holder.foodItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("current_food", currentFood);
Navigation.findNavController(v).navigate(R.id.action_navigation_home_to_navigation_food, bundle);
}
});
}
#Override
public int getItemCount() {
return foods.size();
}
If you want to create a list using recyclerview and each item to be right aligned as in Arabic.. then try these approaches
replace your "recycle_view_item_layout.xml" with this
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
</LinearLayout>
</androidx.cardview.widget.CardView>
replace your "recycle view" with this
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
and attach a layout manager like this (important)
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
setRecyclerView(layoutManager);
and rest of your code seems okay

Problem with horizontal recyclerview in android

I'm filling in my Recycler View with the data of all the records from my SQLite DB (one record under another). I need to scroll the Recycler View horizontal, because some words are larger and don't enter into the screen, like you can see in the image below:
This is my RecyclerView Layout, but still not working:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etIngresar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ingrese un verbo"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
<Button
android:id="#+id/bnMostrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mostrar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.822"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.019" />
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvListItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="70dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="70dp"
android:scrollbarSize="4dp"
android:scrollbars="horizontal"
android:orientation="horizontal" />
</RelativeLayout>
I can't set this in my Activity because it converts the scrolling horizontal BUT it put all the records in one line, and I don't want it.
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true);
Like this:
EDITED
My Recycler View class:
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private ArrayList<Items> listItem ;
public RecyclerViewAdapter(ArrayList<Items> listItem) {
this.listItem = listItem;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista, null);
System.out.println("CREATE VIEW HOLDER: " + viewType);
return new Holder(contentView);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Items item = listItem.get(position);
Holder Holder = (Holder) holder;
Holder.tvVerbo.setText(item.getVerbo());
Holder.tvReferencia.setText(item.getReferencia());
Holder.tvEu.setText(item.getEu());
Holder.tvVoce.setText(item.getVoce());
Holder.tvNos.setText(item.getNos());
Holder.tvVoces.setText(item.getVoces());
System.out.println("BIND VIEW HOLDER: " + position);
}
#Override
public int getItemCount() {
return listItem.size();
}
public class Holder extends RecyclerView.ViewHolder{
TextView tvVerbo;
TextView tvReferencia;
TextView tvEu;
TextView tvVoce;
TextView tvNos;
TextView tvVoces;
public Holder(View itemView) {
super(itemView);
tvVerbo = itemView.findViewById(R.id.tvLista1);
tvReferencia = itemView.findViewById(R.id.tvLista2);
tvEu = itemView.findViewById(R.id.tvLista3);
tvVoce = itemView.findViewById(R.id.tvLista4);
tvNos = itemView.findViewById(R.id.tvLista5);
tvVoces = itemView.findViewById(R.id.tvLista6);
}
}
}
My activity Verbos2 when I call the Recycler:
public class Verbos2 extends AppCompatActivity {
RecyclerView recyclerViewItem;
RecyclerViewAdapter recyclerViewVerbos;
EditText etVerbos;
Button mostrar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbos2);
etVerbos = findViewById(R.id.etIngresar);
mostrar = findViewById(R.id.bnMostrar);
recyclerViewItem = findViewById(R.id.rvListItems);
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
recyclerViewItem.setLayoutManager(manager);
mostrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
List<Items> completeList = new ArrayList<>();
completeList.addAll(mostrarVerbos());
recyclerViewVerbos = new RecyclerViewAdapter((ArrayList<Items>) completeList);
recyclerViewItem.setAdapter(recyclerViewVerbos);
}
});
}
public List<Items> mostrarVerbos(){
BaseDeDatos admin = new BaseDeDatos(getApplicationContext(), "verbos.db", getApplicationContext(), 11);
SQLiteDatabase db = admin.getReadableDatabase();
String[] parametros = {etVerbos.getText().toString()};
Cursor cursor = db.rawQuery("SELECT * FROM verbos WHERE verbos =?", parametros);
List<Items> verbos= new ArrayList<>();
if(cursor.moveToFirst()){
do {
verbos.add(new Items(cursor.getString(1), " " + cursor.getString(2), " " + cursor.getString(3), " " + cursor.getString(4), " " + cursor.getString(5), " " + cursor.getString(6)));
}while (cursor.moveToNext());
}else{
Toast.makeText(getApplicationContext(), "El verbo no existe", Toast.LENGTH_LONG).show();
}
return verbos;
}
}
The list xml:
<?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"
xmlns:andoid="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvLista1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 2"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 3"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 4"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
EDITED 2
Ass you can see, it generated an horizontally scroll to each record. I need just one horizontally scroll that envolves all the records at the same time (3 records in this case)
Try to add horizontal scroll view inside of your item layout and set maxLines 1 into your item textView.
Try below code
<?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">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvLista1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo1"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 2"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 3"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 4"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvLista6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="algo 5"
android:maxLines="1"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I hope this can help You!
Thank You.
i don't The problem is originating from your recyclerview. You have to set multiline to true on the TextView inside the recyclerview's layout item so that the text can span multiple lines.

Change RecycleView item width?

I know there is probably a very simple solution out there, but for the life of me I can't seem to find it. How do I change the width of the RecycleView item? For some reason, it's displaying the first item full width of the parent... It's displaying a lot of white space in between each item.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_wrapper"
android:background="#color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cats"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
CategoryAdapter:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private ArrayList<Category> categories;
public CategoryAdapter(ArrayList<Category> categories) {
this.categories = categories;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cat, parent, false);
return new ViewHolder(v);
}
#Override public void onBindViewHolder(ViewHolder holder, int position) {
Category cat = this.categories.get(position);
holder.text.setText(cat.getText().toString());
}
#Override public int getItemCount() {
return categories.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.cat_title);
}
}
}
single_cat.xml
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Cat"
android:background="#drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="#+id/cat_title"
android:textSize="13sp"
android:fontFamily="#font/open_sans_bold"
android:textColor="#color/colorPrimary"/>
1)in your fragment do :
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, true));
2)for recyclerView single item .xml do :
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
3) and change RecyclerView to :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_wrapper"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:id="#+id/cats"/>
</LinearLayout>
i would change single cat to
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="#+id/cat_title"
android:textSize="13sp"
android:fontFamily="#font/open_sans_bold"
android:textColor="#color/colorPrimary"/>
Finally, after 2 days and looking all over the internet, I figured it out myself.
Make sure your Constraint Layout is wrap_content. This was the issue all along!

How to change layout height automatically with word wrap?

As you see, height sets normally when there's only one text string, but all crashes, when it's wrapped:
Here's my XML:
<?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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingEnd="15dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:theme="#style/checkBoxStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="7dp"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
/>
</LinearLayout>
Here's my adapter:
class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder> {
private Activity activity;
private ArrayList<Item> items;
private int inflater;
private OnItemCheckListener onItemClick;
AppsAdapter (Activity activity, ArrayList<Item> items, int inflater, OnItemCheckListener onItemCheckListener) {
this.activity = activity;
this.items = items;
this.inflater = inflater;
this.onItemClick = onItemCheckListener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView icon;
private TextView title, descr, version;
private CheckBox checkbox;
private ViewHolder (View view) {
super (view);
icon = (ImageView) view.findViewById (R.id.icon);
title = (TextView) view.findViewById (R.id.title);
descr = (TextView) view.findViewById (R.id.descr);
version = (TextView) view.findViewById (R.id.version);
checkbox = (CheckBox) view.findViewById (R.id.checkbox);
}
}
#Override
public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
return new ViewHolder (LayoutInflater.from (parent.getContext ()).inflate (inflater, parent, false));
}
#Override
public void onBindViewHolder (final ViewHolder holder, int position) {
Item item = items.get (position);
if (holder.icon != null)
holder.icon.setImageDrawable (item.icon);
holder.title.setText (item.getItem (0));
holder.descr.setText (item.getItem (1));
// Other actions...
}
}
So, is it really to do it? I don't want to use a standart Android Settings activity, because I want to support KitKat (4.4), but its default themes are awful(
Or perhaps it'll be a solution with TableLayout and so on?
Thanks in advance!
You using your parent layout height was using like this android:layout_height="wrap_content"
User your height android:layout_width="match_parent"
definitely you got o/p
Change the xml
<?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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
android:text="abababababababababbabababababababab"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abababababababababbababababababababsdhsjdhsadsahdkjsahdsadsadhsadhsauidhsauidhsauihsauihsaduisahduisahduisahduisahdusaidhsauidhsauidhsauiidhsauidhsauidhsauidhsauidhsaudhsauidhsadiusahduisahdusahu"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="7dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:theme="#style/checkBoxStyle" />
</LinearLayout>

How to get data from a particular CardView attached RecyclerView

I have used a CardView to display some data (data displayed by setting values to TextView ).This CarView is attached to RecyclerView. Data displayed is coming from Database and hence the CardView also increases in list depending on no of rows in Database. What I want is to get data from particular Card on onclick and I am not sure how to do that. and I also wonder that I have used same card to populate different data and the TextView also have the same Id on different Card. How do I achieve this.I want to Take Data onClick because i will use that to edit the previous Data by modifying it And i Just need the way to get Data onClick of CardView rest I can do.
My code is as:-
MedicationCard:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:elevation="6sp"
android:background="#color/colorPrimary"
card_view:cardUseCompatPadding="true"
android:id="#+id/cardMedview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:padding="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/NameSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name:- "
android:id="#+id/textView6"
android:textColor="#0866C4"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name of Med"
android:textStyle="bold"
android:id="#+id/nameOfUpmingMed"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView6" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/QuntitySection"
android:layout_below="#+id/NameSection"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:text="Quantity:- "
android:textColor="#0866C4"
android:id="#+id/textView5"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="crocin"
android:layout_marginLeft="3dp"
android:id="#+id/QuantOfMed"
android:textColor="#000"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:text="Reffil Amount:- "
android:id="#+id/textView7"
android:textColor="#0866C4"
android:layout_marginStart="18dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/QuantOfMed" />
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="222"
android:textColor="#000"
android:id="#+id/ReffilAmt"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView7" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expirationSection"
android:layout_below="#+id/QuntitySection">
<TextView
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Expiration Date:- "
android:textColor="#0866C4"
android:id="#+id/textView8"/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="XX/YY/ZZZZ"
android:textColor="#000"
android:id="#+id/ExpDate"
android:layout_marginLeft="3dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView8" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/DaysSection"
android:layout_below="#+id/expirationSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Days:- "
android:textColor="#0866C4"
android:id="#+id/textView11" />
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="MON,TUE,WED,THU,FRI,SAT,SUN"
android:id="#+id/NoOfDays"
android:textColor="#000"
android:layout_marginLeft="3dp"
android:layout_alignTop="#+id/textView11"
android:layout_toEndOf="#+id/textView11" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/TimeSection"
android:layout_below="#+id/DaysSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Time:- "
android:textColor="#0866C4"
android:id="#+id/textView9"
/>
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="08:00 AM"
android:textColor="#000"
android:layout_marginLeft="3dp"
android:id="#+id/timeMorning"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView9" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="01:00 PM"
android:textColor="#000"
android:layout_marginLeft="4dp"
android:id="#+id/timeAfternoon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="08:00 PM"
android:textColor="#000"
android:id="#+id/timeEvening"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="49dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/InstructionSection"
android:layout_below="#+id/TimeSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Instructions:- "
android:textColor="#0866C4"
android:textStyle="bold"
android:layout_marginTop="2dp"
android:id="#+id/textView10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="2dp"
android:textColor="#000"
android:text="Take Medicine before bed and after the food.and he shuld take agood sound sleep and shuld wake up early in morning"
android:id="#+id/SpecialInst"
android:layout_toEndOf="#+id/textView10" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
My fragment which is using RecyclerView to populate data on Card view.
PrescriptionFragment
public class PrescriptionFragment extends Fragment {
private Context context;
private RecyclerView recyclerView;
DatabaseAdaptor databaseAdaptor;
public PrescriptionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_prescription, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(linearLayoutManager);
context=getActivity();
databaseAdaptor = new DatabaseAdaptor(context);
initializeData();
initializeAdapter();
return view;
}
private void initializeData(){
databaseAdaptor.getDetailData();
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(databaseAdaptor.persons);
recyclerView.setAdapter(adapter);
}
#Override
public void onResume() {
super.onResume();
initializeData();
initializeAdapter();
}
}
RecyclerViewAdapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
public static class PersonViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
TextView Name,Qunat,refill,expDate,Days,moring,after,evening,inst;
public PersonViewHolder(View itemView) {
super(itemView);
cardView=(CardView)itemView.findViewById(R.id.cardMedview);
Name=(TextView)itemView.findViewById(R.id.nameOfUpmingMed);
Qunat=(TextView)itemView.findViewById(R.id.QuantOfMed);
refill=(TextView)itemView.findViewById(R.id.ReffilAmt);
expDate=(TextView)itemView.findViewById(R.id.ExpDate);
Days=(TextView)itemView.findViewById(R.id.NoOfDays);
moring=(TextView)itemView.findViewById(R.id.timeMorning);
after=(TextView)itemView.findViewById(R.id.timeAfternoon);
evening=(TextView)itemView.findViewById(R.id.timeEvening);
inst=(TextView)itemView.findViewById(R.id.SpecialInst);
}
}
List<Person> persons;
RVAdapter(List<Person> persons){
this.persons=persons;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.medication_card, parent, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.Name.setText(persons.get(i).name);
personViewHolder.Qunat.setText(persons.get(i).qunt);
personViewHolder.refill.setText(persons.get(i).refill);
personViewHolder.expDate.setText(persons.get(i).date);
personViewHolder.Days.setText(persons.get(i).days);
personViewHolder.moring.setText(persons.get(i).morning);
personViewHolder.after.setText(persons.get(i).after);
personViewHolder.evening.setText(persons.get(i).evening);
personViewHolder.inst.setText(persons.get(i).inst);
}
#Override
public int getItemCount() {
return persons.size();
}
}
Person Class
class Person{
String name,qunt,refill,inst,date,days,morning,after,evening;
Person(String name,String quant,String refill,String date,String days,String morn,String Aft,String evening,String inst){
this.name=name;
this.qunt=quant;
this.refill=refill;
this.date=date;
this.days=days;
this.morning=morn;
this.after=Aft;
this.evening=evening;
this.inst=inst;
}
}
And finally my Database which from which i am populating Data.
public List<Person> getDetailData(){
SQLiteDatabase db= helper.getWritableDatabase();
String[] columns={DatabaseHelper.medicationId,DatabaseHelper.medicName,DatabaseHelper.medicationQuant,DatabaseHelper.expirationDate,
DatabaseHelper.RiffilAmount,DatabaseHelper.specialInst,DatabaseHelper.alarmDays,DatabaseHelper.timeMorning,
DatabaseHelper.timeAfternoon,DatabaseHelper.timeEvening};
Cursor mcursor= db.query(DatabaseHelper.MEDICATION_DETAILS_TABLE, columns, null, null, null, null, null);
persons=new ArrayList<>();
StringBuffer buffer=new StringBuffer();
while (mcursor.moveToNext()){
int index1= mcursor.getColumnIndex(DatabaseHelper.medicationId);
int index2=mcursor.getColumnIndex(DatabaseHelper.medicName);
int index3=mcursor.getColumnIndex(DatabaseHelper.medicationQuant);
int index4=mcursor.getColumnIndex(DatabaseHelper.expirationDate);
int index5=mcursor.getColumnIndex(DatabaseHelper.RiffilAmount);
int index6=mcursor.getColumnIndex(DatabaseHelper.specialInst);
int index7=mcursor.getColumnIndex(DatabaseHelper.alarmDays);
int index8=mcursor.getColumnIndex(DatabaseHelper.timeMorning);
int index9=mcursor.getColumnIndex(DatabaseHelper.timeAfternoon);
int index10=mcursor.getColumnIndex(DatabaseHelper.timeEvening);
int medid=mcursor.getInt(index1);
String name=mcursor.getString(index2);
String quant=mcursor.getString(index3);
String expDate=mcursor.getString(index4);
String refill=mcursor.getString(index5);
String specialInst=mcursor.getString(index6);
String alarmdays=mcursor.getString(index7);
String timemorning=mcursor.getString(index8);
String timeafter=mcursor.getString(index9);
String timeevening= mcursor.getString(index10);
persons.add(new Person(name,quant,refill,expDate,alarmdays,timemorning,timeafter,timeevening,specialInst));
}
mcursor.close();
db.close();
return persons;
}
And my O/P is as:
This CardView is attached toRecyclerView. To fetch data from any CardView, you just have to get position of item, which is fetched from RecyclerView position.
Yo can follow this answer:
RecyclerView ItemClickListener
I'm a newbie to Android development but, found something interesting...
My screen looks as below...
So, I've a RecyclerView and a CardView combination, and each item would have a Toolbar attached with it...
So, if I implement the Toolbar.OnMenuItemClickListener, along with the definition of your ViewHolder class, it should resolve the issue.
Note: Even if you don't want to use the Toolbar, then implement View.OnClickListener along with the ViewHolder class and implement the OnClick event and I guess, it would do the trick.
Here is the rest of my ViewHolder class definition...
Hope It Helps!!!

Categories

Resources