Setting Up Horizontal RecyclerView - java

I want to add 10dp padding for my first tile in recycler View.
Just Like in the Book My show app the first App has 10dp padding (assume) then the padding between two tiles (suppose 5dp).
Similarly Last tile also have 10dp (assume) padding in the end
I'm adding my code of the recycler view If anyone wants to add something in it
RecyclerMetroAdapter.java
package com.example.android.indianmetro;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
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.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mPlaceNames = new ArrayList<>();
private ArrayList<String> mPlaceImageUrl = new ArrayList<>();
private ArrayList<String> mPlaceMetroDistance = new ArrayList<>();
private ArrayList<String> mClosestMetro = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context context, ArrayList<String> placeNames, ArrayList<String> placeImageUrl,
ArrayList<String> placeMetroDistance, ArrayList<String> closestMetro ) {
mPlaceNames = placeNames;
mPlaceImageUrl = placeImageUrl;
mPlaceMetroDistance = placeMetroDistance;
mClosestMetro = closestMetro;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onCreateViewHolder: called");
Glide.with(mContext)
.asBitmap()
.load(mPlaceImageUrl.get(i))
.into(viewHolder.placeImage);
viewHolder.placeName.setText(mPlaceNames.get(i));
viewHolder.placeMetroDistance.setText(mPlaceMetroDistance.get(i));
viewHolder.closestMetro.setText(mClosestMetro.get(i));
}
#Override
public int getItemCount() {
if (mPlaceNames.size() < 8){
return mPlaceNames.size();
} else return 8;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView placeImage;
TextView placeName;
TextView placeMetroDistance;
TextView closestMetro;
public ViewHolder(#NonNull View itemView) {
super(itemView);
placeImage = (ImageView) itemView.findViewById(R.id.image);
placeName = itemView.findViewById(R.id.textView);
placeMetroDistance = itemView.findViewById(R.id.textView2);
closestMetro = itemView.findViewById(R.id.placeMetroName);
}
}
}
Code of tiles I'm using inside my RecyclerView
horizontal_item.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="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/delhi1" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="2dp"
android:ellipsize="end"
android:maxLines="1"
android:text="India Gate"
android:textColor="#color/darkHeading"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="36dp"
android:text="2.4 Km Away"
android:textColor="#color/blueAccent"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="24dp"
android:src="#drawable/ic_metro"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:id="#+id/placeMetroName"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Central Secretariat"
android:textColor="#color/subHeading"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
</android.support.constraint.ConstraintLayout>
Also if some one can suggest that how to make a tile fully visible from left and not allow any tile partially hidden from the left side

I guess you can try to set the padding in onBindViewHolder(),
for the first:
if (i == 0)
viewHolder.placeImage.setPadding(10, 5, 5, 5);
and for the last:
if (i == mPlaceImageUrl.size() - 1)
viewHolder.placeImage.setPadding(5, 5, 10, 5);
Change the numbers to what you like they are left->top->right->bottom padding.

Related

xml file doesnt show when app is running for some reason

