Highlight TextView item in the list - android

I have the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="vertical">
<ImageView
android:layout_height="wrap_content"
android:id="#+id/imageView1"
android:src="#drawable/tab"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"></ImageView>
<TextView
android:id="#+id/subcat"
android:background="#b9cd4a"
android:layout_width="fill_parent"
android:textColor="#fff"
android:textStyle="bold"
android:layout_height="35dp"
android:textSize="18dp"
android:paddingLeft="10dp"
android:paddingTop="6dp"
android:text="Choose Location"></TextView>
<ListView
android:id="#+id/sub_catlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="#color/black"></ListView>
</LinearLayout>
And I inflate the list using TextView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#fff"
android:orientation="vertical"
android:layout_height="65dp" >
<TextView
android:text="#+id/albumDetails"
android:id="#+id/albumDetails"
android:textColor="#000"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textSize="16dp"
android:layout_width="wrap_content"
android:paddingTop="8dp"
android:layout_height="45dp"></TextView>
</LinearLayout>
The problem is when I click list item, I don't see any highlight. I need to show the highlight when the user click the item. How can I do that?

TextView is intercept click events from ListView. Set attributes
android:focusable="false"
android:clickable="false"
to the list's TextView.

In Custom Listview below code used.
TextViewClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
int count = listview.getChildCount();
for (int i = 0; i < count; i++) {
View v = listview.getChildAt(i);
TextView tx = (TextView) v.findViewById(R.id.txt1);
txt1.setBackgroundColor(Color.RED);
}
txt1.setBackgroundColor(Color.GREEN);
}
});

I think the issue may be in android:listSelector="#color/black", remove this line
Sample ListView
<ListView
android:id="#+id/nativecontacts_contacts_ListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:background="#color/White"
android:divider="#drawable/menu_line"
android:cacheColorHint="#00000000"
android:clickable="true" />

Related

Custom listview item xml disabling onitemclicklistener

i am using below xml for my ListView 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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
</LinearLayout>
i know in this xml some views are overlapping each other but in my custom array adapter i make visibility to gone for some of them based on certain condition, but what this xml do is to disable my ListView onItemClickListener but if i exclude this from xml then onItemClickListener works fine but i want to make onItemClickListener work without removing xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
In your adapter getView(), set onClickListener to each button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
If you want to handle click event for entire list item then you need to set onClickListener to root LinearLayout of your listViewItem XML:
ListViewItem XML: Give an id to LinearLayout
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp"
android:id="#+id/container">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
.................
...................
Adapter: Set OnClickListener to LinearLayout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
.............
.................
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.container);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
.................
...................
}
Hope this will help!

ListView setOnItemClickListener Does Not Trigger

click listener not working when I click an item.
in this program first two arraylist updated using update(). then this list view are sent to other class to make list view. but clicking each item doesn't toast anything.
public class BlockActivity extends Activity {
ListView lv;
ArrayList<String> contactnumber= new ArrayList<String>();
ArrayList<String> contactname= new ArrayList<String>();
public static SQLiteDatabase database;
SQLiteDatabase myDB= null;
CustomAdapter adapter;
ArrayList<String> contactnum= new ArrayList<String>();
ArrayList<String> contactnam= new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pickContact = (Button) findViewById(R.id.button1);
update();
lv=(ListView) findViewById(R.id.listView1);
adapter = new CustomAdapter(this, contactnam, contactnum);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
/*new AlertDialog.Builder(view.getContext())
.setMessage("Something here")
.setNegativeButton("Close", null).show();*/
Toast.makeText(BlockActivity.this,
"Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
}
});
pickContact.setOnClickListener(new OnClickListener() {
.
.
.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:focusableInTouchMode="false" android:focusable="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
<Button android:id="#+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>
<TextView
android:id="#+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
Thats because of your Button, add android:descendantFocusability="blocksDescendants" in your list item root layout
detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
<Button android:id="#+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>
<TextView
android:id="#+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center"/>
<TextView
android:id="#+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="20dip" android:layout_gravity="center"/>
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
If you have implemented onClik Listener in CustomAdapter class for Button in detail.xml then please remove it.
Because simultaneously you can not have click on listview's item click and customAdaptor's view click.
For TextViews and Button in your detail.xml add this piece of code
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
There could be multiple views in listviews which can be clickable.
Best solution is :
1)Use android:descendantFocusability="blocksDescendants" in top-level container layout of list item.
2) Now whichever views in the listview needs to clickable, Use below attribute:
android:focusable="false"
android:focusableInTouchMode="false"
3)Set click listeners to the views and callback using interface to fragment or activity on click.

getChildAt() InvocationTargetException

