Android setOnclick listner to listview - android

Actually i have a viewpager that holds a Imageview and a listview over it, So when i click on the Listview i need to hide the listview and the bottom bar to make the ImageView fullscreen for all the views in side the viewpager. And onclick of the image view get back the views. But my issues are:
I cant implement onClickListner on Listview, to make the whole view to handle the tap. Am forced to use the onItemclickListner and it only hides when you click on the item(which is not desired).
When i click Listview(which is inside Fragment) inside the pager it doesn't hide for all the views except for the currentview, But am able to hide the actionbar and the bottom bar(on the Fragment Activity).
a screen shot for reference:
How can i achieve this or this is there any better way that i can hide the views.
Here is my ListView in side a Fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:scrollbars="none" />
</RelativeLayout>
Here's my Fragment Activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/form" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="2dp" >
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnSend"
android:drawableRight="#drawable/edit_keyboard"
android:inputType="textMultiLine"
android:maxLines="3" />
<ImageButton
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/send" />
</RelativeLayout>
</RelativeLayout>
Here am successful with the onitemclicklister to the ListView:
listComments.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (getSherlockActivity().getSupportActionBar().isShowing()) {
getSherlockActivity().getSupportActionBar().hide();
listComments.setVisibility(View.GONE);
form.setVisibility(View.GONE);
} else {
getSherlockActivity().getSupportActionBar().show();
listComments.setVisibility(View.VISIBLE);
form.setVisibility(View.VISIBLE);
}
}
});
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getSherlockActivity().getSupportActionBar().show();
listComments.setVisibility(View.VISIBLE);
form.setVisibility(View.VISIBLE);
}
});

Could you set the clickable property for the listView and check ? Also, whenever you add a new edittext to the do you add it dynamically ? If so that would mean that the you are adding items to the listView dynamically. The listView would not know if you have clicked on an item of the view itself hence a callback would get registered when you click the item as that takes preference.

Related

Neither setOnItemSelectedListener nor setOnClickListener works for GridView

The GridView is the following:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:fadeScrollbars="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="afterDescendants"/>
The Java code is the following:
gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(iColumnCount);
gridview.setAdapter(dta);
gridview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int iPosition, long l) {
utility.logd("gridview", "selected item position:" + iPosition);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
utility.logd("gridview", "nothing selected.");
}
});
gridview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
utility.logd("gridview", "item clicked");
}
});
Only onNothingSelected() is called once after the apps starts. None of the other listeners is called when the focus on the GridView is changed with a remote control, or any item is clicked.
Could anyone offer a tip on how to make the listeners work?
Everything else of the GirdView works as expected.
Edit: The following is the layout for each grid cell:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_gravity="center"
android:id="#+id/biyee_relativeLayoutRoot">
<ImageButton
android:id="#+id/imageButton"
android:background="#drawable/button_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="snapshot"
android:layout_gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_margin="0dp"
android:padding="10dp"
android:src="#drawable/ic_launcher"
android:onClick="onClick_imageButton"/>
<LinearLayout
android:id="#+id/linearLayoutCaption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_overlay"
android:orientation="vertical"
android:layout_margin="10dp"
android:padding="5dp">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textViewModel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Model"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<TextView
android:id="#+id/textViewPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="n"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_margin="15dp"/>
</FrameLayout>
Edit (2):
Per the tip of Daniel Nugent, I removed the ImageButton from the custom item view layout for the purpose of testing, and OnItemSelectedListener started working perfectly. Therefore the culprit is ImageButton. It seems any clickable object would ruin OnItemSelectedListener.
Edit (3): For the sake of testing, I used the following layout for the custom item view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical"
android:background="#color/red">
</LinearLayout>
OnItemSelectedListener works perfectly, but OnClickListener and OnItemClickListener are never called no matter whether I use a mouse or remote control to click an item.
Try using OnItemClickListener instead of onItemSelectedListener
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
//Your Logic here
}
);
onItemSelectedListener will do the job if you implements some kind of selection like arrows or some other thing.
From developer.android.com
Callback method to be invoked when an item in this view has been
selected. This callback is invoked only when the newly selected
position is different from the previously selected position or if
there was no selected item.
Impelmenters can call getItemAtPosition(position) if they need to
access the data associated with the selected item.

Touch not working on list view

I am using touch event on LinearLayout in which there is a list view(Its a child). When I touch on list view part touch event does not gets called. below is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llFavourite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:gravity="center"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Loading"
android:indeterminate="true"
android:progressDrawable="#android:drawable/progress_indeterminate_horizontal"
android:visibility="gone" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:focusable="true">
</ListView>
</LinearLayout>
My animation:
final Animation slidedown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down_half);
llFavourite.setOnTouchListener(new OnSwipeTouchListener(MidnightMainActivity.this) {
#Override
public void onSwipeBottom() {
llFavourite.startAnimation(slidedown);
inflateFavouriteLayout(MODE_REFRESH);
}
});
but when I click on layout part event gets called. Is there any property which i should enable or disable?
Thanks in advance.
android:focusable="false"
android:focusableInTouchMode="false"
doesn't work for ImageButton.
In your layout xml, add this property to root layout
android:descendantFocusability="blocksDescendants"
from your calling class add
ListView lView = (ListView) findViewById(R.id.list);
lView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(position);
}
});

ListView item not found