Hi when i run my application for some reason it doesnt run my xml file in the application theres no error or anything but the xml doesnt show.
What the app looks like when it runs (missing home_items xml): https://gyazo.com/1f701b1790f6688d7242eaa5774a3dae
What the home_items looks like: https://gyazo.com/d697abdcd5a4c3d3b2064729fcaa9274
home_items.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:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profileImage"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="8dp"
android:src="#drawable/ic_person"
app:civ_border_color="#434343"
app:civ_border_width="1dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_toEndOf="#+id/profileImage"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/nameTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="#+id/timeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2h"
android:textColor="#000" />
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_below="#+id/profileImage"
android:scaleType="centerCrop" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:orientation="horizontal">
<ImageButton
android:id="#+id/likeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#drawable/ic_heart" />
<ImageButton
android:id="#+id/commentBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#drawable/ic_comment" />
<ImageButton
android:id="#+id/shareBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#drawable/ic_share" />
</LinearLayout>
<TextView
android:id="#+id/descTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:text="Description"
android:textStyle="bold" />
<TextView
android:id="#+id/likeCountTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/descTv"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:text="100 likes"
android:textStyle="bold" />
</RelativeLayout>
HomeAdapter.java
package com.example.soulforge.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.soulforge.R;
import com.example.soulforge.fragments.Home;
import com.example.soulforge.model.HomeModel;
import java.util.List;
import java.util.Random;
import de.hdodenhof.circleimageview.CircleImageView;
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeHolder>{
private List<HomeModel> list;
Context context;
public HomeAdapter(List<HomeModel> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public HomeHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_items,parent, false);
return new HomeHolder(view);
}
#Override
public void onBindViewHolder(#NonNull HomeHolder holder, int position) {
holder.userNameTv.setText(list.get(position).getUserName());
holder.timeTv.setText(""+list.get(position).getTimestamp());
int count = list.get(position).getLikeCount();
if(count ==0){
holder.likeCountTv.setVisibility(View.INVISIBLE);
}else if (count == 1){
holder.likeCountTv.setText(count + " like");
}else{
holder.likeCountTv.setText(count + " likes");
}
holder.descriptionTv.setText(list.get(position).getDescription());
Random random = new Random();
int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
Glide.with(context.getApplicationContext())
.load(list.get(position).getProfileImage())
.placeholder(R.drawable.ic_person)
.timeout(6500)
.into(holder.profileImage);
Glide.with(context.getApplicationContext())
.load(list.get(position).getImageUrl())
.placeholder(new ColorDrawable(color))
.timeout(7000)
.into(holder.imageView);
}
#Override
public int getItemCount() {
return list.size();
}
static class HomeHolder extends RecyclerView.ViewHolder{
private CircleImageView profileImage;
private TextView userNameTv, timeTv, likeCountTv, descriptionTv;
private ImageView imageView;
private ImageButton likeBtn, commentBtn, shareBtn;
public HomeHolder(#NonNull View itemView) {
super(itemView);
profileImage = itemView.findViewById(R.id.profileImage);
imageView = itemView.findViewById(R.id.imageView);
userNameTv = itemView.findViewById(R.id.nameTv);
timeTv = itemView.findViewById(R.id.timeTv);
likeCountTv = itemView.findViewById(R.id.likeCountTv);
likeBtn = itemView.findViewById(R.id.likeBtn);
commentBtn = itemView.findViewById(R.id.commentBtn);
shareBtn = itemView.findViewById(R.id.shareBtn);
descriptionTv = itemView.findViewById(R.id.descTv);
}
}
}
Hi im wondering whats the issue and why the home_items doesnt show in my application.
build -> clean project
then
build -> Rebuild project
then Run
Go to
File -> Invalidate Caches/ Restart -> Invalidate and restart
It fixes minor xml bugs
If not,
Post more of your adapter code
Updated:
Try adding this to your adapter:
#Override
public int getItemViewType(int position)
{
return position;
}
Updated:
Try this again in adapter:
in your onCreateViewHolder:
return new HomeAdapter.HomeHolder(view);

Can somebody show me why ListView isn't showing?

