setVisibility true of dynamic id of a layout - android

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>

Related

How to trigger radioButton to checked if I pressed on Linear layout

How can I make the radio button get selected if I pressed on other place ( not the radio button ) but on image, text view and etc inside 1 Linear layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/card_balance"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="#color/dc_white" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp">
<RadioButton
android:id="#+id/radio_button_payment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>

</RelativeLayout>
And here is the adapater: PaymentAdapter.java
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View allView = inflater.inflate(R.layout.item_choose_payment, parent, false);
TextView txt1 = (TextView) allView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) allView.findViewById(R.id.card_balance);
final RadioButton r = (RadioButton) allView.findViewById(R.id.radio_button_payment);
LinearLayout layout_card = (LinearLayout) allView.findViewById(R.id.layout_1);
r.setChecked(position == selectedPosition);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});
}
Right now the condition is I can pressed only at radio button.
I've already tried to add onClickListener to linear layout, and yes I can pressed on the linear layout however the Radio button not select only 1 but all selected.
this code for linearLayout onClick
layout_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
r.setChecked(position == selectedPosition);
r.setTag(position);
if (r.isChecked()) {
r.setChecked(true);
r.setSelected(true);
} else if (!r.isChecked()) {
r.setChecked(false);
r.setSelected(false);
}
}
});
Sorry for my bad explanation, any help is very grateful for me.
Thank you
inside your getView(...), write
layout_card.setTag(position);
and change click listener as
layout_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = Integer.parseInt(v.getTag().toString());
notifyDataSetChange();
}
});
remove below code, if not needed
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});

Accessing all views of list_item in listview android

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!

how to hide view appear on top of previous selected row on click of another row inside GridView in android

