Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

Android Java: Create view button at runtime

 

How to create buttons or views in an Activity Display in Android Java at runtime?

 

To do this, you must provide the base layout with an ID in the activity.

Then you look at runtime the base layout with findViewByID. Then create a view element with new like here a button

Button button1=new Button(this);

 

And add this to the ad with addView

root_Panel.addView(button1);

 

With a linear layout, the new view elements are simply added as in a stack.

With a relative layout or new constraint layout you should still determine the new position.

//< set_Position >
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
position2.setMargins(
200, 200, 0,0);
position2.addRule(RelativeLayout.
ALIGN_LEFT);
position2.addRule(RelativeLayout.
ALIGN_TOP);
button2.setLayoutParams(position2);
//</ set_Position >

 

 

 

The easiest way to use a layout is a RelativeLayout or LinearLayout.

 

 

 

 

 

Activity_Main

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:id="@+id/root_Layout"
   
android:layout_height="match_parent"
   
android:layout_width="match_parent"
   
android:background="#eeeeee"
   
>
</
RelativeLayout>

 

 

 

 

package com.codedocu.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        create_Buttons();
    }


   
public void create_Buttons()
    {
       
//------------< create_Buttons() >------------
        //< get_Layout >
       
RelativeLayout root_Panel= findViewById(R.id.root_Layout);
       
//< get_Layout >

        //--< Element1 >--
       
Button button1=new Button(this);
        button1.setText(
"Button 1");
        button1.setBackgroundColor(Color.
YELLOW);
       
//< set_Position >
       
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(
10, 10, 0,0);
        params.addRule(RelativeLayout.
ALIGN_LEFT);
        params.addRule(RelativeLayout.
ALIGN_TOP);
        button1.setLayoutParams(params);
       
//</ set_Position >

        //< add >
       
root_Panel.addView(button1);
       
//</ add >
        //--</ Element1 >--

        //--< Element2 >--
       
Button button2=new Button(this);
        button2.setText(
"Button 1");
        button2.setBackgroundColor(Color.
RED);
       
//< set_Position >
       
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        position2.setMargins(
200, 200, 0,0);
        position2.addRule(RelativeLayout.
ALIGN_LEFT);
        position2.addRule(RelativeLayout.
ALIGN_TOP);
        button2.setLayoutParams(position2);
       
//</ set_Position >

        //< add >
       
root_Panel.addView(button2);
       
//</ add >
        //--</ Element1 >--

        //------------</ create_Buttons() >------------
   
}
}

 

 

Activity = Display Page Page

View = Display element

 

Mobile
»
Android Java: Read text from a resource file
»
Android Java: Create unique GUID at runtime
»
Android Java: Comparison with empty string
»
Android java: Code area // region - // endregion
»
Android: Create a color picker
»
Android: View items by name or tag or id
»
Android Java: Create a view event at runtime and assign it to a view element
»
Android Java: Create view button at runtime
»
Android Java: create a view element as a float
»
Input field in Android

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de