Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show a rectangular box in the absolute center in a layout

I want to show a rectangular box in the absolute center in a layout with height h/3 and width 3w/5 (w: width of screen, h: height of screen). Please help me to find a solution, Thanks in advance.

like image 344
Jithu Avatar asked Apr 24 '13 05:04

Jithu


2 Answers

you can adjust it using linear layout using weight i have pasted a sample code below hope this helps.

<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="@color/transparent"
 >

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
 />
 <TextView
    android:id="@+id/desiredbox"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="TextView"
    android:layout_gravity="center"
    android:background="@color/skyblueBackground" 
    android:layout_weight="1"
    />
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
 />
 </LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>

like image 196
varun thomas Avatar answered Sep 20 '22 23:09

varun thomas


Yeah. This is possible using a Relative layout parent and another layout(your box) inside that positioned as center. and the width and height of your box can be mentioned in java, rather than in xml.

like image 27
DGN Avatar answered Sep 17 '22 23:09

DGN