In xml of row_layout I've set a delete button as follows
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:background="#ffffff"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingLeft="10sp"
android:weightSum="100" >
<LinearLayout
android:id="#+id/Layout"
android:layout_width="150sp"
android:layout_height="50dp"
android:layout_weight="90"
android:orientation="vertical" >
<TextView
android:id="#+id/firstTextView"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10sp"
android:gravity="top"
android:text="Activity"
android:textColor="#000" />
<TextView
android:id="#+id/firstTextView2"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="top"
android:text="Sub-Activity"
android:textColor="#000" />
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/edit" />
<ImageView
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onClickDelete"
android:src="#drawable/neg" />
</LinearLayout>
here is onClickDelete method
public void onClickDelete(View v)
{
ListView lvItems = getListView();
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
TextView child = (TextView) vwParentRow.getChildAt(0);
child.setText("Delete Clicked");
}
When i press delete, it shows invocationtargetexception at (TextView child = (TextView) vwParentRow.getChildAt(0);)
Sorry if this is very basic mistake. I am new to android.
Thank you for your reply.
It's better
TextView child = (TextView)findViewById("yourtextfieldIDHere");
child.setText("Delete Clicked");

GridView with image and text

I'm very new in android and need a little help with this task.
I have gridview witch pull images and text from database and display them. Currently is displayed like image and on the right of the image is text. I want to make text under the image. Can you help me with this task. This is the gridview.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2" >
</GridView>
</LinearLayout>
And this is how I display image and text in it table_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Name"
/>
</LinearLayout>
</LinearLayout>
And if need getView()
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.table_column, null);
}
// ColPhoto
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
imageView.getLayoutParams().height = 80;
imageView.getLayoutParams().width = 80;
imageView.setPadding(10, 10, 10, 10);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
// ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText("" + MyArr.get(position).get("name").toString());
return convertView;
}
u can use relative layout and set align textview to imageview.
try this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ColName"
android:layout_alignRight="#+id/ColName"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ColPhoto"
android:gravity="center"
android:text="Name name" />
</RelativeLayout>
I don't know why add a single element LinearLayout in another LinearLayout.
if grid item need to be put in correct position, I'd like use RelativeLayout instead.
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ColPhoto"
android:text="Name"
/>
</RelativeLayout>
`
Try to change your orientation to "vertical".
EDIT:
Like, Tr4X said you don't need second LinearLayout. You can put both in one.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"/>
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
</LinearLayout>

Listview click listener not responding

I have a list view and want to perform clickable event on it. I did it before. but now its not working. I have added my XML as well. I just need to move from one actvity to other on click in list view.
CustomFinalSubmit_ItemDetail item = new CustomFinalSubmit_ItemDetail(Final_Submit.this , R.layout.customview_finalsubmit_itemdetails, itemInfo);
itemList.setAdapter(item);
itemList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
FinalSubmitItem pos = itemInfo.get(position);
String itemType = pos.getItemType();
String itemCountry = pos.getItemCountry();
String itemSerial = pos.getItemNo();
pos.setChecked(true);
Intent inn = new Intent(getApplicationContext(), FinalSubmitDetails.class);
inn.putExtra("itemType", itemType);
inn.putExtra("itemCountry", itemCountry);
inn.putExtra("itemSerial", itemSerial);
startActivity(inn);
}
});
Here is my main Xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/green">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_marginTop="10dip"
android:orientation="vertical"
android:background="#0B3B0B">
<ListView
android:id="#+id/CustomerDetailList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/yellow"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="#+id/Itemtype"
android:paddingTop="5dp"
android:background="#color/green"
android:textColor="#color/yellow"
android:layout_marginLeft="5dip"
android:text="Item Type"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:id="#+id/txtcountry"
android:background="#color/green"
android:textColor="#color/yellow"
android:text="Country"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:id="#+id/txtItemNumber"
android:background="#color/green"
android:textColor="#color/yellow"
android:text="Item No."/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:id="#+id/itemChck"
android:button="#drawable/custom_checkbox"
android:paddingTop="5dp"
android:padding="5dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_margin="5dp"
android:layout_height="1dp"
android:background="#color/white"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/itemList"
android:background="#088A08"
android:divider="#color/white"
android:dividerHeight="1dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I did it this way:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
your code
}});
Just add this much code to my customview
Recently I had the same problem after adding a ToggleButton to my list items. The solution for me was to add android:descendantFocusability="blocksDescendants" to the layout of the item.
Here is the whole XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:paddingLeft="15dp"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="#+id/watch_roadName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Addresse"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<ToggleButton
android:id="#+id/watch_button_arrived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/watch_roadName"
android:layout_alignParentRight="true"
android:textOn="#string/arrived"
android:textOff="#string/arrived"
android:text="#string/arrived" />
</RelativeLayout>
Similar answers to the same issue can be found here:
How to fire onListItemClick in Listactivity with buttons in list?
and here
Android custom ListView with ImageButton is not getting focus
in the widgets(Textview or button whatever it is) of your inflator layout just add:
android:focusable="false"
android:focusableInTouchMode="false"
and in parent layout of your inflator add:
android:descendantFocusability="blocksDescendants"

Categories

Resources