0

I'm trying to dynamically create a button, but nothing I've tried seems to be working. This is what I have so far:

Button test = new Button(this);
test.setText("test");
test.setBackgroundResource(R.color.blue);
test.setLayoutParams (new LinearLayout.LayoutParams(60,ViewGroup.LayoutParams.FILL_PARENT));

This is my xml file, it's a constraint layout, if that maybe conflicts with the dynamically created linear layout:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/profileLayout"
    android:theme="@android:style/Theme.NoTitleBar"
    tools:context="com.example.myfirstapp.MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My Profile"
        android:fontFamily="serif"
        android:textColor="@color/green"
        android:textAlignment="center"
        android:textSize="30dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0" />

    //Directory Icons
    <ImageButton
        android:id="@+id/profileDir"
        android:layout_width="73dp"
        android:layout_height="55dp"
        android:backgroundTint="@color/orange"
        android:scaleType="fitCenter"
        android:src="@drawable/profile_icon"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintVertical_bias="1.0" />
Sea
  • 45
  • 1
  • 9

2 Answers2

0

Did you add this button to a layout? Using layout.addView(test); to add the test button to the layout.

Esc Điệp
  • 346
  • 2
  • 8
  • No, can you elaborate on how to do this? – Sea Apr 24 '17 at 02:08
  • I added this to it, and now it crashes: LinearLayout layout = (LinearLayout) findViewById(R.id.profileLayout); layout.addView(test); – Sea Apr 24 '17 at 02:24
0

Okay, So by not working you meant not showing up on layout.

Simply this is because you have created a button named test but it is in memory only not in the activity's layout!

To add a child-view dynamically, you must add it to a parent-view. In more details if your layout (xml) of the activity contains a container (LinearLayout or any other container layout) and you want to add the child-view (button test) to this container you should:

1- bind this container (parent-view) in you code using findViewById()

2- add the child-view to the parent-view using addView()


here is some example:

Suppose your xml contains a LinearLayout with id: myLinearLayout

1- bind it using findViewById() in onCreate()

mLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);

2- add your button to it using addView()

Button test = new Button(this);
test.setText("test");
test.setBackgroundResource(R.color.blue);
test.setLayoutParams (new 
LinearLayout.LayoutParams(60,ViewGroup.LayoutParams.FILL_PARENT));
mLinearLayout.addView(test); //this will show your button on layout

Also here is a more general example about dynamically created views even if you want to add parent-view in run-time.

And you can read more about addView() method here

Community
  • 1
  • 1
Atef Hares
  • 4,715
  • 3
  • 29
  • 61