How to select custom listview item with edittext? - android

I've made multiple custom listview items with only textviews and when I click them I can select them. But now i've made a list item that has 3 edittexts in it and it won't work. Even tried to put a textview next to it, but that doesn't help eather.
Does anyone know how to?
Thanks in advance!

You just need your list item (whatever type it is - XYZLayout or any other view) to implement the Checkable interface. "Checkable" practically means selectable in the Android terms.
You can just copy this class into your project. Note that it is RelativeLayout but you could make it to work with other layouts type too:
CheckableRelativeLayout.java

I know I'm late for party but I'm posting for someone who might face same issue. To make your list item selctable, you need to make your EditText not focusable by setFocusable(false) in getView().
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
EditText et1 = (EditText) listItemView.findViewById(R.id.view1);
EditText et2 = (EditText) listItemView.findViewById(R.id.view2);
// You need to make EditText not editable to prevent them to intercept focus from list view.
et1.setFocusable(false);
et2.setFocusable(false);
CustomListItem item = listViewItemList.get(position);
et1.setText(item.view1Value);
et2.setText(item.view2Value);
return listItemView;
}

Related

EditText focus in a ListView

I have a ListView and in that ListView I have a Row with some TextViews and EditTexts in it.
When I press anywhere on the row I want the EditText to take focus so text can be entered.
The problem is that I cannot get this to work. When i place the EditText in the ListView the OnItemClick will not respond when I click on the ListView.
I tried using focusable="false" on the EditText and it allowed me to click on the ListView again but I could not get the EditText to get focus even after setting the focusable to true.
Next I tried using the android:descendantFocusability="beforeDescendants" in the ListView but it did not seem to make any change, it still wouldn't work.
Does anyone have an idea on what I can do to make it work?
Try this, it should let you click on a line and focus the EditText (modified from this SO Answer):
public void onItemSelected(AdapterView<?> listView, View view, int position, long id)
{
EditText yourEditText = (EditText) view.findViewById(R.id.youredittextid);
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
yourEditText.requestFocus();
}
public void onNothingSelected(AdapterView<?> listView)
{
// onNothingSelected happens when you start scrolling, so we need to prevent it from staying
// in the afterDescendants mode if the EditText was focused
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
do like this
ListView listView = (ListView) findViewById(R.id.yourListViewId);
listView.setItemsCanFocus(true);
listView.setClickable(false);
ie,
after getting the listview object from xml, disable the click on this listview and pass the focus to the child views
you can add a property as enabled="false" in the xml row layout, and on the onItemClickListener you can setEnabled(true) and then setFocusable(true) , this should solve it.

How to overlay rows of Android Listview

I am trying to implement the below Android ListView. The each yellow box represents one row of ListView. If you see the rows, each row is over lapped by previous and next row. I have searched Google, but I could not find any proper solutions. If anyone has clues, please let me know.
Thanks in advance.
Think outside the box. Imagine this list not having the rows overlap, but just having diagonal lines on it. In your code, set the Listview's divider height to 0:
ListView listView = (ListView)findViewById(R.id.your_listview);
listView.setDivider(null);
listView.setDividerHeight(0);
Then create two drawables for the rows- one for odd rows, another for even rows:
and
(don't use these two images, as they are not properly/evenly sized, but create the ones for your specific list).
Then create an adapter for your list view - and in its getView method, set the corresponding background to your element:
#override
public void getView(int position, View convertView, ViewGroup parent) {
MyView row;
if(convertView == null) {
row = (MyView)convertView;
}
else {
row = ... //create your view - inflate or create in code
}
row.setBackgroundResource(position%2==1 ? R.drawable.row_odd : R.drawable.row_even);
... //populate the rest of the row with whatever you need
return row;
}
And voila! You get what the effect you need (note, on this schematic the black lines represent the boundaries between rows - these are for schematic purposes only - in your final result you will not have them):
Finally, please note that if you want highlight on your "rows" when an item is selected, you'll have to implement custom state change listeners, which would change the background of the cell that's being selected and also the ones above and below it as to create the proper visual effect.
I got the same issue but I managed to find a solution
In the ListView in the xml you can set a negative dividerHeight, it works for me.
<ListView
...
android:dividerHeight="-5dp" />
I hope it can help someone :)

find a Textview in ListActivity in android

I'm usind ListActivity to show stock market information. Each row has my own layout with 4 textviews. I need to update the information of each stock as they change.
My problem is I could extract a specific view from list, however I don't know how to find a textview within this view.The code below might clarify my problem.
This is what I've done which didn't work:
ListView list = (ListView) findViewById(R.id.list);
View r = list.getChildAt(2);
TextView t = (TextView) r.findViewById(R.id.t_buy);
t.setText("3200$");
t_buy is one of the textviews in my layout.
I was hopping that android has something like below, but it hasn't.
TextView t = (TextView) r.getChildAt(R.id.t_buy);
This is the screen shot of my app.
Create you custom adapter from BaseAdapter and override method:
#Override
public View getView(int position, View convertView,
ViewGroup parent)
{
//...
TextView textView;
textView = (TextView)view.findViewById(R.id.itemText);
textView.setText("some text");
}
see mode details here: http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter
You could try using getAdaper() on the list object and then use getItem() to retrieve the one you need.
You can update the items in adapter, and then invoke adapter.notifyDataSetChanged
I think it is best if you can use the View getView method because you are using a ListView and when you click on a List item you want to populate one of your custom layout with four TextViews as you have mentioned. This links show you how to do it clearly.

OnItemClickListener doesn't work with ListView item containing button

I have ListView with custom Adapter which supplies View to ListView in this way:
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout.LayoutParams lineParams;
RelativeLayout line=new RelativeLayout(context);
TextView tv=new TextView(context);
tv.setText("Text in postion="+i);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
line.addView(tv, lineParams);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//checkbox
CheckBox checkBox=new CheckBox(context);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
line.addView(checkBox, lineParams);
return line;
}
And somewhere inside ListView there's setOnItemClickListener(), which should intercept item clicking events. My problem that, whenever I try to add checkbox to item - I don't get any responces from my ListView. If I skip CheckBox or any other Button it works.
I am really stuck with this problem, I have tried all kind of Layouts, aligning, wrapping and so on - useless. Looks like CheckBox interferes ListView item click events.
Any ideas how to overcome?
just add this line into the item views instead of listView itself
android:focusable="false"
check more detail about this from Android custom ListView unable to click on items
If you have ImageButtons inside the list item, you need to add:
android:descendantFocusability="blocksDescendants"
to the root list item element [such as the root layout].
Then within each ImageButton in the list item, you need to add:
android:focusableInTouchMode="true"
This worked for me - but I was using ImageButtons, not the standard button.
I have also faced the same issue I have tried to set android:focusable="false" to listview but it don't work then I add this to listview item.. like in my listview item I have uesed Toggle button which was creating problem, I add android:focusable="false" to Toggle button and listview on item click listener start work again
Add following line to your listView
android:choiceMode="singleChoice"
or make sure to set following lines to your layout text fields
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
I had also had the problem of a Button in my ListView. Unfortunately just setting the focus to false for all objects in my Adapter did not work for me.
I now have a workaround.
In your Adapter create an OnClickListener for the button (or other clickable object) if you have not already done that. In that OnClickListener you call the OnItemClickListener yourself.
public void onClick(View v) {
mOnItemClickListener.setOnItemClick(mListView, v, vPos, vId);
}
It does mean that you will need to give your adapter access to both the parent ListView and the OnItemClickListener.
You can consider to write your on OnTouchEvent in your listview item and send the proper touchEvent to you child view , the button .
Well i know none of the above solutions will work.I tried changing xml attributes but those does not work out, But i implemented it in a new fashion.
Here is how:
Create an interface CheckBoxOnCheckListener with method onCheckBoxChecked and pass needed parameters, implement interface CheckBoxOnCheckListener in your activity or fragment containing listView.
Next in your adapter, declare an mListener as CheckBoxOnCheckListener, and pass this as a parameter to Adapter's constructor from fragment/activity and cast it to CheckBoxOnCheckListener and assign to mListener.
Next set mListener as itemView.onClick or CheckBox.onCheckCheckedListener and onCheckChanged method call mListener.onCheckBoxChecked.
That's it. It will definitely work,it worked for me.
For code just pm.
If you are using ListView in Activity, ensure you have setup setOnItemClickListener()
myListView.setOnItemClickListener(this); // if your activity implement OnItemClickListener

