I've been working with ExpandableListViews before and I know about checkBoxes, Buttons, etc.
It is essential to setFocusable to false for any of these widgets that could be found on the XML that will serve as a groupView of our list, othervise, clicking on the group cell will not expand the group, since these widgets will steal focus from it.
BUT, If I put WebView in my GroupView, and setFocusable & setFocusableInTouchMode to false, I will still NOT be able to expand the group.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="91dp"
android:background="#ffffff">
<ImageView
android:layout_width="30dp"
android:layout_height="91dp"
android:id="#+id/sth_cell_img_correct"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/icon_checked" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/webView2" />
</LinearLayout>
But, if I remove WebView from the XML, I can expand / shrink cells with no trouble whatsoever.
There must be some other thing I should do here.
Som other attribute should be set to false, or something like that, but I cannot figure out what exactly.
Does anyone has a clue ?
Related
In my app, there is a tabbed activity with three fragments. The first fragment has a form to create a new task, the second fragment has the list of all the saved tasks, and the third fragment will show the comments on a task when selected from the list in the second fragment. The third fragment is also supposed to act like a chat activity which posts comments when you type them in and tap the send button. The XML layout of this third fragment is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:transcriptMode="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
As you can see, there is a TextView and a ListView and below that is another LinearLayout. Here is how it should look (the purple bar is the TextView):
And here is how it actually looks:
The TextView shows up above the ListView, but the LinearLayout does not. It is there, though. If I do android:layout_marginBottom="20dp" on the outermost LinearLayout, it does show up, but the ActionBar scrolls up and overlaps with the notification bar so that the title on the ActionBar and the notifications on the notification bar are both visible simultaneously and neither is legible.
I searched a lot and this seemed a common issue, but none of the solutions helped me. Believe me, I tried everything I could find. I tried wrapping the whole thing in a FrameLayout, using a RelativeLayout in place of the outermost LinearLayout, using two LinearLayouts--one to wrap the TextView and the ListView and the other to wrap the EditText and the ImageButton, etc., but nothing was able to show the bottom LinearLayout below the ListView. I even tried to set focus on the EditText when the fragment launches so that the keyboard would show and I can type, but even that doesn't help.
Note: On the other hand, if I use the exact same layout on an activity, the bottom LinearLayout displays exactly as it should.
I am unable to find the bug. Please help!
It looks like your toolbar pushes fragment layout down without decreasing its height. I have no idea why this happens (there are no root layout code here).
As a workaround you can set fragments layout bottom margin to ?attr/actionBarSize
As you have given layout_weight 1 in listview layout, so it occupies the whole space available. In order to get rid of you have to give some static height or use layout_weight in right manner
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="500dp"
android:transcriptMode="normal" />
or try like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
tools:context="com.example.ishita.assigntasks.CommentsFragment">
<TextView
android:id="#+id/frag_task_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dd55ff"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white">
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/white">
<EditText
android:id="#+id/frag_msg_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:layout_weight="1" />
<ImageButton
android:id="#+id/frag_send_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:contentDescription="#string/send_btn"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
All,
Thank you for all your responses. I found a workaround by trial and error now and thought I should post the answer for others who face the same issue. I set android:layout_marginBottom="50dp" on the inner LinearLayout (the one wrapping the comments bar--EditText and ImageButton). Somehow, this sets the layout correctly and the fragment functions properly in both Lollipop and Jellybean OS's. I haven't tested on other OS versions.
First of all you should read this, what is layout_weight and how it works.
You assigned layout_weight to ListView as 1, it means it covers whole height. just change it to 0.7 or any proportion you want (less than 1) will solve the problem.
<ListView
android:id="#+id/frag_comment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:transcriptMode="normal" />
I've been working with ExpandableListViews before and I know about checkBoxes, Buttons, etc.
It is essential to setFocusable to false for any of these widgets that could be found on the XML that will serve as a groupView of our list, othervise, clicking on the group cell will not expand the group, since these widgets will steal focus from it.
BUT, If I put WebView in my GroupView, and setFocusable & setFocusableInTouchMode to false, I will still NOT be able to expand the group.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="91dp"
android:background="#ffffff">
<ImageView
android:layout_width="30dp"
android:layout_height="91dp"
android:id="#+id/sth_cell_img_correct"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/icon_checked" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/webView2" />
</LinearLayout>
But, if I remove WebView from the XML, I can expand / shrink cells with no trouble whatsoever.
There must be some other thing I should do here. Some other attribute should be set to false, or something like that, but I cannot figure out what exactly.
Does anyone has a clue what to do in order to have WebView in group XML and be able to expand the group itself ?
Try setting this on the root element
android:descendantFocusability="blocksDescendants"
And then make the WebView unclickable with
android:clickable="false"
The final XML should be something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="91dp"
android:descendantFocusability="blocksDescendants"
android:background="#ffffff">
<ImageView
android:layout_width="30dp"
android:layout_height="91dp"
android:id="#+id/sth_cell_img_correct"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/icon_checked" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:id="#+id/sth_web_view" />
</RelativeLayout>
Hope this helps.
I'm working on a requirement that involve scrollview and textview. Clients require that normally the scroll bar is hidden but when user click (touch) the scrollview, which contain a textview, the scrollbar will appear for a short time, say 2 secs, to let user know that the this view is scrollable.
It seems very simple. But I have tried some ways but none of it could achieve this effect. I have tried:
-Assign the scrollview a id, then add onclicklistener function in activity, use setScrollbarFadingEnabled() function combined with a timer. Not work. I even tried to set it to both scrollview and textview.
-use ScrollView.scrollBy(0,1) with OnClickListener. Not work.
Here is my xml part for this section:
<ScrollView
android:id="#+id/iScrollView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="left"
android:layout_weight="1.5"
android:background="#drawable/border"
android:clickable="true" >
<TextView
android:id="#+id/iTextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:maxLines="15"
android:minLines="3"
android:padding="5dp"
android:text="Connecting..."
android:textColor="#777777"
android:textSize="15sp" >
</TextView>
</ScrollView>
Is anyone have a simple way to achieve this effect? (I mean, just default android functions, no third party libraries.
Thank you!
Try add android:scrollbarFadeDuration="X" to your ScrollView. X is in miliseconds, for instance, if you want the scroll bar to show for 2 seconds, you should have the following code:
<ScrollView
android:id="#+id/iScrollView"
android:scrollbarFadeDuration="2000"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="left"
android:layout_weight="1.5"
android:background="#drawable/border"
android:clickable="true" >
Consider the Hotmail app for Android. When you check an e-mail item, three buttons appear at the bottom: [Mark Read] [Mark Unread] [Delete]
When you uncheck it, the buttons go away again.
What's the layout for this? I've tried this, but it yields scrolling problems at the bottom (can't see last item):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip" >
<Button
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="#string/mark_read" />
</LinearLayout>
Then, I also need to show/hide this stuff?
Changing the visibility of bottom linearlayout will show/hide it. You'll need to give it an id and then
LinearLayout bottomLayout = (LinearLayout)findViewById(R.id.someId);
bottomLayout.setVisibility(View.GONE)// or View.VISIBLE
As for the scrolling problem, that occurs because the RelativeLayout overlays view components, so you can either show/hide the button overlaying the bottom of the ListView or change the Relativelayout to a LinearLayout so that the ListView ends before the button and change the visibility.
Though I'm not sure this will look very good when you suddenly show the button and the ListView has to resize itself.
Note on visibility
setVisibility(View.GONE);
will remove the view from the layout and other component may resize due to this. However using
setVisibility(View.INVISIBLE);
keeps the space the view took up in the layout and simply makes the view invisible and no resizing will occur.
I have a fairly simple application that uses a ListActivity to display a ListView. Each ListView item has a CheckBox so that the user can specify favorite items. I am using a class that extends SimpleCursorAdapter so that I can capture both the CheckBox changed event (to save the change to the database) and the list item clicked event (to load an item details activity).
After the OnCheckChangedListener implementation updates the database, I call .requery() on the cursor so that when an item scrolls off and then back on to the screen, the CheckBox has the proper state.
That all works fine. Where I am having problems is in another activity where I am reusing the same ListView layout and the same cursor adapter to display a specific subset of items as part of a different activity's layout. When I use the layout there, everything is fine until I tap the CheckBox, at which point the entire ListView disappears. The database is updated, but until I leave the activity and come back, the ListView is just totally gone.
If I don't call .requery() on the cursor, everything works fine; the ListView does not disappear (but the CheckBox state will be wrong some of the time, as described above).
Working layout, event_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" android:text="#+id/eventsHeader"
android:id="#+id/eventsHeader" android:gravity="center_vertical"
android:layout_height="wrap_content" android:textSize="15sp" />
<ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/showHistoricalEvents" />
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/showHistoricalEvents"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:text="Show Past Events" android:textSize="14sp" />
<TextView android:id="#+id/android:empty" android:text="#string/no_events"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/showHistoricalEvents" />
</RelativeLayout>
Broken layout, track_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content" android:id="#+id/InterestLabel"
android:layout_alignParentLeft="true" android:text="#string/interest_level"
android:gravity="center_vertical" android:layout_height="fill_parent"
android:layout_above="#+id/track_name" />
<Spinner android:layout_height="wrap_content" android:id="#+id/InterestSpinner"
android:entries="#array/priority" android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/FavoriteCheckbox" android:layout_width="fill_parent"
android:layout_toRightOf="#+id/InterestLabel" />
<CheckBox android:layout_width="wrap_content" style="?android:attr/starStyle"
android:id="#+id/FavoriteCheckbox" android:layout_alignParentTop="true"
android:layout_alignParentRight="true" android:gravity="center_vertical"
android:layout_height="fill_parent" android:layout_above="#+id/track_name" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/track_name"
android:text="#+id/track_name" android:textSize="25sp"
android:layout_below="#+id/InterestSpinner" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_below="#+id/track_name" android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView android:text="#+id/track_description" android:id="#+id/track_description"
android:scrollbars="vertical" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:paddingLeft="5dp" android:paddingTop="5dp" />
<include layout="#layout/event_list" android:id="#+id/trackEventList"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="2.5" />
</LinearLayout>
</RelativeLayout>
My only thought right now is that the way I am using layout_weight in the second layout is causing some weirdness. I am loath to change that, however, since it took me quite some time to get the layout to work the way I wanted it to in the first place.
One other note, when I am including the first layout in the second, I am hiding the TextView and CheckBox controls because they are unnecessary.
Any ideas what I am doing wrong here?
Where I am having problems is in
another activity where I am reusing
the same ListView layout and the same
cursor adapter to display a specific
subset of items as part of a different
activity's layout.
If you mean the same instance of SimpleCursorAdapter, that is so not a good idea. Please make a new adapter.
When I use the layout there,
everything is fine until I tap the
CheckBox, at which point the entire
ListView disappears. The database is
updated, but until I leave the
activity and come back, the ListView
is just totally gone.
Use hierarchyviewer to determine if the ListView has no children (rows), has no height, is invisible/gone from a visibility standpoint, or is truly removed from the view hierarchy. That will give you better ideas of how to proceed.
Any ideas what I am doing wrong here?
Besides the adapter problem mentioned above, you're using CheckBox. Android has built-in support for multiple-select lists (e.g., android:choiceMode="multiple"), but that needs a CheckedTextView. I would aim to use Android's built-in multiple-selection logic rather than rolling your own.