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.
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"
/>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With