ImageButton in RecyclerView issue - android

I'm using an ImageButton in a RecyclerView. I want to handle a ClickListener in the application, this is the code of the ViewHolder.
I'm using FirebaseUI so the adapter is a FirebaseRecyclerAdapter.
The RecyclerView is displayed correctly, but it doesn't do anything when I click any ImageButton.
What's wrong with it?
This is the code of viewHolder:
public class PicturesHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
Context context;
ImageButton imageButtonView;
TextView textIDView;
TextView latitudeView;
TextView longitudeView;
TextView dateView;
public PicturesHolder(View itemView) {
super(itemView);
this.context = itemView.getContext();
imageButtonView = (ImageButton) itemView.findViewById(R.id.image);
textIDView = (TextView)itemView.findViewById(R.id.texto);
latitudeView=(TextView) itemView.findViewById(R.id.latitude);
longitudeView = (TextView) itemView.findViewById(R.id.longitude);
dateView = (TextView) itemView.findViewById(R.id.dateView);
itemView.setOnClickListener(this);
imageButtonView.setOnClickListener(this);
}
public void setImage(String url) {
Glide.with(context)
.load(url)
.into(imageButtonView);
}
public void setTextIDView(String textID) {
textIDView.setText(textID);
}
public void setLatitude(double latitude)
{
latitudeView.setText(String.valueOf(latitude));
}
public void setLongitude(double longitude)
{
longitudeView.setText(String.valueOf(longitude));
}
public void setDateView(String date)
{
dateView.setText(date);
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Item Pressed = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT);
}
}
I also tried to use the listener inside populateViewHolder() like this:
public void onAttachRecyclerView() {
//Query lastHundred = mURLReference.limitToLast(100);
Query mQuery = mURLReference.orderByChild("date").limitToLast(100);
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Pictures, PicturesHolder>(
Pictures.class,R.layout.item_row,PicturesHolder.class, mQuery
) {
#Override
protected void populateViewHolder(PicturesHolder viewHolder, Pictures model, final int position) {
viewHolder.setTextIDView(model.getPictureID());
viewHolder.setImage(model.getDownloadURL());
viewHolder.setLatitude(model.getLatitude());
viewHolder.setLongitude(model.getLongitude());
viewHolder.setDateView(model.getDate());
viewHolder.imageButtonView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Toast.makeText(view.getContext(), "You clicked on " + position, Toast.LENGTH_SHORT);
}
});
}
};
mImagesRV.setAdapter(mRecyclerViewAdapter);
}
Finally, this is the XML of the item view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="0.5">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/texto"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:src="#color/tw__composer_deep_gray"
android:layout_gravity="center"
android:padding="20dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_height="300dp"
android:layout_width="300dp"
android:id="#+id/image"
android:layout_weight="0.39" />
<TextView
android:text="Coordenadas:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textAppearance="#style/TextAppearance.AppCompat.Body2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/latitude"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/longitude"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/espacioBlanco"
android:text=" "/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dateView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/orderNumber"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
</android.support.v7.widget.CardView>
</LinearLayout>
Greetings!

I believe everything works as it should, you just forgot to call show() on Toast object
Toast.makeText(..).show();

Related

Why UI not displaying properly?

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!

Firebase RecyclerView expandable view not working (Do not wish to use any 3rd Party Library)

I have used a FirebaseRecyclerAdapter in my application for displaying a series of events. When i select an event form the recycler view it must expand the recycler view item and show me the details and other items of that event such as dates, description and others. I used a concept from Google I/O 2016 but could't land anywhere as my event is not showing only. I want only a specific set of items to be displayed when expanded.
EventView.java
public class EventView extends AppCompatActivity {
DatabaseReference databaseReference;
RecyclerView recyclerView;
int mExpandedPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_view);
databaseReference = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");
recyclerView = (RecyclerView) findViewById(R.id.request_EventList);
//Avoid unnecessary layout passes by setting setHasFixedSize to true
recyclerView.setHasFixedSize(true);
//Select the type of layout manager you would use for your recyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
final String event_cat = getIntent().getStringExtra("Category");
FirebaseRecyclerAdapter<Event, RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, RequestViewHolder>(
Event.class,
R.layout.events_list_row,
RequestViewHolder.class,
databaseReference.orderByChild("category").equalTo(event_cat)
) {
#Override
protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setCategory(model.getCategory());
viewHolder.setLocation(model.getLocation());
viewHolder.setPrice(model.getPrice());
viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());
viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(EventView.this, "Image Selected", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onBindViewHolder(RequestViewHolder viewHolder, final int position) {
super.onBindViewHolder(viewHolder, position);
final boolean isExpanded = position == mExpandedPosition;
viewHolder.mView.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.mView.setActivated(isExpanded);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
TransitionManager.beginDelayedTransition(recyclerView);
notifyDataSetChanged();
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class RequestViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView imageButton;
public RequestViewHolder(View itemView) {
super(itemView);
mView = itemView;
imageButton = (ImageView) mView.findViewById(R.id.request_image);
}
public void setTitle(String title) {
TextView a_title = (TextView) mView.findViewById(R.id.request_title);
a_title.setText(title);
}
public void setDesc(String desc) {
TextView a_desc = (TextView) mView.findViewById(R.id.request_desc);
a_desc.setText(desc);
}
public void setLocation(String location) {
TextView a_desc = (TextView) mView.findViewById(R.id.request_location);
a_desc.setText(location);
}
public void setCategory(String category) {
TextView a_category = (TextView) mView.findViewById(R.id.request_category);
a_category.setText(category);
}
public void setPrice(String price) {
TextView a_price = (TextView) mView.findViewById(R.id.request_price);
a_price.setText(price);
}
public void setImageUrl(Context ctx, String imageUrl) {
ImageView a_image = (ImageView) mView.findViewById(R.id.request_image);
Picasso.with(ctx).load(imageUrl).into(a_image);
}
}
}
events_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/request_category"
android:textStyle="bold"
android:textSize="15dp"
android:padding="5dp"
android:layout_gravity="left"
android:text="Category will come here"/>
<TextView
android:layout_width="191dp"
android:layout_height="wrap_content"
android:id="#+id/request_location"
android:textSize="15dp"
android:gravity="right"
android:layout_gravity="right"
android:text="Location will come here"
android:padding="5dp"
android:textStyle="bold"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/request_title"
android:layout_marginTop="300dp"
android:text="Title will come here "
android:padding="10dp"
android:textStyle="bold"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/request_desc"
android:text="Desc will come here "
android:layout_marginTop="250dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="275dp"
android:id="#+id/request_image"
android:src="#drawable/abc_btn_check_material"
android:adjustViewBounds="true"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Price will come here"
android:id="#+id/request_price"
android:layout_marginTop="270dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"/>
</android.support.v7.widget.CardView>
</LinearLayout>
activity_event_view.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.admin.college20.EventView">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/request_EventList"
android:clickable="true"/>
</RelativeLayout>

Button doesn't get focus in Recyclerview item

