Load layout from another XML - android

I want to change the content of a "RelativeLayout", the problem is that the content of that layout is in another xml.
setContentView(R.layout.activity_fullscreen);
//from actual xml
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout);
//from another xml
RelativeLayout rL2= (RelativeLayout) findViewById(R.id.cocinaL);
rL1.removeAllViews();
rL1.addView(rL2); //this fail because rL2 is null
Thank

you can't add another layout View without LayoutInflater
setContentView(R.layout.activity_fullscreen);
//your actule view
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout);
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// your second xml
View view2 = inflater.inflate(R.layout.myview12, null);
// add second View
rL1.addView(view2 );

//from actual xml
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout);
//from another xml
View child = getLayoutInflater().inflate(R.layout.child, null);
RelativeLayout rL2 = (RelativeLayout) child.findViewById(R.id.cocinaL);
rL1.removeAllViews();
rL1.addView(rL2);

Related

Retrieve RelativeLayout inside included layout get null

i get null pointer error in line :
rl.setBackgroundResource(R.drawable.headerbackground);
MainActivity code:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View mainLayout = inflater.inflate(R.layout.main_layout, null, false);
View includedHeader = mainLayout.findViewById(R.id.header);
RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);
rl.setBackgroundResource(R.drawable.headerbackground);
setContentView(mainLayout);
in main_layout.xml I include header_layout :
<include android:id="#+id/header" layout="#layout/header_layout"/>
and in header_layout.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/header_root" />
question: why rl is null and how access to it in my code?
EDITED: logcat logs(mainActivity.java:58 is rl.setBackground(R.drawable.headerbackround) in my code):
Caused by: java.lang.NullPointerException
at com.npi.blureffect.MainActivity.onCreate(MainActivity.java:58)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
   
You are setting your ContentView in the wrong order. You have to first set the ContentView, then change the background color :
setContentView(R.layout.main_layout);
// the include tag is included dynamically in the root view
RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.header_root);
rl.setBackgroundResource(R.drawable.headerbackground);
Instead of fetching it from includedHeader try to fetch it directly from mainLayout. You don't need to assign id to include tag and bind it in java. You can directly fetch views
Replace
RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);
with
RelativeLayout rl = (RelativeLayout) mainLayout.findViewById(R.id.header_root);
Your code will be
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View mainLayout = inflater.inflate(R.layout.main_layout, null, false);
RelativeLayout rl = (RelativeLayout) mainLayout.findViewById(R.id.header_root);
rl.setBackgroundResource(R.drawable.headerbackground);
setContentView(mainLayout);
You should try debugging and see at line rl.setBackgroundResource(R.drawable.headerbackground); if the variable rl is null.
In case it's not then maybe the drawable is null.
EDIT:
rl is null because includedHeader must be null.
Try removing the line View includedHeader = mainLayout.findViewById(R.id.header); and replace with RelativeLayout rl = (RelativeLayout) mainLayout.findViewById(R.id.header_root);
Afterwards check to see if mainLayout or rl are null (while debugging)
EDIT 2:
When you are creating reusable layouts (ones that will be later included using include) it's always best to make the root a FrameLayout
Also check this page out and maybe you can use the <merge> tag instead.
Some code like your works very well for me in Android 4.2.2 until nowadays.
But, I don't know why, after Android 4.4 a (RelativeLayout) included dynamically inside another (RelativeLayout) returns null after a rootLayout.findViewById( ... ).
In this cases, you can safely find by views using root layout directly!
So, to correct work this code you must include only one line of code:
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View mainLayout = inflater.inflate(R.layout.main_layout, null, false);
View includedHeader = mainLayout.findViewById(R.id.header);
RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);
if ( rl == null ) { rl = includedHeader; } // include this line
rl.setBackgroundResource(R.drawable.headerbackground);
setContentView(mainLayout);
This code works succesfully for all Androids before and after 4.4
Berst regards!

Android: Load layout from xml to new layout

I want to load layout dafined in xml to newly created LinearLayout object. Is that possible?
LinearLayout new_layout = new LinearLayout(context);
new_layout.load(R.layout.my_layout); // is something like this possible?
What you're trying to achieve is usually done this way
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout newLayout = (LinearLayout) inflater.inflate(R.layout.my_layout, null);

Getting View from RelativeLayout

Programatically I have a RelativeLayout and TextView (called title) within it defined as follows:
RelativeLayout layout = new RelativeLayout(this);
final RelativeLayout.LayoutParams parameters_title =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
title.setLayoutParams(parameters_title);
layout.addView(title,parameters_title);
I intend to add more textviews and buttons later. But for now, I want to "convert" (I am not sure how to put it into words) the RelativeLayout into a View in order to put it into WindowManager. I was trying to use LayoutInflater like this but it wouldn't work
LayoutInflater layoutInflater =
(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = layoutInflater.inflate(layout, null );
then the goal is to put myView into WindowManger through addView. How can I get a View from my RelativeLayout?
you can do this by getChildAt but you will have to keep the position of the view you want to get from layout. To avoid this you can set an id for your view and get the view with findViewById(). If you inflate a view from xml you will get the same views as xml. if you add some new views to the relativeLayout it will not effect the view you inflate from the xml.
when you are adding the view:
myView.setId(id);
layout.addView(myView);
when you are retrieving
View myView = layout.findViewById(id);

How to get a reference to an XML file from FrameLayout

I need to get reference to separate XML file which is FrameLayout but I can't figure out how to do it, this code doesn't work:
FrameLayout desktopFrameLayout = (FrameLayout) findViewById(R.id.desktopsFramelayout);
desktopFrameLayout.setDrawingCacheEnabled(true);
desktopFrameLayout.buildDrawingCache();
Bitmap bitmap = desktopFrameLayout.getDrawingCache();
For that you have to use inflate view.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
FrameLayout item = (FrameLayout ) view.findViewById(R.id.desktopsFramelayout);

Add custom layout (from file) to another layout

Content view is a LinearLayout. We'll call it llOne and we'll say it's in the file llOne.xml.
The view I'm trying to add is also a LinearLayout but they're in separate files. We'll call it llTwo and we'll say it's in the file llTwo.xml.
setContentView(R.layout.llOne);
LinearLayout llOne = (LinearLayout) findViewById(R.id.llOne);
LinearLayout llTwo = (LinearLayout) findViewById(R.id.llTwo);
llOne.addView(llTwo); //NullPointerException
you need to inflate the second layout, as setContentView only inflate your llOne
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View otherView = inflater.inflate(R.layout.yourSecondLayoutFileName, null);
and then
LinearLayout llTwo = (LinearLayout) otherView .findViewById(R.id.llTwo);
llOne.addView(llTwo);

Categories

Resources