skip to Main Content

I am totally new to Android Studio with Java, I am familiar only with html , CSS , JavaScript and PHP.

I am building an app and I need it to generate a list of items from an array. How do I do that?

I tried to do simply this, but nothing shows up in the activity!

public void view(View v){

    TextView t = new TextView(viewAuto.this);

    t.setTextColor(Color.RED);

t.setText("PIPPO");
}

2

Answers


  1. in Android, in the first step, you put each feature of the palette in xml, you have to identify it in java files
    like this

    TextView textView = findViewById(R.id.textView);

    Then you can apply the changes
    What you did was to create an object from textView, which is wrong in the case of material design
    Rather, it applies to classes, fragments, adapters, etc

    Login or Signup to reply.
  2. You need first create XML layout and create TextView component there

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    
    </androidx.appcompat.widget.LinearLayoutCompat>
    

    then you go code and get text view, and append all the strings using n so that at the end of each string it goes to new line

    class MainActivity : Activity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
    
            val textView = findViewById<R.id.text_view>()
    
            val yourList = listOf("Here", "Will", "Be", "Your", "Strings")
    
            yourList.forEach { textView.text = textView.text + it + "n" }
        }
    
    }
    

    For showing your items in a scrollable list, you need to use ScrollView, for better performance you need to use RecyclerView

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search