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.
Related
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>
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
I am working with a Webservice from which I am fetching the images using retrofit.
I am showing those images in imageView and used a recyclerview to load multiple images.
Here are my layout files:
<?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/planscontainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/subscplans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/backplans"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="50dp"
android:background="#drawable/leftarrow" />
<ImageView
android:id="#+id/appiconhere"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="400dp"
android:src="#drawable/moremovelogo" />
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/planname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="319dp"
android:text="Plan Id:"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/subscplans" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="324dp"
android:layout_marginTop="41dp"
android:layout_marginBottom="28dp"
android:text="1"
app:layout_constraintBottom_toTopOf="#+id/textView11"
app:layout_constraintStart_toEndOf="#+id/planname"
app:layout_constraintTop_toBottomOf="#+id/subscplans"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="28dp"
android:text="Plan Name:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/planname" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="20dp"
android:text="Price:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/duration" />
<TextView
android:id="#+id/noofmoviesdownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="191dp"
android:text="No. of movies can be download:"
app:layout_constraintEnd_toStartOf="#+id/textView13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/price" />
<TextView
android:id="#+id/canbeviewed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="213dp"
android:text="No. of movies can be viewed"
app:layout_constraintEnd_toStartOf="#+id/textView14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/noofmoviesdownload" />
<TextView
android:id="#+id/samemovieviewed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="143dp"
android:text="No. of times same movie can be viewed"
app:layout_constraintEnd_toStartOf="#+id/textView15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/canbeviewed" />
<TextView
android:id="#+id/timesdownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="126dp"
android:text="No. of times same movie can be download"
app:layout_constraintEnd_toStartOf="#+id/textView16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/samemovieviewed" />
<TextView
android:id="#+id/rentpe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="119dp"
android:text="No. of times same movie can be get on rent"
app:layout_constraintEnd_toStartOf="#+id/textView17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timesdownload" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="310dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/duration"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="362dp"
android:layout_marginTop="19dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/price"
app:layout_constraintTop_toBottomOf="#+id/textView11" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="194dp"
android:layout_marginTop="19dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/noofmoviesdownload"
app:layout_constraintTop_toBottomOf="#+id/textView13" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="216dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="480dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/canbeviewed"
app:layout_constraintTop_toBottomOf="#+id/textView14" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginTop="25dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/samemovieviewed"
app:layout_constraintTop_toBottomOf="#+id/textView15" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/timesdownload"
app:layout_constraintTop_toBottomOf="#+id/textView16" />
<Button
android:id="#+id/buyplan"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_marginStart="227dp"
android:layout_marginTop="106dp"
android:layout_marginEnd="59dp"
android:background="#color/tw__composer_blue"
android:text="Buy Plan"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView13"
app:layout_constraintTop_toBottomOf="#+id/subscplans" />
<Button
android:id="#+id/showplans"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_marginStart="224dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="62dp"
android:layout_marginBottom="229dp"
android:background="#d156b8"
android:text="Show plans"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView15"
app:layout_constraintTop_toBottomOf="#+id/buyplan"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="121dp"
android:layout_marginTop="19dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="#+id/rentpe"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<RelativeLayout
android:id="#+id/linearlayoutimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView19">
<android.support.v7.widget.RecyclerView
android:id="#+id/myrecyclerview"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<Button
android:id="#+id/cancelplan"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:layout_marginStart="221dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="60dp"
android:background="#color/tw__composer_deep_gray"
android:text="cancel plan"
android:textColor="#color/white"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView17"
app:layout_constraintTop_toBottomOf="#+id/showplans" />
</android.support.constraint.ConstraintLayout>
and for the items I have:
<?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="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imagethumb"
android:layout_width="180dp"
android:layout_height="200dp"
android:scaleType="fitEnd"/>
<TextView
android:id="#+id/nothing"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And my Adapter class looks like:
this is my adapter class name ImageAdapter.java
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.SubsImageHolder> {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.newlogo)
.showImageForEmptyUri(R.drawable.newlogo)
.showImageOnFail(R.drawable.newlogo)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
private Context context;
private List<ThumbPojo> imageList;
public ImageAdapter(Context context, List<ThumbPojo> items) {
this.context = context;
this.imageList = items;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public List<ThumbPojo> getImageList() {
return imageList;
}
public void setImageList(List<ThumbPojo> imageList) {
this.imageList = imageList;
}
#NonNull
#Override
public SubsImageHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.subscriptionimages, viewGroup, false);
return new SubsImageHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SubsImageHolder subsImageHolder, int i) {
ThumbPojo item = imageList.get(i);
subsImageHolder.textView.setText(item.getMovieName());
subsImageHolder.imageView.setImageDrawable(null);
subsImageHolder.imageView.layout(0,0,0,0);
ImageLoader.getInstance().displayImage(item.getThumbnail(),subsImageHolder.imageView,options);
// Glide.with(context)
// .load(item.getThumbnail())
// .into(subsImageHolder.imageView);
}
#Override
public int getItemCount() {
return imageList.size();
}
class SubsImageHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public SubsImageHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imagethumb);
textView = itemView.findViewById(R.id.nothing);
}
}
}
And my Main Activity where I am setting my adpater is :
SubscriptionPlans.java:
public class SubscriptionPlans extends AppCompatActivity {
TextView planid, planname, planprice, noofmoviedownload, noofmovieviewed, sameviewed, samedownload, rentit, nomovi;
Intent intent = getIntent();
ImageView imageView3, imageView4;
String one, two, three, four, five, six, seven, eight;
List<ThumbPojo> list;
Button button;
Button show, buy, cancel;
int position;
String allvalues;
RelativeLayout linearLayout;
String id;
int userid, usertype;
int po;
String ress;
public String subs;
List<CheckActiveOrNot> list1;
RecyclerView recyclerView;
ImageAdapter imageAdapter;
List<ThumbPojo> imageList;
List<String> images = new ArrayList<>();
AVLoadingIndicatorView progressBar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.premiumsubscriptionplans);
// progressBar=findViewById(R.id.progressBar);
// showProgressDialog();
* recyclerView = findViewById(R.id.myrecyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));*
userid = SharedPrefsHelper.getUserId(SharedPrefsHelper.USER_ID, 0);
usertype = SharedPrefsHelper.getUserType(SharedPrefsHelper.USER_TYPE, 0);
linearLayout = findViewById(R.id.linearlayoutimage);
// imageView4 = findViewById(R.id.imageView4);
planid = findViewById(R.id.textView3);
planname = findViewById(R.id.textView11);
planprice = findViewById(R.id.textView13);
noofmoviedownload = findViewById(R.id.textView14);
noofmovieviewed = findViewById(R.id.textView15);
sameviewed = findViewById(R.id.textView16);
samedownload = findViewById(R.id.textView17);
rentit = findViewById(R.id.textView19);
TextView notshow = findViewById(R.id.planname);
notshow.setVisibility(View.GONE);
planid.setVisibility(View.GONE);
button = findViewById(R.id.backplans);
show = findViewById(R.id.showplans);
buy = findViewById(R.id.buyplan);
cancel = findViewById(R.id.cancelplan);
id = getIntent().getStringExtra("planid");
imageList = new ArrayList<>();
Call<List<ThumbPojo>> caller = ApiClient.getInstance().getApiService().getThumbimages(id);
caller.enqueue(new Callback<List<ThumbPojo>>() {
#Override
public void onResponse(Call<List<ThumbPojo>> call, Response<List<ThumbPojo>> response) {
list =response.body();
// JSONArray jsonArray=new JSONArray(list);
// for(int i=0;i<jsonArray.length();i++)
// {
// try {
// JSONObject jsonObject=jsonArray.getJSONObject(i);
// String name=jsonObject.getString("movieName");
// String imageurl=jsonObject.getString("thumbnail");
//
// ThumbPojo thumbPojo=new ThumbPojo(name,imageurl);
// imageList.add(thumbPojo);
// } catch (JSONException e) {
// e.printStackTrace();
// }
//
// }
*imageAdapter=new ImageAdapter(SubscriptionPlans.this,list);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(imageAdapter);
// dismissProgressDialog();*
// if (list.get(position).getThumbnail() != null) {
// ImageLoader.getInstance().displayImage(list.get(position).getThumbnail(), imageView3, options);
// // ImageLoader.getInstance().displayImage(list.get(position).getThumbnail(),imageView4,options);
// } else {
// nomovi.setText("No movies under this plan");
// }
}
#Override
public void onFailure(Call<List<ThumbPojo>> call, Throwable t) {
t.getMessage();
}
});
for image loading purposes I am using universal image loader.
Now coming to the problem, when a single image is coming from the server then it is loaded according to the width and height mentioned in the imageview .but when two or more are coming the images' sizes are different.
I have tried almost all the solution from the internet but nothing works fine.
I have also set the layout to (0,0,0,0) but that also not able to fix this.
Images are looking like this I don't know why?
Change scaleType of ImageView in items to be center_crop, this will make the image of defined size (dps) and will crop the remaining portion off, making all the images of same size.
Like this:
<?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="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imagethumb"
android:layout_width="180dp"
android:layout_height="200dp"
android:scaleType="center_crop"/>
<TextView
android:id="#+id/nothing"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Try changing scaleType in Imageview, whichever suits best.
<ImageView
android:id="#+id/imagethumb"
android:layout_width="180dp"
android:layout_height="200dp"
android:scaleType="center_inside"/>
or Try staggeredGridLayout.
I am planing a little app with recyclerviews in it.
Sometimes I have to load only 4 items to it and sometimes over 200.
Now I don't get it, how to center the custom recyclerviews items!
I have a custom recyclerview (grid) and I want to have 4 (2x2) items in it beeing centered.
What I have:
What I want:
So where is my mistake in following code?
fragment_single_pokemon.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="#+id/cLayTypOne"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#drawable/customborder_typ"
app:layout_constraintBottom_toTopOf="#+id/cLayTypTwo"
app:layout_constraintEnd_toStartOf="#+id/civSprite"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/txtTypOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="8sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<View
android:id="#+id/vLine2"
android:layout_width="50dp"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="#+id/civSprite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/civSprite"
app:layout_constraintTop_toTopOf="#+id/civSprite">
</View>
<TextView
android:id="#+id/txtPokemonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Pokéball"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/civSprite"
android:layout_width="148dp"
android:layout_height="148dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:src="#mipmap/ic_launcher"
app:civ_border_width="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtPokemonName" />
<View
android:id="#+id/vLine"
android:layout_width="50dp"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="#+id/civSprite"
app:layout_constraintEnd_toStartOf="#+id/civSprite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/civSprite">
</View>
<ImageView
android:id="#+id/imgIsRaidBoss"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="#+id/vLine2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/civSprite"
app:srcCompat="#drawable/raid_sprite" />
<ImageView
android:id="#+id/imgShinyExists"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="#+id/imgIsRaidBoss"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/civSprite"
app:srcCompat="#drawable/ic_shiny" />
<android.support.constraint.ConstraintLayout
android:id="#+id/cLayTypTwo"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#drawable/customborder_typ"
app:layout_constraintBottom_toTopOf="#+id/vLine"
app:layout_constraintEnd_toStartOf="#+id/civSprite"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/txtTypTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="8sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/btnPokeDexEnty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:background="#drawable/boarder_shape"
android:text="Pokédex Eintrag"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/civSprite" />
<TextView
android:id="#+id/txtPokeDexEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textAlignment="center"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="#+id/btnPokeDexEnty"
app:layout_constraintStart_toStartOf="#+id/btnPokeDexEnty"
app:layout_constraintTop_toBottomOf="#+id/btnPokeDexEnty" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rViewPokeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:foregroundGravity="center_vertical"
android:orientation="vertical"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtPokeDexEntry" />
</android.support.constraint.ConstraintLayout>
gridview_pokedex_pokeinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rLayMainContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtPokeInfoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="#+id/txtPokeInfoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPokeInfoTitle"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
recyclerview code
RecyclerView recyclerViewPokeInfo = (RecyclerView)rLayout_mainFragment.findViewById(R.id.rViewPokeInfo);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),2);
recyclerViewPokeInfo.setLayoutManager(layoutManager);
CustomRecyclerAdapterPokeInfoGrid adapter = new CustomRecyclerAdapterPokeInfoGrid(getActivity(), db.getPokeInfo(intPokemonID));
recyclerViewPokeInfo.setAdapter(adapter);
recyclerview adapter
package com.spicysoftware.goexpert;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomRecyclerAdapterPokeInfoGrid extends RecyclerView.Adapter<CustomRecyclerAdapterPokeInfoGrid.ViewHolder> {
private final ArrayList<String> strPokeInfo;
private Context context;
MySQLiteHelper db;
int locationSpriteNo;
String strCatchRate = "";
String strEscapeRate = "";
String strInEggs = "";
String strKmAsFriend = "";
public CustomRecyclerAdapterPokeInfoGrid(Context context, ArrayList<String> strPokeInfo) {
this.strPokeInfo = strPokeInfo;
this.context = context;
}
#Override
public CustomRecyclerAdapterPokeInfoGrid.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.gridview_pokedex_pokeinfo, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final CustomRecyclerAdapterPokeInfoGrid.ViewHolder viewHolder, final int i) {
viewHolder.setClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
}
});
// load image
try {
db = new MySQLiteHelper(context);
String currentString = strPokeInfo.get(i);
if(i == 0){
viewHolder.txtPokeInfoTitle.setText("Fangchance");
}else if(i == 1){
viewHolder.txtPokeInfoTitle.setText("Fluchtchance");
}else if(i == 2){
viewHolder.txtPokeInfoTitle.setText("In Eiern");
}else if(i == 3){
viewHolder.txtPokeInfoTitle.setText("Distanz als Kumpel");
}
viewHolder.txtPokeInfoText.setText(currentString);
}
catch(Exception ex) {
Log.v("Error: ", "-> "+ex);
}
}
#Override
public int getItemCount() {
return strPokeInfo.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
private TextView txtPokeInfoTitle,txtPokeInfoText;
private ItemClickListener clickListener;
private ConstraintLayout cLayoutSingleItem;
public ViewHolder(View view) {
super(view);
txtPokeInfoTitle = (TextView)view.findViewById(R.id.txtPokeInfoTitle);
txtPokeInfoText = (TextView)view.findViewById(R.id.txtPokeInfoText);
cLayoutSingleItem = (ConstraintLayout)view.findViewById(R.id.cLayoutSingleItem);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
#Override
public void onClick(View view) {
clickListener.onClick(view, getPosition(), false);
}
#Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
}
Set the list item layout to match_parent like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rLayMainContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtPokeInfoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="#+id/txtPokeInfoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPokeInfoTitle"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
Try setting layout_gravity in your textViews instead of the relativeLayout.
I have a grid layout with 4 cardviews and I have data in firebase database to be displayed in the cards. The data retrived from Firebase is stored in a class. The thing is I'cant use getter functions and display the data in setText. Whenver I do the app crashes. It is a fragment.
Here is the fragment :
public class HomeFragment extends Fragment {
usageData fetcheduserdata;
private DatabaseReference uidDatabase;
private DatabaseReference rootDatabase;
View v;
FirebaseAuth mAuth;
FirebaseUser mUser;
RecyclerView recyclerView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.v = view;
getActivity().setTitle("Home");
mUser = mAuth.getInstance().getCurrentUser();
TextView userPoints = (TextView) view.findViewById(R.id.user_points);
TextView userAdrequests = (TextView) view.findViewById(R.id.user_adrequests);
TextView userClicks = (TextView) view.findViewById(R.id.user_adclick);
TextView userRedeemthreshold = (TextView) view.findViewById(R.id.user_rthreshold);
final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
rootDatabase = FirebaseDatabase.getInstance().getReference();
uidDatabase = rootDatabase.child("Data").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Integer up = dataSnapshot.child("userPoints").getValue(Integer.class);
Integer ua = dataSnapshot.child("userAdrequests").getValue(Integer.class);
Integer uc = dataSnapshot.child("userClicks").getValue(Integer.class);
Integer ur = dataSnapshot.child("userRedeemthreshold").getValue(Integer.class);
fetcheduserdata = new usageData(uid,up,ua,uc,ur);
Toast.makeText(getActivity(), "Fetched UsageData From Firebase", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), "UsageData Error", Toast.LENGTH_SHORT).show();
}
};
uidDatabase.addValueEventListener(valueEventListener);
userPoints.setText(String.valueOf(fetcheduserdata.getUserPoints()));
userAdrequests.setText(fetcheduserdata.getUserAdrequests());
userClicks.setText(fetcheduserdata.getUserClicks());
userRedeemthreshold.setText(fetcheduserdata.getUserPoints());
}
}
XML File :
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Your Performance"
android:textSize="24sp"
android:textStyle="italic" />
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="2">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<TextView
android:id="#+id/user_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="Points"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="2"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<android.support.constraint.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<TextView
android:id="#+id/user_adrequests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center_horizontal"
android:text="Ad Requests"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="2"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<android.support.constraint.Guideline
android:id="#+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<TextView
android:id="#+id/user_adclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/guideline5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center_horizontal"
android:text="Ad Clicks"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="2"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="2"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<android.support.constraint.Guideline
android:id="#+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<TextView
android:id="#+id/user_rthreshold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:text="0%"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center_horizontal"
android:text="Redeem Threshold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</GridLayout>
Here is the class UsageData :
package com.balaji.earnyourself;
public class usageData {
String userId;
Integer userPoints,userAdrequests,userClicks,userRedeemthreshold;
public usageData(String userId, Integer userPoints, Integer userAdrequests, Integer userClicks, Integer userRedeemthreshold) {
this.userId = userId;
this.userPoints = userPoints;
this.userAdrequests = userAdrequests;
this.userClicks = userClicks;
this.userRedeemthreshold = userRedeemthreshold;
}
public usageData(String userId) {
this.userId = userId;
this.userPoints = 0;
this.userAdrequests = 0;
this.userClicks = 0;
this.userRedeemthreshold = 0;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public int getUserPoints() {
return userPoints;
}
public void setUserPoints(int userPoints) {
this.userPoints = userPoints;
}
public int getUserAdrequests() {
return userAdrequests;
}
public void setUserAdrequests(int userAdrequests) {
this.userAdrequests = userAdrequests;
}
public int getUserClicks() {
return userClicks;
}
public void setUserClicks(int userClicks) {
this.userClicks = userClicks;
}
public int getUserRedeemthreshold() {
return userRedeemthreshold;
}
public void setUserRedeemthreshold(int userRedeemthreshold) {
this.userRedeemthreshold = userRedeemthreshold;
}
}
How to get through this ? I've tried the ways that I know but I couldn't get it to work.
Firebase APIs are asynchronous, meaning that onDataChange() method returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because that method returns immediately, the value of your fetcheduserdata variable you're trying to use it outside the onDataChange() method, will not have been assigned from the callback yet.
Basically, you're trying to use a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.
A quick solve for this problem would be to move the following lines inside the onDataChange() method right after you toast that message:
userPoints.setText(String.valueOf(fetcheduserdata.getUserPoints()));
userAdrequests.setText(fetcheduserdata.getUserAdrequests());
userClicks.setText(fetcheduserdata.getUserClicks());
userRedeemthreshold.setText(fetcheduserdata.getUserPoints());
Otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.
Note that there is no need to create a new object of usageData in order to get the data back, you can simply use:
userPoints.setText(String.valueOf(up));