BergströmOscar
BergströmOscar

Reputation: 17

Dynamic buttons dont show in android studio using fragments

Im trying to dynamicaly add buttons to a fragment, but they do not show.

This is the listCharacter.java:

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_list_character, container, false);
        context = view.getContext();

        for (int i = 1; i <= 20; i++) {
            LinearLayout layout = new LinearLayout(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            Button firstBtn = new Button(this.context);
            firstBtn.setId(i);
            final int id_ = firstBtn.getId();
/**
 * The text is not updated(?)
 */
            firstBtn.setText("button " + id_);
/**
 * it does not add 20 button, might be on top of each other
 */
            layout.addView(firstBtn, params);

            firstBtn = view.findViewById(R.id.characterNameBtn);
            firstBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Toast.makeText(view.getContext(),
                            "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                            .show();
                    Navigation.findNavController(view).navigate(R.id.action_listCharacter_to_loadCharacter);
                }
            });
        }

        return view;
    }

And this is the fragment_list_character.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".listCharacter"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/list_character_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
    <Button
        android:id="@+id/characterNameBtn"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="#66000000"
        android:fontFamily="@font/main"
        android:gravity="center"
        android:padding="5dp"
        android:text=""
        android:textColor="#fff"
        android:textSize="40sp"
        />
    </LinearLayout>

</LinearLayout>

I dont get any error messages atm, but i cant see the buttons when i run the program. The on click works and it says it has id 20.

Upvotes: 1

Views: 100

Answers (1)

Cheticamp
Cheticamp

Reputation: 62841

You're adding the buttons to a LinearLayout that you are recreating each time through your loop.

for (int i = 1; i <= 20; i++) {
    LinearLayout layout = new LinearLayout(context);
    ...

    layout.addView(firstBtn, params)
    ....
}

layout is never added to the view that is returned. You will need to add the buttons to the view or to a child of the view that you return to see them on the screen. I think that there are a few other issues, but this would be the first one to address.

Upvotes: 1

Related Questions