user10570021
user10570021

Reputation:

Set Layout width percentage of the total screen width

Is there's way to set Button width 50% of the total screen size (from java code). I have found a few answers but i would like to change Button size with java code instead of XML.

Upvotes: 1

Views: 1603

Answers (6)

Ashvin solanki
Ashvin solanki

Reputation: 4809

I hope it Helps

possible with of Guideline constraint

Guideline

Utility class representing a Guideline helper object for ConstraintLayout. Helper objects are not displayed on device (they are marked as View.GONE) and are only used for layout purposes. They only work within a ConstraintLayout.

A Guideline can be either horizontal or vertical:

Vertical Guidelines have a width of zero and the height of their ConstraintLayout parent Horizontal Guidelines have a height of zero and the width of their ConstraintLayout parent Positioning a Guideline is possible in three different ways:

specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin) specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end) specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent) Widgets can then be constrained to a Guideline, allowing multiple widgets to be positioned easily from one Guideline, or allowing reactive layout behavior by using percent positioning.

See the list of attributes in ConstraintLayout.LayoutParams to set a Guideline in XML, as well as the corresponding ConstraintSet.setGuidelineBegin(int, int), ConstraintSet.setGuidelineEnd(int, int) and ConstraintSet.setGuidelinePercent(int, float) functions in ConstraintSet.

<android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.50" />

**

Example

**

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.50" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline3"
        tools:layout_editor_absoluteX="104dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.80" />

</android.support.constraint.ConstraintLayout>

Example with Layout height 50% of screen

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline4"
    app:layout_constraintTop_toTopOf="parent">

</RelativeLayout>

<android.support.constraint.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.50" />

2nd Way

getDefaultDisplay ()

public abstract Display getDefaultDisplay () Returns the Display upon which this WindowManager instance will create new windows.

Despite the name of this method, the display that is returned is not necessarily the primary display of the system (see Display.DEFAULT_DISPLAY). The returned display could instead be a secondary display that this window manager instance is managing. Think of it as the display that this WindowManager instance uses by default.

To create windows on a different display, you need to obtain a WindowManager for that Display. (See the WindowManager class documentation for more information.)

Display mDisplay = getWindowManager().getDefaultDisplay();
final int width  = mDisplay.getWidth();
final int height = mDisplay.getHeight();

// now simple height/2  use it as u want

Upvotes: 1

Harshil kakadiya
Harshil kakadiya

Reputation: 252

It's so simple just do.

button.getLayoutParams().width = Resources.getSystem().getDisplayMetrics().widthPixels/2;

Upvotes: 1

user10573977
user10573977

Reputation:

Try

LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(left, top, right, bottom); yourbutton.setLayoutParams(params);

Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    yourdpmeasure, 
    r.getDisplayMetrics());

Upvotes: 0

navylover
navylover

Reputation: 13609

Many ways to achieve this, you could try like below:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;

ViewGroup.LayoutParams params= yourBtn.getLayoutParams();
params.width= width * 1/2;
yourBtn.setLayoutParams(params);
yourBtn.requestLayout(); 

Upvotes: 1

gvnbsg
gvnbsg

Reputation: 11

Try this out..

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
ViewGroup.LayoutParams params = yourButton.getLayoutParams();
params.width = metrics.widthPixels;

    try {
        double ratio = 0.5;
        params.height = Double.valueOf(ratio * height).intValue();
        yourButton.setLayoutParams(params);
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 1

lasagnakid77
lasagnakid77

Reputation: 328

something like this perhaps? Not sure if it will work exactly as you want it but thats the general idea of it

Configuration configuration = getActivity().getResources().getConfiguration();
        int screenWidthDp = configuration.screenWidthDp;
        final float scale = context.getResources().getDisplayMetrics().density;
        int pixels = (int) (screenWidthDp * scale);
button1.setWidth(pixels*0.5);

Upvotes: 0

Related Questions