How to draw line between some linear layouts? - android

I want to create a layout like the below picture.
I have four linear layout and I want to separate them with the black lines same the picture.
How can I create this??

Add this between your horizontal layouts
<View
android:layout_width="5dp"
android:layout_height=“match_parent”
android:paddingTop=“16dp”
android:paddingBottom=“16dp"
android:background=“#android:color/black”/>
And this between you vertical ones
<View
android:layout_width=“match_parent"
android:layout_height=“5dp”
android:paddingRight=“16dp”
android:paddingLeft=“16dp"
android:background=“#android:color/black”/>
You’ll have to add these twice, as you have four layouts and hence four dividers.

if you wanted to do it with just layouts you could make a frame layout which layers layouts and then have two layouts for vertical and horizontal and then add your layout in. i think this might be what you are looking for as this will put the lines overtop over your other views.
<?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="horizontal">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<YOUR LAYOUT HERE/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_gravity="center_vertical"
android:layout_margin="40dp"
android:background="#ff000000" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_gravity="center_vertical"
android:layout_margin="40dp"
android:background="#ff000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal">
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_margin="40dp"
android:background="#ff000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal">
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_margin="40dp"
android:background="#ff000000" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

Related

android studio LinearLayout image starting point

I am very new to android programming and I am now trying to position a "circle ball" image file into the LinearLayout in the middle. So, I have tried making 7 LinearLayouts equally divided and I am trying to put an image at the bottom of the 4th LinearLayout. The code below shows that I have 7 LinearLayouts. How can I put a "circle ball" image at the bottom of the 4th LinearLayout? Also, is creating 7 LinearLayouts a good idea? The purpose of the ball would be to move the ball to the next LinearLayout when I click "next" Button.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_above="#+id/view">
<LinearLayout
android:id="#+id/layout1"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<LinearLayout
android:id="#+id/layout2"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<LinearLayout
android:id="#+id/layout3"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<LinearLayout
android:id="#+id/layout4"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<LinearLayout
android:id="#+id/layout5"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<LinearLayout
android:id="#+id/layout6"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<LinearLayout
android:id="#+id/layout7"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
Layout you have posted is very badly designed layout. If image is all you want to show on the screen then directly just put the image in the LinearLayout and set layout's gravity to center.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match+parent"
android:gravity="center">
<ImageView ... />
</LinearLayout>
Or, if there are other views too in your layout and you want the image to be placed in the center then use RelativeLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_centerInParent="true"
... />
</RelativeLayout>
IMO this is not a good approach having 7 nested layouts. If you want a view to be moved dynamically (let's say on click) you could use relative layout and then attach onClickListener to that view.
Using RelativLayout add Imageview as done below, if you want to move that image than for that You can use the onClick method and in that method change the src of the image views.
myImage.setImageResource(R.drawable.yourImage);
change your layout to :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/view"
android:orientation="vertical"
android:weightSum="7">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/layout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/layout6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/layout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" />
</LinearLayout>
<ImageView
android:layout_width="350dp"
android:layout_height="100"
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="300dp"
android:src="#drawable/cal" />
</RelativeLayout>

Design a round button over two colored layouts

I'm trying to design something like that :
Currently i'm having issue putting the button at "the middle" of the two layouts reunion point. Is there a way i do this ?
Thanks.
Here is a way to do it
<?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_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#FF0000">
<!-- TO ADD CONTENT HERE -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#FFFF00"
android:weightSum="0.5">
<!-- TO ADD CONTENT HERE -->
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000"
android:text="TEST"
android:textColor="#FFFFFF"/>
</RelativeLayout>
OUTPUT:
You can change the layout height from android:layout_weight="0.5"
you can use RelativeLayout for your parent and try adding twoLinearLayouts , one below another and set their heights, now place a Button in your RelativeLayout and set android:layout_centerInParent="true" for your Button .
and if you want to use weight for layouts, instead of adding two LinearLayout to your RelativeLayout, add one LinearLayout with weightSum=100 and then add two LinearLayouts to it with custom weights.
for your case , you have to do sth like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:background="#f00" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"
android:background="#0f0" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Android layout over more linearlayouts

I want to create layout like on the image
3 green layouts are linearlayouts and code is shown below.
Can some one give give me code for other red layouts over this three green. What type of layout should be ?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
Thanks
You want to put all your overlapping layouts into a Relative layout. So to re-create something similar to your picture do:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
android:layout_marginRight="10dp" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
android:layout_marginRight="10dp" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
android:layout_marginRight="10dp" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#ff0000"
android:orientation="vertical"
android:layout_marginTop="200dp"
android:layout_marginRight="50dp">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#ff0000"
android:orientation="vertical"
android:layout_marginTop="200dp"
android:layout_marginRight="50dp">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
RelativeLayout is generally used to overlap other layouts. In this case, you can put your LinearLayouts into a RelativeLayout then add the other two layouts accordingly as children of the parent RelativeLayout.
Example:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:layout_alignParentLeft="true">
</LinearLayout>
<LinearLayout
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginTop="200dp"
android:layout_alignParentLeft="true">
</LinearLayout>
</RelativeLayout>
There are many other attributes you can apply to the children that RelativeLayout will respond to, but that don't seem to apply to what you were asking for.
You can use RelativeLayout on the top of the whole layout hierarchy. The first layout in it will be the linear layout, which will will the whole relative layout. Above that, you can put another layouts, which will be placed next in the relative layout. You can align them as you want with aligment according to parent or acoording to each other. good luck

Android Layout - not displaying image view

This is my layout:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView android:id="#+id/preview_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"/>
<ViewfinderView
android:id="#+id/viewfinder_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/transparent"/>
<LinearLayout
android:id="#+id/result_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/result_view"
android:visibility="gone"
android:padding="4dip">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="#+id/App_logo"
android:src="#drawable/mylogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:id="#+id/status_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/transparent"
android:text="#string/msg_default_status"
android:textColor="#color/status_text"
android:textSize="14sp"/>
</LinearLayout>
</FrameLayout>
The problem is that the ImageView in the bottom of the XMLdoes not get displayed.
Any ideas?
10X :)
Each of your views contain the properties of "fill_parent" for their layout widths and heights. You need to either specify "wrap_content" for them, or place them in containers with positive and negative margins, or else they will all attempt to take up all of the available space.
You should remove android:layout_weight="1" from image view and image will appear.

