Thursday, October 25, 2018

Android: Get linearLayout (Any layout file from layout folder by name) and add to current Layout

In need to get a LinearLayout (Any layout file from layout folder by name) and add to current Layout
package com.pritom;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class TestActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_main_page);

        LinearLayout mainLayout = this.findViewById(R.id.mainLayoutScrollView);
        /* If you want to remove all views from current layout */
        if (mainLayout.getChildCount() > 0) mainLayout.removeAllViews();

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        /* There must be a layout file named 'home_content.xml' in layout folder */
        View view = inflater.inflate(R.layout.home_content, null);
        mainLayout.addView(view);
    }
}

No comments:

Post a Comment