I am trying to achieve this in an android app layout.
So far I have managed to get the buttons the way I wish, but I am having trouble working out how I am going to implement the divider and the layout on the right side of it.
Here is what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<!-- this is main activity's layout. a main menu of sorts. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" android:background="#drawable/background">
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:id="#+id/layout_maintest2_relativelayoutleft">
<Button android:text="#string/string_main_NewCalculation" android:drawableLeft="#drawable/calculator5" android:onClick="onClickButton_NewCalculation" android:id="#+id/button_main_NewCalculation" android:layout_width="300px" android:layout_height="90px" android:drawablePadding="0px"></Button>
<Button android:text="#string/string_main_Help" android:drawableLeft="#drawable/question" android:onClick="onClickButton_Help" android:id="#+id/button_main_Help" android:layout_width="150px" android:layout_height="90px" android:drawablePadding="0px" android:layout_below="#id/button_main_NewCalculation"></Button>
<Button android:text="#string/string_main_Share" android:drawableLeft="#drawable/share" android:onClick="onClickButton_Share" android:id="#+id/button_main_Share" android:layout_width="150px" android:layout_height="90px" android:drawablePadding="0px" android:layout_below="#id/button_main_NewCalculation" android:layout_toRightOf="#id/button_main_Help"></Button>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/layout_maintest2_relativelayoutright" android:layout_toRightOf="#id/layout_maintest2_relativelayoutleft" android:layout_alignTop="#id/layout_maintest2_relativelayoutleft">
<TextView android:layout_width="wrap_content" android:textSize="24px" android:textStyle="bold" android:layout_height="match_parent" android:id="#+id/textView_calculator_CalculatorTitle" android:text="#string/string_calculator_CalculatorTitle"></TextView>
</RelativeLayout>
</LinearLayout>
Its hard to tell from the image you uploaded but are you trying to keep the two relative layouts side by side?
You can try to position the layouts by declaring them to be drawn to one side of the parent or the other. I cant remember off the top of my head at the moment and I know thats a crappy answer but I think the answer is somewhere in here
http://developer.android.com/guide/topics/ui/layout-objects.html
Take a look at the android:layout_alignParentRight="true" declaration.
Im pretty sure I used something similar to position multiple views and buttons in the same parent before. Try it out and when I get back to my work computer on monday Ill look through my projects and see which one I know works the best.
Related
I am trying to design the layout of my Android project, but I must have done something wrong. Now when I try to drag the text boxes around, they just would not move on the screen. I am guessing it is one of the format setting, but not sure which one it is.
This is the XML file of how it looks:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.oldimagerevieal.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
FrameLayouts use gravity to position items on a screen using the
android:layout_gravity="left|top" //whatever you want.
Unless you need the FrameLayout, you can change the root element to a RelativeLayout or LinearLayout (don't forget to specify orientation), and you should be able to drag things around like normal.
I would high recommend getting familiar with building views in XML. It will significantly help you.
Enable "Autoconnection to parent" by clicking on the magnet button in the design tools section.
I am new to android programming and am trying to understand the android architecture and how are applications built around it.
So there is no real world need for this as of now. Its just some experimentation that I am doing to learn the stuff. What I want here is 3 different views, TextView, EditText and Button, horizontally next to each other. To achieve this here's the activity_main.xml that I am using : -
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_to_appear_on_button" />
</LinearLayout>
On running the MainActivity.java, that has setContentView(R.layout.activity_main);, in the onCreate(), I get the TextView and the EditText widgets on the screen, horizontally next to one another, but not the Button. I wonder why ?
And strangely I have observed that the last element inside
<LinearLayout>..</LinearLayout>
is the one that gets vanished from the screen. So if <Button .. /> is exchanged with say <TextView .. />then its the <TextView> element that will not be visible on the screen now.
Please explain what am I missing out here.
I am running the MainActivity.java on the emulator and am using Eclipse as my IDE, if this information helps further.
It depends on what you want to do. If you want three things horizontally in a LinearLayout, you will likely run out of space on the screen. To guarantee that all three fit, set:
android:layout_width="match_parent"
android:layout_weight="1"
For all 3. You can mess around with the weight as you see fit, but basically this will tell the rendering to fit all three objects on the screen horizontally, each one taking up 1/3 of the screen (if you change weight, it will be different values).
If using LinearLayout, you will probably nest multiple layouts, with a main vertical LinearLayout containing several horizontal ones. It is a valid approach, and is probably a matter of preference. LinearLayout allow for weights, which can be extremely useful because they are one way of guaranteeing things don't get cut off the screen.
RelativeLayout is another approach, wherein you specify where things on the screen are relative to each other (Left, Right, Above, Below). While these don't use weights, you can align elements with the edges of the screen and get the same effect.
As I said, the approach is largely a matter of preference, and usually some mesh of both works pretty well.
I recommend to you use relative layout for your xml ,If you use linear your widgets are assigned one by one,not your wish.its for your further developement
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="101dp"
android:layout_marginTop="50dp"
android:text="TextView" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="67dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="59dp"
android:text="Button" />
</RelativeLayout>
I've tried a couple of things and read many topics, but I didn't find any solution for my problem.
At the moment I have an xml file with the layout on the picture. This is how it should look like the design and I did it with LinearLayouts and works perfect.
The problem is that I want when I click somewhere on the screen to add a highlighted column from top to bottom.
I read that this should be done with RelativeLayout, but I tried to do it that way, but I can't arrange my other elements to be with equal size.
Do you know how this could be done ?
This is part of my xml:
<LinearLayout
android:id="#+id/chart1Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:background="#color/color"
android:gravity="bottom|center"
android:maxLines="4"
android:padding="3dp"
android:text="#string/cap"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/orange"
android:textSize="30sp"
android:textStyle="bold" />
<com.some.chart
android:id="#+id/result_widget_chartView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="8"
palette="grayscale"
android:background="#color/dark_header"
android:padding="0dp"
android:paddingBottom="0dp"
android:paddingLeft="10dp" />
</LinearLayout>
Design : http://i.stack.imgur.com/fkHvG.png
Thanks a lot.
see my pic:
if this what you want?
Thanks for your answers. It's not exactly what I tried to achieve, but you gave some direction and I did it as I want.
The solutions was to add Relative Layout as root and keep everything as-is, to keep the design.
http://i.stack.imgur.com/j18Dw.png
I was doing wrong when I tried to replace my Linear layout container with Relative layout - I just needed to add all my things in new Relative container (not to replace).
Thanks.
I'm new to Android programming (although I'm an expert in C and intermediate in Java).
I don't understand why my app crashes when I try to setContentView() with the default RelativeLayout that Eclipse automatically generates when I create a new project.
It crashes both in Emulater and on my phone.
But if I change the layout to a LinearLayout it works perfectly fine.
Can anybody give me a clear explanation to why this hapens?
Thanks
Firstly let me explain you about layouts and follow the given example:
Now let’s turn our attention to those helpful layout controls that organize other controls. The most commonly used layout classes are:
FrameLayout – designed to display a stack of child View controls. Multiple view controls can be added to this layout. This can be used to show multiple controls within the same screen space.
LinearLayout – designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout – designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
TableLayout – designed to organize child View controls into rows and columns. Individual View controls are added within each row of the table using a TableRow layout View (which is basically a horizontally oriented LinearLayout) for each row of the table.
try the following code for relative layout:
<?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:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/textMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp" />
</LinearLayout>
<Button
android:id="#+id/btn2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Contacts"
android:textColor="#342D7E" />
<Button
android:id="#+id/btn1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Send"
android:textColor="#342D7E" />
<EditText
android:id="#+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn2"
android:layout_alignParentLeft="true"
android:ems="10"
android:hint="Type a Message"
android:maxLines="5" >
<requestFocus />
</EditText>
</RelativeLayout>
let me know if it crashes again
I just had the same problem.
How I fixed it:
After changing the layout in main.xml I had the layout declared in the mainActivity.java file still as a LinearLayout.
Go into your mainActivity.java file and change it to relatively out.
Check yours it might solve it
I'm using textview objects to hold labels such as Score, Level etc on my game screen but they don't seem to be displayed where I want them to be. I understand about view hierarchies (parents, children) and am using the gravity tags in the XML layout file but it doesnt seem to have any effect.
Could someone just quickly provide a guide to positioning a textview object on the screen, and also linking it in the code so that its contents can be programmatically controlled (I believe this would by done via =(TextView) findViewById(r.id.resourcename))?
Many thanks
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gestures"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeWidth="2.0"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true">
<com.darius.android.distractions.DistractionsView
android:id="#+id/distractions_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text"
android:text="Hello"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#88ffffff"
android:textSize="24sp"/>
<TextView
android:id="#+id/textLevel"
android:text="#string/level_count"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="bottom"
android:textColor="#EB0000"
android:textSize="10sp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/concentration_bar"
android:textColor = "#EB0000"
android:textSize="10sp"
android:id="#+id/conbartext"
android:visibility="visible"
></TextView>
</RelativeLayout>
</android.gesture.GestureOverlayView>
</FrameLayout>
These are some really helpful tutorials on getting started with android layout and widgets: http://developer.android.com/resources/tutorials/views/index.html
Documentation and a guide on the layout itself is here:
http://developer.android.com/guide/topics/ui/declaring-layout.html
Common Layout Objects is a good guide to the basics of layouts. The gravity tag only has meaning in certain layout types.
Another useful tool is the Heirarchy Viewer, found in the tools folder of your Android installation. This allows you to visualize the View heirarchy of your running activity.
If you post your layout XML, and a mockup of what you are trying to accomplish, we might be able to help you accomplish it.