-2

How to add a view like TextView from another layout to the LinearLayout in current Layout in android .

I want to add TV_Added TextView from items.xml layout to LIN_Main LinearLayout in activity_main.xml Layout .

activity_main.xml layout in my current Layout . but there is a error .

items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
    <TextView 
        android:id="@+id/TV_Added"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"/>

</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    
    <LinearLayout 
        android:id="@+id/LIN_Main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/hello_world" />

    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.testaddview;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    LinearLayout LIN_Main;

    TextView TV_GroupTitle;

    LayoutInflater LYOTInf;

    View VIW_AllItems;


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


        LIN_Main = (LinearLayout) findViewById(R.id.LIN_Main);

        LYOTInf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        VIW_AllItems = LYOTInf.inflate(R.layout.items,null);

        TV_GroupTitle = (TextView) VIW_AllItems.findViewById(R.id.TV_Added); 

        LIN_Main.addView(TV_GroupTitle);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

1 Answers1

0

I find the link that tell about this . The answer is :

How to display a TextView from another layout

Community
  • 1
  • 1