Android textView scroll - android

I have been trying to develop an android app that contain an activity which is scrollable
and inside the activity, I have 2 textView which I want them to be scrollable as well.
My problem is that
whenever I touch inside the textView the scrolls show up but the main activity part which is the linearLayout directly steel the focus and no longer the textView scroll
the way I have it now is setup for the textView to scroll in the xml file I have done that.
and if I keep raising my finger and put it back on the screen trying to scroll the textView I can move a little bit but as I said, the layout that contains the textView capture the focus and leave me unable to scroll the textView.
I hope I did explain my problem very well.
Please any suggestion to help.
let me explain how the app is built first
first part is: the main(first) activity is a tabHost
which I defined as scrollable
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="422dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="63dp" >
</TabWidget>
</LinearLayout>
</ScrollView>
</TabHost>
where I am having problem with is this activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/light_gray_color"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/linearLayout3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/black_color"
android:layout_gravity="center_horizontal"
android:text="#string/display_label"
android:id="#+id/display_label">
</TextView>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.83"
android:baselineAligned="false"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/english_scrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.03" >
<TextView
android:id="#+id/display_english_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2px"
android:layout_marginRight="2px"
android:layout_weight="0.03"
android:background="#drawable/black_rectangle"
android:focusable="true"
android:paddingLeft="2px"
android:paddingRight="2px"
android:scrollbars="vertical"
android:textColor="#color/black_color" />
</ScrollView>
<ScrollView
android:id="#+id/translation_scrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.03" >
<TextView
android:id="#+id/display_translation_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2px"
android:layout_marginRight="2px"
android:layout_weight="0.03"
android:background="#drawable/black_rectangle"
android:focusable="true"
android:paddingLeft="2px"
android:paddingRight="2px"
android:scrollbars="vertical"
android:textColor="#color/black_color" />
</ScrollView>
</LinearLayout>
</LinearLayout>

I guess you should cut <ScrollView> out of the first xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="422dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="63dp" >
</TabWidget>
</LinearLayout>
</TabHost>

My understanding is that you have a vertically scrolling activity, which contains two vertically scrolling text views? If so, you can't do it. Its not just a poor design choice, its literally impossible to tell which view the user intends to scroll when they make swipe motion. You can have horizontal scrolling inside a vertically scrolling view, or vice versa. You can't have horizontal scrolling inside horizontal scrolling or vertical scrolling inside vertical scrolling.
I believe this is mentioned in the listview talk in the 2010 google io conference: http://www.youtube.com/watch?v=wDBM6wVEO70

Related

Got Warning : this linearlayout layout or its framelayout parent is possibly useless

