How to create android activity that shows at specified screen position? - android

My current layout displays activity that is not full screen (that's OK).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dip"
android:layout_height="200dip" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="fill_parent" android:text="#string/hello"></TextView>
I also added android:theme="#android:style/Theme.Translucent" to manifest for my activity.
My 200x100dip activity now shows in the upper left corner. How can i specify position of my linear layout (or my activity)?

You can use either FrameLayout or RelativeLayout as outer most layout for this. Ant then use absolute position in dp or android:layout_centerInParent or similar.

I believe your Activity´s outmost layout element (LinearLayout) will be placed in a FrameLayout that is the parent given from Android. I suggest you let your outmost layout match_parent/fill_parent in layout_height and _width and then center the content inside it with gravity="center" on your outmost layout. By letting the outmost layout being transparent and not catch click element it will appear as layout in the middle where elements behind is visible. If Im correct guessing that's what you want to achieve here.

put that layout in another absolute layout in which you use android:layout_width="fill_parent" and android:layout_height="fill_parent" the other thing you can do is to use this: http://www.droiddraw.org/
you can move your elements around manually with that and it will give you the XML code that is used to do that. I found it very useful in laying out XML in android.

Related

Android - White space on the right and the left of a LinearLayout

When I add element into a layout(in this case a LinearLayout), there is a white space on the two side of the element, but the attribute layout_width is "match_parent". Why?
Here's a screenshot:
http://s32.postimg.org/o4vr8kzbp/device_2016_07_30_001720.png
The code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#color/blue"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
I found this lines somewhere on this site and it might be worth checking;
This is possibly because you are using the auto-generated layout files in Android Studio when starting a new project, which by default adds a margin of 64dp to tablet screens (the value found inside values-w820dp\dimens). – PPartisan 6 hours ago
Maybe you should check your layout's parent's layout in the host activity for one more time.
It is possible that the root layout does not fill the parent or that the child you are adding has a margin, to know exactly whats going on use the android tool called DDMS or Android Device Monitor (you can launch it from Eclipse or Android Studio) then click on Dump View Hierarchy. This will give you details on the size, padding, margin and other properties of every view on the view hierarchy
You should to check your code, may be you update margins of layout at your classes.
And try to check layouts of home activity, may be they are contains layout_marginLeft(layout_marginStart) and layout_marginRight(layout_marginEnd).
Also parent layouts may contains any white views on the sides or parameter layout_weight with specific value.

listView inside of a linear layout with something else

i was just wondering if it was possible to have in one layout xml file, maybe a vertical linear layout at the top have a horizontal linearLayout and then below that (inside of the vertical layout) have a list view that takes up the rest of the screen?
I couldnt find any information specifically on this, and i'd like to know if its possible before i attempt it, thanks!
Give the ListView layout_height="0dp" and layout_weight="1". It will take up the remaining space not used buy the sibling LinearLayout.
<LinearLayout
android:layout_width:match_parent
android:layout_height:match_parent
android:orientation:vertical >
<LinearLayout
android:layout_width:match_parent
android:layout_height:wrap_content
android:orientation:norizontal >
<!-- other views --->
</LinearLayout>
<ListView
android:layout_width:match_parent
android:layout_height:0dp
android:layout_weight:1 />
</LinearLayout>

Android overlay outside/between layout boundaries

I have to add an overlay (ImageView) so that it's a bit shifted to the left of the containing layout's left boundary.
What is the best way to do this?
Tried something simple, like putting the ImageView inside the layout and use negative margin
android:layout_marginLeft="-20dip"
This made this:
(Correction: Text in the image should be 20dip not 20px)
AbsoluteLayout is deprecated. Is there something like z-order? Or what do I do?
Thanks in advance.
Edit: I tried using relative layout instead. Same effect. Here's the xml reduced to a minimum:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:paddingLeft="50dip"
>
<ImageView
android:id="#+id/myId"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_marginLeft="-30dip"
android:clipChildren="false"
android:src="#drawable/pic" />
</RelativeLayout>
Result
Also happens when the containing layout has a background image smaller than the screen instead of padding.
Using RelativeLayout instead of LinearLayout (to allow overlapping) and adding this to the RelativeLayout fixed it:
android:clipToPadding="false"
set "android:clipChildren = false" in xml
Instead of
android:layout_marginLeft="-30dip"
try with
android:paddingLeft="-30dp"
Use a transparent(android:background="#00000000") imageview to the left of linear layout with width = 30dp. And make myId as aligning left in case of relative layout. If you are using linear layout make orientation as horizontal and let the transparent imageview be the first entry in it.

How to float/overlap a view in android?

I have many activities with a scrollview inside a tablelayout. However, it is necessary a small design change, so I have to put a black transparent view over the whole screen from the top to the bottom. Is it possible to do it in the tablelayout or the scrollview?
RelativeLayout allows for easy overlapping of views. You'll have to adjust the existing views in your app because it doesn't do anything automatically.
EDIT:
A quick way to do this would be to take your existing view (the ScrollView) that is already organized and put it in a top-level RelativeLayout. Then, all you have to do is add new view inside the RelativeLayout with the width and height both set to MATCH_PARENT. The result should be the black transparent view will be visible over the ScrollView.
I normally use FrameLayout to achieve any kind of 'layering' of views.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
//your existing layout
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#33000000" />
</FrameLayout>
As DeeV said, you can probably use RelativeLayout in a similar way, but you might have to set additional attributes on its children to achieve this.

Using layout_gravity="bottom" to place at bottom of LinearLayout

I would like to place a layout on the bottom of a LinearLayout, but I can't seem to get it to work. I know that I can use RelativeLayout to do this, but I should be able to use LinearLayout, shouldn't I?
EDIT: Actually this is more confusing than I thought. The layout below is simplified. In reality, I'm using fragments on a pre-3.0 device, with the compatibility layer.
I used Hierarchy Viewer to examine what's going on, and found that an android.support.v4.app.NoSaveStateFrameLayout was added to my layout, and that layout has layout_height set to wrap_content. That seems to be what's causing my problem, but I haven't yet figured out how to fix it.
Specifically, why doesn't this work? Shouldn't the layout_gravity place it at the bottom?
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... stuff here ...
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
... more stuff here ...
</LinearLayout>
</LinearLayout>
BTW, changing layout_height to fill_parent or setting layout_weight don't seem to work either. I just want to better understand what is going on, because clearly I'm missing something important. Thanks.
First of all nice question.
Android behaves we can say weird in the situation like this.
if you have selected your parent linear layout's orientation horizontal then you can set its child component at bottom by setting its layoug_gravity=bottom. suppose you have added 2 text views in that horizontal linear layout and second textview's layout_gravity is bottom then it will set to bottom but it work like it is set at bottom in other column then the first text view. NOTE : you can set textview's layout_gravity = "left" or "right" when its parent linearlayout is horizontal but you cant see its result.
Oppositely, if you have selected parent linearlayout's orientation vertical then you can set its child component at left or right by using layout_gravity. but the second textview will shown in you can say next row with left or right gravity as you have set. NOTE you can set textview's layout_gravity = "top" or "bottom" when its linear layout is vertical but you can not see its result.
Try to make sample xml design as i have stated above so you get better idea.
Strange but True!!! Try to understand this behavior. :)
Just add space between what you want at the bottom and all the rest:
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
So I resolved the problem. It's a two-part solution:
First, the way to do this without using LinearLayout is to provide weight to the element above so that it takes up all of the empty space. BTW, you can see this example in the API demos: http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_3.html
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... stuff here ...
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weight="1"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
... more stuff here ...
</LinearLayout>
</LinearLayout>
This by itself didn't solve my problem, as I had a NoSaveStateFrameLayout with layout_width="wrap_content" as a parent view, and so I needed to get that fixed first. I'm using code based on the wonderful Google I/O App, and when I searched the code for NoSaveStateFrameLayout, I found this:
// For some reason, if we omit this, NoSaveStateFrameLayout thinks we are
// FILL_PARENT / WRAP_CONTENT, making the progress bar stick to the top of the activity.
mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
Thanks for an awesome comment Google!!! I added this into my source and everything worked great!
The moral of the story: Hierarchy Viewer and comments are your friends.
LinearLayout will just stack things as they are placed in there. Since it is vertical, it will keep placing items one after the next in a vertical manner. Can you change the android:gravity of the linearLayout and not the layout_gravity of the nested one and see if that works.
RelativeLayout of course should be the first way but you stated you didnt want to do that. Is there reason for that?
It could be that, as per https://stackoverflow.com/a/13366783/513038, you need to set the parent LinearLayout to have android:baselineAligned="false". Worked in my case.

Categories

Resources