How can I make my Android LinearLayout weights work with values of 1 and 2?

I'm trying to make a LinearLayout that has three rows, each of equal height. I am making this using 3 LinearLayouts inside the main one, each with a layout_weight of 1.
The middle LinearLayout should contain an ImageView, TextView, ImageView, with weights of 1,2,1. To do this, inside the middle LinearLayout I created another 3.
I want the TextView to be double the size of each ImageView. If you see the code below, when layout_weight is 1 for the inner LinearLayouts I have no problems, the three appear of equal size. However when I set the middle one (with id=textLayout) to layout_weight=2, it completely disappears (as seen in the Eclipse graphical layout viewer) and only the two others are visible.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/fondo"
android:gravity="center_vertical|center_horizontal">
<LinearLayout
android:id="#+id/LayoutTop"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:id="#+id/LayoutMid"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ImageView>
</LinearLayout>
<LinearLayout
android:id="#+id/textLayout"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="2">
<TextView
android:text="Status"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/LayoutBottom"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
You don't need the additional layouts to space the middle row appropriately, and your weights are backwards. If you want the text to be 2x the outer images, you want it to 1, and the outer columns to be 1.5
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/LayoutTop"
android:orientation="horizontal" android:layout_height="0dp"
android:layout_width="fill_parent" android:layout_weight="1" android:background="#ffff0000"/>
<LinearLayout android:id="#+id/LayoutMid"
android:orientation="horizontal" android:layout_height="0dp"
android:layout_width="fill_parent" android:layout_weight="1">
<ImageView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:background="#drawable/icon"
android:layout_weight="1.5" />
<TextView android:layout_height="fill_parent" android:id="#+id/TextView01"
android:layout_weight="1" android:text="Status" android:layout_width="fill_parent" />
<ImageView android:layout_height="fill_parent"
android:layout_weight="1.5" android:background="#drawable/icon"
android:layout_width="fill_parent" />
</LinearLayout>
<LinearLayout android:id="#+id/LayoutBottom"
android:orientation="horizontal" android:layout_height="0dp"
android:layout_width="fill_parent" android:layout_weight="1" android:background="#ff00ff00"/>
</LinearLayout>
I used an icon (if you have one) to put a picture in the middle row, and some colors in the top and bottom in to show the spacing more clearly.

Categories

Resources