Home > other >  Why won't my programmatically created textview work?
Why won't my programmatically created textview work?

Time:03-17

'''

package com.example.pillappreal;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Array;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    public ArrayList<Drug> list = new ArrayList<>();
    public ArrayList<String> testList = new ArrayList<>();
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void addNewDrug(View view) {
        Intent intent = new Intent(this, AddNewDrugActivity.class);
        startActivity(intent);
    }

    public void testMethod(View view) {
        Intent intent = new Intent(this, MainActivity.class);
    }

    public void addNewDrug(Drug drug) {
        list.add(drug);
        if (list.size() > 0) {
            Log.d("actually works ffs", list.get(0).name);
            for (int i = 0; i < 5; i  ) {
                LinearLayout linearLayout = findViewById(R.id.rootLayout);

                // Create TextView programmatically.
                TextView textView = new TextView(this);
                textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                textView.setGravity(Gravity.CENTER);
                textView.setText(list.get(i).name);

                // Add TextView to LinearLayout
                if (linearLayout != null) {
                    linearLayout.addView(textView);
                }

            }
            Log.d("Mainlist", list.get(0).name);
        }
    }
}

'''

The textview won't actually be created in the app. I know the textview code works because when I use it to print out "test" in the onCreate method, it works. I know the list arraylist gets its objects from the helper class because Log.d shows the values in the addNewDrug method. For some reason the textView code is not working, even when I use a string instead of value from the arraylist, and i have no clue why.

CodePudding user response:

Why do you want to create a TextView programmatically? You can hide and show the TextView with visibility.

If you want add new TextViews based on a user event, you can use ListView or RecyclerView

CodePudding user response:

ListView sample

xml:

   <ListView
    android:id="@ id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Java code:

    ListView listView;

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

    listView = findViewById(R.id.listView);
    test();
}

private void test() {
    ArrayList arrayList = new ArrayList();
    arrayList.add("abcd");
    arrayList.add("1234");

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(adapter);

}
  • Related