SIDEBAR
»
S
I
D
E
B
A
R
«
ListView not responding to onItemClick() from OnItemClickListener
Oct 19th, 2010 by Sachin Shah

While developing an Android app, I came across this issue when working on different ListViews under TabHost. I googled all around to find the answer to this issue and couldn’t find one solid answer. Many recommendations were to use ListActivity instead of TabActivity. But I did not want to use ListActivity, as there could be situation in my app where I have to add a new tab at runtime which will have ListView content. So finally I came across a post on stackoverflow and the solution was just too simple. The issue in my case was I had a custom list item layout that had a thumbnail ImageView, 2 TextViews and a CheckBox and following is the layout xml:

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/list_item"
	android:layout_width="fill_parent"
	android:layout_height="?android:attr/listPreferredItemHeight"
	android:padding="6dip"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<ImageView android:id="@+id/thumbnail"
		android:layout_width="60dip"
		android:layout_height="60dip"
		android:scaleType="fitCenter" 
		android:src="@drawable/placeholder_thumb"/>
 
	<CheckBox android:id="@+id/star"
		android:layout_width="40dip"
		android:layout_height="wrap_content" style="?android:attr/starStyle"
		android:layout_alignParentRight="true"
		android:layout_centerVertical="true"
		android:paddingLeft="6dip"
		android:paddingRight="6dip" />
 
	<TextView android:id="@+id/title"
		android:layout_toRightOf="@id/thumbnail"
		android:layout_toLeftOf="@id/star"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:textAppearance="?android:attr/textAppearanceLarge"
		android:gravity="center_vertical"
		android:singleLine="true"
		android:ellipsize="marquee"
		android:paddingLeft="6dip"
		android:paddingRight="6dip"
		android:layout_alignWithParentIfMissing="true" />
 
	<TextView android:id="@+id/description"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_below="@id/title"
		android:layout_alignLeft="@id/title"
		android:layout_toLeftOf="@id/star"
		android:gravity="center_vertical"
		android:paddingLeft="6dip"
		android:paddingRight="6dip"
		android:singleLine="true"
		android:ellipsize="marquee"
		android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>

So as you can see, there is a CheckBox in the list item and that takes the focus when I try to click on any list item. Here is a sample Java code to test it out:

TabsDemo.java

package com.android.tabsdemo;
 
import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
 
public class TabsDemo extends TabActivity implements TabHost.TabContentFactory {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		TabHost tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1")
				.setContent(this));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2")
				.setContent(this));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
				.setContent(this));
	}
 
	@Override
	public View createTabContent(String tag) {
		final ListView lv = new ListView(this);
		MyListAdapterAndListener adapterAndListener = new MyListAdapterAndListener(
				this, tag);
		lv.setAdapter(adapterAndListener);
		lv.setOnItemClickListener(adapterAndListener);
		return lv;
	}
 
	private class MyListAdapterAndListener extends BaseAdapter implements
			AdapterView.OnItemClickListener {
		private Context mContext;
		private String mTag;
 
		public MyListAdapterAndListener(Context context, String tag) {
			mContext = context;
			mTag = tag;
		}
 
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			Toast.makeText(TabsDemo.this, "Postion:" + position,
					Toast.LENGTH_SHORT).show();
		}
 
		@Override
		public int getCount() {
			return 10;
		}
 
		@Override
		public Object getItem(int position) {
			return position;
		}
 
		@Override
		public long getItemId(int position) {
			return position;
		}
 
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(mContext);
				convertView = inflater.inflate(R.layout.list_item, null);
			}
			TextView title = (TextView) convertView.findViewById(R.id.title);
			title.setText("Position: " + position);
			TextView description = (TextView) convertView
					.findViewById(R.id.description);
			description.setText(mTag);
			return convertView;
		}
	}
}

Solution:
So in order to receive the onItemClick() event, I had to set the android:focusable="false" and android:focusableInTouchMode="false" in the CheckBox layout xml and it started working!! Hope this helps others who come across same issue. Please post your comments if there is a better way to achieve the same.

Update:
If the widget inside the list item is ImageButton, then the above solution will not work. You will have to programatically tell the button to not focus using setFocusable(false). So your code may look like following:

			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(mContext);
				convertView = inflater.inflate(R.layout.list_item, null);
				ImageButton button = (ImageButton) convertView.findViewById(R.id.star);
				button.setFocusable(false);
			}
Fix missing icons in Eclipse menus on Lucid
Mar 12th, 2010 by Sachin Shah

I came across the same issue which was there in Karmic where Eclipse menu items had missing icons next to it. So I tried to fix it with the gnome-appearance-properties preference. But I couldn’t find “Interface” tab on that window, so I had to figure out a workaround for that. Luckily I came across following command:

gconftool -s /desktop/gnome/interface/menus_have_icons -t bool true

That fixed the issue!! So posting this here for others.

Source: LaunchPad

My thesis on Google Code
Jun 29th, 2009 by Sachin Shah

I just finished my master’s degree in Computer Science from SDSU after defending my thesis on topic of Distributed Computing. My thesis was a proof of concept to use Software Testing Automation Framework (STAF by IBM) for achieving similar results as @Home projects. You can get the source code from google code. Let me know if you are studying similar topic and have any questions about it.

Adding a XIB file associated with a Tab Bar Controller
Jun 27th, 2009 by Sachin Shah

I was struggling to get UIView in a different XIB file to be associated with file’s owner. Then I was able to figure out the differences between the XIB file I created and the XIB file created by XCode when creating a Project with type “Tab Bar Application”.

Following are the steps I had to take after creating a XIB file which gets associated with one of the Tabs in Tab Bar Controller.

  1. Select “File’s Owner” object in the new XIB file you created with a UIView and change the “Class Identity” to “UIViewController”.
  2. Then right click on “File’s Owner” and associate the “view” with the only UIView (may say “View” in the IB’s object viewer window) in the XIB.
  3. Select the UIView object from the Object Viewer Window and change the “Bottom Bar” attribute to “Tab Bar”.

That’s it!!! You should be able to build and run your project in simulator and see the Tabs are now loading correct views without any errors.

Basil Thai near SDSU
Apr 16th, 2009 by Sachin Shah

I tried this nice little Thai restaurant just outside SDSU and I loved it. Nice ambience, nice food at reasonable price.

UPDATE:
Seems like it is not there anymore :(

JCSC Pratishta Mahotsav 2008
Sep 27th, 2008 by Sachin Shah

It was a breathtaking experience for Jain community in southern California. Attaching pictures.

»  Substance:WordPress   »  Style:Ahren Ahimsa
© Copyright Sachin Shah