Bom dia, tudo certo?
Estou tendo problema na criação de um layout dinâmico, onde não posso devo criar os componentes da tela e "settar" o id de cada dinamicamente também.
package br.agr.terraviva.teste_5s;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Parent layout
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.layout);
// Layout inflater
LayoutInflater layoutInflater = getLayoutInflater();
View view;
for (int i = 1; i <= 21; i++) {
// Add the text layout to the parent layout
view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);
// In order to get the view we have to use the new view with text_layout in it
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText("Row " + i);
textView.setId(R.id.layout1);
// Add the text view to the parent layout
parentLayout.addView(textView);
}
}
}
Através do codigo acima, pude realizar a criação de componentes, no caso TextView, mas tive problema na hora de criar o "FindViewById", de forma que seja refenciado pelo ID de forma dinâmica.