The following project was copied verbatim from an online lesson. It is supposed to display a ListView but nothing shows up. I am brand new to Android Studio and this is only my second project in it. There are 5 files and I will display the contents of each of them below. I just need another pair of eyes or two to find the problem, because I'm "bug blind". Any help will be greatly appreciated.
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.tadhg.uiuxadapterpractical;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView lv;
Context context;
TypedArray images;
String [] titleList;
String [] descList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
lv = (ListView) findViewById(R.id.listView);
}
}
OurAdapter.java
package com.example.tadhg.uiuxadapterpractical;
import android.content.Context; import android.content.res.TypedArray;
import android.view.LayoutInflater; import android.view.View; import
android.view.ViewGroup; import android.widget.BaseAdapter; import
android.widget.ImageView; import android.widget.TextView;
public class OurAdapter extends BaseAdapter {
private String [] titles;
private String [] desc;
private TypedArray imageID;
private Context context;
private static LayoutInflater layoutInflater;
//constructor
OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
titles = titleList;
desc = descList;
imageID = images;
context = adapterContext;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return titles.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
private class ViewHolder {
TextView tv1, tv2;
ImageView img;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = new ViewHolder();
if (view == null) {
view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tv1.setText(titles[position]);
viewHolder.tv2.setText(desc[position]);
viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
return view;
} }
List_layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cplus" />
<TextView
android:id="#+id/textView7"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView8"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/textView7"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
UIUXAdapterPractical
<string-array name="Languages">
<item>Java</item>
<item>Swift</item>
<item>C#</item>
<item>SQL</item>
<item>Javascript</item>
<item>Jquery</item>
<item>C++</item>
</string-array>
<string-array name="LanguageDesc">
<item>Desc. of Java</item>
<item>Desc. of Swift</item>
<item>Desc. of C#</item>
<item>Desc. of SQL</item>
<item>Desc. of Javascript</item>
<item>Desc. of Jquery</item>
<item>Desc. of C++</item>
</string-array>
<array name="LanguageImg">
<item>#drawable/java</item>
<item>#drawable/swift</item>
<item>#drawable/csharp</item>
<item>#drawable/sql</item>
<item>#drawable/js</item>
<item>#drawable/jquery</item>
<item>#drawable/cplus</item>
</array> </resources>
you have missed attaching your adapter to listview...you must set your adapter to your listview then only you will see your listview
change your code with following code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
hope it will solve your problem :)
In your listview you have:
android:layout_width="0dp"
android:layout_height="0dp"
instead put
android:layout_width="match_parent"
android:layout_height="match_parent"
You're forcing the size to be 0 so you can't see it. Tell me if it works

How do I fix my FirebaseRecyclerAdapter on a fragment?

I am working on this application and I'm trying to show my "expenses" database data in my personal fragment. I have created a recycler view file to replace the default layout. I am struggling to make this work. Can anyone help with the FirebaseRecycler Adapter and the holder class? I am not able to make this work, my fragment display the default layout with no information. I have tried and follow some codes but it is not working.
here is my personal fragment where the recycler view should appear
package uk.brighton.ama75.project;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.time.chrono.JapaneseDate;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class personalFragment extends Fragment {
private FirebaseAuth mAuth;
private String currentUserID;
private DatabaseReference databaseReference;
private RecyclerView recyclerView;
private Adapter adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_personal, container, false);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(currentUserID);
recyclerView = myView.findViewById(R.id.recycler_expense);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
return myView;
}
#Override
public void onStart() {
super.onStart();
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("expenses")
.limitToLast(5);
FirebaseRecyclerOptions<PersonalExpenses> options =
new FirebaseRecyclerOptions.Builder<PersonalExpenses>()
.setQuery(query, PersonalExpenses.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<PersonalExpenses, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull PersonalExpenses model) {
holder.setDate(model.getDate());
holder.setDescription(model.getDescription());
holder.setAmount(model.getAmount());
holder.setType(model.getType());
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.expense_recycler, parent, false);
return new MyViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
#Override
public void onStop() {
super.onStop();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
private void setDate(String date) {
TextView mDate = mView.findViewById(R.id.date_income);
mDate.setText(date);
}
private void setType(String type) {
TextView mType = mView.findViewById(R.id.type_txt);
mType.setText(type);
}
private void setDescription(String description) {
TextView mNote = mView.findViewById(R.id.note_txt);
mNote.setText(description);
}
private void setAmount(String amount) {
TextView mAmount = mView.findViewById(R.id.amount_income);
String stramount = String.valueOf(amount);
mAmount.setText(stramount);
}
}
}
This is the expenses recycler view file
<?xml version="1.0" encoding="utf-8"?>
<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/expense_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/date_income"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6 June 2019"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<TextView
android:id="#+id/type_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Type"
android:textAlignment="center"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/note_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/type_txt"
android:text="#string/description"
android:textAlignment="center"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3
">
<TextView
android:id="#+id/amount_income"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0000"
android:textAlignment="center"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
This is the personal layout
<?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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#FFF1F3FC"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardElevation="5dp"
android:elevation="10dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="10dp"
android:layout_height="wrap_content"
tools:ignore="Suspicious0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"
android:id="#+id/expenses"
android:text="expense"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center"
android:layout_margin="10dp"
android:layout_height="wrap_content"
tools:ignore="Suspicious0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/expense_txt"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"
android:text="000.00"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="#+id/recycler_expense"
android:layout_height="match_parent"/>
</LinearLayout>
I believe you are missing these two main code.
Set adapter.startListening() inside onStart()
Set adapter.stopListening() inside onStop()
Make sure to put the code before the scope end of onStart() as you are performing a lot of operations and setting your adapter above.

