How to add button to custom adapter? - android

I am using FunDapter for my project.When i try to add imagebutton to my layout other texts become unclickable.Does nothing when i click them.How can i add button to this adapter?
Below is list_layout where everything works fine
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Isimsiz"
android:id="#+id/Housename"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="2dp"
android:clickable="false"
android:textColor="#241d1d"
android:layout_row="0"
android:layout_column="0"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Aciklama Yok"
android:id="#+id/Desc"
android:layout_below="#+id/Housename"
android:clickable="false"
android:layout_alignStart="#+id/Housename"
android:textColor="#241d1d"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_alignEnd="#+id/Housename" />
</RelativeLayout>
When i add image button nothing is clickable except imagebutton
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_alignTop="#+id/Housename"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/Desc"
android:clickable="true"
android:onClick="berk"
/>
And this is part of the class where i create my adapter
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.Housename, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.HouseID;
}
});
dict.addStringField(R.id.Desc, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.Desc;
}
});
FunDapter<Product> adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.layout_list, dict);
lvProduct = (ListView)findViewById(R.id.lvProduct);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do sth
}
public void berk(View v) {
//do sth
}
Note:Also when i give clickable attribute to texts in xml they stop working.Like When i play with layout XML everything stops working.

In Adapter Class
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent, false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
}
But you can get an issue - listView row not clickable. Solution:
make ListView focusable android:focusable="true"
Button not focusable android:focusable="false"

First of all if u are using button or edit text in your adapter or row file Then every thing will get stop working .
You should use
1.Set descendant Focusablity="before Descendants" in the list view.
2.Set window Soft Input Mode="adjust Pan" in the manifest
3.Set list view.set Items Can Focus(true)

you have to give separate click event for all views.
Your ImageButton has android:clickable="true" which makes sub view clickable. But you other views are not clickable.
Solution
For click event in textview
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
similar for Imagebutton
remove
android:clickable="true"
hope it helps.

Related

How to add Different OnClickListeners on views of ListView item to perform different actions

Are there any possibilities to check what view inside ListView Item was clicked?
In other words, when you click on different Views inside ListView item app should perform a different action.
In details, I have a simple Book.java class that contains some book description.
Then I create ListView<Book> using BooksAdapter.class:
public class BooksAdapter extends ArrayAdapter<Book> {
public BooksAdapter (Context context, List<Book> books) {
super(context,0,books);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// ConstraintLayout constraintLayout = new ConstraintLayout();
// constraintLayout.setVisibility();
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.books_list_item, parent, false);
}
Book currentBook = getItem(position);
ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
if (currentBook.getImage() == "No cover") {
coverView.setImageResource(R.drawable.no_book_cover);
} else {
Picasso.get().load(currentBook.getImage()).into(coverView);
}
TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
titleTextView.setText(currentBook.getTitle());
//TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
//descrTextView.setText(currentBook.getDescription());
return listItemView;
}
private String formatAuthor (String name,String date ) {
name = name.substring(2,name.length()-2);
date = date.substring(0,4);
String fullString = name + ", " + date;
return(fullString);
}
}
books_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:paddingBottom="16dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/preview_image_view"
android:layout_width="80dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/autor_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="#color/textColorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="0dp"
android:textColor="#color/textColorLight"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="#drawable/ic_info_black_24dp"
android:scaleType="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#android:color/darker_gray"/>
</LinearLayout>
In MainActivity.java create mAdapter and override setOnItemClickListener:
ListView booksListView = (ListView) findViewById(R.id.list);
mAdapter = new BooksAdapter(this, new ArrayList<Book>());
booksListView.setAdapter(mAdapter);
booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Book book = mAdapter.getItem(position);
ad = new AlertDialog.Builder(BooksListActivity.this);
ad.setTitle("Book description");
ad.setMessage(book.getDescription());
AlertDialog alert = ad.create();
alert.show();
//ad.create();
}
});
And here are some general question can we set onClickListeners on different Views inside Item (#+id/autor_text and #+id/title_text for example) and perform different actions in these cases?
I'm reading about this several places but doesn't find any helpful things.Thanks for any help.
Inside your getView() method set onClickListener to all your different Views and perform respective action.
By implementing this code:-
authorTextView.setOnClickListener(this);
titleTextView.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.authorTextView:
//code to be written to handle the click event
break;
case R.id.titleTextView:
//code to be written to handle the click event
break;
}
}
};
Inside your getView
authorTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});
titleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});

setVisibility true of dynamic id of a layout

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>

Android - Adding a button causes a custom listener to not work

