I want to add a scrolling to my main layout(linear layout)
if I add the scrollbars and isScrollContainer attributes in the linear layout, the scrolling does not work. any explanation why these attributes do not activate scrolling
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical"
android:isScrollContainer="true">
To Make your LinearLayout Scrollable put it in ScrollView.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView ...>
<LinearLayout ...>
...
...
</LinearLayout>
</ScrollView>
isScrollContainer="true"
This property is used when your softkey in android pops up and still you want your view to scroll.
Related
I'm having problems when using RelativeLayout, ScrollView and some LinearLayout to get some elements always on top and something scrollable.
This is my XML
https://pastebin.com/Uai2UynP
(I don't know why it got separated in this post but it's the same code)
The point is, when i add the android:alignParentBottom="true" to the RelativeLayout with the "always on top bar", it hides me a part of the ScrollView above. This can be solved adding a android:PaddingBottom="[something]" to the ScrollView above, but doing this it hides me something at the top, since I basically have the LinearLayout with email, gold and coin that i want too always on top.
Use RelativeLayout as main layout and use android:layout_alignParentTop="true" in your layout which you want to place top and use android:layout_alignParentBottom="true" in your next layour which you want to set bottom of your layout. I am giving you a Example,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
I'm adding images to the layout via code like this:
linear = (LinearLayout)findViewById(R.id.linearLayout);
for(int i=0;i<15;i++)
{
image = new ImageView(this);
image.setImageResource(R.drawable.img);
linear.addView(i);
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
My question is that the scroll bar is not showing up when I add more than 3 images.
Instead of Adding LinearLayout as parent layout, add linear layout inside ScrollView and add the child into the LinearLayout
Note:
ScrollView should have only one immediate child
Iam not see element scrollview in your codes
Try to add element scrollview.
for example :
Xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android_layout_width="fill_parent">
<ScrollView
android:layout_height="fill_parent"
android_layout_width="fill_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
</ScrollView>
</LinearLayout>
Java :
linear = (LinearLayout)findViewById(R.id.linearLayout);
for(int i=0;i<15;i++)
{
image = new ImageView(this);
image.setImageResource(R.drawable.img);
linear.addView(i);
}
try this use ScrollView as parent layout and make LinearLayout child of ScrollView and all controls in linear layout
like below layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
// add here all your controls
</LinearLayout>
</ScrollView>
Like all the other comments say, add a ScrollView as the root component of your layout.xml file.
Then, inside that ScrollView, add another LinearLayout tag and inside that add your items and components.
Just a reminder, only put the LinearLayout tag directly inside of the ScrollView, as a child.
If you put more than one child inside a ScrollView tag, you will get an error.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Add your components, if they go beyond the screensize, the ScrollView will automatically generate a Scrollbar functionality for you-->
</LinearLayout>
<!-- Don't put another component inside the ScrollView that isn't the LinearLayout-->
</ScrollView>
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.
I'm having a slight android layout issue...
I have a ScrollView element holding a LinearLayout. I want the scrollview to fill the entire device screen and the linearlayout to fill the whole height of the scrollview.
I have the code below which is resulting in the scrollView filling the whole screen but the linearLayout only filling about half of the height of the scrollview.
Here is my code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff036c07">
<LinearLayout
android:background="#FF0000"
android:id="#+id/overview_form"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
Any ideas what I am missing?
Add android:fillViewport="true" to the scrollview .
Add the
android:fillViewport="true" property in your ScrollView
and
also remove unncessary namespace from linearlayout which is
xmlns:android="http://schemas.android.com/apk/res/android"
try adding the weight attribute
android:weight="1" in linearlayout and check
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>