Issues With Android App Recycler View Displaying User Thumb and User Name

I am programming a simple search for users but my app is not displaying the results of the query and crashes about 40% of the time after I click search. Here is my code. Any advice would be appreciated.
My Activity:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.MyApp.Objects.ParticipantsObject;
import com.MyApp.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class ParticipantsActivity extends AppCompatActivity {
private EditText mSearchField;
private ImageButton mSearchBtn;
private RecyclerView mResultList;
private DatabaseReference mUserDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_neighbors);
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("Participants");
mSearchField = (EditText) findViewById(R.id.search_field);
mSearchBtn = (ImageButton) findViewById(R.id.search_btn);
mResultList = (RecyclerView) findViewById(R.id.result_list);
mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));
mSearchBtn.setOnClickListener(view -> {
String searchText = mSearchField.getText().toString();
firebaseUserSearch(searchText);
});
}
private void firebaseUserSearch(String searchText) {
Toast.makeText(ParticipantsActivity.this, "Started Search", Toast.LENGTH_LONG).show();
Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText);
FirebaseRecyclerOptions<ParticipantsObject> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<ParticipantsObject>()
.setQuery(firebaseSearchQuery, ParticipantsObject.class)
.build();
class UserHolder extends RecyclerView.ViewHolder {
private TextView imageThumbTextView, nameTextView
UserHolder(View itemView) {
super(itemView);
imageThumbTextView = itemView.findViewById(R.id.profile_image);
nameTextView = itemView.findViewById(R.id.name_text);
}
void setUsers(ParticipantsObject participantsObject) {
String imageThumb = driverObject.getThumb_image();
imageThumbTextView.setText(imageThumb);
String name = participantsObject.getName();
nameTextView.setText(name);
}
}
FirebaseRecyclerAdapter<ParticipantsObject, UserHolder> firebaseRecyclerAdapter;
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ParticipantsObject, UserHolder>(firebaseRecyclerOptions) {
#Override
protected void onBindViewHolder(#NonNull UserHolder userHolder, int position, #NonNull ParticipantsObject participantsObject) {
userHolder.setUsers(participantsObject);
}
#Override
public UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);
return new UserHolder(view);
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
}
My activity_participants.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"
android:background="#ffffff"
tools:context="com.MyApp.ParticipantsActivity">
<TextView
android:id="#+id/heading_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="Firebase Search"
android:textColor="#555555"
android:textSize="24sp" />
<EditText
android:id="#+id/search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/heading_label"
android:layout_below="#+id/heading_label"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_toStartOf="#+id/search_btn"
android:background="#drawable/search_layout"
android:ems="10"
android:hint="Search here"
android:inputType="textPersonName"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:textColor="#999999"
android:textSize="16sp" />
<ImageButton
android:id="#+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/search_field"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/search_field"
android:layout_marginRight="30dp"
android:background="#android:color/background_light"
app:srcCompat="#mipmap/search_button" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/result_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/search_field"
android:layout_marginTop="50dp">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
My list layout:
<?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:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
app:srcCompat="#mipmap/ic_default_user" />
<TextView
android:id="#+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="14dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="213dp"
android:layout_marginRight="20dp"
android:layout_toEndOf="#+id/profile_image"
android:text="Username"
android:textColor="#555555"
android:textSize="16sp" />
</RelativeLayout>
Anyone have any ideas as to why it's not displaying results?
My relevant Firebase DB basic paths are like so:
Users -> Participants -> UserIDs -> name, image, etc
Logcat isn't giving me much at the moment.

