android can't display the bottom of the layout - android

I made an android app that contain scrollview layout and inside it AbsoluteLayout, the problem is I can't display the bottom of the layout it only display the top of the layout. I tried to use layout_marginBottom in the AbsoluteLayout but it didn't work.

Use RelativeLayout instead of AbsoluteLayout. Also don't scroll the total layout. Just scroll the View only.
Check it once:
<?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"
android:orientation="vertical"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:background="#drawable/bg" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</ScrollView>
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btn1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp"
android:text="#string/s3"
android:textSize="16sp"
android:text="TextView" />
</RelativeLayout>

Use RelativeLayout and set left and right margins like
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin = y;

True , absolute layout is deprecated . Also , here is a tip :
When creating an xml layout for Android , the easiest way to check for error is using the "graphical layout" tab of the xml layout file . . Use an appropriate device configuration ( match your device ) and the API version , reproduce that error and see if changing the layout parameters help .

use property android:fillViewPort=true inside ScrollView as follows
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical" >

Related

My android xml layout, stucks

I have this android layout that I've created, now I wanted to add a new button but I find out that I can't put it in the center ! I can only put it on the left or right sides ..
I've tried
android:layout_gravity="center"
but still the same !
and btw, I'm using a linear layout
Apply android:layout_gravity="center" onto your LinearLayout and not your Button. This should center it. If you just want to center it vertically or horizontally, set layout_gravity to center_horizontal or center_vertical.
Try , this to ur LinearLayout. And tell us how it works.
android:layout_gravity="center"
android:gravity="center_horizontal"
Try to use RelativeLayout and set you button, and add to button parameters by your need:
android:layout_centerVertical="true"
or/and
android:layout_centerHorizontal="true"
or just
android:layout_centerInParent="true"
You need to wrap your button in a relative layout.
<?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:id="#+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>

Android Layout: cannot see the children over the parent

I have the Layouts as you can see in the picture (sorry I cannot post them normally yet):
https://dl.dropboxusercontent.com/u/28640502/Unbenannt.bmp
However, when I execute the app I can only see the camera preview and not the text nor the SeekBar. I know they work, because when I reduce the size of the camera I can see them and interact, but if I want the camera to be like background and then the children over it, it doesn't work.
I have been checking a lot of threads with similar problems but I don't find the solution:
1, 2, 3... Any idea? Thanks a lot!
Here is my xml code just in case:
<?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:layout_weight="0.55"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/show_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:layout_marginRight="10sp"
android:layout_weight="0.12"
android:text="H" />
<SeekBar
android:id="#+id/select_height"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="5sp"
android:layout_weight="0.91" />
<TextView
android:id="#+id/show_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:text="Dist" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
try to input both of your layouts in relative layout for example
<LL>
<RL>
<FL>
</FL>
<LL>
</LL>
</RL>
</LL>
this is hapenning because of overlaiyng of LL by FL
You could use the bringToFront() method of the View class.
SeekBar seeker = (SeekBar)findViewById(R.id.select_height);
seeker.bringToFront();
Alernatively you could use the sendToBack() function to put a view behind other views.
Also remember that a view's z-index is determined by the order in which the view is declared in the xml layout file.
I'd also suggest changing the camera preview layout and it's first child layout to be siblings. The one added last in the xml file will be on top.

How to put one View over other in RelativLlayout programmatically in Android?

I am programmatically creating view in Android. Parent layout is RelativLlayout and I'm adding two views in it. First is ListView and second is custom view. I want my second custom view to come on top of the list, but somehow it is getting hidden behind the ListView. How do I make sure that my custom view is on top.
Views get placed in the order you add them to their parent views. The view added last will be on top. You might also want to try
View.bringToFront() http://developer.android.com/reference/android/view/View.html#bringToFront()
Try to use: View.bringToFront();
Solution 1 - View#bringToFront()
View.bringToFront() should work if used properly. Maybe you forgot this (source: http://developer.android.com/reference/android/view/View.html#bringToFront()):
Prior to KITKAT this method should be followed by calls to requestLayout() and invalidate()
Solution 2 - reattach view
If View.bringToFront() will not work try to remove and add the view again. The advantage is that it has the same code on pre-KITKAT versions too.
I am doing it this way in my code:
ViewGroup parentView = (ViewGroup) (listView.getParent());
parentView.removeView(addFab);
parentView.addView(addFab);
Use FrameLayout, frame layout shows last added view on top..
Try this :
<?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" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<YourCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Please try adding android:elevation attribute to your view in the XML. It will always place your custom view over the ListView.
Again: Views get placed to the layout in the order you add it. Question is: is it realy hidden or does it not render?. Check LogCat and Console for errors. If you only add this single Custom View to your layout, does it get rendered without any problems? If so, ensure you realy add your custom to the same parent view (group), as you add your ListView. If you don't get any further, provide de respective code sample.
Give an elevation to the view which you want to bring up.
android:elevation="2dp"
This works for me :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="220dp"
android:gravity="bottom"
android:orientation="vertical"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/details_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="16dip"
android:scaleType="centerCrop"
android:src="#drawable/temp_nav_image">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Kapil Rajput"
android:textStyle="bold"
android:textColor="#android:color/black"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textStyle="bold"
android:text="kapil#gnxtsystems.com"/>
</LinearLayout>
</RelativeLayout>
I used relative layout and the order of the views somehow determines which one is on top.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:splitTrack="false"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
In this layout, the textview will be on top of the seekbar
Make parent view as relative layout and add your both view in xml.It wont work for Linear Layout.
And then add FirstView.bringToFront() programtically.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/bg"
android:orientation="vertical"
android:padding="15dp">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="-30dp"
android:layout_marginTop="30dp"
android:src="#mipmap/login_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/imgIcon"
android:layout_gravity="bottom"
android:background="#B3FFFFFF"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<View 1/>
<View 2/>
</LinearLayout>
Java file
imgIcon.brigtToFront();
what you should do is using android:layout_below or android:layout_above programmatically. so you should do this:
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.below_id);
viewToLayout.setLayoutParams(params);
and i suggest you take a look at this answer
hope this helps

