How to change source of Button in ListView? - android

I know, similar questions (ListView selected items are messed up when scroll the ListView...) are asked before and the answer is doing operation in onItemClick, not in getView.
My situation is a little bit complicated. I have a custom view in each ListView row. This custom view has 5 ImageViews. So I catch onClick event of these ImageViews and change drawable image. In this case, I don't know how to move this operation to onItemClick. Any ideas?

do like this. you can change the color or background of selected item on listview. use this code in getView() method.
m_Button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
indexClick = position;
indexClickFlag = true;
notifyDataSetChanged();
Log.i("in click--------->", "" + position);
Intent i = new Intent(ArtistPlaylistActivity.this,
PlayerMainActivity.class);
i.putExtra("songnameIndexinPlaylist", position);
startActivity(i);
ArtistPlaylistActivity.this.finish();
}
});
if (indexClickFlag) {
if ((indexClick) == (position)) {
itemLayout.setBackgroundResource(R.drawable.tab_select);
Log.d("System out", "iclick" + indexClick + "pos"
+ position);
}
}
where, int indexClick ; boolean indexClickFlag = false;
itemLayout -- > is layout that background that you want to change and m_Button.
Hope helpful to you.

You can get the Child from the view using getChildAt(index) method. put the index of image view to get the image view object
Sample Code
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println(".....view..."+arg1);
LinearLayout ll =(LinearLayout) arg1;
ImageView imageView = (ImageView) ll.getChildAt(0);
imageView.setImageDrawable(drawable);
}

Related

Highlight listView selection background programmatically

I have a ListView with a simple ArrayAdapter
dogsList.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, dogsArray));
If a user clicks on an item, the background color changes. So far so great.
Here is my problem: when the user returns, I want to pre-highlight the last selection. I have tried a number of things to pre-highlight one of the entries but it’s not working.
Assume I know the previous selection and pass it through intent or whatever, say prev=5. How do I highLight the background?
I am using a real device (Note 5) to test. And I don't understand whether this applies or not: Android ListView programmatic selection/highlight. In any case, I did it and it didn't work: no highlight.
May be this will help
View row = null;
list_view.post(new Runnable() {
#Override
public void run() {
int nPos = Prev;
list_view.setSelection(nPos);
View v = list_view.getChildAt(nPos);
if (v != null) {
option_list.performItemClick(v, nPos, list_view.getItemIdAtPosition(nPos));
v.requestFocus();
}
}
});
list_view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
// TODO Auto-generated method stub
if (row != null) {
row.setBackgroundResource(R.color.DarkGray);
}
row = v;
v.setBackgroundResource(R.color.White);
}
});

Change layout background color of listview item when is selected in MULTIPLE CHOICE MODE

I'm trying to use in listSelector, but state_selected and state_checked are not working as I thought. I have also tried to use the selector in android:background tag on the list item XML, but it doesn't work. Thanks
Try this way in your activity
#Override
public void onItemClick(AdapterView<?> parent, View clickedView,
int position, long id) {
// TODO Auto-generated method stub
switch (clickedView.getId()) {
case R.id.itemLayoutId:
lytBackground = (LinearLayout) clickedView.findViewById(R.id.itemLayoutId);
lytBackground.setBackgroundColor(Color.parseColor("#0e95cf"));
break;
Here lytBackground is parent layout of your listView row item.
To have feature like on first click row is selected and on second click (on same row) it gets unselected, save clickedView in a global variable
private View clickedView;
#Override
public void onItemClick(AdapterView<?> parent, View clickedView,
int position, long id) {
// TODO Auto-generated method stub
switch (clickedView.getId()) {
case R.id.itemLayoutId:
if (this.clickedView == clickedView ) {
lytBackground = (LinearLayout) this.clickedView
.findViewById(R.id.itemLayoutId);
lytBackground .setBackgroundColor(Color.parseColor("#3eb4d8")); //normal color
}
lytBackground = (LinearLayout) clickedView.findViewById(R.id.itemLayoutId);
lytBackground.setBackgroundColor(Color.parseColor("#0e95cf")); // selected color
this.clickedView = clickedView;
break;

How to get horizental Listview's position to variable in android?

I am developing an Application.
My layout is in my Previous Question.
For that I am using horizontal List view. In that I want to Get the position of the images while Scroll is changed.
I had tried for this:
hlv.setOnScrollStateChangedListener(new OnScrollStateChangedListener() {
#Override
public void onScrollStateChanged(ScrollState scrollState) {
// TODO Auto-generated method stub
for(int i=0;i<albumList.size();i++)
{
if(hlv.callOnClick())
{
int id=i;
Toast.makeText(getApplicationContext(), ""+id, 1000).show();
}
}
}
});
But didnt successed. I didnt got the position of element in the Variable.
Did you tried the setOnItemClickListener for listview?. Even for a custom HorizontalListView it should be implemented as it is based on a normal ListView. Try it:
hlv.setOnItemClickListener(
new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
Toast.makeText(this, "POSITION: "+position, Toast.LENGTH_SHORT).show();
}
}
);
Hope it helps!

listview with button and call the previous step after clicking

I'm new to android development. I have 2 ListViews. when I click on one item on the first ListView, the new dataset will show in the second one. I have added a Button to the second ListView (onItemClick). Using an Adapter. So when I click on the Button (Read more) it will load a new Activity. So when I click on the Back Button i need to load the same data(listview2) in my previous step.
int images[] ={R.drawable.boc, R.drawable.commercial, R.drawable.nations, R.drawable.popls};
adp = new ItemsAdapter(getActivity(), images);
menu.setAdapter(adp);
menu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv2 = (TextView) v.findViewById(R.id.listViewText);
/*ImageButton imgbtn = (ImageButton) v.findViewById(R.id.AddButton);
ImageButton imgbtn2 = (ImageButton) v.findViewById(R.id.AddInfo);
*/
switch (arg2) {
case 0:
ListAdapter adapter3 = new ListAdapter(getActivity(), boc) ;
menu2.setAdapter(adapter3);
break;
case 1:
// menu2.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1,subitems2));
ListAdapter adapter4 = new ListAdapter(getActivity(), bankcrcards) ;
menu2.setAdapter(adapter4);
break;
default:
break;
}
}
});
menu2.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ImageView imgbtn1 = (ImageView) arg1.findViewById(R.id.imageView2);
imgbtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
expandableListView=new ExpandableListFragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.setCustomAnimations( R.anim.fade_in, R.anim.fade_out);
transaction.replace(R.id.myFragement,expandableListView);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
});
}
});
Can I propose that instead of using two ListViews, you use an ExpandableListView. Two ListViews on the UI might not lead to a good experience.
There's details here on how to deal with vast amounts of data.
An example here should help you with implementation of an ExpandableListView
Using 2 listviews in 1 activity is not recommended as it can cause you flows issues (Like the one you are facing right now).
Consider using only 1 listview, if there's different items override the following function (of BaseAdapter) to show correct views:
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
This will solve the 2 listview issue, than, when clicking on item and opening new activity you can save the an instance state of the list view so when you press back you can use the following example to see how to return to previous state.
Good luck

