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>
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 have vertical linearLayout. First element is a framelayout that has some content and second element is button that should always be at the very bottom.
While button should be at the bottom, the framelayout should take the rest space that is in the linearlayout
<LinearLayout
...>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
...
</FrameLayout>
<Button
.../>
</LinearLayout>
The key to this solution is the combination of 0dp height for your FrameLayout and the layout_weight attribute. This attribute allows a LinearLayout to divide the "extra" space up between its children. Your button takes up a fixed amount of space, and your FrameLayout takes up no space at all... so everything that isn't the button winds up being given to the FrameLayout and now it fills up the whole LinearLayout while leaving enough space for the Button below it.
Use android:layout_height="match_parent" on your FrameLayout
you should use RelativeLayout somthing Like that :
<?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"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:layout_alignParentBottom="true"
android:id="#+id/btnTest"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:layout_above="#id/btnTest"
>
</FrameLayout>
</RelativeLayout>
i want to create the Login layout like given below..
with using weight attribute..
it should be device independent..
means in all android device it should look same
Split main container in 2 parts for Login part set weight 2, bottom part as 1, then add containers with width and height = match_parent and first part done, then set padding for containers, those containers itself can be relative nested in linear containers so it will be easier to compose Login part.
<?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_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout >
Your views here
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout >
Your views here
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
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>
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.