I have a Fragment with custom Adapter extending BaseAdapter that manages a GridView.
I want to show images on each row of the GridView and onClick of each cell, I want to open a translucent view on top of that particular image which will show image details.
Problem is that when I click on the image, a translucent view opened on top of the image but when I click on next image then a translucent view opened on previous images is not hiding,it still appears over the first images..
See the following code
My Fragment
public class ProductFragment extends Fragment {
String ss = null;
GridView gridView;
ArrayList<ProductParameterBO> productlist;
GridViewCustomAdapter gridViewCustomAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState!=null){
View rootView = inflater.inflate(R.layout.empty_catalog_item,null);
TextView configure = (TextView)rootView.findViewById(R.id.text_conf);
configure.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),SplitViewActivity.class);
intent.putExtra("firstTab","1stTabs");
startActivity(intent);
}
});
TextView text_here = (TextView)rootView.findViewById(R.id.text_here);
text_here.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
FloatingActionButton fab = (FloatingActionButton)rootView.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), EditUploadActivity.class);
startActivity(intent);
}
});
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_product,null);
gridView =(GridView)rootView.findViewById(R.id.gridView);
productlist = new ArrayList<ProductParameterBO>();
productlist.add(new ProductParameterBO("Type","Numeric","yes","yes"));
productlist.add(new ProductParameterBO("Typess","Numericss","yesss","yesss"));
productlist.add(new ProductParameterBO("Typess","Numericss","yesss","yesss"));
productlist.add(new ProductParameterBO("Typess","Numericss","yesss","yesss"));
gridViewCustomAdapter = new GridViewCustomAdapter(getActivity(),productlist);
gridView.setAdapter(gridViewCustomAdapter);
gridViewCustomAdapter.notifyDataSetChanged();
return rootView;
}
// return inflater.inflate(R.layout.fragment_product,null);
}
}
My Adater
public class GridViewCustomAdapter extends BaseAdapter {
private List<ProductParameterBO> availList;
private LayoutInflater inflater;
Context context;
public GridViewCustomAdapter(Context ctx,List<ProductParameterBO> list){
this.context = ctx;
this.availList = list;
}
#Override
public int getCount() {
return availList.size();
}
#Override
public Object getItem(int position) {
return availList.get(position);
}
#Override
public long getItemId(int position) {
ProductParameterBO c = availList.get(position);
// long id = c.getTimeId();
return 0;
}
#Override
public View getView(int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.img_ovrly);
row.setTag(holder);
} else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
holder.myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rows = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rows = inflater.inflate(R.layout.image_overlay,parent,false);
// holder.rl = (RelativeLayout)rows.findViewById(R.id.img_ovrly);
holder.rl.setVisibility(View.VISIBLE);
}
});
return row;
}
static class TeeTimeHolder {
ImageView myImage;
TextView name,edit;
RelativeLayout rl;
//TextView descriptions;
/* TextView coursefee;
TextView viewdetils;*/
}
}
My XML layout grid_row.xml
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#drawable/toi"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Name"
android:id="#+id/textView47"
android:layout_below="#+id/imageView10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:id="#+id/textView49"
android:layout_below="#+id/textView47"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/img_ovrly"
android:background="#color/opacity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mail"
android:layout_marginTop="10dp"
android:id="#+id/image_i"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title :"
android:id="#+id/text_ttle"
android:layout_below="#+id/image_i"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_ttle"
android:text="hmmmm"
android:layout_below="#+id/image_i"
android:layout_marginTop="20dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dimensions :"
android:id="#+id/text_dimen"
android:layout_below="#+id/text_ttle"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_dimen"
android:text="32*23"
android:layout_below="#+id/text_ttle"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price :"
android:id="#+id/text_prce"
android:layout_below="#+id/text_dimen"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_prce"
android:text="32*23"
android:layout_below="#+id/text_dimen"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color :"
android:id="#+id/text_clr"
android:layout_below="#+id/text_prce"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_clr"
android:text="red,white"
android:layout_below="#+id/text_prce"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min. Quantity :"
android:id="#+id/text_minq"
android:layout_below="#+id/text_clr"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_minq"
android:text="1000"
android:layout_below="#+id/text_clr"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type :"
android:id="#+id/text_typ"
android:layout_below="#+id/text_minq"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_typ"
android:text="creamic"
android:layout_below="#+id/text_minq"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_flp"
android:background="#drawable/flip"
android:layout_below="#+id/text_typ"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</RelativeLayout>
Am I missing something? kindly help me to resolve this issue. I want a screen something like that. Am I doing right to open tranlucent screen on top of image and how to hide or make visivility gone on click of other images inside my Adater class.Any help would be appreciated in advanced
You can do one of two things.
1. Use a flag
NOTE this code is untested.
You can create an encapsulated flag in your ProductParameterBO object to determine the visibility of your overlay view. Something like the following
boolean mInformationViewVisible = false;
public void setInformationViewVisible(boolean visible) {
mInformationViewVisible = visible;
}
public boolean isInformationViewVisible() {
return mInformationViewVisible;
}
Then use that in your getView method to set the visibility of the view.
#Override
public View getView(int position,View convertView,final ViewGroup parent) {
...
holder.myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int itemPosition = 0; itemPosition < availList.size(); itemPosition++) {
if(itemPosition != position){
availList.get(itemPosition)
.setInformationViewVisible(false);
}
availList.get(itemPosition).setInformationViewVisible(true);
}
});
if (currentItem.isInformationViewVisible()) {
holder.rl.setVisibility(View.VISIBLE);
} else {
holder.rl.setVisibility(View.GONE);
}
...
}
2. Switch to RecyclerView
The second and better option is to switch to RecyclerView. This view is specifically built for individual interaction with items in your Adapter. It gives you more power over the View in terms of animations and removal/adittion of items to the adapter. You can read more on it here and here.
It is very likely that the app in the image you attached is using RecyclerView to manage their content. The code to manage the selected info view will be very similar in RecyclerView to what I posted for the ListView, the only difference would be that you would pass the OnClickListener to the RecyclerView.ViewHolder and set it to the info view in there in stead of in the getView() method. I highly recommend using this approach as it will benefit you in the future of your app.
In ProductParameterBO, you can keep a boolean member variable, and in getView() and if one image is clicked, you can set the boolean variables from that list as false, except the one you clicked. If the variable is true, it will display the image details, and if the variable is false, you can set the visibillty of the translucent view as Invisible of that particular position.
#Override
public View getView(int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.img_ovrly);
row.setTag(holder);
}
else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
holder.myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rows = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rows = inflater.inflate(R.layout.image_overlay,parent,false);
// holder.rl = (RelativeLayout)rows.findViewById(R.id.img_ovrly);
// holder.rl.setVisibility(View.VISIBLE);
for(int i=0;i<availList.size();i++){
if(i!=position){
availList.get(i).flag=false;
}
}
availList.get(position).flag=true;
}
});
if(availList.get(i).flag){
holder.rl.setVisibility(View.VISIBLE);
}else{
holder.rl.setVisibility(View.INVISIBLE);
}
return row;
}

