How to display a TextView from another layout

别等时光非礼了梦想. 提交于 2019-12-06 07:33:13

You can use intent and pass the value to the next Activity. You only inflate the layout but that is not added to Activity.

You will have to get the reference to RelativeLayout and add the inflated view to it

Have a id for RelativeLAyout

<RelativeLayout android:id="@+id/relativelayout

Then

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
View vi = inflater.inflate(R.layout.layout2, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.text1);
tv.setText("hELLLLOO");
rl.addView(vi);

You may also use the

http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/

Or

In MainActivity

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("key","hello");
startActivity(intent);

Then in SecondActivity

// Inflate Layout 2 and set text to textview.
String value = getIntent().getStringExtra("key");
i.n.e.f

do something like this in MainActivity.java

View view;
/* We inflate the xml which gives us a view */
view = mInflater.inflate(R.layout.my_list_custom_row, false);

 /* Get the widget with id name which is defined in the xml of the row */
 TextView name = (TextView) view.findViewById(R.id.name);

 /* Populate the row's xml with info from the item */
 name.setText(myObject.getName());

Where mInflater is an Object of LayoutInflator. Or Check this link. hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!