How to create Recyclerview game time line events - android

I am developing a Game fans app. I want to create a layout given below
I have great experience in making custom recycelerviews with multiple LAYOUT_VIEW_TYPES.
My question is this the best way to create that recyclerview or I can do this in more easyway

Yes the best way to achieve that result is by creating a recyclerview with multiple layouts and I think that it's the only way if you are using xml.
But this could be a lot easier if you make it using jetpack compose no need for recycler view or adapter anymore with compose and you can make any custom layout that you want.

Phew... Finally I did it with Recyclerview custom layout items left and right.
Sharing my work.
here is my layout_item_time_line_left.xml
<?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"
tools:context=".livescore.TimeLineFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:background="#drawable/rounded_corner_time_line_item_bg"
android:padding="10dp"
app:layout_constraintEnd_toStartOf="#+id/txtPlayerName2"
app:layout_constraintTop_toTopOf="parent">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgPlayerImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/img_upload_profile"
app:border_color="#color/white"
app:civ_border_width="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<TextView
android:id="#+id/txtPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Neymar"
android:textColor="#66666a"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtEventName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Goal"
android:textColor="#color/txt_color_live_score"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName" />
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgEventImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:src="#drawable/ic_sports_footbal"
app:border_width="1dp"
app:civ_border_width="1dp"
app:layout_constraintStart_toEndOf="#+id/txtEventName"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName"
app:selector_stroke_color="#color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="#+id/imgEventImage2"
android:layout_width="12dp"
android:layout_height="12dp"
android:scaleType="fitXY"
android:src="#drawable/ic_online"
app:border_width="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<View
android:id="#+id/viewLineUpper"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toTopOf="#+id/imgEventImage2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/viewLineLower"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgEventImage2" />
<TextView
android:id="#+id/txtPlayerName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="33''"
android:textColor="#66666a"
android:textSize="8sp"
app:layout_constraintEnd_toStartOf="#+id/imgEventImage2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
here is my layout_item_time_line_right.xml
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:background="#drawable/rounded_corner_time_line_item_bg"
android:padding="10dp"
app:layout_constraintStart_toEndOf="#+id/txtPlayerName2"
app:layout_constraintTop_toTopOf="parent">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgPlayerImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/img_upload_profile"
app:border_color="#color/white"
app:civ_border_width="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<TextView
android:id="#+id/txtPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Neymar"
android:textColor="#66666a"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtEventName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:gravity="center_horizontal"
android:text="Goal"
android:textColor="#color/txt_color_live_score"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/imgPlayerImage"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName" />
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgEventImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginStart="4dp"
android:layout_marginTop="2dp"
android:src="#drawable/ic_sports_footbal"
app:civ_border_width="1dp"
app:layout_constraintStart_toEndOf="#+id/txtEventName"
app:layout_constraintTop_toBottomOf="#+id/txtPlayerName"
app:selector_stroke_color="#color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="#+id/imgEventImage2"
android:layout_width="12dp"
android:layout_height="12dp"
android:scaleType="fitXY"
android:src="#drawable/ic_online"
app:border_width="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selector_stroke_color="#color/white" />
<View
android:id="#+id/viewLineUpper"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toTopOf="#+id/imgEventImage2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/viewLineLower"
android:layout_width="1dp"
android:layout_height="20dp"
android:background="#41415A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imgEventImage2" />
<TextView
android:id="#+id/txtPlayerName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="33''"
android:textColor="#66666a"
android:textSize="8sp"
app:layout_constraintStart_toEndOf="#+id/imgEventImage2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
here is my fragment fragment_time_line.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:background="#020321"
tools:context=".livescore.TimeLineFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_border_live_score_tab_table"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/txtBallPossession"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:gravity="center_horizontal"
android:text="Ball possession"
android:textColor="#66666a"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/layout_possession_percentage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/txtBallPossession"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/txtLeftPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="70%"
android:textColor="#00ffac"
android:textSize="12sp"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/progressBarLeft"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:indeterminate="false"
android:indeterminateTint="#00ffac"
android:max="100"
android:progress="50"
android:rotation="180" />
<TextView
android:id="#+id/txtRightPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30%"
android:textColor="#ff0909"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_corner_card"
android:clipToOutline="true"
android:scaleType="fitXY"
android:src="#drawable/img_footbal_ground_green"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_possession_percentage" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_match_timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
and here is my JAVA code for fragment
/**
* A simple {#link Fragment} subclass.
* Use the {#link TimeLineFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class TimeLineFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public TimeLineFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment TimeLineFragment.
*/
// TODO: Rename and change types and number of parameters
public static TimeLineFragment newInstance(String param1, String param2) {
TimeLineFragment fragment = new TimeLineFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
private View mView;
private RecyclerView recyclerViewMatchTimeline;
private ArrayList<MatchTimeLine> items = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_time_line, container, false);
recyclerViewMatchTimeline = mView.findViewById(R.id.recycler_view_match_timeline);
LinearLayoutManager matchTimelineLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerViewMatchTimeline.setHasFixedSize(true);
recyclerViewMatchTimeline.setLayoutManager(matchTimelineLayoutManager);
recyclerViewMatchTimeline.setAdapter(new TimeLineMatchEventsRecyclerViewAdapter(items));
return mView;
}
}
and lastly my magic Adapter TimeLineMatchEventsRecyclerViewAdapter.java (copied from stackoverflow answer)
public class TimeLineMatchEventsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MatchTimeLine> mModelList;
public TimeLineMatchEventsRecyclerViewAdapter(List<MatchTimeLine> modelList) {
this.mModelList = modelList;
}
#Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
LayoutItemTimeLineLeftBinding bindingLeftView = LayoutItemTimeLineLeftBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolderLeft(bindingLeftView);
case 2:
LayoutItemTimeLineRightBinding bindingRightView = LayoutItemTimeLineRightBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolderRight(bindingRightView);
default:
LayoutItemTimeLineRightBinding bindingRightDefault = LayoutItemTimeLineRightBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolderRight(bindingRightDefault);
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, #SuppressLint("RecyclerView") int position) {
// final Team team = mModelList.get(position);
switch (holder.getItemViewType()) {
case 0:
ViewHolderLeft viewHolderLeft = (ViewHolderLeft) holder;
break;
case 2:
ViewHolderRight viewHolderRight = (ViewHolderRight) holder;
break;
}
}
#Override
public int getItemCount() {
// return mModelList == null ? 0 : mModelList.size();
return 20;
}
class ViewHolderLeft extends RecyclerView.ViewHolder {
LayoutItemTimeLineLeftBinding binding;
public ViewHolderLeft(LayoutItemTimeLineLeftBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
class ViewHolderRight extends RecyclerView.ViewHolder {
LayoutItemTimeLineRightBinding binding;
public ViewHolderRight(LayoutItemTimeLineRightBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
Here is the output I got

Related

Recyclerview expanding itself

I am trying to do same design in here. I have nested Recyclerview in my structure. I have BasketParentItem class and it keeps child products list. As you can see in here when i click the checkbox list expanding itself. This happens when i call notifyDataChangeSetChildAndParent() in listener. Any idea how to solve it ?
Edit: I solved problem. I removed divider on parent adapter now it's working well here
holder.rv_child_Basket_Parent.addItemDecoration(new DividerItemDecoration(mActivity.getApplicationContext(), DividerItemDecoration.VERTICAL));
holder.cb_Basket_CardView_Child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(list.get(position).isChecked()){
holder.cb_Basket_CardView_Child.setChecked(false);
list.get(position).setChecked(false);
}else{
holder.cb_Basket_CardView_Child.setChecked(true);
list.get(position).setChecked(true);
}
boolean allCheck = false;
for (BasketParentItem b : Basket.basketList) {
ArrayList<MyProduct> tempList = b.getChildItemList();
if (tempList.contains(list.get(position)) ) {
for (MyProduct mP : tempList) {
if(mP.isChecked()){
allCheck = true;
}
}
if(!allCheck){
//set parent uncheck
b.setChecked(false);
}
}
}
notifyDataChangeSetChildAndParent();
}
});
#Getter
#Setter
public class BasketParentItem {
ArrayList<MyProduct> childItemList = new ArrayList<>();
boolean checked=true;
public void addToList(MyProduct myProduct){
childItemList.add(myProduct);
}
public boolean removeFromList(MyProduct myProduct){ return childItemList.remove(myProduct); }
}
#Getter
#Setter
#NoArgsConstructor
public class MyProduct {
#NonNull
String id;
#NonNull
String name;
#NonNull
String description;
#NonNull
String imageURL;
#NonNull
String type;
#NonNull
ArrayList<String> seller;
int stock;
double price;
Boolean fastDelivery;
int amount;
String sellerName;
double sellerRate;
boolean checked=true;
//Returns selled Id
public String getSellerId() {
return seller.get(0);
}
}
public class Basket {
public static ArrayList<BasketParentItem> basketList = new ArrayList<>();
public static ArrayList<String> bestSellerStringList = new ArrayList<>();
public static ArrayList<MyProduct> bestSeller = new ArrayList<>();
}
public class BasketFragment extends Fragment {
//...
public void basketNotifyDataSetChanged(){
if(!Basket.basketList.isEmpty()){
ll_BasketFragment.setVisibility(View.GONE);
rv_parent_FragmentBasket.setVisibility(View.VISIBLE);
}else{
ll_BasketFragment.setVisibility(View.VISIBLE);
rv_parent_FragmentBasket.setVisibility(View.GONE);
}
if(parentAdapter != null){
parentAdapter.notifyDataSetChanged();
}
}
public void bestSellerNotifyDataSetChanged(){
if(adapter!=null){
adapter.notifyDataSetChanged();
}
}
}
private void notifyDataChangeSetChildAndParent(){
BasketFragment basketFragment = (BasketFragment) ((MainActivity) mActivity).findFragment(MainActivity.BASKET_FRAGMENT);
basketFragment.basketNotifyDataSetChanged();
basketFragment.bestSellerNotifyDataSetChanged();
}
Parent Row
<?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:layout_marginHorizontal="8dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="0dp"
android:background="#drawable/custom_background_3"
android:elevation="3dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/cb_Basket_CardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_Basket_Parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Satıcı"
android:textColor="#color/grey"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/green_ice"
android:gravity="center"
android:orientation="horizontal"
android:paddingVertical="2dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_package" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_shipping"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_child_Basket_Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"/>
</LinearLayout>
Child 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"
android:layout_marginVertical="14dp">
<CheckBox
android:id="#+id/cb_Basket_CardView_Child"
android:layout_width="14dp"
android:layout_height="15dp"
android:layout_marginRight="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.367" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/iv_Basket_CardView_Child"
android:layout_width="70dp"
android:layout_height="102dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/linearLayout"
app:layout_constraintStart_toEndOf="#+id/cb_Basket_CardView_Child"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="#style/roundedImageViewRounded"
app:srcCompat="#drawable/ic_home" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintTop_toTopOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/tv_productName_Basket_CardView_Child"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/iv_deleteProduct_Basket_CardView_Child"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="15dp"
app:srcCompat="#drawable/ic_trash" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:background="#drawable/custom_background_7"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="1.0">
<ImageView
android:id="#+id/iv_Decrease_CardView_Child"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="2dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_decrease_disabled" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="#+id/tv_ProductAmount_Basket_CardView_Child"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginVertical="2dp"
android:background="#drawable/custom_background_8"
android:gravity="center"
android:maxLength="4"
android:text="1"
android:textColor="#color/pumpkin"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/pb_CardView_Child"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_gravity="center"
android:indeterminateTint="#color/pumpkin"
android:visibility="invisible"/>
</FrameLayout>
<ImageView
android:id="#+id/iv_Increase_CardView_Child"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="2dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_increase" />
</LinearLayout>
<TextView
android:id="#+id/tv_FastDelivery_Basket_CardView_Child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout2"
app:layout_constraintEnd_toEndOf="#+id/linearLayout"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/tv_Price_Basket_CardView_Child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TL"
android:textColor="#color/pumpkin"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/linearLayout2"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lagging recycler view

I have made a recyclerView which is inside a fragment. It has an ImageView in each item and the image is loaded with Picasso from a url. But the problem is that the recyclerView lags when i scroll for the first time, but when the data is loaded, it scrolls normally. I tried to remove all the operations in the onBindViewHolder but the problem still persists. I then removed the complex part of the item layout but still no improvements. I am running on Snapdragon 845 so there is no issue with the phone's performance, I tried the same with other devices and it lags. I even tried with hardware acceleration, but it didn't help.
Here is my Adapter:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieHolder> {
private List<Movie> mMovies;
private Context mContext;
public MovieAdapter(Context mContext, List<Movie> mMovies) {
this.mContext = mContext;
this.mMovies = mMovies;
}
#NonNull
#Override
public MovieHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_movie_movie_layout, viewGroup, false);
return new MovieHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MovieHolder movieHolder, int i) {
Movie movie = mMovies.get(i);
if (movie.getTitle2().equals("")) {
movieHolder.mTextViewTitle1.setText(movie.getTitle1());
movieHolder.mTextViewTitle2.setVisibility(View.GONE);
} else {
movieHolder.mTextViewTitle1.setText(movie.getTitle1());
movieHolder.mTextViewTitle2.setVisibility(View.VISIBLE);
movieHolder.mTextViewTitle2.setText(movie.getTitle2());
}
movieHolder.mTextViewGenre.setText(movie.getGenre());
movieHolder.mRatingBar.setRating((float) 4.5);
String url = "https://image.tmdb.org/t/p/w342";
url += movie.getPoster_path();
Transformation transformation = new Rounded(8, ALL);
Picasso.get().load(url)
.placeholder(R.color.colorAccent)
.transform(transformation)
.resize(100, 150)
.centerCrop()
.into(movieHolder.mImageViewPoster);
}
#Override
public int getItemCount() {
return mMovies.size();
}
public class MovieHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mImageViewPoster;
private TextView mTextViewTitle1;
private TextView mTextViewTitle2;
private TextView mTextViewGenre;
private RatingBar mRatingBar;
public MovieHolder(#NonNull View itemView) {
super(itemView);
mImageViewPoster = itemView.findViewById(R.id.image_view_poster);
mTextViewTitle1 = itemView.findViewById(R.id.text_view_title_1);
mTextViewTitle2 = itemView.findViewById(R.id.text_view_title_2);
mTextViewGenre = itemView.findViewById(R.id.text_view_genre);
mRatingBar = itemView.findViewById(R.id.rating_bar);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Movie movie = mMovies.get(getAdapterPosition());
Intent intent = new Intent(mContext, MovieActivity.class);
intent.putExtra("EXTRA", movie);
mContext.startActivity(intent);
}
}
}
Here is my item layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="192dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.CardView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_marginStart="28dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="8dp"
android:elevation="0dp"
app:cardBackgroundColor="#color/colorAccent"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</android.support.v7.widget.CardView>
<ImageView
android:id="#+id/image_view_poster"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_marginStart="12dp"
android:layout_marginBottom="12dp"
android:adjustViewBounds="true"
android:elevation="16dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:elevation="8dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="8dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toEndOf="#+id/image_view_poster"
app:layout_constraintTop_toTopOf="#+id/imageView">
<TextView
android:id="#+id/text_view_title_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Skyscraper"
android:textSize="16dp" />
<TextView
android:id="#+id/text_view_title_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.3"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Skyscraper"
android:textSize="12dp" />
<Space
android:layout_width="0dp"
android:layout_height="8dp" />
<android.support.v7.widget.AppCompatRatingBar
android:id="#+id/rating_bar"
style="#style/Base.Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:outlineProvider="paddedBounds"
android:progressBackgroundTint="#FFFFFF"
android:progressTint="#FECE00"
android:stepSize="0.5" />
<Space
android:layout_width="0dp"
android:layout_height="8dp" />
<TextView
android:id="#+id/text_view_genre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="Action | Adventure"
android:textSize="12dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

