Update List Item Selector - android

I have 2 views one below the other :
<Button />
<ListView />
The list view has item defined in xml and its inflated in the Adapter.
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#drawable/list_item_selector"
android:textColor="#drawable/text_color_selector"
android:textSize="16sp" >
</TextView>
in the Adapter :
if (convertView == null) {
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
....etc...
Now the you can see that the list_item.xml textview has selectors added to it.
The Button which is above the ListView has a onClick to it, onclick i need to remove those selectors and replace with some other color.
How can i do this ?
EDIT
We can refresh the Adapter onClick but list view is blank(something like visibility is INVISIBLE), but when i touch that view it again shows up.

button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
listView.setSelector(R.color.white);
}
});
EDT:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
});
Hope this helps.

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

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>

How To make Buttons Clickable in ListView Header

I have two buttons in ListView Header and I want to detect button is clicked from the header. how can I do this..
here is my code:
header_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b2" />
</LinearLayout>
in java code I have done this
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
You can add listener to these buttons.
btnB1 = (Button) mTop.findViewById(R.id.b1);
btnB1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
})
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
Button _btnb1 = (Button) mTop.findViewById(R.id.b1);
_btnb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
Button _btnb2 = (Button) mTop.findViewById(R.id.b2);
_btnb2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
Try this code

Remove item from GridView

I am very new to android development and I feel like this is very simple, yet, I haven't managed to find anyone on google with the same problem.
I have a Gridview that is filled with a TextView (which has an image on top) and an ImageButton (to delete the current item). What I want to do is to remove the item of which I click the ImageButton.
Here is my Main :
public class ActivityMain extends Activity
{
GridView gridview;
public GridAdapter mainActivityAdapter;
public ArrayList<String> listService = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listService.add("Market");
listService.add("Recherche");
listService.add("Quiz");
gridview = (GridView) findViewById(R.id.mainActivity_grid);
mainActivityAdapter = new GridAdapter(this.getApplicationContext(), listService);
gridview.setAdapter(mainActivityAdapter);
}
}
And here is my Adapter :
public class GridAdapter extends BaseAdapter
{
Context context;
ArrayList<String> list = new ArrayList<String>();
GridAdapter adapter = this;
public GridAdapter(Context context, ArrayList<String> list)
{
this.context = context;
this.list = list;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int arg0)
{
return null;
}
#Override
public long getItemId(int arg0)
{
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_gridmain, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.gridMain_text);
holder.close = (ImageButton) convertView.findViewById(R.id.mainActivity_delete);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
// Doing stuff on views
holder.close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg1)
{
// list.remove(position);
list.remove(position);
adapter.notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder
{
TextView textView;
ImageButton close;
}
}
The thing is, when I click on one ImageButton, it's always the last item that has been added that is being removed and I can't figure out why or how to fix this.
Thank you.
==================
EDIT :
Here is my activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DDDDDD"
android:orientation="vertical" >
<GridView
android:id="#+id/mainActivity_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:clickable="false"
android:gravity="center"
android:horizontalSpacing="15dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
And my item_gridmain.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/gridMain_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textColor="#android:color/holo_blue_dark"
/>
<ImageButton
android:id="#+id/mainActivity_delete"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/deleteFav"
android:src="#drawable/boutoncroixfermer" />
</RelativeLayout>
You have ImageButton's in every item right? You can set
holder.close.setTag(Integer.valueOf(position));
And then, you have all positions hidden in the right buttons.Change the OnClickListener like below:
close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
list.remove((Integer) v.getTag());
adapter.notifyDataSetChanged();
}
});
Ok actually it was my own mistake and I feel incredibly dumb now.
I was actually removing the right item but in each view I was replacing each item at position with what was there in the first place. So each item was the correct one but the picture/text were the old one because I was modifying them inside getView().
Sorry for that lose of time, my bad, I want to punch myself now.
Please use ViewHolder pattern, and also add your gridMain_text.xml
else you will keep receiving wrong index of click
http://www.binpress.com/tutorial/smooth-out-your-listviews-with-a-viewholder/9
This is example of ListView but equally applicable for GridView
Please use
holder.close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg1)
{
// list.remove(position);
list.remove(position);
adapter.notifyDataSetChanged();
}
});
instead of close.setOnClickListener, i ran your this code an now it work fine
add Log.debug() in listener to ensure the remove position is right.
How do you know after click,the last item is removed?I mean,if there has text label in item view?Maybe you remove the right item,but after gridview refresh,it looks like the last item is removed.
You can add position label with setText in getView.That may helps you.
Ok,I know.Where did you do render item view?You must do it in getView.The AdapterView cached it's item view.As you removed the right position.May be the position is not the right view.You can run hierarchyviewer to detect what happen.

to add textview and button in each row of android listview

to add textview and button in each row of android listview and if a button in a row is clicked only its row's textview should be edited or changed and other rows must remain unaffected
To acquire this, you need to use custom adapter and your custom layout.
In your row XML file:
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button" />
In your base adapter, you can then set the listener to do something:
public class MyBaseAdapter extends BaseAdapter {
// Some other method implementation here...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Initialize the convertView here...
LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.row_layout);
Button button = (Button) convertView.findViewById(R.id.button);
layout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Row clicked!", Toast.LENGTH_LONG).show();
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Button clicked!", Toast.LENGTH_LONG).show();
}
});
}
}

Categories

Resources