Actually I m new to android development and i m trying to implement my product images like a carousel with a library called "why not" using glide with recyclerview. but I m not able to see the photos like this. I am not getting how to implement a carousel like this, What i m doing wrong please help me . Please guide me someone please
I am trying to make it like this
MY Design xml
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
app:cardCornerRadius="8dp"
app:cardElevation="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<org.imaginativeworld.whynotimagecarousel.ImageCarousel
android:id="#+id/carousel"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop">
</org.imaginativeworld.whynotimagecarousel.ImageCarousel>
<TextView
android:id="#+id/roomName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Room with kitchen"
android:textColor="#232222"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/roomRent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="Rent 4500/- month"
android:textColor="#232222"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Model Class
public class Rooms {
public Rooms(String carousel, String title, int rent) {
this.carousel = carousel;
this.title = title;
this.rent = rent;
}
private String carousel,title;
private int rent;
public String getCarousel() {
return carousel;
}
public void setCarousel(String carousel) {
this.carousel = carousel;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
}
My Adapter class
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.RoomsViewHolder>{
Context context;
ArrayList<Rooms> rooms;
public RoomsAdapter(Context context, ArrayList<Rooms> rooms) {
this.context = context;
this.rooms = rooms;
}
#NonNull
#Override
public RoomsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RoomsViewHolder(LayoutInflater.from(context).inflate(R.layout.room_design_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull RoomsViewHolder holder, int position) {
Rooms room = rooms.get(position);
Glide.with(context)
.load(room.getCarousel())
.into(holder.carousel);
holder.roomtitle.setText(room.getTitle());
holder.roomrent.setText(String.valueOf(room.getRent()));
}
#Override
public int getItemCount() {
return rooms.size();
}
public class RoomsViewHolder extends RecyclerView.ViewHolder{
ImageView carousel;
TextView roomtitle, roomrent;
public RoomsViewHolder(#NonNull View itemView) {
super(itemView);
carousel = itemView.findViewById(R.id.carousel);
roomtitle = itemView.findViewById(R.id.roomName);
roomrent = itemView.findViewById(R.id.roomRent);
}
}
}
Here is Sample Code with carousel View by using WHY NOT IMAGE CAROUSEL library. Check this code
Related
I need to create a chat view like below image. if there are members more than 3, need to show number. But images need to retrieved from URL. i have done a research but can't find any example. here i have set one Image in a imageView.Can anyone help me?
ChatAdapter.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/chat_list_border"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/view_background"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:gravity="end|center"
android:text="#string/delete"
android:textColor="#color/white"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/view_foreground"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10sp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/chat_image"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_gravity="center"
android:layout_weight="0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/chat_name"
style="#style/defaultTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/abhaya_libre_extra_bold"
android:textColor="#color/defaultTextColor"
android:textSize="16sp" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/chat_message"
style="#style/defaultTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/defaultTextColor"
android:fontFamily="#font/abhaya_libre_semi_bold"
android:textSize="14sp" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/chat_date"
style="#style/defaultTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="0"
android:gravity="center"
android:text=""
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp" />
</LinearLayout>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
ChatAdapter.java
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
Context context;
List<ChatList> chatLists;
public ChatAdapter(Context context, List<ChatList> chatLists) {
this.context = context;
this.chatLists = chatLists;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_adapter, null, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ChatList item = chatLists.get(position);
holder.name.setText(item.getName());
holder.message.setText(item.getMessage());
holder.date.setText(item.getDate());
Glide.with(context)
.load(item.getImage())
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(holder.image);
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, ChatMessageActivity.class);
intent.putExtra("chatname", item.getName());
context.startActivity(intent);
});
}
public void removeItem(int position) {
chatLists.remove(position);
notifyItemRemoved(position);
}
public void restoreItem(ChatList item, int position) {
chatLists.add(position, item);
notifyItemInserted(position);
}
public List<ChatList> getData() {
return chatLists;
}
#Override
public int getItemCount() {
return chatLists.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image;
AppCompatTextView name, message, date;
public LinearLayout forground, background;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.chat_image);
name = itemView.findViewById(R.id.chat_name);
message = itemView.findViewById(R.id.chat_message);
date = itemView.findViewById(R.id.chat_date);
forground = itemView.findViewById(R.id.view_foreground);
background = itemView.findViewById(R.id.view_background);
}
}
}
ChatList.java
public class ChatList {
String name, message, date, image;
public ChatList(String name, String message, String date, String image) {
this.name = name;
this.message = message;
this.date = date;
this.image = image;
}
public String getName() {
return name;
}
public String getMessage() {
return message;
}
public String getDate() {
return date;
}
public String getImage() {
return image;
}
}
here is my chatListData
ChatList list = new ChatList("Ellen, Grandpa + 1", "Thanks for letting us know!", "Jan 04", "https://media.istockphoto.com/photos/senior-adult-male-laughing-portrait-he-is-90-years-old-picture-id155357459?k=6&m=155357459&s=612x612&w=0&h=E_uK43zNoAnt9ohSdYMbNgCyFJliuKIzTynduh7d-Ck=");
chatLists.add(list);
list = new ChatList("Ellen, Grandpa + 1", "Thanks for letting us know!", "Jan 04", "https://media.istockphoto.com/photos/senior-adult-male-laughing-portrait-he-is-90-years-old-picture-id155357459?k=6&m=155357459&s=612x612&w=0&h=E_uK43zNoAnt9ohSdYMbNgCyFJliuKIzTynduh7d-Ck=");
chatLists.add(list);
list = new ChatList("Ellen, Grandpa + 1", "Thanks for letting us know!", "Jan 04", "https://media.istockphoto.com/photos/senior-adult-male-laughing-portrait-he-is-90-years-old-picture-id155357459?k=6&m=155357459&s=612x612&w=0&h=E_uK43zNoAnt9ohSdYMbNgCyFJliuKIzTynduh7d-Ck=");
chatLists.add(list);
This question already has answers here:
FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1
(2 answers)
Closed 2 years ago.
My RecyclerView is not displaying any data.
buynow.java
public class buynow extends AppCompatActivity {
private RecyclerView recyclerView;
DatabaseReference ProductRef;
private EditText searchField;
private Button search_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buynow);
ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchField = (EditText) findViewById(R.id.search_field);
search_btn = (Button) findViewById(R.id.search_button);
search_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firebaseUserSearch();
}
});
}
private void firebaseUserSearch(){
FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(ProductRef,Products.class).build();
FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull
Products model) {
holder.setDetails(model.getPname(), model.getPprice(), model.getPmrp(),
model.getPcondition(), model.getPimage());
}
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
//View Holder Class
public class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(String phoneName, String phonePrice, String phoneMrp, String
phoneCondition, String phoneImage){
TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);
phone_name.setText(phoneName);
phone_price.setText(phonePrice);
phone_mrp.setText(phoneMrp);
phone_condition.setText(phoneCondition);
Picasso.with(getApplicationContext()).load(phoneImage).into(phone_image);
}
}
}
Products.java
public class Products {
private String pname,pprice,pimage,pmrp,pcondition;
public Products(){
}
public Products(String pname, String pprice, String pimage, String pmrp, String pcondition) {
this.pname = pname;
this.pprice = pprice;
this.pimage = pimage;
this.pmrp = pmrp;
this.pcondition = pcondition;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPprice() {
return pprice;
}
public void setPprice(String pprice) {
this.pprice = pprice;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPmrp() {
return pmrp;
}
public void setPmrp(String pmrp) {
this.pmrp = pmrp;
}
public String getPcondition() {
return pcondition;
}
public void setPcondition(String pcondition) {
this.pcondition = pcondition;
}
}
Layout for RecyclerView
<?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:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/primanrybg">
<RelativeLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/buyphones_layout_bg">
<ImageView
android:id="#+id/product_image"
android:layout_width="60dp"
android:layout_height="90dp"
android:src="#drawable/rapid_pickup_foreground" />
<TextView
android:id="#+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:textSize="16sp"
android:textColor="#color/black"
android:paddingStart="16dp"
android:paddingBottom="4dp"
android:text="iPhone 6s Space Grey (64GB)"/>
<LinearLayout
android:id="#+id/condition"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:layout_marginLeft="16dp"
android:background="#drawable/search_frame"
android:layout_below="#id/product_name"
android:layout_marginBottom="3dp"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/yes_no_bg"
android:textColor="#color/white"
android:paddingStart="4dp"
android:padding="1dp"
android:paddingEnd="4dp"
android:text="Condition:" />
<TextView
android:id="#+id/product_condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="3dp"
android:padding="1dp"
android:textColor="#color/black"
android:text="Like New" />
</LinearLayout>
<TextView
android:id="#+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/condition"
android:layout_toRightOf="#+id/product_image"
android:textSize="18sp"
android:textColor="#color/black"
android:paddingStart="16dp"
android:text="9,9999"/>
<TextView
android:id="#+id/product_mrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/product_price"
android:layout_below="#id/condition"
android:textSize="18sp"
android:text="24000"
android:textColor="#color/grey"
android:paddingStart="10dp"/>
I am not getting data into my RecyclerView. It is just not showing anything no error nothing. Any help is very appreciated.
I am getting my data from firebase and putting them into the recyclerview to display a list of products.
I would be really great if you can point out what is wrong with my code so that i can correct it.
You need to add startListening() to start listening for data in firebaseui:
The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
I tried to retrive images from firebase with correct url link. The RecyclerView keep shown "first" info of Food which is cookie-milk-cup:
This is my firebase data structure:
all_uploaded_image //directory
|-cookie milk cup
|-description: "yes!" //shown successful
|-image_uri: "content://com.android.providers.media.documents..." //no necessary in this case
|-name: "cookie milk cup" //shown successful
|-price: "1.20" //not shown
|-uploader_uid: "GSsY4tEo5ZZpS8kkJEpWGLCbFmC3" //no necessary in this case
fish n chips //recursive only on cookie milk cup, so nothing appear in here
|-description: "delicious fc"
|-image_uri: "content://com.android.providers.media.documents..."
|-name: "fish n chips"
|-price: "1.20"
|-uploader_uid: "GSsY4tEo5ZZpS8kkJEpWGLCbFmC3"
I am using android with java, want to get the info from firebase using fragment.
This is the AllFOodFragment.java
public class AllFoodFragment extends Fragment {
private View contactsView;
private ArrayList<Food> entries;
private DatabaseReference ref;
private FirebaseDatabase dbr;
private FoodAdapter adapter;
private RecyclerView recycler_view;
public AllFoodFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View contactsView = inflater.inflate(R.layout.home_main, container, false); //v
recycler_view = (RecyclerView) contactsView.findViewById(R.id.recycler_view);
recycler_view.setHasFixedSize(true);
recycler_view.setItemAnimator(new DefaultItemAnimator());
final FragmentActivity ga = getActivity(); //2a
LinearLayoutManager lm = new LinearLayoutManager(ga); //2
recycler_view.setLayoutManager(lm);
entries = new ArrayList<Food>();
ref = FirebaseDatabase.getInstance().getReference().child("all_uploaded_image");
ref.addChildEventListener(new ChildEventListener(){
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot ds: dataSnapshot.getChildren()) {
//Log.e("Count " ,"" + dataSnapshot.getChildrenCount());
Food data = dataSnapshot.getValue(Food.class);
//data.setPrice(dataSnapshot.child("price").getValue().toString());
//data.setImageUrl(dataSnapshot.child("image_uri").getValue().toString());
entries.add(data);
//Log.e("Get All Description", data.get_price());
}
adapter = new FoodAdapter(getActivity(), entries);
adapter.notifyDataSetChanged();
adapter.setListFood(entries);
//3; No adapter attached in here, but program runs
recycler_view.setAdapter(adapter);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
return contactsView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onViewCreated(View view, #Nullable Bundle savedUbstabceState){
super.onViewCreated(view, savedUbstabceState);
this.contactsView = view;
}
}
And, this is FoodAdapter.java
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {
private Context m_context;
private ArrayList<Food> m_listFood = new ArrayList<Food>();
public FoodAdapter(Context context, ArrayList<Food> food_list) {//m_context, food_list
this.m_context = context;
this.m_listFood = food_list;
}
#NonNull
#Override
public FoodViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fragment_all_food, parent, false);
return new FoodViewHolder(v);
}
public class FoodViewHolder extends RecyclerView.ViewHolder{
public ImageView FoodIcon;
public TextView FoodName;
public TextView FoodDescription;
public TextView FoodPrice;
public Button Fooddetail;
public FoodViewHolder (View itemView){
super(itemView);
FoodIcon = (ImageView)itemView.findViewById(R.id.thumbnail); //icon
FoodName = itemView.findViewById(R.id.foodname);
FoodDescription = itemView.findViewById(R.id.fooddescription);
FoodPrice = itemView.findViewById(R.id.foodprice);
Fooddetail = itemView.findViewById(R.id.fooddetail);
}
public void onClick(View view) {
}
}
#Override
public void onBindViewHolder(#NonNull FoodViewHolder holder, int position) {
Food f = m_listFood.get(position);
holder.FoodName.setText(f.get_name());
holder.FoodDescription.setText(f.get_description());
holder.FoodPrice.setText(f.get_price());
holder.FoodPrice.setText(f.get_url());
//Picasso.get().load(m_listFood.get(position).get_url()).fit().into(holder.FoodIcon);
Glide.with(m_context).load(f.get_url()).into(holder.FoodIcon); //getListFood().get(position).get_icon()
}
#Override
public int getItemCount() {
return m_listFood.size();
}
public Context get_context() {
return m_context;
}
public void setListFood(ArrayList<Food> m_listFood){
this.m_listFood = m_listFood;
}
}
this is item_fragment_all_food.xml
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView //icon not shown, white
android:id="#+id/thumbnail"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/star_big_on" />
<TextView
android:id="#+id/foodname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="191dp"
android:layout_marginTop="16dp"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/thumbnail"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/fooddescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/foodname"
android:layout_marginEnd="86dp"
android:layout_marginBottom="-91dp"
android:text="Description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/thumbnail"
app:layout_constraintTop_toBottomOf="#+id/foodname" />
<TextView
android:id="#+id/foodprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="85dp"
android:layout_marginEnd="95dp"
android:text="Price"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toEndOf="#+id/thumbnail"
app:layout_constraintTop_toBottomOf="#+id/fooddescription"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/fooddetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="79dp"
android:layout_marginBottom="16dp"
android:background="#color/colorPrimary"
android:text="Detail"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toEndOf="#+id/thumbnail"
app:layout_constraintTop_toBottomOf="#+id/foodprice"
app:layout_constraintVertical_bias="0.0" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
And, this is activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:autofillHints="email"
android:layout_margin="15dp"
android:inputType="textEmailAddress"
android:hint="Enter Email"
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_margin="15dp"
android:inputType="textPassword"
android:hint="Enter Password"
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="15dp"
android:hint="Register"
android:id="#+id/btn_register"
android:layout_width="match_parent"
android:onClick="click_to_register_new_user"
android:layout_height="wrap_content" />
<Button
android:layout_margin="15dp"
android:hint="Login"
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="137dp"
android:text="#string/reset_string" />
</RelativeLayout>
Lastly, my Food Model
public class Food {
public String name;
public String description;
public String price;
public String img_url;
public String icon;
public Food() {
//empty
}
public Food(String name, String description, String price, String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
this.name = name;
this.description = description;
this.price = price;
this.img_url = imageUrl;
}
public String get_name() {
return name;
}
public String get_description() {
return description;
}
public String get_price() {
return price;
}
public String get_icon() {
return icon;
}
public String get_url() {
return img_url;
}
public void setPrice(String price) {
this.price = price;
}
public void setImageUrl(String imageUrl) {
img_url = imageUrl;
}
public void set_icon(String icon) {
this.icon = icon;
}
}
The error I get is E/RecyclerView: No adapter attached; skipping layout
and No setter/field for image_uri found on class com.example.hungrystomach.Model.Food [but really I do not want image_uri shown in my recyclerview so I skip the TextView]
The recyclerView does not have any Image shown even I've FoodIcon with thumbnail in FOodAdaper.java. I've tried for three weeks already still no hope. I suppose to get all the info I need. I know it is little bit long but any suggestion and answer would appreciate.
Hello I just started the Android and I do not understand how to put more TextView in a RecyclerView
I have already seen this solution but I do not understand: How to create RecyclerView with multiple view type?
I tried to make a table with multiple dimensions but it did not work.
Adapter:
public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {
private String[]data;
public DeviceRecyclerView(String[]data){
this.data = data;
}
#Override
public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.device_list_row, parent , false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String name = data[position];
String rssi = data[position];
String uuid = data[position];
holder.DeviceNameTextView.setText(name);
holder.DeviceUUIDTextViewValue.setText(rssi);
holder.DeviceRSSIVTextViewValue.setText(uuid);
}
#Override
public int getItemCount() {
return data.length;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView DeviceNameTextView;
TextView DeviceUUIDTextViewValue;
TextView DeviceRSSIVTextViewValue;
public ViewHolder (View itemView){
super(itemView);
DeviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
DeviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
DeviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
}
}
MainActivity:
RecyclerView deviceList = (RecyclerView)findViewById(R.id.recycler_view_NoPaired);
deviceList.setLayoutManager(new LinearLayoutManager(this));
String[]names = {"Samsung64656"};
String[]rssi = {"ezez"};
String[]uuid = {"08:90:e5:90"};
deviceList.setAdapter(new DeviceRecyclerView(names));
My 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:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="180dp"
android:gravity="center_vertical"
tools:layout_editor_absoluteX="30dp"
tools:layout_editor_absoluteY="81dp">
<View
android:id="#+id/ColoredRect"
android:layout_width="10dp"
android:layout_height="160dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="#E27F26"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/DeviceNameTextView"
android:layout_width="133dp"
android:layout_height="24dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/device_name_label"
android:textColor="#color/TextColor"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/DeviceUUIDTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:text="#string/deviceUUIDTextView"
android:textColor="#color/TextColor"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/DeviceUUIDTextViewValue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DeviceNameTextView" />
<TextView
android:id="#+id/DeviceUUIDTextViewValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="14sp"
android:text=""
android:textColor="#color/TextColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DeviceUUIDTv" />
<TextView
android:id="#+id/rssiLabelTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/deviceRSSITextView"
android:textColor="#color/TextColor"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/DeviceUUIDTextViewValue" />
<TextView
android:id="#+id/DeviceRSSIVTextViewValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="14sp"
android:text=""
android:textColor="#color/TextColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rssiLabelTextView" />
</android.support.constraint.ConstraintLayout>
Please explain me clearly how to do it. I am French sorry for spelling errors.
You just need to add some textView in you'r XML row (named device_list_row.xml), and init their in your adapter, like you have already did with 3 textview.
But's more easy with Object than use array of string, for data. for example in you'r case , make Device class object
Device.java
public class Device {
String name;
String rssi;
String uuid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRssi() {
return rssi;
}
public void setRssi(String rssi) {
this.rssi = rssi;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
And adapter code ->
public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {
private ArrayList<Device> deviceList;
public DeviceRecyclerView(Arraylist<Device> deviceList){
this.deviceList= deviceList;
}
#Override
public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.device_list_row, parent , false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Device device = deviceList.get(position);
holder.deviceNameTextView.setText(device.getName());
holder.deviceUUIDTextViewValue.setText(device.getUuid());
holder.deviceRSSIVTextViewValue.setText(device.getRssi());
}
#Override
public int getItemCount() {
return deviceList.length;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView deviceNameTextView, deviceUUIDTextViewValue, deviceRSSIVTextViewValue;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
deviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
deviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
deviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
}
#Override
public void onClick(View v) {
Log.i("DEBUG","Item RecyclerView Cliqué");
}
}
With that, you init your Adapter with list of Device Object, who can have any variable has you want without need to pass more array or data to Adapter:-)
And for make a new Device, and use it in RV
Device device = new Device();
device.setName("Device Name 01");
device.setUuid("uuid value");
device.SetRssi("rssi value");
ArrayList<Device> deviceList = new ArrayList<>();
deviceList.add(device);
//Init adapter and fill it
DeviceRecyclerView deviceRecyclerView = new DeviceRecyclerView(deviceList);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(deviceRecyclerView);
I want to do list view like this. What I want is when user touch 1st item, the 2nd item row appear. When user touch 3rd row item the previous item automatically hide again. How can i achieve this instead of using expandable listview? guide me in achieving this. Thanks in advance.
I'm sorry that i'm a bit busy, so i can't write the code right now. But below is the way which you can use to get what you want. I will add code when i will get time.
There is a parameter name "android:animateayoutchanges", apply it to the parent of your view which will be visible and invisible.
Save the position of the item which you have clicked and expanded.
When you click that item, Set the visibility of the view to visible.
When you again click on that item position, set that item's visibility to gone.
When you click some other item, then set the visibility of the stored item position to gone, set currently selected item's visibility to visible and store it's position.
That's it.
android:animateayoutchanges is used so that when you set the visibility to visible or gone and views move upward or downward, then this movement will have smooth animation.
I will update my answer with code later But you will get an idea from above steps.
UPDATE
public class ShippingAdapter extends RecyclerView.Adapter<ShippingAdapter.ViewHolder> {
private ArrayList<ShippingOptions> list;
private int expandedPosition = -1;
private int listSize = 0;
private int checkedPositon = -1;
private String currency;
public ShippingAdapter(ArrayList<ShippingOptions> list, String currency, AdapterInterface listener) {
this.list = list;
listSize = list.size();
this.currency = currency;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_shipping_options, parent, false);
// Layout in which you will add **android:animatelayoutchanges**
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
ShippingOptions item = list.get(position);
holder.name.setText(item.getName());
holder.fee.setText(utils.getFormattedCurrency(currency, Double.parseDouble(item.getShippingFee())));
holder.description.setText(item.getDescription());
holder.deliveryTime.setText(item.getDeliveryTime());
if (listSize == 1) {
list.get(position).setExpanded(true);
}
**if (list.get(position).isExpanded()) {
holder.expandableView.setVisibility(View.VISIBLE);
holder.checkBox.setVisibility(View.VISIBLE);
if (checkedPositon == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.parent.setElevation(12);
holder.parent.setTranslationZ(6);
holder.parent.setClipToPadding(false);
holder.parent.setClipToOutline(false);
}
} else {
holder.expandableView.setVisibility(View.GONE);
holder.checkBox.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.parent.setElevation(0);
holder.parent.setTranslationZ(0);
}
}**
**holder.parent.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public void onClick(View v) {
if (listSize > 1) {
if (expandedPosition == position) {
if (list.get(position).isExpanded()) {
list.get(position).setExpanded(false);
} else {
list.get(position).setExpanded(true);
}
notifyItemChanged(position);
} else {
if (expandedPosition >= 0) {
list.get(expandedPosition).setExpanded(false);
notifyItemChanged(expandedPosition);
}
expandedPosition = position;
list.get(expandedPosition).setExpanded(true);
notifyItemChanged(expandedPosition);
}
}
}
});**
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name, fee, description, deliveryTime;
public LinearLayout parent, expandableView;
public CheckBox checkBox;
public ViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.tv_shipping_name);
fee = (TextView) v.findViewById(R.id.tv_shipping_fee);
description = (TextView) v.findViewById(R.id.tv_description);
deliveryTime = (TextView) v.findViewById(R.id.tv_delivery_time);
parent = (LinearLayout) v.findViewById(R.id.parent);
expandableView = (LinearLayout) v.findViewById(R.id.expandable_view);
checkBox = (CheckBox) v.findViewById(R.id.checkbox);
}
}
}
list_shipping_options.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#color/white"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeightSmall"
android:gravity="center_vertical">
<TextView
android:id="#+id/tv_shipping_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/text_medium"
android:textColor="#color/colorPrimary" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:background="?android:attr/listChoiceIndicatorMultiple"
android:button="#null"
android:checked="false"
android:visibility="visible" />
</RelativeLayout>
<LinearLayout
android:id="#+id/expandable_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/label_shipping_fee" />
<TextView
android:id="#+id/tv_shipping_fee"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/lable_description" />
<TextView
android:id="#+id/tv_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/delivery_time" />
<TextView
android:id="#+id/tv_delivery_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
ShippingOptions.class
public class ShippingOptions implements Serializable {
private String id;
private String name;
private String shippingFee;
private String description;
private String deliveryTime;
private boolean isExpanded;
private boolean isChecked;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShippingFee() {
return shippingFee;
}
public void setShippingFee(String shippingFee) {
this.shippingFee = shippingFee;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDeliveryTime() {
return deliveryTime;
}
public void setDeliveryTime(String deliveryTime) {
this.deliveryTime = deliveryTime;
}
public boolean isExpanded() {
return isExpanded;
}
public void setExpanded(boolean expanded) {
isExpanded = expanded;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}