My app is using landscape full screen mode and the navigation drawer. I am using listView in my app along with an edittext. The edittext is the search bar that will search the listview. Both the listview and the edittext are in the navigation drawer. But, when there is no list item that matches the searched word, the listview gets empty.
So how can I add a "Item not found" message instead of the blank listview?
I searched a lot on the internet and found a method setEmptyView(); but I couldn't understand it and hence it is not working. Please help me! Maybe this question is already asked here but please give me an easy explanation.
Here is my code:
MainActivity.java
public class MainActivity extends FragmentActivity {
final String[] data = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium"};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
//the code below will automatically close the keyboard when the user will touch the listview
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
}
mainactivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="240dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="230dp"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint=" search" >
</EditText>
<TextView
android:id="#+id/notfound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium"
android:gravity="center">
</TextView>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Yes you have mentioned about setEmptyView() correctly, you should use it if you would want to show empty message whenever ListView gets empty.
Now here is a xml layout and code depicts how to use setEmptyView exactly.
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layoutTitlebar" >
<ListView
android:id="#+id/listViewFriends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000" >
</ListView>
<TextView
android:id="#+id/empty"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/strNoRecordsFound" >
</TextView>
</LinearLayout>
Now, you have to set this empty view (i.e. TextView) to ListView by using:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
// set your adapter here
// set your click listener here
// or whatever else
listViewFriends.setEmptyView(findViewById(R.id.empty));
This method is all that you need
http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)
Have in mind it accepts a view, so you have to inflate the layout and fill out any/all text you may have there by yourself.
EDIT:
Let's assume you have a layout named "empty_text" like this:
<?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="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There are no entries in this list"
android:gravity="center"/>
</LinearLayout>
Hint: I burned in the string for example purposes, heed the IDE warning and use a string identifier for I18n's sake
Now you would use this code to make it all work with the ListView:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View emptyTextView = inflater.inflate(R.layout.empty_text, null, false);
listView.setEmptyView(emptyTextView);
This code assumes you are executing inside an Activity, but just in case you don't plan on pasting it on an Activity, any Context instance will work.
That should be it.
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
Then your listview will automatically use this view when its adapter is empty.
Detailed Solution:
Layout:
<ListView
android:id="#+id/listViewFriends"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000">
</ListView>
<TextView
android:id="#+id/empty"
android:text="#string/strNoRecordsFound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Large"
android:gravity="center">
</TextView>
</LinearLayout>
Class File:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
listViewFriends.setEmptyView(findViewById(R.id.empty));
Source

Android: Listview's row height changes when clicking on the checkbox it contains

I got an activity that contains a single listview.
This listview displays a relativeLayout with a bitmap and a checkbox and when no bitmap is available, a text is displayed.
My problem is with rows that contains no bitmap. When I click on the checkbox, the height of the row changes (increase when checkbox is checked, de crease when unchecked)
activity.xml
<?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/gradient_divider"
android:dividerHeight="1dp" />
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:layout_toLeftOf="#+id/registrationCheckBox"
android:filter="true" >
</ImageView>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:paddingLeft="5dp"
android:layout_toLeftOf="#+id/registrationCheckBox" >
</TextView>
<CheckBox
android:id="#+id/registrationCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:paddingRight="5dp" >
</CheckBox>
</RelativeLayout>
My activity code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
hideActionBar();
// init the controls
initControls();
// populate the listView
this.adapter = new RegistrationAdapter(this, refreshContent());
this.listView.setAdapter(this.adapter);
this.listView.setItemsCanFocus(false);
this.listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.listView.setOnItemClickListener(
new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
...
}
}
);
}
Any idea on how to prevent my row height to change when clicking on the checkbox ?
I think you are missing the important part of this code, and that is the Adapter code.
How are you handling switching between bitmap and text?
Perhaps set the ImageView to a fixed height (tall enough to fit in any actual image), or provide a dummy invisible image when there is no real image to show.

android how to access customized listview item

i tryied to custom my ListView , so i use this item layout 「 list_item.xml」
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="10dp"
android:textSize="16dip"
android:textColor="#000000"
android:gravity="center_vertical"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1">
</TextView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFFFFF"
android:padding="10dip"
android:gravity="center_vertical">
<ImageView
android:id="#+id/pause"
android:src="#drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseBtn_onClick"
/>
<ImageView
android:id="#+id/play"
android:src="#drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playBtn_onClick"/>
</RelativeLayout>
</LinearLayout>
just as what you saw, there are 2 images in this layout, what i wanna do is,
when user click any item , the play_icon(the second image in the listview item) , will
disappear.
so i write code as the following :
mp3_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View _view, int index , long arg3)
{
String _url = musicList.get(index).get("link").toString();
LinearLayout _container = (LinearLayout)mp3_listView.getChildAt(index);
RelativeLayout _container2 = (RelativeLayout) _container.getChildAt(1);
ImageView img = (ImageView)_container2.getChildAt(1);
img.setVisibility(View.INVISIBLE);
Toast.makeText(tabDigTest.this , "GG : "+_container.findViewById(R.id.play) , Toast.LENGTH_SHORT).show();
}
}
but something strange occurred .... more than one item's img be hidden because of the:
img.setVisibility(View.INVISIBLE);
i don't why because i only set one img to INVISIBLE ......i think i need help O_o
To retrieve the user click, I guess you set up an OnItemClickListener.
ListView lv = (fecth your list vieW)
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//you might want to use 'view' here
}
});
In the onItemClick method, the view parameter is the view you returned in your ListAdapter's getView method. So if say, you have returned a LinearLayout containing three childrens with id ("textview", "imageview1", "imageview2"), you would simply call view.findViewById(R.id.imageview1) to retrieve the first ImageView.

Categories

Resources