position android widgets - android

How to place a widget exactly at the required location, say for example in relative layout, I am trying to place two buttons one below another and then increase width of button, but using drag and drop in eclipse, it resizes other widgets in the layout. In short, I want to place widget independent of parent widgets, anywhere in layout to visualize the UI I want to design. Thanks for your help.

You could use RelativeLayout and margins to position precisely. For example:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">
<!-- Use android:layout_marginLeft for X and android:layout_marginTop for Y -->
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="50dp" android:layout_marginTop="50dp"
android:text="Button1" />
</RelativeLayout>
You could also use an AbsoluteLayout, which is actually probably more suited for this:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<!-- Use android:layout_x for X and android:layout_y for y -->
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_x="50dp" android:layout_y="50dp" android:text="Positionable" />
</AbsoluteLayout>

Related

Change the position of a text in android studio

I just started a programing in course in android studio using Java and XML and cant really figure out how to do a simple task. I have 3 buttons at the top of the screen, they fill up the whole width of the screen. I want to add a text below these 3 buttons, but i dont really now how to specify this. Right now i have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_textcolor" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_textsize" />
<TextView
android:text="South Africa"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
Now, the text in the text element is displayed at the right side of the screen, its barely visible. The text gets cramped up so tight that it gets misaligned verticaly. What would i do if i instead wanted the text inside the textview element to be displayed just below the 3 buttons, to the left horizontaly, like normal text?
Thank you!
Use something like this. Inside the TextView tag add:
android:layout_below="#+id/buttonid"
Obviously you have to use relative layout for using this
Here you go
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_send" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_textcolor" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_textsize" />
</LinearLayout>
<TextView
android:text="South Africa"
android:layout_height="match_parent"
android:layout_width="wrap_content"
/>
</LinearLayout>
Use RelativeLayout instead of LinearLayout. There are also many other layouts you can try. Check here for the other type of available layouts.
RelativeLayout lets child views specify their position relative to the
parent view or to each other (specified by ID). So you can align two
elements by right border, or make one below another, centered in the
screen, centered left, and so on. By default, all child views are
drawn at the top-left of the layout, so you must define the position
of each view using the various layout properties available from
RelativeLayout.LayoutParams.

Android/XML - How to align an immobile layout to top of parent and have scrollview below?