am trying to click button in Recyclerview item but it not receive onclick event.but it worked for double tap to respond onclick listener
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginRight="5dp"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:id="#+id/calllog_contact_img"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calllog_contact_name"
android:layout_marginBottom="2dp"
android:text="Pounkumar Purushothaman"
android:textSize="18dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="20dp"
android:focusableInTouchMode="false"
android:src="#android:drawable/sym_call_incoming"
android:layout_marginBottom="10dp"
android:id="#+id/calllog_call_type"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calllog_no"
android:text="+919043974134"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20"
android:layout_marginRight="20dp"
android:id="#+id/calllog_call_duration"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20"
android:id="#+id/calllog_call_timing"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/llExpandArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:id="#+id/call_txt"
android:text="Call" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:paddingLeft="15dp"
android:id="#+id/block_txt"
android:text="Msg" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
am trying to click button in Recyclerview item but it not receive onclick event.but it worked for double tap to respond onclick listener
Adapter:
public class calllog_customadapter extends RecyclerView.Adapter<calllog_customadapter.RecordHolder> {
private List<calllog_item> calllog_items;
Context c;
RecyclerView list;
private int expandedPosition = -1;
#Override
public RecordHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.calllog_item, parent, false);
return new RecordHolder(itemView);
}
calllog_customadapter(Context context, RecyclerView list, final List<calllog_item> calllog_item){
this.c=context;
this.list=list;
this.calllog_items=calllog_item;
list.addOnItemTouchListener(new RecyclerItemClickListener(c, list ,new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Button call= (Button) view.findViewById(R.id.call_txt);
Button block= (Button) view.findViewById(R.id.block_txt);
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, "call", Toast.LENGTH_SHORT).show();
}
});
block.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c, "blockbbb222", Toast.LENGTH_SHORT).show();
}
});
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
expandedPosition = position;
notifyItemChanged(expandedPosition);
}
#Override
public void onLongItemClick(View view, int position) {
// do whatever
}
}));
}
#Override
public void onBindViewHolder(RecordHolder holder, int position) {
calllog_item list=calllog_items.get(position);
holder.contact_name.setText(list.getContact_name());
holder.contact_img.setImageBitmap(list.getContact_img());
holder.call_type.setImageBitmap(list.getCall_type());
holder.call_duration.setText(list.getCall_duration());
holder.call_timing.setText(list.call_timing);
holder.contact_no.setText(list.getCall_no());
if (position == expandedPosition) {
holder.llExpandArea.setVisibility(View.VISIBLE);
} else {
holder.llExpandArea.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return calllog_items.size();
}
public class RecordHolder extends RecyclerView.ViewHolder {
TextView contact_name,call_timing,call_duration,contact_no;
ImageView contact_img,call_type;
LinearLayout llExpandArea;
public RecordHolder(View itemView) {
super(itemView);
contact_img= (ImageView) itemView.findViewById(R.id.calllog_contact_img);
call_type= (ImageView) itemView.findViewById(R.id.calllog_call_type);
call_duration= (TextView) itemView.findViewById(R.id.calllog_call_duration);
call_timing= (TextView) itemView.findViewById(R.id.calllog_call_timing);
contact_name= (TextView) itemView.findViewById(R.id.calllog_contact_name);
contact_no= (TextView) itemView.findViewById(R.id.calllog_no);
llExpandArea=(LinearLayout)itemView.findViewById(R.id.llExpandArea);
}
}
}
Did you try to set it as clicable and focusable?
XML
android:focusable="true"
android:clickable="true"
Java
button.setClickable("true");
button.setFocusable("true");
If nothing changes, try to put a log call in your onClick method (maybe there's something bad in the code), or in OnTouch.

Next item appearing after scolling in RecyclerView

I have created RecyclerView. When it renders on screen it shows only one item per screen. I can see next item when I scroll down RecyclerView. Here is my code
public class LeaveStatusViewAdapter extends RecyclerView
.Adapter<LeaveStatusViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<Employee> mDataset;
private static MyClickListener myClickListener;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView startDate;
TextView endDate;
TextView leaveType;
TextView leaveDays;
TextView leaveReason;
TextView leaveStatus;
public DataObjectHolder(View itemView) {
super(itemView);
startDate = (TextView) itemView.findViewById(R.id.textView_StartDate);
endDate = (TextView) itemView.findViewById(R.id.textView_EndDate);
leaveType = (TextView) itemView.findViewById(R.id.textView_LeaveType);
leaveDays = (TextView) itemView.findViewById(R.id.textView_LeaveDays);
leaveReason = (TextView) itemView.findViewById(R.id.textView_LeaveReason);
leaveStatus = (TextView) itemView.findViewById(R.id.textView_LeaveStatus);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public LeaveStatusViewAdapter(ArrayList<Employee> myDataset) {
mDataset = myDataset;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_row, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.startDate.setText(mDataset.get(position).getLeaveStartDate());
holder.endDate.setText(mDataset.get(position).getLeavesEndDate());
holder.leaveType.setText(mDataset.get(position).getLeaveType());
holder.leaveDays.setText(mDataset.get(position).getLeaveDays());
holder.leaveReason.setText(mDataset.get(position).getLeaveReason());
holder.leaveStatus.setText(mDataset.get(position).getLeaveStatus());
}
public void addItem(Employee dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
Following is activity class
public class LeaveStatusActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private static String LOG_TAG = "CardViewActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave_status);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new LeaveStatusViewAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
}
#Override
protected void onResume() {
super.onResume();
((LeaveStatusViewAdapter) mAdapter).setOnItemClickListener(new LeaveStatusViewAdapter
.MyClickListener() {
#Override
public void onItemClick(int position, View v) {
Log.i(LOG_TAG, " Clicked on Item " + position);
}
});
}
private ArrayList<Employee> getDataSet() {
ArrayList results = new ArrayList<Employee>();
for (int index = 0; index < 3; index++) {
Employee obj = new Employee(" : 15/07/2016",
" : 20/07/2016"," : EL: Earned Leave"," : 5"," : Not feeeling well"," : Pending");
results.add(index, obj);
}
return results;
}
}
Following is Layout files
<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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.leave.mcgm.mcgmleavemanagement.LeaveStatusActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</LinearLayout>
Following custom view for each item in RecyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lbl_StartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:text="Start Date"/>
<TextView
android:id="#+id/textView_StartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/lbl_StartDate" />
<TextView
android:id="#+id/lbl_EndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Date"
android:textStyle="bold"
android:layout_marginStart="24dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView_LeaveReason" />
<TextView
android:id="#+id/textView_EndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/lbl_EndDate"
android:layout_alignBottom="#+id/lbl_EndDate"
android:layout_toEndOf="#+id/lbl_EndDate" />
<TextView
android:id="#+id/lbl_LeaveType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="LeaveType"
android:layout_below="#+id/lbl_EndDate"
android:layout_alignParentStart="true"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView_LeaveType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:layout_alignBaseline="#+id/lbl_LeaveType"
android:layout_alignBottom="#+id/lbl_LeaveType"
android:layout_toEndOf="#+id/lbl_LeaveType" />
<TextView
android:id="#+id/lbl_LeaveDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Days"
android:singleLine="true"
android:layout_below="#+id/lbl_EndDate"
android:layout_alignStart="#+id/lbl_EndDate"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView_LeaveDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:layout_alignBaseline="#+id/lbl_LeaveDays"
android:layout_alignBottom="#+id/lbl_LeaveDays"
android:layout_toEndOf="#+id/lbl_LeaveDays" />
<TextView
android:id="#+id/lbl_LeaveReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Reason"
android:singleLine="true"
android:layout_alignTop="#+id/textView_LeaveReason"
android:layout_alignParentStart="true"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView_LeaveReason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:layout_below="#+id/textView_LeaveDays"
android:layout_toEndOf="#+id/lbl_LeaveReason" />
<TextView
android:id="#+id/lbl_LeaveStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Status"
android:singleLine="true"
android:layout_alignBaseline="#+id/textView_LeaveStatus"
android:layout_alignBottom="#+id/textView_LeaveStatus"
android:layout_alignParentStart="true"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView_LeaveStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:layout_below="#+id/lbl_LeaveReason"
android:layout_toEndOf="#+id/lbl_LeaveReason"
android:textAllCaps="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Am I missing something here??
You should change the view of each element of the list to android:layout_height="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
.....
</LinearLayout>
When you use android:layout_height="match_parent" every item of the list will try to use the maximum space possible in the screen

No toast shown when item clicked RecyclerView

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>

Categories

Resources