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?

  • OpenGL ES 2.0 screen flickering
  • How can I get the line number where the exception was thrown using Thread.UncaughtExceptionHandler?
  • How can I attach an image file in email?
  • Android: class not found exception: android.support.v4.app.FragmentPager
  • Preventing Facebook Android SDK from displaying password as plain text
  • Circle voice animation in Android
  • Related posts:

    Using Camera in the Android emulator
    Android 5.0: howto change Overview Screen Task Title background color
    Create Smaller Tabs in Android
    How to get an attribute from an XMLReader
    Why canvas is not the same size as view in onDraw? (Android)
    Change absolute position with FrameLayout
  • RxJavaPlugins Error Didn't find class “com.google.devtools.build.android.desugar.runtime.ThrowableExtension”
  • Android + NoSQL
  • what R.java file actually does and how
  • Android ImageView does not match_parent inside FrameLayout below GingerBread
  • Firebase Cloud Messaging - Android: click-action of notification when app is closed doesn't work
  • Ubuntu 64 with Android 64 Bundle cannot find adb executable
  • 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.