Android listview with header and footer buttons

folks!
I need to make such layout: I've got listview and I need to put buttons on top and on the bottom of it, i.e. when user scrolls list to the end, he can see bottom button, and when user is on the top of list, he can see top button. But when user is 'in the middle' of listview, he can't see those buttons. I've got no idea how to do it. Thanks for help.
UPDATE
listView=(ListView)findViewById(R.id.listSearchResults);
LayoutInflater inflater=this.getLayoutInflater();
View header=inflater.inflate(R.layout.list_header, null);
btnBack=(Button)header.findViewById(R.id.btnBack);
btnBack.setOnClickListener(this);
btnBack.setEnabled(false);
listView.addHeaderView(header);
View footer=inflater.inflate(R.layout.list_footer, null);
btnForward=(Button)footer.findViewById(R.id.btnForward);
btnForward.setOnClickListener(this);
btnForward.setEnabled(false);
listView.addFooterView(footer);
First create two layout file. like as footer_layout.xml & header_layout.xml and add footerview-headerview in list view
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);
You can use addHeaderView() and addFooterView() on your ListView
Check here:
Android Dynamically load Listview at scroll end?
This is with the help of list's footer and header. Basically, you use addHeaderView()
/addFooterView (). Check all versions of the methods, since they allow you to have the view not being selectable.

Categories

Resources