HorizontalScrollView issue - android

I want to make a news ticker using a HorizontalScrollView. As a first step, I am simply trying out the HorizontalScrollView which contains only a Button as the content. I want the Button to fill the entire screen and the Button's text to be centered. But so far, the Button is stuck on the left side. Here is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_marginTop="50dip"
android:background="#000000"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button 1"
android:textSize="20sp" />
</LinearLayout>
</HorizontalScrollView>
How to solve this problem?

How to solve this problem?
Try to use the fillViewport attribute on the HorizontalScrollView:
android:fillViewport="true"

Related

scrollview and linearlayout weird view [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I put a ListView into a ScrollView without it collapsing?
I am trying to make the food menu to scrollable but once i add it, it become like in the image. Before I add the scrollview, I cant view my back button in my screen eventhough I have added it. this is my weird code.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/foodbg"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="#+id/backToMenu"
android:text="Back" >
</Button>
</LinearLayout>
Hi, you can use RelativeLayout instead of LinearLayout.
In such case you should place your button at the bottom of the layout. After that place your list at the top of the layout and above the button. RelativeLayout has special xml-attributes to help you do this. After that you do not need a ScrollView.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1" />
<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_gravity="bottom"
android:id="#+id/backToMenu"
android:text="Back" >
</Button>
</LinearLayout>
</ScrollView>
Try to implement above code

Making TextView scrollable without knowing max lines

I want to make my TextView vertically scrollable. I have read "Making TextView Scrollable in Android" but since I don't know the size of the screen of the final device, I can't know which value should be set for maximum lines.
Is there any other way, so it gets the scrolling for any screen size?
My XML looks 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="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_weight="1.0">
<TextView
android:id="#+id/consola"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</ScrollView>
<EditText
android:id="#+id/comando"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0">
<Button
android:text="Conectar"
android:id="#+id/boton_conectar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Enviar"
android:id="#+id/boton_enviar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
Look, here the structure of my XML layout with scroll:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:visibility="visible">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
.....
//your sub-widgets
.....
</LinearLayout>
</ScrollView>
As for me, works fine.
If you do not know number of lines (moreover, it could vary if soft input method are shown or not), you should use RelativeLayout to place you widget like TextView. And then connect both (it's critical, not one but both) top and bottom edges of it to parent withandroid:layout_alignParentTop and android:layout_alignParentBottom attibutes, or neighbors with android:layout_above and android:layout_below attributes.
After both sides are connected to other widgets, android will recalculate size of TextView and scroller properly, even if you change orientation or show/hide soft keyboard.
ScrollView is only allowed to have one sub widget.
You should put the linearlayout inside of the scroll view. and set the textview inside of the linear layout.
Put the TextView inside ScrollView with height = MATCH_PARENT
try:
<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:layout_width="0dp"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_weight="1.0">
<TextView
android:id="#+id/consola"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</ScrollView>
<EditText
android:id="#+id/comando"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:text="Conectar"
android:id="#+id/boton_conectar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Enviar"
android:id="#+id/boton_enviar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>

Android layout textview and listview combined

I am trying to layout a home automation app. The top part of the screen will be custom status text (Garage door is OPEN, System is DISARMED, etc). Under the status text there will be a refresh link/button/list item. Something that the user can click on to refresh the status text. At the bottom will be a listview that will be links to other pages and links to actions such as "Security Page", or "Close Garage Door", etc. My question is, how do I configure the layout? I have tried several LinearLayouts, RelativeLayouts, nested layouts, etc, and none have worked so far.
Here is my main.xml as of now:
<?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:layout_height="wrap_content"
android:layout_width="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/statustext">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/refresh">
</TextView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView
android:layout_width="fill_parent"
android:id="#android:id/list"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
The refresh link could just be an item of the listview, but I would like to style it differently so it stands out, such as a different color font or background. Or it could be a button. But it needs to be between the statustext and itemlist. And everything should scroll as one unit.
Is this possible?
Is this works?
<?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/ScrollView01"
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:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="status"
android:id="#+id/statustext">
</TextView>
<TextView
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:text="refresh"
android:id="#+id/refresh" android:layout_width="fill_parent" android:layout_gravity="fill_horizontal">
</TextView>
<ListView
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:id="#android:id/list"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
If you want to have a scroll bar, use ScrollView to warp all sub-items. But ScrollView it self can host only one direct child so you'll need to wrap them all with a LinearLayout which has everything you want inside.
Add you status text content to listheaderview.here is the link for that.
Android listview with header and footer buttons
This way the whole list ll be scrolled with your status text.

Android UI difficulties

My layout was perfect until I decided to add a scrollview.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView android:id="#+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="#+id/linearLayout1">
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/tableLayout1"/>
</LinearLayout>
</ScrollView>
<Button android:layout_width="match_parent" android:id="#+id/button1" android:text="Start" android:layout_height="50dip" android:onClick="getOutput"></Button>
</TableLayout>
I would like the button to stay at the bottom of the screen, I would actually like the scrollview to take up the entire screen. With just the inner table layout it works fine. In the Graphical Layout preview it looks how I want it, but not when I run it.
I would like the scrollview to take up the whole screen except the button, which I'd like to be at the bottom.
you could use RelativeLayout or LinearLayout as Root view. Giving Button more weight. That way ScrollView won't take up the whole thing.
this should work
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1.0">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:id="#+id/linearLayout1">
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableLayout1"/>
</LinearLayout>
</ScrollView>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Bottom Button"/>
</LinearLayout>
Try setting ScrollView fillViewport attribute to true.

Why is my button hidden?

The button is hidden...why?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/feed_list"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="More"
android:id="#+id/feed_more"/>
</LinearLayout>
Assuming that the ListView has enough content to fill up the whole screen, it will since you told it to wrap_content. You can use weights to instruct the ListView to take up as much of the screen as is available, after the button has what it needs:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="0"
android:weight="1"
android:id="#+id/feed_list"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weight="0"
android:text="More"
android:id="#+id/feed_more"/>
you need to give weight 1 to the list view. then your button will be visible. try it.

Categories

Resources