I added a button on list_item.xml and my setOnDataSelectionListener(OnDataSelectionListener listener) doesn't seem to work. All I did was add a button to the LinearLayout, and the listener stopped working. Is there something that needs to be done after adding the button?
list_item.xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"
>
<ImageView
android:id="#+id/iconItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="center_vertical"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>
<TextView
android:id="#+id/dataItem01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff888888"
android:textSize="12dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/dataItem02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#ccf88107"
android:textSize="10dp"
android:textStyle="bold"
android:paddingRight="4dp"
android:clickable="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</RelativeLayout>
<TextView
android:id="#+id/dataItem03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#191775"
android:textSize="12dp"
android:padding="4dp"
/>
</LinearLayout>
</LinearLayout>
MainActivity:
statusList = (StatusListView) findViewById(R.id.statusList);
statusAdapter = new StatusListAdapter(this, mHandler);
statusList.setAdapter(statusAdapter);
statusList.setOnDataSelectionListener(new OnDataSelectionListener() {
#Override
public void onDataSelected(AdapterView parent, View v, int position, long id) {
Status curItem = (Status) statusAdapter.getItem(position);
String curText = curItem.getText();
Log.d(TAG, "display curtext"); // no log displayed
Toast.makeText(getApplicationContext(), curText, Toast.LENGTH_LONG).show(); // obviously no toast message
}
});
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout list_item = (LinearLayout)inflater.inflate(R.layout.list_item, null);
Button deleteBtn = (Button)list_item.findViewById(R.id.deleteBtn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "delete button clicked");
statusList.setOnDataSelectionListener(new OnDataSelectionListener() {
#Override
public void onDataSelected(AdapterView parent, View v, int position, long id) {
Log.d(TAG, "delete button clicked inside onDataselected");
DeleteStatusThread thread = new MainActivity.DeleteStatusThread(id);
thread.start();
}
});
StatusListView:
public class StatusListView extends ListView {
private OnDataSelectionListener selectionListener;
public StatusListView(Context context) {
super(context);
init();
}
public StatusListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
setOnItemClickListener(new OnItemClickAdapter());
}
class OnItemClickAdapter implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (selectionListener == null) {
return;
}
selectionListener.onDataSelected(parent, view, position, id);
}
}
public void setOnDataSelectionListener(OnDataSelectionListener listener) {
selectionListener = listener;
}
public void setAdapter(BaseAdapter adapter) {
super.setAdapter(adapter);
}
public BaseAdapter getAdapter() {
return (BaseAdapter) super.getAdapter();
}
}
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
The row item must have a param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
set any focusable or clickable view in this item with:
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
If you added the button to list_item.xml you wouldn't initialize the button there because MainActivity as I assume it is not MainActivity's layout file. Instead you should inflate the list_item.xml layout in your MainActivity with a LayoutInflater. Also, I don't see where you initialize your button or declare it in MainActivity but don't forget when doing so it is
Button buttonExample = (Button)findViewById(R.id.btn_id_from_xml);
Then you can use an onClickListener
buttonExample.setOnClickListener(this);
I usually make one onClickListener method with a switch case that changes what happens based on the id of whichever button was clicked that called onClickListener. Good Luck!
set android:focusable="false" to your button in the list_item.xml.

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;
}

How to change button background when you click on the listview item

I have a listview and I have inflate it with a custom layout containing 1 ImageView, 2 TextView and 1 Button. I want to change the button background when I click on a listview item , but I don't be able to perform it. Can someone help me, please? Thank you so much in advance.
row_segnalibro.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="50dp" >
<ImageView android:id="#+id/ImageView01" android:src="#drawable/star"
android:layout_height="40dp"
android:layout_width="40dp"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout02"
android:orientation="vertical"
android:layout_width="220dp" android:layout_height="match_parent">
<TextView
android:id="#+id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvRow2"
android:layout_below="#id/tvRow1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:paddingLeft="10dp"
android:textColor="#0967AD"
android:textStyle="bold"
/>
</RelativeLayout>
<Button
android:id="#+id/butt_segnalib"
android:background="#drawable/freccia_o"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center" />
Segnalibro.java
public class Segnalibro extends ListActivity{
super.onCreate(savedInstanceState);
setContentView(R.layout.segnalibro);
lv = (ListView) getListView();
.....some code
MySimpleCursorAdap myadap = new MySimpleCursorAdap(getApplicationContext(),R.layout.rowlayout_segnalibro,curr,campi, to);
lv.setAdapter(myadap);
lv.setClickable(true);
lv.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,int position, long arg3) {
Log.i("","I have clicked on item "+position);
}
});
MySimpleCursorAdap.java
public class MySimpleCursorAdap extends SimpleCursorAdapter{
public MySimpleCursorAdap(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mLayout = layout;
this.curr = c;
this.cont = context;
}
public void bindView(View view, Context context, final Cursor cursor) {
....some code
/* Button butt = (Button) view.findViewById(R.id.butt_segnalib);
butt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
.....code to change the background
}
}); */
}
in MySimpleCursorAdap I can get the Button with the method findViewById, how I can to do it in the class Segnalibro?
Ok.. so basically you need a state list for acheiving it.. see this to know how to create it.. and then set it as background of your button..
When you use ListActivity as base class you don't have to create a onItemClickListener yourself, it's already there. Just override protected void onListItemClick(ListView l, View v, int position, long id). Inside you will be able to fetch the Button via it's id.
http://developer.android.com/reference/android/app/ListActivity.html
protected void onListItemClick(ListView l, View v, int position, long id)
{
Button bt = (Button) v.findViewById(R.id.button);
//do something fancy with the button
}
Try to add a ClickListener for the list item view in the bindView method:
public void bindView(View view, Context context, final Cursor cursor) {
//some code
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final ImageButton button = (ImageButton)v.findViewById(R.id.list_item_button);
button.setBackgroundColor(...)
}
});
)

Categories

Resources