NestedScrollView not scrolling in recyclerView generated data

I'm using a NestedScrollView in a CardView that's generated in a RecyclerView. I can't get the scrolling to work - there seems to be sporadic moments when I can scroll through the list in the top item but no other scrolls seem to work. Cheers!
This is the layout of the cardView that's generated in the RecyclerView:
<?xml version="1.0" encoding="utf-8"?
<android.support.constraint.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/checkout_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/merchantPurchase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="4dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/checkoutCard"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginStart="12dp" />
<android.support.v7.widget.CardView
android:id="#+id/checkoutCard"
android:layout_width="340dp"
android:layout_height="90dp"
android:layout_margin="5dp"
android:layout_marginBottom="11dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_goneMarginLeft="5dp"
app:layout_goneMarginRight="5dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingRight="5dp">
<TextView
android:id="#+id/textCentre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Test2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintLeft_toLeftOf="#+id/extraContainer"
app:layout_constraintRight_toRightOf="#+id/extraContainer" />
<TextView
android:id="#+id/textRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:text="Test3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintLeft_toLeftOf="#+id/otherContainer"
app:layout_constraintRight_toRightOf="#+id/otherContainer" />
<TextView
android:id="#+id/textLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Test1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintLeft_toLeftOf="#+id/priceContainer"
app:layout_constraintRight_toRightOf="#+id/priceContainer" />
<TextView
android:id="#+id/extraContainer"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/priceContainer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="#+id/priceContainer"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/itemTitle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="#+id/otherContainer"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/extraContainer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="#+id/itemTitle"
android:layout_width="71dp"
android:layout_height="39dp"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />
<LinearLayout
android:layout_width="63dp"
android:layout_height="53dp"
android:layout_marginLeft="0dp"
android:orientation="vertical"
app:layout_constraintLeft_toRightOf="#+id/otherContainer"
tools:layout_editor_absoluteY="19dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteX="258dp">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_gravity="fill_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView45"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView44"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView43"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
Then, my abridged code for MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkout_home);
cardPurchasesDataSet = new ArrayList<>();
for (int i = 0; i < productsForPurchase.length; i++) {
cardPurchasesDataSet.add(productsForPurchase[i]);
}
card_totalPrice = new ArrayList<>();
for (int i = 0; i < totalPriceData.length; i++) {
card_totalPrice.add(totalPriceData[i]);
}
card_extras = new ArrayList<>();
for (int i = 0; i < extrasData.length; i++) {
card_extras.add(extrasData[i]);
}
card_more = new ArrayList<>();
for (int i = 0; i < moreData.length; i++) {
card_more.add(moreData[i]);
}
merchants = new ArrayList<>();
for (int i = 0; i < merchantsData.length; i++) {
merchants.add(merchantsData[i]);
}
card_recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
card_layoutManger = new LinearLayoutManager(this);
card_recyclerView.setLayoutManager(card_layoutManger);
card_adapter = new checkout_card_Adapter(cardPurchasesDataSet, card_totalPrice, card_extras, card_more, merchants);
card_recyclerView.setAdapter(card_adapter);
}
UPDATE 01: Added mainActivity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="368dp"
android:layout_height="551dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:overScrollMode="never"
android:layout_marginBottom="8dp" />
Adapter
public class Checkout_Card_Adapter extends
RecyclerView.Adapter<Checkout_Card_Adapter.ViewHolder>{
private ArrayList<String> titleOfPurchase;
private ArrayList<String> priceOfPurchase;
private ArrayList<String> extrasOfPurchase;
private ArrayList<String> moreOfPurchase;
private ArrayList<String> merchantOfPurchase;
public Checkout_Card_Adapter(ArrayList<String> titleOfPurchase, ArrayList<String> priceOfPurchase, ArrayList<String> extrasOfPurchase, ArrayList<String> moreOfPurchase, ArrayList<String> merchantOfPurchase) {
this.titleOfPurchase = titleOfPurchase;
this.priceOfPurchase = priceOfPurchase;
this.extrasOfPurchase = extrasOfPurchase;
this.moreOfPurchase = moreOfPurchase;
this.merchantOfPurchase = merchantOfPurchase;
}
public interface VenueAdapterInterface {
void onVenueButtonClick(int position);
}
#Override
public Checkout_Card_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.checkout_scrollable_card, viewGroup, false);
Checkout_Card_Adapter.ViewHolder viewHolder = new Checkout_Card_Adapter.ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(Checkout_Card_Adapter.ViewHolder viewHolder, final int position) {
viewHolder.itemTitle.setText(titleOfPurchase.get(position));
viewHolder.itemPrice.setText(priceOfPurchase.get(position));
viewHolder.itemExtras.setText(extrasOfPurchase.get(position));
viewHolder.itemMore.setText(moreOfPurchase.get(position));
viewHolder.merchant.setText(merchantOfPurchase.get(position));
}
#Override
public int getItemCount() {
return titleOfPurchase.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageButton itemImage;
public TextView itemTitle;
public TextView itemPrice;
public TextView itemExtras;
public TextView itemMore;
public TextView merchant;
public ViewHolder(final View itemView) {
super(itemView);
itemTitle = (TextView) itemView.findViewById(R.id.itemTitle);
itemPrice = (TextView) itemView.findViewById(R.id.priceContainer);
itemExtras = (TextView) itemView.findViewById(R.id.extraContainer);
itemMore = (TextView) itemView.findViewById(R.id.otherContainer);
merchant = (TextView) itemView.findViewById(R.id.merchantPurchase);
}
}
}
As per your screen shot how do you expect it to scroll the inner view while it showing all the views? it will only scroll on small devices where your all textview won't be able to display. Can you please post a proper screenshot of this view from app.

