how to use horizontalscroll view component? - android

i want to create a horizontal scroll view just like the one shown in image
In android widgets i found Horizontal Scroll View widget in composite section but dont know how to use it.Please help

Create xml file and add HorizontalScrollView first then add one layout to this as child. Then you can add any no. of views to the newly added child, that makes entire things scroll.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- child views here -->
</RelativeLayout>
</HorizontalScrollView>
</LinearLayout>

Related

Stop views overlapping horizontally in a relative layout

I have two views, a day view and a event view as part of a calendar. I want my events to fill the day view in width (which is working) but when I add another at the same position/time I want them to split the width... I am adding my event dynamically using java. Is there an attribute I can use so that views don't overlap?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/day_view">
</RelativeLayout>
.
<?xml version="1.0" encoding="utf-8"?>
<ie.test.calendar.CalendarEventView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="#+id/eventView"
android:background="#drawable/event_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/eventTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/white"/>
</ie.test.calendar.CalendarEventView>
I think you need to use Linier layout as parent view with horizontal orientation. And also use concept of weight for width. It will devide your parent view according to child quantity
Take a look on RelativeLayout.LayoutParams
You could set rules at runtime, using ID's.
layoutparams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.id1)
Although, i think, that using LinearLayout would be much simpler.

Dynamic control inside a layout in android

I have an activity Which has Text to show then Some content under that then a list view.
While some content could be a picture, relative layout, Video control, or a linear layout. Depending upon the data it got from previous activity.
Is that possible to do that or I have to make separate layouts for all items?
You need a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<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/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<FrameLayout
android:id="#+id/flSpecialContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
You can add any View you want in code to the FrameLayout (flSpecialContainer) based on what you need.
you can create all possible items in one layout and set visible and gone them depend on data you receive
in this case you don't need create them in code , and can preview activity layout in design mode.

I want to bring my relative layout in bottom

what changes shall I make to bring the relative layout in bottom?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_alignParentBottom="true"
android:layout_weight="0.08"
android:gravity="bottom" >
</RelativeLayout>
</LinearLayout>
You need to change your parent Layout to be a RelativeLayout, since the attribute android:layout_alignParentBottom does nothing for LinearLayout children:
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_alignParentBottom="true" >
</RelativeLayout>
</RelativeLayout>
You can achieve that by using a FrameLayout instead of a LinearLayout since it stacks childs from top to bottom by default.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp"
android:layout_gravity="bottom" >
</RelativeLayout>
</FrameLayout>
use
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- replace this by any other layout or view. You might want something here, right? -->
<View android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="86dp">
</RelativeLayout>
</LinearLayout>
There are two issues here. The first being that
android:gravity
is wrong. This sets the gravity for the contents of this Views children. So for example used in a TextView it would place the text at the bottom if the TextView. What you want instead is
android:layout_gravity
this sets the Views gravity within its parent.
The second problem is that android:gravity="bottom" doesn't work as you would expect in a LinearLayout who's orientation is vertical. I'm not sure I can explain why well so I will try to find a link in a bit that explains it. But if you change the orientation of the LinearLayout to horizontal you will see this.
So your best options are to change the orientation if that's a possibility and if not then change the root ViewGroup to a RelativeLayout and use the property android:alignParentBottom="true" as someone already suggested.
Edit for explanation
This blog post and this SO answer, among others explain it a little. But basically, if the orientation is horizontal then the Views are layed out from left to right as we expect so you can't control the left and right gravity. With a vertical orientation, the same is true for top and bottom gravity because where they are placed vertically is already determined by Android when you set that orientation. I hope this makes sense.

Inflate a view between elements in a view (Android XML)

I want to inflate some views but have a button at bottom of the view. I already know how to dynamically inflate views and then inflate the button below the other inflated views. However, when there are only a few inflated views (causing the combined view to take less space than an entire screen) the button does not rest at the bottom of the page as i would like. My idea is that i need to have the button aligned at the bottom of the relative layout by setting the button to: android:layout_alignParentBottom (when i try to do this though the app crashes)
here is a screenshot of what i have, followed by a screenshot of what i want, all the items you see are inflated onto a scrollview that is inside a relative layout. I think the problem is that the button is inflated onto the scrollview which is only as large at the items force it to be (unfortunately I'm too noob to know how to inflate onto a different part of the view)
the code for the image on the left is as follows (i dont have and errors for the view on the left):
parentView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/blurybg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_height="wrap_content"
android:id="#+id/scrollView1"
android:layout_width="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id= "#+id/parent_of_inflated_view_id">
</LinearLayout>
</ScrollView>
</RelativeLayout>
childButton (other views are similar to this)
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="30dp"
android:layout_width="fill_parent"
android:background="#drawable/done"
android:layout_alignBottom ="#layout/availablerestaurants"
/>
Java for creating the inflator and inflating the button. other inflated views are similar to this (i use the same inflator of course). I am adding the inflated button last to get it at the bottom of the view.
LayoutInflater theInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout linLayout = (LinearLayout)findViewById(R.id.parent_of_inflated_view_id);
Button doneButtonCurrentRest= (Button) theInflater.inflate(R.layout.done_button_availrest, null);
linLayout.addView(doneButtonCurrentRest);
Thanks for the help,
Adam
Please check my code. It should work as intended:
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parent_of_inflated_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
What about your code. I can see several mistakes:
1) You should specify android:layout_height for your button. Without this parameter exception will be thrown. You use android:height but it is just not the same as android:layout_height.
2) android:layout_alignBottom should point to the id, but not to the layout. Moreover, i don't see the purpose of this attribute in the separate layout for button.
Update
Here is the solution for the question in the comment below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/layout_for_positioning"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/parent_of_inflated_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="0" />
</LinearLayout>
</ScrollView>
Note that i used android:fillViewport="true" for the ScrollView so it can fill the entire screen. Also i used android:layout_weight to stretch views properly (you can find the explanation of the attribute here).

how to fit a large layout within a single screen with multiple listviews

i am working with a project in which what i have to do that there are two lists and the layout is very large you can say that it is just double to a normal layout. i have to add scroll view to show whole layout but the problem is that there can't be a list within a scroll view if i do so then there is ambiguity to compiler that which is scrolling if it is list or scroll view . Now what should i do help me thanks in advance ...
I am also attaching an image which is half of my layout....
From your query i designed one sample XML file :
<?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" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
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_weight="1"
android:background="#ffffff" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffeabc" >
</LinearLayout>
</LinearLayout>
</ScrollView>
In above you will have Scroll View for larger height Views and also you have equal proportion of list View. Thing you have to concentrate is on android:layout_weight , you should check how to play using Weight attributes
Edit :
Okay one thing you can do is that , We can use Header footer concept of List View - make ImageView and 2nd List View as footer of 1st List View so that ImageView and 2nd ListView will always comes Below 1st List View..
For Header Footer concept check this Link.

Categories

Resources