I have a list view with my custom content. I have on message iamgeview, one call and other option. So that user can select any one of the option, but if user select any text(touch the textview) or any other portion of ListView, it should not respond. I have small UI.
So if user touches on MyText, nothing should be done. Not even Visual feed back what we get when we touch list tiem normally in android. But touch event for call, or message should come.
My code snippet
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listitem_contact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="#+id/tv_contact"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="false"
android:gravity="left|center_vertical"/>
<ImageView
android:id="#+id/iv_call"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/list_item_image_color"
android:src="#drawable/ic_call"/>
<ImageView
android:id="#+id/iv_message"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/list_item_image_color"
android:src="#drawable/ic_textmessage"/>
<ImageView
android:id="#+id/ic_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/list_item_image_color"
android:src="#drawable/ic_phone"/>
</LinearLayout>
PS: On touching the portion of textview, by default first ImageView is getting selected. I don't want that any wodget should respond unless touched
The visual feedback on touching an item is a style on the ListView. You can change it as below:
<ListView
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
/>
However, it's best to define your own styles and apply them to the views.
Related
Quick background: I have a portion of my activity in a relative layout. I did this on purpose because I wanted a button directly below a listview. I want the button to move down as the listview expands which is why I set it up this way. I've set the height of the listview to wrap content, this way in the relative layout, the button will move down as the list expands.
Issue: Once the list gets big enough such that the content fills up the screen, the button remains below the list (which is fine) but I can't scroll down the list to reveal the button. The button "disappears" below the list. How can I make it so that I can scroll on the list/screen to reveal my button?
Edit: I do want the button to go off screen, I just want to be able to scroll down to see it again.
Sample code below + images:
3 pics, one showing the intial layout, next you can see the button moves as my list expands, 3rd, eventually the button reaches the bottom and I can't scroll more to click it.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/workoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/logExerciseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/workoutList"
android:text="#string/log_set" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/workoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/logExerciseButton" />
<Button
android:id="#+id/logExerciseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/log_set" />
</RelativeLayout>
wrap your ListView with ScrollView:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/workoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/logExerciseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scrollView"
android:text="#string/log_set" />
</RelativeLayout>
Adding the button as a footer to the listview resulted in the desired behavior: https://www.tutorialspoint.com/how-to-add-footer-in-android-listview
I have an interesting dilemma. I have a ListView which I am using to contain CardViews. However, each CardView is actually a pair of two: one default (front) card, and then a back card that I am animating as a flip upon the user clicking the card. Since the user may not click on any list item, I am using a ViewStub as the back card, and will lazy inflate upon the list item click.
So the first time the user clicks a card to flip to the back, everything looks good and the flip works (onItemClick() gets called from clicking anywhere within the card). However, any further attempts to flip over to the front again do not trigger the onItemClick() call, UNLESS I click the very top of the view (list item).
I've done lots of debugging (changing focusability and sizing) and figured out that this is being caused only after the ViewStub inflation. Making both front and back cards normal CardViews allows the onItemClick() to fire successfully every time.
So what is the reason why the ViewStub is fubaring my onItemClick()? Is there a way to fix it, rather than have all of my views inflated automatically?
Here is the code I'm using:
card_container_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<include layout="#layout/card_front_view"
android:id="#+id/main_card_view_layout" />
<ViewStub android:id="#+id/stub_flipped_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="#+id/flipped_card"
android:layout="#layout/card_back_view" />
</FrameLayout>
card_front_view.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
android:id="#+id/card_view"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/card_view_height">
<ImageView android:id="#+id/img_main_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/my_image"
android:background="#ff10fffd" />
<TextView
android:id="#+id/lbl_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
style="#style/main_card_title_text"
android:layout_alignParentBottom="true"
android:padding="15dp"
android:background="#5b000000"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:padding="4dp" >
<TextView
android:id="#+id/lbl_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
style="#style/main_card_text" />
<ImageView android:id="#+id/img_sync_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
card_back_view.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="5dp"
android:id="#+id/flipped_card"
android:alpha="0"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here are the items:"
android:gravity="center"
style="#style/card_flipped_title_text"
android:background="#ad000000"/>
<GridView android:id="#+id/grid_images"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="3">
<!-- make believe there were items here -->
</GridView>
</LinearLayout>
</android.support.v7.widget.CardView>
Ok, I did more digging into it and it appears that it's not the ViewStub at all causing it, but in fact the GridView. For reasons that I was not able to figure out, the GridView continued to hijack all touch events, despite setting focusable and clickable to false and blocking descendent focusability as seen in the sample XML above.
Therefore the hacky way I've worked around it, is to intercept all touch events from the GridView, and within the onTouchListener(), if I detect a MotionEvent.ACTION_UP, indicating that I have been touching the screen and just released, I will then manually perform the same action as I am doing in the rest of the CardView.
itemGrid.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View grid, MotionEvent event) {
// hacky way to make sure that we absorb the GridView's focus stealing and still do the right thing
if (MotionEvent.ACTION_UP == event.getAction()) doExpectedStuff();
return true; // absorb the touch here
}
});
Sorry that this isn't the most elegant of solutions, and agree that capturing an up event is not a one-size-fits-all approach for most people, but it helped in my specific situation. Hope that this still may help others.
In a layout file I have a Listview whose size can grow/shrink dynamically. I have a button btn_rec_add and it's click event I add an item in the ListView. I have tried many changes in the Layout file but haven't been able to make the button shift its location based on number of items in the ListView. If I keep the button in the same RelativeLayout which has the ListView, then the button moves dynamically which is exactly how I want but I can't see the button after adding 5 or more elements in 4.3 inch display phones. If I keep the button outside the RelativeLayout of the ListView, then it is fixed on the screen.
Currently, the btn_rec_add is fixed to the bottom of the layout. Can someone please help me solve this problem.
Here is the XML code:
<?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:background="#drawable/bg" >
<ImageView
android:id="#id/top_bar_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/top_bar"
android:contentDescription="#string/content" />
<TextView
android:id="#+id/txt_recipients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:padding="8dp"
android:text="#string/text_recipients"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<ImageButton
android:id="#id/btn_back"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/content"
android:paddingTop="6dp"
android:src="#drawable/ic_back" />
<RelativeLayout
android:id="#+id/Rlayout_recipients"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/btn_rec_add"
android:layout_alignParentLeft="true"
android:layout_below="#id/top_bar_view" >
<ListView
android:id="#+id/rec_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp"
android:paddingTop="20dp" />
</RelativeLayout>
<ImageButton
android:id="#+id/btn_rec_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/content"
android:src="#drawable/icon_add" />
</RelativeLayout>
If I understand correctly, you want the behavior of the button to be as follows:
Appear below the last ListView item if the ListView does not extend to fill screen
If the ListView extends the full height of the screen, the button should be at the bottom of the screen, but the list should remain scrollable
If my understanding is correct, you should place your ListView and your button in a LinearLayout as follows:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="#dimen/button_height"
android:background="#drawable/button_image" />
</LinearLayout>
The effect of the above layout is as follows:
Layout in which items are vertically placed
Layout which will be as wide as parent, but as tall as ListView and Button
ListView will take up all of the space in the layout that the button does not occupy (this is layout_weight="1" whereas Button has no layout weight so it will simply fill as much space as it needs as defined in this case by #dimen/button_height)
Great Android layout question!
Your Problem is related to User Experience.
You have to decide whether user will like to scroll to end of the list to press add button
or user want to add without scrolling to end of list.
Since you only have two options with our scenario,
either keep add button fixed or add it as footer of listview.
I would like to split an item in a ListView to get the same result as the ListView in Samsung's Contact phones list:
When clicking on the left/right column, only that column's background changes. I tried inserting an Image Button to a one column list view, but when not clicking directly on the button, the whole row's background changes (including the ImageButton's background).
How is it possible to get the same ListView as the of Samsung?
I used this code and it work for me good
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:drawableRight="#drawable/ic_launcher"
android:gravity="center_vertical"
android:text="trtrtrt" />
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Use android:listSelector="#null" in list's xml and if it does not work then I want to have a look into your code if you can post it.
I am creating an application that contains a header with a button on the right that should slide a list view to left when I click on it(similar to the chat list in Facebook)
-->
When I was including this header without putting the list view in it, it was working properly. But now I am inserting this list view inside the .xml file of the header (because the header is always shown for the user and he can click on this list view button in any layout that is shown for him).
The problem that I want to include this header to all the other layouts without changing the way they looks and show this list each time he click on the button.
I searched and found a lot of answers but every solution was about making the same as I did in the previous screen shots in each page which is not an optimistic solution for multiple pages.
*I am working on API level 10 and want to find the solution without using Actionbar.
Tell me please if more information would be helpful, any help will be really appreciated.
Thanks in advance.
UPDATE my xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GeneralRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout android:id="#+id/rel_layout"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_alignParentRight="true">
<LinearLayout android:id="#+id/fake_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"></LinearLayout>
<ListView android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/background_dark"
></ListView>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_layout_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<LinearLayout android:id="#+id/lin_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="vertical"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide"
android:layout_gravity="right|center"></Button>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Try to create a RelativeLayout for the ListView with a android:visibility="gone" and all the other components in the same User Interface inside another RelativeLayout.
Then change its visibility upon the state of the menu if it's opened or closed.
Guess this should be a way to go:
<?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:clickable="false">
<LinearLayout
android:id="#+id/thisistopbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="3dp"/>
<include here listview etc../>
</LinearLayout>
you can also make the top bar general and include it in every screen. But the basic idea is like above xml, and put the button on the top bar.