Android ListView Item is not Clickable

I want to design each list item in the ListView can be clickable, and triger out which list item be clicked. But it can not. I tried the two methods: setOnItemClickListener() and setOnItemSelectedListener() on my code. I have had googled couple of references about the article, however it still can not work(clickable).
I would like post the code below: The code can display the list items and I can see the Log.d content for the line of Log.d(" mListView01.getCount()="," "+vc); on LogCat well. But, there is no any response if I clicked on the list Item.
if you don't mind, could you help point me out where I was wrong, thanks !
Code for creating the listView using the Activity Widget:
......
setContentView(R.layout.main_open);
TextView itemText = (TextView) findViewById(R.id.itemText);
TextView codeText = (TextView) findViewById(R.id.codeText);
itemText.setText(selectedItem);
codeText.setText(selectedCode);
ListView mListView01 = (ListView)findViewById(R.id.main_open_listview1);
String[] keys = new String[] {"title","title_image", "content",
"title1","title1_image","content1","title2","title2_image","content2"};
int[] resValues = new int[] { R.id.title, R.id.title_image, R.id.content,
R.id.title1, R.id.title1_image, R.id.content1,R.id.title2, R.id.title2_image, R.id.content2};
openDocAdapter opendoc = new openDocAdapter(this,localdcoumentlist, R.layout.main_open_content, keys, resValues );
mListView01.setSelected(true);
mListView01.setClickable(true);
mListView01.setAdapter(opendoc);
int vc = mListView01.getCount();
Log.d(" mListView01.getCount()="," "+vc);
mListView01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("Selected From setOnItemSelectedListener, arg2=", " "+ arg2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
mListView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedViewPos = arg2;
Log.d("TitlesSelectionDialog(),selectedViewPos= "," "+ selectedViewPos);
Toast.makeText(getApplicationContext(), "selectedViewPos= "+ selectedViewPos, Toast.LENGTH_LONG).show();
}
});
......
Code for openDocAdapter:
private class openDocAdapter extends SimpleAdapter
{
private Context _con;
private List _List;
private int _listviewId;
private String[] _keys;
private int[] _resValues;
public openDocAdapter(Context context, ArrayList<HashMap<String,Object>> List , int listviewId, String[] keys, int[] resValues )
{
super(context, List, listviewId, keys, resValues);
_con =context;
_List = List;
_listviewId = listviewId;
_keys = keys;
_resValues = resValues;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.main_open_content, null);
}
TextView title = (TextView) v.findViewById(R.id.title);
(...Similiar codes define textView, imageViewsd.)
return v;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return super.getCount();
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return super.getItem(position);
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return super.getItemId(position);
}
}
Edit1: I found an article here About the Focus setting on the layout will cause clickable work or not work. So, I remove the lines of (I don't while it be coded here) in the xml file of layout. Then the setOnItemSelectedListener() method is worked while scrolling the list list with orange focus change. But it still not meet my expection.
Provlem Solved ! After couple hous googling/search and try_eror. And I would like share it if you are interesting.
The main cause of the problem is: I used the ScrollView as the basic layout for the row.xml(containing the content for each listview row). Then, I used the LinearLayout(Vertial) instead of it. The setOnItemClickedListener() method works fine now. I do not have any idea regarding this that will cause the ListView to be not clickable. If somebody know it, please tell us,

Categories

Resources