Setting a horizontal scrollView layout - android

I have a layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/textView2"
android:layout_below="#+id/textView1" android:columnWidth="90dp"
android:gravity="center" android:horizontalSpacing="10dp"
android:numColumns="3" android:stretchMode="spacingWidth"
android:verticalSpacing="10dp" />
<TextView android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" android:text="Level 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
So I want to have same layout when user scrolls right. There will be again gridview and textviews with different images. However I could not achieve it even if I looked examples. I tried to convert it to Linear Layout but again I could not create scroolview.
In other words, I have a layout like this one:
And when user scrolls right I want to have another gridview that holds different images. When user scrolls nothing changes but just images. Any help?

As I understood you want to make it possible to scroll your images in GridView horizontally. You can't achieve it with ScrollView. Try to use TwoWayGridLayout. Or you can use ViewPager and Fragments. Choose one that is more convenient for your purposes.

Related

Multiple textviews (over multiple lines) below a gridview

For the UI of my app, I want a Gridview above two lines if Textviews, as shown in the image.
I can manage to get one textview to sit nicely below the gridview (using this method) but I cannot work out how to get the second one to display properly. It always seem to be off the bottom of the screen or incredibly thin down the right hand side of the full height of the screen.
Code:
<GridView
android:id="#+id/gridview"
android:layout_alignParentTop="true"
android:layout_above="#+id/textLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="135dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<LinearLayout
android:id="#+id/textLinearLayout"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textviewTimerLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Elapsed time: "/>
<TextView
android:id="#+id/textviewTimerTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="00:00:00"/>
<TextView
android:id="#+id/textviewPCUTotalLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total PCU's: "/>
<TextView
android:id="#+id/textviewPCUTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"/>
</LinearLayout>
</LinearLayout>
put the grid layout and other two text fieltds in a linear layout. Then make all of their layout_width attributes match_parent. Finally add weight to the views so that the grid layout has twice as much space as the text fields have. for example grid layout gets weight=2 and the text views get weight=1 each
<LinearLayout
...>
<GridView
...
/>
<LinearLayout
...
android:orientation="vertical"
>
<TextView
...
/>
<TextView
...
/>
<TextView
...
/>
</LinearLayout>
</LinearLayout>
I'm not sure exactly why you're nesting two LinearLayouts together when one should suffice, but all you should need is to set the LinearLayout's orientation to vertical.

Grid view overlapping other views android

I need to have a button, a grid view and a button, arranged vertically. I can see the Button and the GridView but can't see the button declared after the grid view. Why's that? This happens with any view below the grid view declaration.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Fragment1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="match_parent"overla
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
</LinearLayout>
As you have taken LinearLayout with vertical orientation, give wrap_content to the height of GridView.
Now, if you want last button at below to the screen, then include android:layout_weight="1" in GridView.
try this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button1"/>
as you have declared height of gridview as match_parent it should overlap the views declared below it if it can not find enough screen space available to show it.
You should try calling bringtoFront() method on the view declared below gridview.
Other thing from my viewpoint is to try relativeLayout if possible and use android:layout_above and android:layout_below with gridview.

scrolling a background image and floating buttons

I need a screen that is filled with an image as background and three buttons floating over the image. I created this fine in xml and in vertical orientation it does what I want. However in landscape it doesnt so I have tried to add a scrollview and am getting very confused as to how to combine these elements correctly. After various tries I reached the stage shown below
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="#+id/home_container"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:src="#drawable/alert"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:scaleType="fitCenter" android:clickable="true"
android:enabled="true" />
<LinearLayout android:layout_width="fill_parent"
android:paddingBottom="25px" android:paddingLeft="25px"
android:orientation="vertical" android:paddingTop="90px" android:id="#+id/toplinear"
android:layout_height="fill_parent" android:paddingRight="25px"
android:gravity="top">
<Button android:layout_width="fill_parent" android:textSize="20dp"
android:id="#+id/startprogrambutton" android:paddingBottom="12dp"
android:text="#string/text1" android:layout_height="wrap_content"
android:paddingTop="12dp"></Button>
<Button android:layout_width="fill_parent" android:textSize="20dp"
android:id="#+id/button1" android:paddingBottom="12dp"
android:layout_marginTop="10dp" android:background="#drawable/webview_buttons"
android:text="#string/text2" android:layout_height="wrap_content"
android:paddingTop="12dp"></Button>
<Button android:layout_width="fill_parent" android:textSize="20dp"
android:id="#+id/button2" android:paddingBottom="12dp"
android:layout_marginTop="10dp" android:background="#drawable/webview_buttons"
android:text="#string/text3" android:layout_height="wrap_content"
android:paddingTop="12dp"></Button>
<Button android:layout_width="fill_parent" android:textSize="20dp"
android:id="#+id/button3" android:paddingBottom="12dp"
android:layout_marginTop="10dp" android:background="#drawable/webview_buttons"
android:text="#string/text4" android:layout_height="wrap_content"
android:paddingTop="12dp"></Button>
</LinearLayout>
Now it errors with scrollview can only contain one child. Is there some way to rearrange so that I can have my image and the floating buttons all scroll together?
put both ImageView and LinearLayout in another Vertical LinearLayout. This solves your scrollView child problem.
<ScrollView>
<LinearLayout>
<Imageview />
<LinearLayout />
</LinearLayout>
</ScrollView>
Why don't you give image as background for linearLayout with buttons, it will serve your purpose.
In case you want to scroll only your buttons and not the ImageView you can try this:
<LinearLayout>
<Imageview />
<ScrollView>
<LinearLayout>
<Button />
<Button />
<Button />
<Button />
</LinearLayout>
</ScrollView>
</LinearLayout>
Hope this helps!
Just so you know, you're not limited to trying to find a single solution for both orientations. You can have 2 separate layouts for both orientations by specifying separate layout xml files in a land & port directory. See the official documentation here:
http://developer.android.com/guide/practices/screens_support.html

Android Linear Layout Problem

Hi all,
I'm a newbie to Android and having problems displaying items using a Linear layout and wondering if anyone can help me. The item list and pictures display fine but the textbox and search button are displayed for each item rather than getting displayed once. My code looks like:
<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:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:id="#+id/selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/searchHeader"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/searchItem"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/search"
android:onClick="Search"/>
</LinearLayout>
</LinearLayout>
With this XML you should only see exactly one imageview and not a list of images as you discribed. Do you alter this layout programmatically in order do display various images? Maybe there you also add again the items you only want once.
Try moving the textview and search button outside of the linearlayouts they are in.

Android layout not showing properly

I'm writing an android app and in it I want to have a sort of title bar including the name of the game, the score, and things like that. Then below that I want to have an grid of image views, which will kind of be like tiles on which I can display different characters in my game.
I have the following xml layout, but when I run it and I get a black screen:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/app_name"
/>
<TextView android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/score"
/>
<TextView android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="0"
/>
<TextView android:id="#+id/robotsLeft"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/robots"
/>
<TextView android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="0"
/>
</LinearLayout>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
GridView is initialized as per this tutorial:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
ie. in my main activity's onCreate():
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
Can anyone see anything I've done wrong?
Thanks!
Well, that's invalid XML, because there's no root element. Also, all of your TextView widgets have android:layout_height="fill_parent", which is rather strange. You might also consider using hierarchyviewer to examine your layout more closely and see what else may be going wrong.
you should put a (linear)layout underneath them, as your xml is invalid.

Categories

Resources