Listview with button OnItemClickListener is not calling

I create a custom listview with a TextView and 2 ImageButton. But onitemclicklistner is not calling. Here is my code
police_station_list = (ListView) findViewById(R.id.police_station_listView_xml);
adapter = new ArrayAdapter<String>(this, R.layout.coustome_listview, R.id.district_listview_xml1, police_station_name);
PSAdapter = new Police_Station_Adapter(this);
police_station_list.setAdapter(PSAdapter);
police_station_list.setOnItemClickListener(PSAdapter);
Here is my custom ArrayAdapter class:
public class Police_Station_Adapter extends ArrayAdapter<String> implements OnItemClickListener{
Context context;
public Police_Station_Adapter(Context context) {
//super(context, R.layout.police_station_listview, R.id.police_station_listView_textView, police_station_name);
super(context, R.layout.police_station_listview, police_station_name);
this.context = context;
}
private class ViewHolder {
TextView tv;
ImageButton mobile, phone;
public ViewHolder(View v) {
tv = (TextView) v.findViewById(R.id.police_station_listView_textView);
mobile = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_mobile);
phone = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_phone);
}
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.police_station_listview, parent, false);
holder = new ViewHolder(v);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
holder.tv.setText(police_station_name[position]);
holder.mobile.setTag(position);
holder.mobile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Message(context, police_station_name[(Integer) v.getTag()]+" button", 500);
}
});
return v;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new Message(context, "Item Clicked", 500); //Toast message
}
}
Here is the custom ListView xml:
<?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="40dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/police_station_listView_textView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="8"
android:textSize="16sp"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_mobile"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="15dp"
android:background="#drawable/mobile_icon" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_phone"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="#drawable/telephone_icon" />
</LinearLayout>
it's seems like i can add onClikcListner for textview but it doesn't show any pressed animation. It may not be the appropriate solution. I need the proper way to setOnItemClickListner.
You need to set OnItemClickListener in your Activity. then change this
police_station_list.setOnItemClickListener(PSAdapter); to
police_station_list.setOnItemClickListener(this);
Remove implementation of OnItemClickListener from adapter.
Check this tutorial.
add this attribute to imageButton:
android:focusable="false"

Toggle a "ToggleButton" when clicking anywhere outside it

