1

I would like to access a textview called user_status that's inside a custom xml layout file in order to populate it with some data. I would like to access it from my main activity java class. How do I do this? I have tried some answers already on stack overflow but it's either not working or too outdated.

LayoutInflater inflater = MessageActivity.this.getLayoutInflater();
                    View inflatedView = inflater.inflate(R.layout.message_row, null);
                    TextView userMessage= (TextView) inflatedView.findViewById(R.id.user_status);
                    userMessage.setText("NEW MESSAGE DUMMMY!");
Arron Lapta
  • 198
  • 2
  • 9
  • what data you want to populate the textview with? JSON, a data array?, Please provide more info and code that you already tried please – Ruben Meiring Oct 16 '19 at 06:43
  • I would just like to populate it with a simple string data. – Arron Lapta Oct 16 '19 at 06:44
  • share what u tried yet...... – Omkar Oct 16 '19 at 06:44
  • ` TextView t =findViewById(R.id.t); t.setText(String.valueOf(list.size()));` This is code to set a textview with data from a list try removing the inflater and just using it as a plain normal texxtview – Ruben Meiring Oct 16 '19 at 06:49
  • Check your ID of the textview, change it to something simple and try the code you have also remove the (TextView) you have its unnecessary – Ruben Meiring Oct 16 '19 at 06:54

2 Answers2

2

Use

TextView userMessage = inflatedView.findViewById(R.id.user_status);
userMessage.setText("NEW MESSAGE DUMMMY!");
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

To achieve this in your MainActivity, you should add view first then find TextView or you may include your custom layout file in your MainActivity's xml layout.

Add View can find here

include layout can find here

PRIYA PARASHAR
  • 777
  • 4
  • 15