Please refer to example below. I want to have the top layout (below encased in red) to be unmoving in a scrollview in my activity. I have a scrollview as the parent layout and then I thought having a relative layout for the top one would work, and align it to the top, but that didn't really work out as it still remained within the scrollview. I would like to have the users have the red-layout box remain static when they scroll down.
I figure I would also have to put in a topMargin at the top of the scrollview or something in order to fit the redbox layout in.
XML Code posted here: http://pastebin.com/bxdREbeG
Do something like this (hand code, for reference only):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/YourTopStaticView"
android:layout_width="match_parent"
android:layout_height="48dp"> //Or any other height you want
//Contents of the top view
</RelativeLyout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/YourTopStaticView">
//Contents of the ScrollView
</ScrollView>
</RelativeLayout>
As a side note, do not hardcode children into the ScrollView like that. Use the RecyclerView (which is an updated, modern replacement for ListView), which you will be expected to know how to use if you want to move into serious Android programming. It is actually super easy to use, once you get the hang of it :-)
You should use the ScrollView with only one child (official documentation - http://developer.android.com/reference/android/widget/ScrollView.html). According to your xml, your ScrollView is very complicated with a lot of child widgets.
The best option for you is to use a LinearLayout as the root for the whole container, a LinearLayout( or Relative) for the top layout containing the Reset and Save buttons, and a ListView for the long list that you have. ListView takes care of it's own scrolling. So you don't have to worry about that.
This will improve your code performance as well.
This should suit your needs:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="#+id/topPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Multi TTS Implementation"/>
<Button android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="SAVE"/>
<Button android:id="#+id/resetAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/save"
android:layout_centerVertical="true"
android:text="RESET ALL"/>
</RelativeLayout>
<ScrollView android:id="#id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/topPanel"
android:layout_alignParentBottom="true"
android:padding="5dp">
<!-- Your scrollable content here -->
</ScrollView>
</RelativeLayout>

Incorrect height of RemoteViews notification when using linear layout

I've created a notification using RemoteViews with a custom layout. The layout structure is as below.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingTop="8.0dip" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:divider="?android:listDivider"
android:dividerPadding="12.0dip"
android:gravity="center_vertical"
android:layout_gravity="center"
android:orientation="horizontal"
android:showDividers="middle|beginning" >
</LinearLayout>
</LinearLayout>
The effect of it all appears only in a fraction of the notification height, and not in the full height of the notification. E.g. check-out the buttons in the screenshot at http://i57.tinypic.com/14dzo9i.png (the middle notification is the focus of the question), which are in the last child of the parent LinearLayout, and should be all vertically centre-aligned.
My question is very similar to Height of notification incorrect "match_parent", but since there isn't a real answer present there, I'm asking this. The answer on that question does refer to a way to dynamically figure out the notification height. If that is the solution to use, I'd like to know how to use the height obtained from there to dynamically set the height of the remote view, since I don't see a height setter on the remote views object.
Please note that the height of the image is fine when I use a relative layout instead. Relative layout still has two problems: 1. the buttons are still not vertically centre-aligned and 2. I can only wrap content or fill parent, not divide the available space into the two children linear layouts. Screenshot: http://i59.tinypic.com/o1000j.png
Seems to be working on changing android:layout_height="wrap_content" to android:layout_height="match_parent" for the views within. Somehow missed it earlier, interesting though the behaviour for this combination of values.
Change your Layout to RelativeLayout and use param android:toLeftOf="#+id/...", android:toRightOf="#+id/..." to align your subView in layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/image1"
android:src="#drawable/ic_launcher" />
</RelativeLayout>

Center One Element in Android Layout Relative to the Other Elements

Is there a way to have some elements on the layout which would be called "primary" for example and then have another element to be centred relative to those?
Imagine you have two slide out menus at the left and right of the screen and then a content section in the middle that you want to be always at the center of whatever empty space you have.
So if they're both out, it's at the center but if you open the left one, the main section is pushed to the right to still be in the center of the remaining empty space on the screen.
Therefore the menu sections are "primary" because then they determine where the rest of the elements, i.e. the content section, should be .
So I think I've figured out a way to do this:
Wrap the whole thing in a RelativeLayout
Put the primary ones there
Create the secondary RelativeLayout and make it "toRightOf" or "toLeftOf" the primary one(s) and set it to fill_parent both on height and width with zero margin
Place whatever you want the content to be in another Relative or Linear layout within the last created RelativeLayout and make it to centerInParent
E Voila!
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="0dp" >
<!-- this would contain the "content section" -->
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#+id/primaryObject"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="testButton"
/>
</LinearLayout>
</RelativeLayout>
<!-- this would be the primary thing -->
<FrameLayout
android:id="#+id/primaryObject"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" >
<ImageButton
android:id="#+id/menuPulloutButton"
style="#android:style/Widget.Holo.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/menu_sidebar_button" />
</FrameLayout>
</RelativeLayout>

How do I stack Buttons and TextViews neatly in FrameLayout?

I am trying to program an alternative landscape view file (an xml file) for my app, and I must use FrameLayout instead of LinearLayout (that's what the book said). But Framelayout does not stack well, so we are supposed to use android:layout_gravity and then assign an x/y dimension, for example: android:layout_gravity="center". This example centers something exactly in the middle (both vertically and horizontally).
But my problem is, I have 4 levels on the vertical plane, where I want to place things. A text line, a line with 2 buttons (true, false), another button (cheat), then finally a line with 2 arrow buttons (previous and next). But with the layout_gravity, they only have very crude placements: top, center, bottom. I noticed that if you do not assign anything, they all end up in the upper left corner.
So how do I stack these vertically so they fall nicely spaced from top to bottom? Assigning 2 things the same parameters does not stack them, but rather overlaps them terribly.
Thank you for your help, below is my code. I have not put any gravity layouts in there yet.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
<Button
android:id="#+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cheat_button" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_left"
android:contentDescription="#string/move_back"
/>
<ImageButton
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_right"
android:contentDescription="#string/move_forward"
/>
</LinearLayout>
</FrameLayout>
You should use a Relative Layout or Linear Layout to achieve this because the Frame Layout is simply not designed for this.
Here is the api documentation for frame layout-
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
So controlling the position of child in Frame layout is very much limited,ie only using gravity.

Categories

Resources