androidbuttonandroid-activitymain-activity

Is it possible to add a button to an empty MainActivity in android?


I have an empty Activity_main layout. I want to add a number of buttons that have to be a certain % of the screen width and for this reason I want to add them programmatically. Every example on this site uses a LinearLayout. Is it possible to add a button directly to the Activity_main?

Thanks.


Solution

  • You need a ViewGroup because it is the base class for layouts and views containers.

    If don't want to use a linear layout, you can use a constraint layout. Constraint layouts come with Guidelines - they can help you measure a specific percentage of the screen and align buttons (or views) next to them.

    guidelines with constraintlayout

    The image above is an example of how guidelines work. The first is a vertical guideline that takes up 25% of the screen (you can think of it as splitting the screen vertically into 4 pieces). The second is a horizontal guideline that takes up 50% of the screen (splitting the screen horizontally into 2 pieces).

    i want to add a number of buttons that have to be a certain % of the screen width

    You can achieve this with guidelines. Here's how to do it using only xml:

    <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"
        tools:context="com.chornge.myapplication.MainActivity">
    
        <android.support.constraint.Guideline
            android:id="@+id/vertical_guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.25" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toRightOf="@id/vertical_guideline" />
    
        <android.support.constraint.Guideline
            android:id="@+id/horizontal_guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.50" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/horizontal_guideline" />
    
    </android.support.constraint.ConstraintLayout>
    

    And here's how to create the same effect using only Java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
    
        ConstraintLayout constraintLayout = new ConstraintLayout(this);
        constraintLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        setContentView(constraintLayout);
    
        /* vertical guideline */
    
        Guideline verticalGuide = new Guideline(this);
        ConstraintLayout.LayoutParams verticalGuideParams =
                new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
    
        verticalGuideParams.guidePercent = 0.25f;
        verticalGuideParams.orientation = LinearLayout.VERTICAL;
        verticalGuide.setLayoutParams(verticalGuideParams);
        verticalGuide.setId(View.generateViewId());
        constraintLayout.addView(verticalGuide);
    
        /* create button1 and align its left edge to the right edge of vertical guideline*/
    
        Button button1 = new Button(this);
        button1.setId(View.generateViewId());
        ConstraintLayout.LayoutParams button1Params =
                new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
    
        button1Params.leftToRight = verticalGuide.getId();
        button1.setLayoutParams(button1Params);
        constraintLayout.addView(button1);
    
        /* horizontal guideline */
    
        Guideline horizontalGuide = new Guideline(this);
        ConstraintLayout.LayoutParams horizontalGuideParams =
                new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
    
        horizontalGuideParams.guidePercent = 0.5f;
        horizontalGuideParams.orientation = LinearLayout.HORIZONTAL;
        horizontalGuide.setLayoutParams(horizontalGuideParams);
        horizontalGuide.setId(View.generateViewId());
        constraintLayout.addView(horizontalGuide);
    
        /* create button2 and align its top edge to the bottom edge of horizontal guideline*/
    
        Button button2 = new Button(this);
        button2.setId(View.generateViewId());
        ConstraintLayout.LayoutParams button2Params =
                new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
    
        button2Params.topToBottom = horizontalGuide.getId();
        button2.setLayoutParams(button2Params);
        constraintLayout.addView(button2);
    }
    

    Note: