Assign width to half available screen width declaratively

Is it possible to assign a widget width to half the available screen width, and do it using declarative xml?

  • Android View onSaveInstanceState not called
  • @Named providers with same return types end up giving java.lang.IllegalArgumentException: Duplicate
  • How to enable/disable toolbar scrolling programmatically when using design support library
  • Charts for Android
  • Android call method on notification click
  • Android: How to generate a frequency?
  • Related posts:

    Generic class that extends class and implements interface
    Gradle sync failed: Unable to find method
    Widget shows “App Isn't Installed” Toast in ICS
    onSharedPreferenceChanged not fired if change occurs in separate activity?
    Android Material Design - How to change background color of Toolbar after CollapsingToolbarLayout is...
    Get facebook email after login in Android
  • Facebook login with Android SDK 3.0 causing ANRs or not working at all
  • How can I add a JAR in my gradle project?
  • RecyclerView Adapter notifyDataSetChanged stops fancy animation
  • Obfuscation: hide hardcoded values in java
  • Android For loop
  • Android Webview with Back Button, if else
  • 4 Solutions collect form web for “Assign width to half available screen width declaratively”

    If your widget is a Button:

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="horizontal">
        <Button android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="somebutton"/>
    
        <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    </LinearLayout>
    

    I’m assuming you want your widget to take up one half, and another widget to take up the other half. The trick is using a LinearLayout, setting layout_width="fill_parent" on both widgets, and setting layout_weight to the same value on both widgets as well. If there are two widgets, both with the same weight, the LinearLayout will split the width between the two widgets.

    give width as 0dp to make sure its size is exactly as per its weight
    this will make sure that even if content of child views get bigger, they’ll still be limited to exactly half(according to is weight)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
         >
    
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="click me"
        android:layout_weight="0.5"/>
    
    
        <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:layout_weight="0.5"/>
      </LinearLayout>
    

    Another way for single item in center, which fill half of screen:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:visibility="invisible" />
    
            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2" />
    
           <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:visibility="invisible" />
    
    </LinearLayout>
    
    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textD_Author"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Author : "
        android:textColor="#0404B4"
        android:textSize="20sp" />
     <TextView
        android:id="@+id/textD_Tag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Edition : "
        android:textColor="#0404B4"
        android:textSize="20sp" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:weightSum="1" >
        <Button
            android:id="@+id/btbEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Edit" />
        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Delete" />
    </LinearLayout>
    </LinearLayout>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.