Readdy Write

Android Java: View Button zur Laufzeit erstellen

25.09.2018 (👁3260)


Wie erstellt man in Android Java zur Laufzeit Buttons oder Views in einer Activity Anzeige ?

Hierzu muss man in der Activity das Basis-Layout  mit einer ID versehen.

Dann sucht man zur Laufzeit das Basis-Layout mit findViewByID. Dann erstellt man ein View-Element mit new wie hier eine Button

Button button1=new Button(this);

Und fügt dieses der Anzeige hinzu mit addView

root_Panel.addView(button1);

Bei einem LinearLayout werden die neuen View-Elemente einfach wie in einem Stapel hinzugefügt.

Bei einem RelativeLayout oder neuen ConstrainLayout sollte man noch die neue Position bestimmen.

//< 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 >

 

 

 

Am einfachsten verwendet man als Layout ein RelativeLayout oder 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=Anzeige Page Seite

View=Anzeige Element


0,00 €