Sorin Grecu
Sorin Grecu

Reputation: 1034

How to add view to another layout

So,i have my MainActivity with its layout activity_main.xml.From this Activity,when a Print button is pressed i want that activity to send the data that the users inputs,and add TextViews to my lista.xml that will get turned into a bitmap and then sent to my receipt printer.

After some headache with getting a NullPointerException i've learned that i have to do something like this setContentView(R.layout.lista); before doing this ll1= (LinearLayout) findViewById(R.id.layoutlista1);. The problem is that this switches the layout i see,when using setContentView it shows my lista.xml.I guess i could fix this by using setContentView(R.layout.activity_main) but i'm pretty sure this isn't how things should be done to accomplish what i want.

So my question is,how do you add Views to another layout from the current activity layout without getting a nullPointerException ?

This is my lista.xml layout :

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="S.C. INTER S.R.L."
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="GALATI" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="Data:" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="-----------------------------------------" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1000" >

    <LinearLayout
        android:id="@+id/layoutlista1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="500"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nume produs" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutlista2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="250"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cantitate" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutlista3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="250"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pret" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="-----------------------------------------" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TOTAL: " />

    <TextView
        android:id="@+id/totallista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0.0" />
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="-----------------------------------------" />

This is how i initialize the layouts :

    public LinearLayout ll1;
public LinearLayout ll2;
public LinearLayout ll3;
    setContentView(R.layout.lista);
ll1= (LinearLayout) findViewById(R.id.layoutlista1);
ll2= (LinearLayout) findViewById(R.id.layoutlista2);
ll3= (LinearLayout) findViewById(R.id.layoutlista3);

Upvotes: 0

Views: 5864

Answers (1)

Van Vu
Van Vu

Reputation: 303

Umh if you're still want to know the answer then you could read from this post How to inflate one view with a layout.

In case you're still wonder about how to initialize an inflater then this would be another way

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

I've get the idea from Inflate a view / layout into another layout?. I'll try to wrote it again and hope this could help. Did not check it's actually working or not

//at first add contain view to main layout
setContentView(R.layout.activity_main);

//set reference to recently mail layout. Set id to your main layout
//change it to watever LinerLayout ...etc
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.your_main_layout);

// create inflater 
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// infalate lista and infalate it to root - main layout in this case.
// check http://developer.android.com/reference/android/view/LayoutInflater.html for more

View view = (View) inflater.inflate(R.layout.lista, mainLayout, true);

// doing blah blah with view

Well I did not check is it work or not but IMO it should work.

Upvotes: 2

Related Questions