<TabHost
android:id="#+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
// --------------------------- Here I got warning -------------------------
<LinearLayout
android:id="#+id/chat_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="visible" >
// --------------------------------------------------------------------------
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white" >
</ListView>
<LinearLayout
android:id="#+id/text_entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<EditText
android:id="#+id/txt_inputText"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:hint="#string/enter_text"
android:inputType="text" />
<Button
android:id="#+id/btn_Send"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="#string/send" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" >
</TabWidget>
</LinearLayout>
</TabHost>
How to solve this warning ? I can't find any solution. Any help will be appreciated.
I am not sure what exactly you are trying to accomplish, but you can get rid of the warning by moving FrameLayout content to a separate file and add it back to your TabHost by using <include> tag.
A possible version could look something like this:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<include layout="#layout/tab_chat_list" />
</FrameLayout>
</LinearLayout>
</TabHost>
And tab_chat_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/chat_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="visible" >
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white" >
</ListView>
<LinearLayout
android:id="#+id/text_entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<EditText
android:id="#+id/txt_inputText"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:hint="#string/enter_text"
android:inputType="text" />
<Button
android:id="#+id/btn_Send"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="#string/send" />
</LinearLayout>
</LinearLayout>
</merge>
Hope this helps.
Update
I noticed that some people are suggesting to remove FrameLayout. Although, that could have solved the problem in other cases, TabHost needs TabWidget and FrameLayout to function correctly.
The warning you are getting is this one:
UselessParent
Summary: Checks whether a parent layout can be removed.
Priority: 2 / 10 Severity: Warning Category: Performance
A layout with children that has no siblings, is not a scrollview or a
root layout, and does not have a background, can be removed and have
its children moved directly into the parent for a flatter and more
efficient layout hierarchy.
It can be turned off in Lint.
Update: I missed that tabhost requires a framelayout to work. Disabling the warning or going with shoe rat's solution should still fix the problem. (ignore paragraph below)
Assuming you don't need the FrameLayout (i.e. to add stuff dynamically), you can try removing it and adjusting the LinearLayout accordingly:
<LinearLayout
ndroid:id="#+id/chat_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="visible" >
Take a look at the following layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rlInner"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text" />
</RelativeLayout>
</LinearLayout>
There is nothing wrong with it and any activity using this layout will run without a problem. But eclipse will give you a warning:
This RelativeLayout layout or its LinearLayout parent is useless
As you can see, I only need one of the two container layouts above. Adding the RelativeLayout with id="rlInner" does nothing to change the layout. When eclipse finds a scenario like this, it gives you an appropriate warning to indicate the redundancy.
In your scenario, you can lose the FrameLayout altogether without disturbing the layout. And this would fix the warning.
Add an ID to the parent linear layout.
It's useless because the parent linear layout doesn't have an id. It means it would be difficult or impossible to add stuff in code outside the linear layout where you got the error.

How to place both listview and textview in one page in android?

I am displaying a TextView and below that a ListView in one page. My code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="#+id/widgetlay"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txt1"
android:text=""
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
</TextView>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:id="#+id/listlist"
android:layout_height="0dp"
android:layout_weight="1.0">
</ListView>
</RelativeLayout>
Here I am displaying the ListView data dynamically....but in my emulator it is displaying a TextView at the top and then the ListView with scroll...It is not expanding the list and instead it is scrolling with the content which is generated dynamically...Please tell me how to expand the entire ListView....
Thanks in advance
<?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">
<LinearLayout android:gravity="center|center_vertical"
android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="status"
android:id="#+id/txtvw1">
</TextView>
<ListView
android:layout_width="fill_parent"
android:id="#+id/listlist"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtvw1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="status" >
</TextView>
<ListView
android:id="#+id/listlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1">
</ListView>
</LinearLayout>
You have an error here
<ListView
android:layout_width="fill_parent"
android:id="#+id/listlist" //error here
android:layout_height="fill_parent">
</ListView>
the ListView is filled of the LinearLayout
so that you can't get the TextView.
instand of this
you can layout your view as follows:
<ListView
android:layout_width="match_parent"
android:id="#+id/listlist" //error here
android:layout_height="0"
android:layout_weight="1.0>
android:layout_weight="1.0" //this will make the list view filled with the rest of the screem
Further more
I suggest you use "match_parent" instand of "fill_parent"
this WAS CHANGED in the SDK level 8...
OK, now you use the RelativeLayout install of the LinearLayout
So you should add the relationship with your views in your layout.
You can layout as this:
<RelativeLayout
android:id="#+id/widgetlay"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</TextView>
<ListView
android:id="#+id/listlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txt1" />
android:layout_below="#id/txt"----Indicate the relationship with the layout
The RelativeLayout does not have the attr "orientation" and "layout_weight". So I don't suggest you to use this layout, It can't be fixed with the screen height in that way.
The good layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtvw1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="status" >
</TextView>
<ListView
android:id="#+id/listlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
Further more, what do you want to layout your views?
This is a part of my work. List view will appear in the middle of your screen and what ever u need at the top, put it in place of header() and things that u need in bottom of the screen , put it in place of footer(). U can use relative layout instead of the linear layout
Hope this helps.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:layout_width="match_parent" android:layout_alignParentTop="true"
android:layout_height="wrap_content" android:id="#+id/layout_header">
<include layout="#layout/header"/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:gravity="center" android:orientation="horizontal" android:weightSum="1"
android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:id="#+id/layout_footer">
<include layout="#layout/footer" />
</LinearLayout>
<ListView android:id="#android:id/list" android:layout_below="#id/layout_header" android:layout_above="#id/layout_footer"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:visibility="visible" />
<!-- <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/> -->
</RelativeLayout>
I don't know what is meaning of expanding meaning here, but listView in scroll view dont behave normally. To resolve this issue you need to implement list view outside of scrollview.
I don't quite understand what you mean, but if you want to have the textView scroll with the content of the listView, you need to add it as a header in your listView.
Moreover, it is not a good idea to put a listview in a scrollview, the listview being scrollable, it messes with the scrollview mechanism.
You can NEVER ever place a listview in the SCrollView.It wont behave properly.Instead of it try using LinearLayout TextView ListView < /Linearlayout>

