1

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.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • 2
    You are trying to show some `listCharacter` list and open a specific character screen `loadCharacter` on button click. There is no need to add buttons into linear layout at runtime like this. You can use a simple [RecyclerView](https://stackoverflow.com/questions/40584424/simple-android-recyclerview-example) to inflate as many characters as you need; not only 20 as in your example. And then register a click listener to the buttons in ViewHolder object; based on the position you can transfer necessary info to `loadCharacter` screen. – Jay Jul 26 '21 at 21:38
  • Thank you! This actually works for me! For those who tries too recreat this aswell, the recycle viewer did not work for me so i used this code " – BergströmOscar Jul 27 '21 at 20:02

1 Answers1

1

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.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131