I have two textviews and one imageview in my list_item_layout I want to perform some operations on each view when clicked.How do I do it?Also i have to perform delete action when long pressed on list item 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="100dp"
android:background="#drawable/rectangle_shadow"
android:orientation="horizontal">
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:fontFamily="monospace"
android:minHeight="30dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_margin="2dp"
android:id="#+id/t2"
android:layout_gravity="center_vertical"
android:textStyle="italic"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/word_list_popup_menu_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center_vertical"
android:alpha="0.5"
android:background="#null"
android:src="#drawable/ic_more_vert_black_24px" />
</LinearLayout>
Set OnClickListener for each of this views in getView() method of your adapter:
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.t1 = convertView.findViewById(R.id.t1);
viewHolder.t1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
viewHolder.t2 = convertView.findViewById(R.id.t2);
viewHolder.t2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
viewHolder.word_list_popup_menu_button = convertView.findViewById(R.id.word_list_popup_menu_button);
viewHolder.word_list_popup_menu_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView t1;
TextView t2;
ImageView word_list_popup_menu_button;
}
If you follow the guides outlined for RecyclerView, they'll explain how you bind to your layout and the views you're interested in using ViewHolders. This is because of the fundamental way RecyclerViews (and ListViews) work - they don't hold all their items at once, instead they re-use views to increase performance.
All that's in the guides though, that's where I and everyone else learned it from!
Related
I have created ListView in which each button id comprises of database_idx10+button_number
For example, I have set the id as 101 i.e. 10=database_id & 1=button_number
Now I've to setVisibility of id 101 to View.GONE which is a unique id generated by me.
How can I use this generated id to set visibility true.
I am retriving this id by calling a user define function "click" and in xml I have set android:onClick="click"
public void click(View view) {
final int position = view.getId();
int button_number = position % 10;
int id = position/10;
int layout_id=id*10+2;
if(button_number==1){
//have to set visibity true of layout_id
}
}
NOTE
I was able to set visibility from visible to gone button but not the
opposite.
If this button is Child of your view object you can do something like this
Button btn = (Button) view.findViewById(your_id);
btn.setVisibility(View.VISIBLE);
Or if not you have to findById this view in his parent layout or in activity.
This is not the solution of the question asked, but anyway to uniquely identify a button in your list item is easy. You don't have to set unique IDs yourself actually. The View reference of each button in your list is different already. You just have to identify them using the position in your getView method.
Here's a sample getView method of the adapter in your list.
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
// Here we can do changes to the convertView
Button b = convertView.findViewById(R.id.button);
// Set your visibility here
if(your_condition_is_true) b.setVisibility(View.VISIBLE);
else b.setVisibility(View.GONE);
return convertView;
}
try to do this
view.setVisibility(View.VISIBLE);
You need a boolean variable in the Details class to hold the visibility of the details section.
Your adapter code should be,
class MyAdapter extends BaseAdapter {
ArrayList<Details> list;
Context context;
public MyAdapter(Context context, RealmResults<Details> result2) {
this.context = context;
this.list = result2;
}
public class Holder {
TextView topic;
TextView detail;
LinearLayout details;
ImageButton send;
ImageButton edit;
ImageButton delete;
public Holder(View v) {
this.topic= (TextView) v.findViewById(R.id.TextView1);
this.detail= (TextView) v.findViewById(R.id.td);
this.details = (LinearLayout) v.findViewById(R.id.details);
this.send= (ImageButton) v.findViewById(R.id.imagebutton1);
this.edit= (ImageButton) v.findViewById(R.id.imagebutton3);
this.delete= (ImageButton) v.findViewById(R.id.imagebutton2);
}
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row=view;
Holder holder=null;
if(row == null) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(MainActivity.context.LAYOUT_INFLATER_SERVICE);
row= inflater.inflate(R.layout.layout,viewGroup,false);
holder = new Holder(row);
row.setTag(holder);
} else {
holder= (Holder) row.getTag();
}
final Details temp = (Details) getItem(i);
holder.topic.setText(temp.getTopic());
holder.detail.setText(temp.getTopic_details());
if(temp.isDetailButtonVisible()){
holder.details.setVisiblity(View.VISIBLE);
} else {
holder.details.setVisiblity(View.GONE);
}
holder.topic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
temp.setDetailButtonVisibility(false);
}
});
holder.send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder.detail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder.edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return row;
}
}
You layout should be,
<?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:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="6"
android:orientation="horizontal"
android:background="#5894CA"
android:clickable="true">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:id="#+id/TextView1"
android:text="Bank of baroda details...."
android:layout_marginStart="10dp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:layout_marginTop="10px"
android:textSize="20dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imagebutton1"
android:background="#drawable/share" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imagebutton3"
android:layout_marginStart="10dp"
android:background="#drawable/edit_text" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imagebutton2"
android:layout_marginStart="10dp"
android:background="#drawable/gnome_edit_delete" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="vertical"
android:clickable="true"
android:id="#+id/details"
android:visibility="invisible" >
<TextView
android:layout_width="match_parent"
android:layout_height="148dp"
android:background="#drawable/bg"
android:text="I have a LinearLayout that I've styled to look like a button, and it contains a few text/ImageView elements. I would like to make the whole LinearLayout act like a button, in particular to give it states that are defined in a so it has a different background when it is pressed..."
android:layout_margin="10dp"
android:id="#+id/td" />
</LinearLayout>
</LinearLayout>
I have implemented a custom listview with checkboxes in the single choice mode of the android listview.I want a functionality such that when I click on the checkbox the row corresponding to that check box must get added(or rather say the "Person" object shown in that row should get added to another list and be removed from that list) and on itemclick on that listview should take me to another screen in android.I tried the other ways but they specify that the checkbox needs to be focasable:false.Also I want the click listener to work on the check box only.Please any suggestions or help on these.Thanks in advance
This id the xml code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/projects_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:focusable="false" />
<TextView
android:id="#+id/project_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/projects_check"
android:layout_alignBottom="#+id/projects_check"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/projects_check"
android:layout_toRightOf="#+id/projects_check"
android:text="TextView" />
</RelativeLayout>
And this is my onitem click listener code..
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
Projects info = (Projects) parent.getItemAtPosition(position);
info.toggleChecked();
ProjectsHolder viewHolder = (ProjectsHolder) view.getTag();
viewHolder.getmChoiceSelect().setChecked(info.isChecked());
if (viewHolder.getmChoiceSelect().isChecked()) {
completed_projects.add(info);
mCompProjAdapter.notifyDataSetChanged();
projects_list.remove(info);
mProjAdapter.notifyDataSetChanged();
}
}
To manage click onto the row itself, you can just write listener for listView itself.
This is how i manage checkbox click.
class CustomAdapter extends ArrayAdapter<BeanClass> {
private ArrayList<BeanClass> items;
public FieldPlannedAdapter(Context context,
ArrayList<BeanClass> items) {
super(context, R.layout.custom_list_row, items);
this.items = items;
}
public class ViewHolder {
protected CheckBox checkbox;
protected TextView name;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list_row,
null);
viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.checkIsMissed);
viewHolder.name = (TextView) convertView
.findViewById(R.id.txtViewMTPname);
convertView.setTag(viewHolder);
viewHolder.checkbox
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final BeanClass item = (BeanClass) cb
.getTag();
item.setSelected(cb.isChecked());
if (cb.isChecked()) {
// DO SOMETHING
} else {
}
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
BeanClass item = items.get(position);
viewHolder.name.setTag(item);
viewHolder.checkbox.setTag(item);
viewHolder.name.setText(item.getName());
viewHolder.checkbox.setChecked(item.isSelected());
if (viewHolder.checkbox.isChecked()) {
// DO SOMETHING
} else {
// DO SOMETHING
}
return convertView;
}
}
Custom row example :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/card_ui"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/txtViewMTPname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Name"
android:textColor="#drawable/text_selector"
android:textSize="14sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtViewMtpDarCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="dgdgdgdg"
android:textColor="#drawable/text_selector"
android:textSize="16sp"
android:textStyle="italic" />
<CheckBox
android:id="#+id/checkIsMissed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="Missed" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
I have a custom Listview whose adapter extends BaseAdapter class. Inside every row in listview there is an invisible EditText. it get visible to only particular row which has been click and on rest of the row it remains invisible.Problem comes when I touch edittext to get keyboard, as soon as keyboard appears, listview gets recycle and edittext again become invisible.
I am looking for a solution in which either keyboard comes with focus on edittext as soon as that row is selected or edittext does not disapear when keyboard is poped-up.Following is the adapter I am using:
this is my adapter xml file
saved_option_adapter_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/lineItem"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<TextView
android:id="#+id/textLine"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="15sp"
android:layout_marginBottom="5sp"
android:textSize="14sp"
android:paddingTop="-5dp"
android:layout_width="wrap_content"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<EditText
android:id="#+id/edittext_qty"
android:visibility="invisible"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"
android:maxLength="4"
android:textSize="14sp"
android:inputType="numberPassword"
android:focusable="true"
android:textColor="#android:color/black"
android:hint="cvv"/>
</LinearLayout>
</RelativeLayout>
and this is my getView Method:
public View getView(final int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.saved_options_adapter_content, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
holder.Edittext = (EditText) convertView.findViewById(R.id.edittext_qty);
editTextList.add(holder.Edittext);
convertView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(final View v)
{
selected_Txt="";
if(view==null || view!=v){
v.post(new Runnable() {
#Override
public void run() {
view=v;
ViewHolder holder = ((ViewHolder)v.getTag());
holder.Edittext.setVisibility(View.VISIBLE);
holder.Edittext.requestFocus();
holder.Edittext.setCursorVisible(true);
holder.Edittext.setFocusable(true);
if(selectedHolder != null ){
selectedHolder.Edittext.setVisibility(View.INVISIBLE);
}
selectedHolder = holder;
}
});
}
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
((ViewHolder)convertView.getTag()).Edittext.setTag(title.get(position));
}
convertView.setTag(holder);
return convertView;
}
I don't see listview in your code. Maybe ArrayAdapter can help you: http://developer.android.com/reference/android/widget/ArrayAdapter.html
I use StickyGridHeader library with a custom header view like image below. The problem is this header only work when it's sticky on the top. On normal state, it show width size incorrectly, I think it caused by measuring header size from StickyGridHeadersGridView
Here is my header layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/tvHeaderName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/tvHeaderDate"
android:minWidth="250dp"
android:paddingLeft="10dp"
android:text="aaa"
android:textColor="#color/white"/>
<TextView
android:id="#+id/tvHeaderDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:text="aaa"
android:textColor="#color/white" />
<!--gray line-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tvHeaderDate"
android:layout_marginTop="10dp"
android:background="#color/gray_line" />
</RelativeLayout>
And my adapter:
public class GalleryAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter {
// initialize image item ...
#Override
public int getCountForHeader(int position) {
return getHeaderItem(position).getImages().size();
}
public GalleryItem getHeaderItem(int position) {
return parents.get(position);
}
#Override
public View getHeaderView(int position, View view, ViewGroup parent) {
HeaderViewHolder viewHolder;
if (view == null) {
view = mInflater.inflate(R.layout.item_header_gallery, parent, false);
viewHolder = new HeaderViewHolder();
viewHolder.tvHeaderName = (TextView) view.findViewById(R.id.tvHeaderName);
viewHolder.tvHeaderDate = (TextView) view.findViewById(R.id.tvHeaderDate);
view.setTag(viewHolder);
} else {
viewHolder = (HeaderViewHolder) view.getTag();
}
viewHolder.tvHeaderName.setText(getHeaderItem(position).getGroupName());
viewHolder.tvHeaderDate.setText(getHeaderItem(position).getTime());
return view;
}
#Override
public int getNumHeaders() {
return parents.size();
}
private static class HeaderViewHolder {
public TextView tvHeaderName;
public TextView tvHeaderDate;
}
Does anyone know how to solve this?
I think you should try what I say in this other question.
Basically, you have to replace your stickygridheaders.jar or your dependency in gradle with this other jar stickygridheaders.jar
When I click the button in the ListView, I see the toast once. Then further clicks don't show anything when I click away from the button and in an empty space in the ListView, all the button click events that were not responding earlier appear at once (many toasts pop up one after another). Any idea how to fix this?
class Adapter extends ArrayAdapter
{
HashMap<Integer, View> rowViews = new HashMap<Integer, View>();
public Adapter(Context context) {
super(context, R.layout.manage_member_row, members);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View rowView;
if(rowViews.containsKey(position)) {
rowView = rowViews.get(position);
} else {
rowView = inflater.inflate(R.layout.manage_member_row, parent, false);
((TextView) rowView.findViewById(R.id.manage_member_row_name)).setText(members.get(position).name);
rowViews.put(position, rowView);
}
((Button) rowView.findViewById(R.id.manage_member_row_delete)).setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "btnclick", Toast.LENGTH_SHORT).show();
}
});
Log.i("CRC", "called");
return rowView;
}
//manage_member_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView
android:id="#+id/manage_member_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="18sp"
android:textColor="#53585E"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp" />
<Button
android:id="#+id/manage_member_row_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:paddingLeft="8dip"
android:clickable="true"
android:focusable="true"
android:textSize="18sp"
android:textColor="#4E4E4E"
android:background="#EAEAEA"
android:paddingRight="8dip"
android:textStyle="bold"
android:text="REMOVE"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip" />
</RelativeLayout>
//fragment_manage_members.xml
<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"
tools:context="activities.ManageMembersActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="0dip"
android:dividerHeight="2.0dip"
android:divider="#EEEDF3"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</RelativeLayout>
The problem is that sometimes position from getView are not synchronized have that problem before..
Also note that you use a HashMap<Integer, View> just to check if the view is already there that cost a lot of memory and will cause OutOfMemoryException..
so instead of putting it in a hashMap just create a ViewHolder for all your views. and add the object of the ViewHolder to the tag of the View..
example:
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if(view == null)
{
viewHolder = new ViewHolder();
view = mInflater.inflate(R.layout.chat_online_user_list_view_item, null);
viewHolder.userName = (TextView) view.findViewById(R.id.chat_online_user_name);
view.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) view.getTag();
//DO THE ON CLICK LISTENER HERE
return view;
}
private class ViewHolder
{
private TextView userName;
}