I have a listview which should contain some data (text) and an ImageView in each item.
So this is what I do, but it looks that the ImageView is not clickable :
here's a part the layout code :
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingBottom="5dp" >
<ImageView
android:id="#+id/searchIcone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scaleType="center"
android:background="#c7c7c7"
android:layout_marginTop="60dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="false"
android:src="#drawable/search" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="25dp"
android:onClick="searchVisiter"
android:textColor="#000" />
<TextView
android:id="#+id/CityCountry"
android:layout_below="#id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:textColor="#000" />
and my Java code :
final ListView lv1 = (ListView) findViewById(R.id.ListViewEvents);
lv1.setAdapter(new EventListViewAdapter(EventListActivity.this, records));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
JsonObject response = (JsonObject)o;
ID = response.getString("ID");
// If the image is clicked (doesn't work)
final ImageView img1 = (ImageView) findViewById(R.id.searchIcone);
img1.setClickable(true);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
}
});
// If the list item is clicked (works)
Intent intent = new Intent(EventListActivity.this, SearchActivity.class);
startActivity(intent);
}
});
Please, do you have any idea about this ?
Thank you.
What you do is setting listeners after clicking item on list - it doesn't make sense.
In method onItemClick you actually have View v, which was clicked, so you can handle that.
I think you tried to write something like:
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
JsonObject response = (JsonObject) lv1.getItemAtPosition(position);
ID = response.getString("ID");
if (v.equals(findViewById(R.id.searchIcone)) {
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
} else {
Intent intent = new Intent(EventListActivity.this, SearchActivity.class);
startActivity(intent);
}
}
});
Add onClickListner to getView method of EventListViewAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
....
ImageView img1 = (ImageView) convertView.findViewById(R.id.searchIcone);
img1 .setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
}
});
....
}
This should work
I have experienced the same problem.It is Because when you click on image on listItem ..the onclick() Method of ListView is triggered by Default.
The Mistake is you are registering setOnItemClickListener() for your listView lv1.
Instead just disable setOnItemClickListener() on listView and In your Adapter class EventListViewAdapter.java getView() Method just insert following code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//your layout inflater code here
ImageView iv = (ImageView) convertView.findViewById(R.id.searchIcone);
iv.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
}
});
....
}
Related
I m raeding csv file and showin it using the listview and textview so i want set setOnItemClickListener on list view of text...and when i click on item then other activity will get open..so please tell how do i code for this..
Here is my code...
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String listItem = (String) listView.getItemAtPosition(position);
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
}
Here is catlog report..
12-08 11:17:28.630 20936-20936/com.technostark.com.myloginactivity W/ResourceType: Skipping entry 0x108035a in package table 0 because it is not complex!
Update
<RelativeLayout
xmlns:android= "schemas.android.com/apk/res/android";
xmlns:tools= "schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape">
<ListView android:id="#+id/editlistview"
android:layout_width="match_parent"
android:layout_height= "wrap_content" />
</RelativeLayout>
Here is textview xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/et_Studname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Student Name"
android:textColorHint="#660000"
android:textSize="20dp"
android:textColor="#660000"
android:clickable="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:ems="20" />
</LinearLayout>
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView c = (TextView) view.findViewById(R.id.rating);
TextView d = (TextView) view.findViewById(R.id.title);
TextView e = (TextView) view.findViewById(R.id.releaseYear);
//Toast.makeText(Video_Main_List.this,Config.YOUTUBE_VIDEO_CODE, Toast.LENGTH_SHORT).show();
Intent launch = new Intent(Video_Main_List.this,Youtube_Player_Activity.class);
launch.putExtra("c", c.getText().toString());
launch.putExtra("d", d.getText().toString());
launch.putExtra("e", e.getText().toString());
startActivity(launch);
}});
// you can also take value from listview items and send into second activity that i mansion above.
ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
I am using FunDapter for my project.When i try to add imagebutton to my layout other texts become unclickable.Does nothing when i click them.How can i add button to this adapter?
Below is list_layout where everything works fine
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Isimsiz"
android:id="#+id/Housename"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="2dp"
android:clickable="false"
android:textColor="#241d1d"
android:layout_row="0"
android:layout_column="0"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Aciklama Yok"
android:id="#+id/Desc"
android:layout_below="#+id/Housename"
android:clickable="false"
android:layout_alignStart="#+id/Housename"
android:textColor="#241d1d"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_alignEnd="#+id/Housename" />
</RelativeLayout>
When i add image button nothing is clickable except imagebutton
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_alignTop="#+id/Housename"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/Desc"
android:clickable="true"
android:onClick="berk"
/>
And this is part of the class where i create my adapter
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.Housename, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.HouseID;
}
});
dict.addStringField(R.id.Desc, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.Desc;
}
});
FunDapter<Product> adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.layout_list, dict);
lvProduct = (ListView)findViewById(R.id.lvProduct);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do sth
}
public void berk(View v) {
//do sth
}
Note:Also when i give clickable attribute to texts in xml they stop working.Like When i play with layout XML everything stops working.
In Adapter Class
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent, false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
}
But you can get an issue - listView row not clickable. Solution:
make ListView focusable android:focusable="true"
Button not focusable android:focusable="false"
First of all if u are using button or edit text in your adapter or row file Then every thing will get stop working .
You should use
1.Set descendant Focusablity="before Descendants" in the list view.
2.Set window Soft Input Mode="adjust Pan" in the manifest
3.Set list view.set Items Can Focus(true)
you have to give separate click event for all views.
Your ImageButton has android:clickable="true" which makes sub view clickable. But you other views are not clickable.
Solution
For click event in textview
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
similar for Imagebutton
remove
android:clickable="true"
hope it helps.
I'm a beginner in android app development. I write simple app to show listview and items in the list. I want when users click on the item show me row text.
adapter
ArrayAdapter<String> myAdaptor = new ArrayAdapter<String>(MainActivity.this, R.layout.rowlayout,R.id.label, itemsList)
rowlayout.xml
<ImageView
android:id="#+id/icon"
android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="#drawable/maleki" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="50px"
android:layout_gravity="right"
android:paddingLeft="100px"
>
</TextView>
I write this code into the listview click:
ListView myLST=(ListView)findViewById(R.id.listView1);
myLST.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemString=((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), tourdetail.class);
i.putExtra("countrydetail",itemString);
startActivity(i);
}
});
but in the this line I get error:
String itemString=((TextView) view).getText().toString();
How can I solve this problem? Thanks.
Try:
String itemString = ((TextView) view.findViewById(R.id.label)).getText().toString();
or
String itemString = myAdaptor.getItem(position)
Why would you cast the selected view item to TextView, which is a combination of ImageView and TextView. Check this for your solution:
myLST.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView label= (TextView) view.findViewById(R.id.label);
String text = label.getText().toString();
//remaining stuff
}
});
I have a ListView in an activity which does note react to being selected. This could be a small thing as I am relatively new to Android, but don't seem to be able to figure it out myself.
My onLoad() in my Activity loads like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article_list);
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.containsKey("categoryId")) categoryId = extras.getLong("categoryId");
}
ListView lv = (ListView) findViewById(R.id.article_list_activity);
iDomsAndroidApp app = ((iDomsAndroidApp) getApplicationContext());
try {
objects = app.getDataManager().getArticlesForCategory(categoryId, 15);
adapter = new ArticleListAdapter(this, R.layout.article_list_cell, objects, categoryId);
lv.setOnScrollListener(adapter);
lv.setAdapter(adapter);
} catch (Exception e){
Log.e(iDomsAndroidApp.TAG, "Error " + e.getMessage(), e);
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), objects.get(position).getTitle(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), ArticleActivity.class);
intent.putExtra("articleId", objects.get(position).getId());
startActivity(intent);
}
});
}
Then in my Adapter (ArrayAdapter)
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = inflater.inflate(R.layout.article_list_cell, viewGroup, false);
}
Article article = getItem(i);
TextView titleText = (TextView)view.findViewById(R.id.article_list_titleText);
try{
TextView siteText = (TextView)view.findViewById(R.id.article_list_siteText);
TextView summaryText = (TextView)view.findViewById(R.id.article_list_summaryText);
RadioButton readBttn = (RadioButton) view.findViewById(R.id.article_list_readButton);
TextView updatedText = (TextView) view.findViewById(R.id.article_list_updatedText);
TextView date = (TextView) view.findViewById(R.id.article_cell_date);
titleText.setText(article.getTitle());
siteText.setText(article.getSite().getName());
summaryText.setText(article.getSummary());
date.setText(DateFormat.getMediumDateFormat(this.getContext()).format(article.getPubDate()));
if(article.getRead()){
readBttn.setChecked(false);
} else {
readBttn.setChecked(true);
}
if(article.getUpdated()){
updatedText.setVisibility(View.VISIBLE);
} else {
updatedText.setVisibility(View.INVISIBLE);
}
} catch (Exception e) {
titleText.setText("Error loading article content!");
}
return view;
}
article_list_cell.xml:
<?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:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Site"
android:id="#+id/article_list_siteText" android:paddingLeft="10dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Today 12:34"
android:id="#+id/article_cell_date"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:gravity="center">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/article_list_readButton" android:checked="false"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Updated"
android:id="#+id/article_list_updatedText" android:textColor="#1898ff"
android:layout_gravity="right"
android:visibility="visible"
android:textSize="12dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/list_background_overlay">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:id="#+id/article_list_titleText" android:maxLines="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Summary"
android:id="#+id/article_list_summaryText" android:maxLines="3"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My guess is RadioButton takes focus when you click list item.
So add
android:descendantFocusability="blocksDescendants"
to root element in article_list_cell.xml
Edit:
Try to add #Override to your onItemClick method
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {}
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String name=(String)parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(), name, Toast.LENGTH_LONG).show();
Intent i = new Intent(getBaseContext(),Webview.class);
//i.putExtra("USERNAME", name);
startActivity(i);
}
});
mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
lvUsers.setClickable(true);
lvUsers.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
// TODO Auto-generated method stub
Object o = lvUsers.getItemAtPosition(position);
UserBO obj = (UserBO) o;
Toast.makeText(Select.this, "Record Selected= "+obj.getId()+" "+obj.getName()+" "+obj.getEmail()+" "+obj.getContact(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(Select.this,Update.class);
startActivity(intent);
}
});
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="6px"
android:orientation="vertical"
android:layout_height="67px" android:id="#+id/rlt_main"
android:background="#E0FFFF" android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="16px" android:id="#+id/rlt_main"
android:background="#E0FFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_pid"
android:text="12"
android:layout_centerVertical="true"
android:textColor="#E0FFFF" >
</TextView>
</LinearLayout>
select.xml
<ListView
android:layout_height="wrap_content"
android:id="#+id/lv_user"
android:layout_width="fill_parent"
android:clickable="true"
>
Hi,above code is of onclick listview items selection.but when i click on item from listview nnothing is happening instead i want to call update activity.what is the mistake in my code??if anyone want to see code of XML layout then let me know..
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
Toast.makeText(Context,"Selected item is "+ position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(ListActivity.this, SecondActivity.class);
ListActivity.this.startActivity(i);
}
}
Hi I think you should try like this,
Intent intent = new Intent();
intent.setClassName(Select.this, <packagename>.Update.class.getName());
startActivityForResult(intent,0);