Android - why do two TextView elements line up side by side instead of one below the next?

I have this layout:
<?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" >
<TextView
android:id="#+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="#+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
I am combining two TextView elements with a ListView. For some reason, the TextView items render next to each other instead of one below the other. Why does that happen? And how do I make them line up one below the other?
Thanks!
in the LinearLayout you need to specify the orientation as follow:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
That's because the LinearLayout has a horizontal orientation by default.
Specify android:orientation="vertical" for it.
See LinearLayout tutorial
But you want: android:orientation="vertical"
Have you tried to set the attribute:
android:orientation="vertical"
Hello See Vertical orientation
You may consider to have two linear layouts and have the vertical orientation within another linear layout which is horizontal if that helps.

Adding view to bottom of layout inside a scrollview

So my layout looks basically like this:
<ScrollView>
<RelativeLayout>
<BunchOfViews/>
<ImageView android:layout_alignParentBottom="true"/>
</RelativeLayout>
</ScrollView>
I have the ScrollView so all of the layout always is visible no matter the height of the screen. The problem is that on a very high screen, I still want my ImageView to be at the bottom. However, a child of a ScrollView don't seem to have a defined bottom. The View is placed at the top of the layout. How can I solve this problem in a neat way?
I ran into the same issue. I never could find a very pleasing solution, but here is how I did it. Maybe someone else has a better way, I hate adding layouts that don't do anything.
My hack was to add a dummy linearlayout at the bottom of the scrollview that has fill_parent to take up all the room and force the scrollview to fill the screen. Then add whatever component I want to that linearlayout.
Here is one of my layouts that does this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15px" >
<!-- bunch of components here -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/spinner"
android:layout_marginTop="5px"
android:gravity="center_horizontal|bottom"
android:paddingTop="2px" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20px"
android:paddingRight="20px"
android:text="Delete" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
I had the same issue and found this page:
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Basically, you set the ScrollView's android:fillViewport to true, which will allow the child view to expand to the same height as the ScrollView itself, filling out the space. You then just need to have one of the child controls' layout_height set to fill_parent and layout_weight to 1, causing that control to "spring" to fill the empty space.
Note that if the contents of the ScrollView are already tall enough to fill the ScrollView, the android:fillViewport has no effect, so the setting only kicks in when needed.
My final XML looks like similar to this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_height="fill_parent"
android:layout_weight="1">
<!-- this expands to fill the empty space if needed -->
</LinearLayout>
<!-- this sits at the bottom of the ScrollView,
getting pushed out of view if the ScrollView's
content is tall enough -->
<ImageView
android:layout_height="wrap_content"
android:src="#drawable/footer_image">
</ImageView>
</LinearLayout>
</ScrollView>
it seems that the linearlayout isn't necessary, all that is important is the fillViewPort.
you could just use
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottomParent="true">
now that you have specified the relativelayout to be at least the size of the screen.
its work for me perfectly android:fillViewport="true"
Joel Malone's answer Adding view to bottom of layout inside a scrollview does that trick. My solution is almost the same, except that I use Space widget to do the work of filling the rest height inside the parent layout. Like this:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
>
<android.support.v4.widget.Space
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_height="wrap_content"
android:src="#drawable/footer_image"
/>
</LinearLayout>
</ScrollView>
Attention:
fill_parent in the other answers is noted as deprecated for a long time;
Compare to an empty LinearLayout to fill the rest of parent layout, I think Space is much more appropriate(it's designed to do that job.)
Finally, don't forget that important attribute at the beginning of ScrollView: android:fillViewport="true". They together make this trick.
just put this attribute android:fillViewport="true" to your ScrolView
and you will got what you need
On your view that you want to be at the bottom use android:gravity="bottom"
From: http://code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314
This should help you:
<RelativeLayout
android:layout_marginTop="-45dip"
android:padding="0dip"
android:layout_alignParentBottom="true"
android:gravity="bottom|right"
android:background="#android:drawable/bottom_bar"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="#+id/manual_setup"
android:text="#string/account_setup_basics_manual_setup_action"
android:minWidth="#dimen/button_minWidth"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="-4dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="false"
/>
<Button
android:id="#+id/next"
android:text="#string/next_action"
android:minWidth="#dimen/button_minWidth"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawableRight="#drawable/button_indicator_next"
android:layout_marginBottom="-4dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="false"
/>
</RelativeLayout>
ScrollView view = new ScrollView( this );
ScrollView.LayoutParams lps = new FrameLayout.LayoutParams( FILL_PARENT, FILL_PARENT, Gravity.CENTER );
LinearLayout layout = new LinearLayout( this );
// what ever you want in the Layout
view.addView( layout, lps );
I tried alot to align the Scroll View to bottom of the screen but thats not possible according to this link.
https://newbedev.com/how-do-i-align-views-at-the-bottom-of-the-screen
The way i found was to create a view with 1dp height and aa id (lets say android:id="#+id/bottomView") at the bottom of your XML page.
now just add these attributes to your scroll view..
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_alignBottom="#+id/bottomView"
>
Here is how you can use a ConstraintLayout to align the image view at the bottom:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Text 1"
app:layout_constraintBottom_toTopOf="#+id/space_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
<Space
android:id="#+id/space_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/image_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view" />
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/space_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Categories

Resources