Constraint layout with child constraint layouts not expanding horizontally

I'm trying to build a rectangular view (spanning the width of the screen) which is split horizontally into three blocks, and I am doing so using a constraint layout. The left and right blocks have a specified width, but the centre block should expand to fit the remaining space. Because I require that the left-most block have a different background colour to the parent, I have grouped its children into another constraint layout. I have given the layout XML below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#10000000"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:id="#+id/abc"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#50000000"
android:layout_marginEnd="8dp"
android:padding="2dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/jkl"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/def"
android:gravity="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ghi"
android:gravity="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/def" />
</android.support.constraint.ConstraintLayout>
<!-- This should expand to fit the remainder of the screen width -->
<android.support.constraint.ConstraintLayout
android:id="#+id/jkl"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="#+id/stu"
app:layout_constraintStart_toEndOf="#+id/abc">
<TextView
android:id="#+id/mno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:textColor="#a0000000"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pqr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:textColor="#a0000000"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mno" />
</android.support.constraint.ConstraintLayout>
<ImageButton
android:id="#+id/stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="#drawable/x"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/jkl"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Unfortunately, the middle block does not expand to fit the remaining width of the screen. Does anyone know how to achieve this? Am I doing anything fundamentally wrong here?
Edit:
Based on Ben P.'s response it may be that the XML above is fine, but the layouts in which this layout is contained are at fault. I have therefore provided them below.
This layout XML is the parent of the XML above:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Parent">
</android.support.v7.widget.RecyclerView>
This layout XML is the parent of the RecyclerView XML above:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include layout="#layout/bottom_navigation" />
</android.support.constraint.ConstraintLayout>
Edit 2:
As requested I've provided the complete code base:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private FragmentManager mFragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Foo());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Foo.java:
public class Foo extends Fragment {
private String[] mDataset;
private OnFragmentInteractionListener mListener;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public Foo() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment Foo.
*/
// TODO: Rename and change types and number of parameters
public static Foo newInstance(String param1, String param2) {
Foo fragment = new Foo();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
initDataset();
}
private void initDataset() {
mDataset = new String[1];
mDataset[0] = "Hello World";
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_foo, container, false);
mRecyclerView = view.findViewById(R.id.foo_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new FooAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
FooAdapter.java:
public class FooAdapter extends RecyclerView.Adapter<FooAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView mFoofoo;
private TextView mBarbar;
public ViewHolder(View itemView) {
super(itemView);
mFoofoo = itemView.findViewById(R.id.foofoo);
mBarbar = itemView.findViewById(R.id.barbar);
}
public TextView getFoofoo() {
return mFoo;
}
public TextView getBarbar() {
return mBar;
}
}
public FooAdapter(String[] mDataset) {
this.mDataset = mDataset;
}
#Override
public FooAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bar, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(FooAdapter.ViewHolder holder, int position) {
String text = mDataset[position];
holder.getBarbar().setText(text);
holder.getFoofoo().setText(text);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eaff73"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
fragment.xml:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/foo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8df78b"
tools:context="com.example.Foo">
</android.support.v7.widget.RecyclerView>
bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="104dp"
android:layout_marginTop="4dp"
android:background="#10000000"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/backg"
android:background="#50000000"
android:layout_width="104dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/foofoo"
android:gravity="start"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/action"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:src="#drawable/pic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/barbar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/barbar"
android:autoSizeMaxTextSize="24sp"
android:autoSizeMinTextSize="14dp"
android:autoSizeStepGranularity="2dp"
android:autoSizeTextType="uniform"
android:background="#ef0505"
android:gravity="top|start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:textColor="#a0000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/action"
app:layout_constraintStart_toEndOf="#id/backg"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Any help is greatly appreciated!
I do not know why this works, but it does.
In bar.xml, replace your outer ConstraintLayout's height with wrap_content (instead of 104dp).
Also in bar.xml, replace your ImageView's height with 104dp (instead of 0dp).
This has the net effect of keeping your layout's design the same... it just has the ConstraintLayout's height defined by the ImageView's height, instead of the other way around.
If I had to guess at why this makes a difference, I'd assume that there's some quirk in the way LinearLayoutManager measures its children, and that when it sees a fixed height it ignores the match_parent width. But that's wild speculation.
Update
Here is a flat ConstraintLayout that does what I believe you want:
<android.support.constraint.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="100dp">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#33000000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="#+id/image"
app:layout_constraintRight_toRightOf="#+id/image"
tools:text="line 1"/>
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toBottomOf="#+id/text1"
app:layout_constraintLeft_toLeftOf="#+id/image"
app:layout_constraintRight_toRightOf="#+id/image"
tools:text="line 2"/>
<TextView
android:id="#+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/button"
tools:text="line 3"/>
<TextView
android:id="#+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintTop_toBottomOf="#+id/text3"
app:layout_constraintLeft_toRightOf="#+id/image"
app:layout_constraintRight_toLeftOf="#+id/button"
tools:text="line 4"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="button"/>
</android.support.constraint.ConstraintLayout>
And what it looks like:
Original
This does not strictly answer the question (since as far as I can tell, there is no problem with the posted layout), but...
I see no reason to implement this UI using ConstraintLayout. The benefit of ConstraintLayout is that it allows you to avoid nesting ViewGroups inside your top-level View. But as your ConstraintLayout is written, you're still grouping your TextViews inside a mid-level View.
If you are not taking advantage of ConstraintLayout's ability to do organization without nesting, I think it is a better idea to use a simpler top-level view. Here is your UI reimplemented with LinearLayouts:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#10000000"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/abc"
android:layout_width="100dp"
android:layout_height="match_parent"
android:padding="2dp"
android:background="#50000000"
android:orientation="vertical">
<TextView
android:id="#+id/def"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="18sp"
tools:text="line 1"/>
<TextView
android:id="#+id/ghi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="14sp"
tools:text="line 2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/jkl"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/mno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a0000000"
android:textSize="14sp"
android:textStyle="bold"
tools:text="line 1"/>
<TextView
android:id="#+id/pqr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a0000000"
android:textSize="14sp"
tools:text="line 2"/>
</LinearLayout>
<ImageButton
android:id="#+id/stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:src="#drawable/plus"/>
</LinearLayout>

Categories

Resources