RecyclerView with GridLayoutManager left justifies views

I am trying to add a RecyclerView with GridLayoutManagerto my app. Everything works as expected, only problem is it seems the columns are left justified instead of center justified. Any ideas? Thanks in advance!
Here is an image showing how it looks:
Here is the single item layout:
<?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"
android:layout_gravity="center_horizontal"
android:background="?android:selectableItemBackgroundBorderless"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/iconSpot"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher"
tools:ignore="MissingPrefix" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Dummy Text"
android:textSize="16sp" />
</LinearLayout>
And here is the full activity layout:
<?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/totalScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/fab"
android:layout_gravity="bottom|center"
android:gravity="bottom|center_vertical">
<TextView
android:id="#+id/sheetTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#424242"
android:padding="12dp"
android:text="Dummy Title"
android:textSize="18sp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/sheetTitle"
android:layout_marginEnd="20dp"
android:layout_marginTop="-28dp"
android:visibility="gone"
app:fabSize="normal" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gridScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sheetTitle"
android:background="#424242"
android:gravity="bottom|center"
android:layout_gravity = "center"
android:orientation="vertical"
android:paddingBottom="24dp" />
</RelativeLayout>
Ok! I figured it out, it ended up being the fact that I needed to make the item layout width set as match_parent instead of wrap_content. So that makes it look 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="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="?android:selectableItemBackgroundBorderless"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/iconSpot"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_launcher"
tools:ignore="MissingPrefix" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Dummy Text"
android:textSize="16sp" />
</LinearLayout>
It is hard to fix with just the layout file since the item layout needs to be inflated and attached to the main layout in order to see the result.
So I decided to recreate it. I made modification to your layout file.
item layout
<?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"
android:layout_gravity="center"
android:background="#color/colorPrimary"
android:layout_margin="2dp"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/iconSpot"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:src="#android:drawable/ic_delete"
tools:ignore="MissingPrefix" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Dummy Text"
android:textSize="16sp" />
</LinearLayout>
Main activity layout
<?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/totalScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/fab"
android:layout_gravity="bottom|center"
android:gravity="bottom|center_vertical">
<TextView
android:id="#+id/sheetTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#424242"
android:padding="12dp"
android:text="Dummy Title"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/gridScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sheetTitle"
android:background="#424242"
android:gravity="bottom|center"
android:layout_gravity = "center"
android:clipToPadding="false"
android:orientation="vertical"
android:paddingBottom="24dp" />
</RelativeLayout>
Main Activity class. This class (GridSpacingItemDecoration) used for item margin is copied from here
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private GridLayoutManager layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = new GridLayoutManager(MainActivity.this, 2);
RecyclerView rView = (RecyclerView)findViewById(R.id.gridScreen);
rView.setHasFixedSize(true);
rView.setLayoutManager(layout);
int spanCount = 2;
int spacing = 50;
boolean includeEdge = true;
rView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, getAllItemList());
rView.setAdapter(rcAdapter);
}
private List<ItemObject> getAllItemList(){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
allItems.add(new ItemObject(android.R.drawable.ic_delete, "United"));
return allItems;
}
}
RecycleView Adapter Class
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
#Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.displayedImage.setImageResource(itemList.get(position).getImage());
holder.textTitle.setText(itemList.get(position).getTitle());
}
#Override
public int getItemCount() {
return this.itemList.size();
}
}
ViewHolder class
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView displayedImage;
public TextView textTitle;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
displayedImage = (ImageView)itemView.findViewById(R.id.iconSpot);
textTitle = (TextView)itemView.findViewById(R.id.textLabel);
}
#Override
public void onClick(View view) {
}
}
ObjectEntity class
public class ItemObject {
private int image;
private String title;
public ItemObject(int image, String title) {
this.image = image;
this.title = title;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
The Result is below. Try and see if it works for you.

Categories

Resources