I have a list (ExpandableListView), and every item of the list has a ToggleButton that shows/hides the child items when toggled (the child is always 1 and it's a sort of toolbar with two buttons). Thanks to this tutorial I could set a custom button instead of the expandablelistview's indicator and I made it so that I can do something else than showing the toolbar when clicking on a list item. Also, I used the answer to this question to automatically close an open toolbar when opening another one.
So, I need to collapse the currently expanded toolbar when touching anything on the screen that is not its ToggleButton (I would need to collapse it even when clicking on one of the toolbar's buttons, as long as its own "onClick" is sent anyway).
here's an image of the app:
Here's the ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
Here's the xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginRight="15dp"
android:background="#drawable/toggle_button_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
The child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
</LinearLayout>
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
private final Integer[] imageId;
private int lastExpandedGroupPosition;
public CustomList(Activity context, List<Item> names, Integer[] imageId) {
this.context = context;
this.names = names;
this.imageId = imageId;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_single, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.text);
TextView txtData = (TextView) convertView.findViewById(R.id.counter);
ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
final ExpandableListView list = (ExpandableListView) parent.findViewById(R.id.normalList);
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
}
});
} else if(/*other conditions*/) {
imageView.setImageResource(imageId[2]);
toggle.setVisibility(View.GONE);
} else {
imageView.setImageResource(imageId[1]);
toggle.setVisibility(View.GONE);
}
return convertView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
}
TextView details = (TextView) convertView.findViewById(R.id.details);
TextView send = (TextView) convertView.findViewById(R.id.send);
details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
}
});
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something else
}
});
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
ToggleButton button = (ToggleButton) ((ExpandableListView) context
.findViewById(R.id.normalList)).getChildAt(lastExpandedGroupPosition)
.findViewById(R.id.toggle);
if(groupPosition != lastExpandedGroupPosition && button.isChecked()) {
button.performClick();
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
}
And finally the important parts in the activity:
lv = (ExpandableListView)findViewById(R.id.normalList);
lv.setGroupIndicator(null);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
//do some things
return true; //this tells the list that it mustn't show the child items
}
});
adapter = new CustomList(myActivity.this, list, icons);
lv.setAdapter(adapter);
P.S.: The code to hide the open toolbars doesn't work properly, but I really can't understand why. But this is another story...
--- EDIT ---
About the P.S. above: now I know why that happens.
Your list looks like ExpandableListView. Did you try to use it? It has build it second level rows, expanding, hiding, etc. Simply add your pdf rows as group headers and then add tool rows as child rows. Then you can handle row clicks to show and hide rows you would like to show/hide. See: http://developer.android.com/reference/android/widget/ExpandableListView.html
And, btw., using additional rows with hiding is a bit wrong UX design. Maybe it would be better to use drop down lists or custom dialog windows? Both of them have built in functionality do disappear when touched outside.
You can simply put all your ToggleButton whenfilling the list in an ArrayList. This way you just have to force the click on each button of list toggled when one of them is clicked.
Ok I solved the problem.
Instead of toggling the buttons with "performClick()" or "setChecked" in some methods and managing the expand/collapse action in an OnCheckedChangedListener, I decided to store the status of the togglebuttons in an array, and to expand/collapse the child views in the "getGroupView" method depending on the state of the groupview's button, whose position in the array corresponds to the groupview's position in the list. To update the list, I call notifyDataSetChanged() on the adapter.
I added a FrameLayout around the ToggleButton to increase its clickable area.
Here's the code for clarity:
The relevant parts of the MainActivity:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.
.
.
ExpandableListView lv = (ExpandableListView) findViewById(R.id.normalList);
CustomList adapter = new CustomList(MainActivity.this, list /*the list of Items, declared elsewhere*/, lv);
lv.setAdapter(adapter);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.filesListDrawerLayout);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
uncheckButtons(mDrawerLayout);
//do some unrelated stuff
return true;
}
});
}
public void uncheckButtons(ViewGroup vg) {
for(int i = 0; i < adapter.buttons.length; ++i)
adapter.buttons[i] = false;
adapter.lastChecked = -1;
adapter.notifyList();
}
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
public boolean[] buttons;
public int lastChecked;
private final Integer[] imageId = new Integer[] { R.drawable.image1, R.drawable.image1, R.drawable.image3 };
private ExpandableListView list;
public CustomList(Activity context, List<Item> names, boolean forUpload, ExpandableListView list) {
this.context = context;
this.names = names;
this.list = list;
this.buttons = new boolean[names.size()];
this.lastChecked = -1;
}
private static class GroupHolder {
TextView txtTitle;
TextView txtData;
ImageView imageView;
ToggleButton toggle;
FrameLayout frame;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_single, null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
holder.txtData = (TextView) convertView.findViewById(R.id.counter);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
holder.frame.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(lastChecked != -1) {//if there's a checked button
buttons[lastChecked] = !buttons[lastChecked];//uncheck it
if(position == lastChecked)//if I clicked on the last checked button
lastChecked = -1;//there's no checked button
else {
buttons[position] = !buttons[position];//check the button
lastChecked = position;//and set it as the last checked one
}
notifyList();
}
return false;
}
});
holder.toggle.setChecked(buttons[position]);
if(holder.toggle.isChecked()) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
list.expandGroup(position);
else
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
} else if(/*other conditions*/) {
//do other stuff with the views that don't interest us
}
return convertView;
}
private static class ChildHolder {
TextView details;
TextView send;
TextView other;
}
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
holder = new ChildHolder();
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.send = (TextView) convertView.findViewById(R.id.send);
holder.other = (TextView) convertView.findViewById(R.id.other);
} else {
holder = (ChildHolder) convertView.getTag();
}
//maybe I could manage the listeners more nicely and without redundance...
holder.details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.other.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
return convertView;
}
public void notifyList() {
this.notifyDataSetChanged();
}
//I don't need OnGroupExpanded anymore
The ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
The xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginTop="-15dp"
android:clickable="true"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingLeft="10dp" >
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/toggle_button_selector"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
</FrameLayout>
</RelativeLayout>
The xml of the child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
<!-- this one is new -->
<TextView
android:id="#+id/other"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:background="#drawable/toolbar_selector"
android:clickable="true"
android:drawablePadding="-12dp"
android:drawableTop="#drawable/ic_action_other"
android:gravity="center"
android:text="#string/other" />
</LinearLayout>
There. I'm happy now.

Categories

Resources