I use RecyclerView to display Cards.
How can I make columns fit display, because
layoutManager = new GridLayoutManager(getBaseContext(), int);
can set only defined number of columns.
As I know there is auto_fit in GridView, but I dont know where should I use it.
cards_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/moccasin"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
android:layout_marginLeft="#dimen/activity_vertical_margin"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageView"
android:tag="image_tag"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="#+id/textViewEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<PersonData> peopleDataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewEmail;
ImageView imageViewIcon;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewEmail = (TextView) itemView.findViewById(R.id.textViewEmail);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView);
}
}
public MyAdapter(ArrayList<PersonData> people) {
this.peopleDataSet = people;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView textViewName = holder.textViewName;
final TextView textViewEmail = holder.textViewEmail;
ImageView imageView = holder.imageViewIcon;
textViewName.setText(peopleDataSet.get(listPosition).getName());
textViewEmail.setText(peopleDataSet.get(listPosition).getEmail());
imageView.setImageResource(peopleDataSet.get(listPosition).getImage());
}
#Override
public int getItemCount() {
return peopleDataSet.size();
}
}
Related
I am making an android cv app but I want to implement the UI shown in the screenshot.
screenshot of ui I want
below current UI from real device
current ui
The XML layout where I have implemented my UI, which consists of an ImageView and some TextViews which shows subjects. I have implemented all the tasks but UI is not showing how I want it to show.
<?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:background="#color/colorBlust"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<ImageView
android:id="#+id/educationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/education_information"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/education_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="#string/education_information"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="#+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_duration"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/institution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_institution"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/degree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_degree"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<Space
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/subjectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/university_subjects"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/subjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/university_subjects"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<include
layout="#layout/subject_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/subjects"
android:layout_marginTop="60dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I have created another adapter and created dummy data in subjectivity
below adapter class
public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below subject XML where I have hosted RecyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
below subject_list.xml where I have host items
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBlust"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:padding="5dp"
android:src="#drawable/computer_science"
android:layout_marginStart="10dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/computers_science"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#color/colorWhite" />
</LinearLayout>
I have created fake data in order to host other images and texts
below fakeModel class
public class FakeData {
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSubjects() {
return subjects;
}
public void setSubjects(String subjects) {
this.subjects = subjects;
}
String image;
String subjects;
}
below adapter class where I have extended with RecyclerView
public class SubjectAdapter extends RecyclerView.Adapter {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below My Subject class where I have implemented fake images and data
public class SubjectActivity extends Activity {
List<FakeData> fakeData;
int [] subjectImage = {R.drawable.computer_science,
R.drawable.data_structure,
};
ListView list;
String[] subjectText = {
"Computer Science",
"Data Structure",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subject);
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.list);
SubjectAdapter adapter = new SubjectAdapter(SubjectActivity.this, subjectImage,
subjectText, fakeData);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
}
}
This looks like you only have used Android Studio's drag and drop feature to position the UI elements. The problem is, Android Studio shows those UI elements in a generic device, which wont match all devices. When I was starting off with Android, this document helped me a lot to understand how elements in the UI must be placed.
https://developer.android.com/studio/write/layout-editor
You XML is malformed. Your LinearLayout orientation is wrong:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
It should be vertical.
Then validate its content. You have multiple RelativeLayout with orientation attribute.
It should be LinearLayout instead. RelativeLayouts don’t have orientation.
Besides those errors, your layout is very complex and have a deep hierarchy. This will lead to performance issues. My suggestion to you is to learn how to use ConstraintLayout.
The learning curve is a bit high, but it will be worth it!
I am searching for a long time on net. But no use. Please help or try to give some ideas how to solve this problem.
1. Bug Description
image link:https://i.stack.imgur.com/ixvdB.jpg
"left":Run the apk
"Right":When I swipe down, the size of the items is changing.
2. Code
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
List<Member> memberList = new ArrayList<>();
memberList.add(new Member(1, R.drawable.baishatunbeach1, "白沙屯海灘1"));
//add Members
recyclerView.setAdapter(new MemberAdapter(this, memberList));
}
private class MemberAdapter extends RecyclerView.Adapter<MemberAdapter.ViewHolder> {
private Context context;
private List<Member> memberList;
MemberAdapter(Context context, List<Member> memberList) {
this.context = context;
this.memberList = memberList;
}
#Override
public MemberAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_cardview_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(MemberAdapter.ViewHolder holder, int position) {
final Member member = memberList.get(position);
holder.imageId.setImageResource(member.getImage());
holder.textId.setText(String.valueOf(member.getId()));
holder.textName.setText(member.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(member.getImage());
Toast toast = new Toast(context);
toast.setView(imageView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
});
}
#Override
public int getItemCount() {
return memberList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageId;
TextView textId, textName;
ViewHolder(View itemView) {
super(itemView);
imageId = (ImageView) itemView.findViewById(R.id.imageId);
textId = (TextView) itemView.findViewById(R.id.textId);
textName = (TextView) itemView.findViewById(R.id.textName);
}
}
}
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
recyclerview_cardview_item.xml
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:padding="6dp"
app:cardBackgroundColor="#ffdddddd"
app:cardCornerRadius="28dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageId"
android:layout_width="120dp"
android:layout_height="160dp"
android:layout_marginStart="16dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginStart="20dp" />
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginStart="24dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
You need to change the height of cardview and linearlayout to wrap_content
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
app:cardBackgroundColor="#ffdddddd"
app:cardCornerRadius="28dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
I am using Recyclerview and my onCreateViewHolder and onBindViewHolder are not called. I am getting the data but it is not displaying.
My adapter class
public class DishesAdapter extends RecyclerView.Adapter<DishesAdapter.MyViewHolder> {
private Context mContext;
List<List> DialogList = new ArrayList<List>();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView dishnames;
public RatingBar dishratings;
public ImageView dishimages;
private ImageLoader mLoader;
public MyViewHolder(View view) {
super(view);
Log.d("follower2","hi");
dishnames = (TextView)view.findViewById(R.id.dishname);
dishimages = (ImageView)view.findViewById(R.id.dishimage);
dishratings=(RatingBar)view.findViewById(R.id.dishrating);
}
}
public DishesAdapter(Context mContext, List objects) {
super();
Log.d("follower1","hi");
this.mContext = mContext;
this.DialogList = objects;
Log.d("follower1",objects.toString());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext)
.inflate(R.layout.recipe_list, parent, false);
Log.d("follower","hi");
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final int i=position;
Log.d("follower","hi");
List dialog = DialogList.get(i);
Log.d("follower",dialog.toString());
String dishid = dialog.get(0).toString();
final String dishname = dialog.get(1).toString();
//byte[] dishimage = Base64.decode(dialog.get(2).toString(), Base64.DEFAULT);
String dishimage=dialog.get(2).toString();
String rating=dialog.get(3).toString();
holder.dishnames.setText(dishname);
holder.dishratings.setRating(Float.parseFloat(rating));
holder.mLoader.DisplayImage(dishimage.replaceAll(" ", "%20"),holder.dishimages);
}
#Override
public int getItemCount() {
return DialogList.size();
}}
Recyclerview
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(mLayoutManager);
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mealplanner.com.main.mealplanner.MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
</LinearLayout>
Row List
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp"
android:id="#+id/cv">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/dishimage"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginRight="6dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_margin="10dp"
android:src="#drawable/portraitlanding"
/>
<TextView
android:id="#+id/dishname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_toRightOf="#+id/dishimage"
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
android:text="Andrew" />
<RatingBar
android:id="#+id/dishrating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.1"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_below="#+id/dishname"
android:layout_toRightOf="#+id/dishimage"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
DishesAdapter constructor is being called. I am getting follower1 log along with the list of items.
So in my recyclerview, i have a CARDVIEW which consists of
ImageView, EditText and a button. and when i click the button, it will send the text from EditText to PARSE.com. My problem is that, i cannot link that text to it's ImageView
i want when i open the app, i can see the imageView and at the bottom there will be a description for the image. i want the editText to be dynamic. Can Someone guide me?
this is the XML layout:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card_view"
android:layout_margin="10dp"
android:layout_marginBottom="30dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
<ImageView
android:id="#+id/imageViewRecycler"
android:background="#color/border"
android:layout_width="match_parent"
android:layout_height="380dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:contentDescription="TODO"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/separator"
android:id="#+id/separator"
android:layout_above="#+id/firstLine"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey">
<EditText
android:id="#+id/firstLineText"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignWithParentIfMissing="true"
android:gravity="top"
android:background="#color/colorPrimary"
android:textSize="16sp"
android:textColor="#color/colorAccent"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/linearLayout"
android:layout_alignParentTop="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:id="#+id/firstLineButton"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
this is my recyclerview adapter:
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
LayoutInflater inflater;
List<imageDescription> data= Collections.emptyList();
private Context context;
ViewHolder holder;
public myAdapter(Context context, List<imageDescription> data){
this.context=context;
this.data=data;
}
#Override
public myAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.from(context).inflate(R.layout.custom_layout, parent, false);
holder= new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(myAdapter.ViewHolder holder, int position) {
imageDescription current= data.get(position);
Glide.with(context)
.load(current.url)
.centerCrop()
.override(500,500)
.placeholder(R.drawable.whitebackground)
.into(holder.imageViewRecycler);
holder.firstLineText.setText(current.firstLine);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageViewRecycler;
EditText firstLineText;
Button firstLineButton;
public ViewHolder(final View itemView) {
super(itemView);
imageViewRecycler=(ImageView)itemView.findViewById(R.id.imageViewRecycler);
firstLineText=(EditText)itemView.findViewById(R.id.firstLineText);
firstLineButton=(Button)itemView.findViewById(R.id.firstLineButton);
firstLineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject obj= new ParseObject("newMessage");
obj.put("imageDetail",firstLineText.getText());
obj.saveInBackground();
}
});
}
}
}
Ok, I know, probably someone has already asked this question. I have already found a possible solution for my problem on this link. But, for an unknown reason, that solution doesn't work for me.
I want to display a Toast when an item (a CardView) is clicked inside a RecyclerView. This Toast must show me the position of the item clicked.
When I click on a CardView I see the ripple - so that means that the card is clicked - but unfortunately, I don't see any toast. Can someone explain me why? Thanks in advance.
This is my Adapter:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.MyMainViewHolder> {
private List<Information> mainInfo;
MyDatabase myDatabase;
public MainRecyclerViewAdapter(List<Information> mainInfo) {
this.mainInfo = mainInfo;
}
public static class MyMainViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView, notesTextView, dateTextView, timeTextView;
ImageView imageView;
LinearLayout linearLayout;
public MyMainViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
imageView = (ImageView) itemView.findViewById(R.id.imgIcon);
titleTextView = (TextView) itemView.findViewById(R.id.title_TextView);
notesTextView = (TextView) itemView.findViewById(R.id.notes_TextView);
dateTextView = (TextView) itemView.findViewById(R.id.date_TextView);
timeTextView = (TextView) itemView.findViewById(R.id.time_TextView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
}
});
}
}
#Override
public MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_single_raw, parent, false);
MyMainViewHolder holder = new MyMainViewHolder(v);
return holder;
}
public void delete(int position){
mainInfo.remove(position);
notifyItemRemoved(position);
}
#Override
public void onBindViewHolder(final MyMainViewHolder holder, final int position) {
holder.imageView.setImageResource(mainInfo.get(position).getICON_ID());
holder.titleTextView.setText(mainInfo.get(position).getTITLE());
holder.notesTextView.setText(mainInfo.get(position).getNOTES());
holder.dateTextView.setText(mainInfo.get(position).getDATE());
holder.timeTextView.setText(mainInfo.get(position).getTIME());
}
#Override
public int getItemCount() {
return mainInfo.size();
}}
This is the layout file of my cardView:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view1"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="3dp"
android:layout_margin="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0.5dp"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_weight="8.5">
<TextView
android:id="#+id/title_TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="8dp"
android:layout_marginTop="5dp"
android:text="Title"
android:fontFamily="sans-serif-light"
android:textColor="#F57C00"
android:textSize="25sp"
/>
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:id="#+id/divider1"
android:layout_height="0.1dp"
android:layout_below="#+id/title_TextView"
android:background="#android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_weight="8.5">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#mipmap/ic_alarm_check_grey600_48dp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical">
<TextView
android:id="#+id/notes_TextView"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:text="notes"
android:textColor="#ff565656"
android:textSize="12sp" />
<TextView
android:id="#+id/date_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:text="date"
android:textColor="#ff565656"
android:textSize="12sp" />
<TextView
android:id="#+id/time_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:text="time"
android:textColor="#ff565656"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Problem solved! This is the working code:
public static class MyMainViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView, notesTextView, dateTextView, timeTextView;
ImageView imageView;
LinearLayout linearLayout;
public MyMainViewHolder(final View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
imageView = (ImageView) itemView.findViewById(R.id.imgIcon);
titleTextView = (TextView) itemView.findViewById(R.id.title_TextView);
notesTextView = (TextView) itemView.findViewById(R.id.notes_TextView);
dateTextView = (TextView) itemView.findViewById(R.id.date_TextView);
timeTextView = (TextView) itemView.findViewById(R.id.time_TextView);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
}
});
linearLayout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Position: " + Integer.toString(getAdapterPosition()), Toast.LENGTH_LONG).show();
}
});
}
}
So now, if i click on the linearLayout - so on the entire cardView - I see its position!
Credits: link
Add the following line in your view.xml
android:clickable=true
Remove the following line from first LinearLayout
android:clickable="true"
try this,
#Override
public MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_single_raw, parent, false);
MyMainViewHolder holder = new MyMainViewHolder(v);
return holder;
}
public void delete(int position){
mainInfo.remove(position);
notifyItemRemoved(position);
}
#Override
public void onBindViewHolder(final MyMainViewHolder holder, final int position) {
holder.imageView.setImageResource(mainInfo.get(position).getICON_ID());
holder.titleTextView.setText(mainInfo.get(position).getTITLE());
holder.notesTextView.setText(mainInfo.get(position).getNOTES());
holder.dateTextView.setText(mainInfo.get(position).getDATE());
holder.timeTextView.setText(mainInfo.get(position).getTIME());
holder.titleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,""+position,Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return mainInfo.size();
}}
Pay attention in cardview scope clickable is true
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_weight="1"
android:gravity="center_vertical|center|center_horizontal"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
card_view:contentPadding="10dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:clickable="true"
android:transitionGroup="false"
android:layout_margin="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/select_photo"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/select_name"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="bottom|center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txt_zoom_in"
/>`enter code here`
</android.support.v7.widget.CardView>
</LinearLayout>