Linear layout inside TabHost only showing one element

I have this layout file in my android application:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<LinearLayout
android:id="#+id/layoutSearch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Search here" />
<EditText
android:id="#+id/editSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</TabHost>
My problem is that LinearLayout android:id="#+id/layoutSearch" shows only one of the elements it contains (the text view, in the code above) If I remove the textview, it shows the EditText.
How do I make it display both (and more) elements?
There's a lot of excess layout there, but it looks like the real issue is that layoutSearch is a vertical LinearLayout with two children, but the first child's height is set to fill_parent, so you naturally never see the second one. Try setting its height to 0px but giving it a layout_weight of 1, and it should expand to take all the available space not taken by the EditText below it.

ScrollView Not Responding

I have the following XML and am not doing anything fancy with overriding scrolling behaviors:
<?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" >
<ScrollView
android:id="#+id/myscrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_weight="1">
<ListView
android:id="#+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
<TextView
android:id="#+id/my_empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/my_foo"
android:gravity="center"
android:visibility="gone"
/>
</LinearLayout>
However, while the embedded listview does respond to presses and long presses, it does not scroll. What am I doing wrong?
The trailing TextView is the listview's emptyview.
ScrollView does not play nicely with any other view that scrolls natively, i.e WebView, ListView, etc.
Try a Relative layout and see if this does what you need.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alingParentTop="true"
/>
<TextView
android:id="#+id/my_empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_foo"
android:gravity="center"
android:visibility="gone"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
In android it should not put ListView inside a ScrollView. It will not work.
Take the ListView out of the ScrollView. It causes problems, and is unnecessary (ListView handles scrolling on its own.)

Keep Buttons at bottom of layout visible when list is long

I have a layout that is three linear layouts. The top has some icons and the middle has a tabhost. Each tabhost has a list inside it. The bottom linearlayout has two buttons that should stay at the bottom on the screen at all times. The problem is when a list in the tabhost gets too long, it displays over the buttons. I tried to find some way to get the buttons to bedisplayed over the list but have failed so far. Any help would be appreciated. :-) Thanks!
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="395dp"
android:background="#000000">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
</LinearLayout>
</TabHost>
</LinearLayout>
<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="fill"
android:layout_margin="0dp"
android:padding="0dp"
>
<Button
android:id="#+id/btnGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ptt"
android:layout_gravity="bottom"
android:layout_weight="2"
android:hapticFeedbackEnabled="true"/>
<Button
android:id="#+id/btnMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Menu"
android:layout_gravity="bottom"
android:layout_weight="2"
android:hapticFeedbackEnabled="true"/>
</LinearLayout>
I generally put my LinearLayouts inside of a ScrollView to solve this situation. (The buttons stay outside) It allows your buttons to stay in the correct location at all times and the user scroll the rest of the view.
ScrollView Docs

Categories

Resources