Get XML layout inside another XML layout by code [Android] - android

I have one xml file main_layout.xml and in this I have another xml layout popup_layout.xml which is added by :
<include
android:id="#+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
layout="#layout/popup_layout" />
In the file MainActivity.java I would like to get the popup object so I can change it's textfields etc. What do i write?
I've tested
View v = findViewById(R.id.popup);
TextView tv= (TextView) v.findViewById(R.id.ip_answer);
but it doesn't work

The id attribute for include is not required. Add id attribute to the layout you want to include , then find view by that id an collect it accordingly as the type of layout
eg:
RelativeLayout/LinearLayout= findViewById(id);
then find the text view .
This should work

Related

Cannot find view by ID when overlaying resources?

I have a layout that contains a TextView that looks like this
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/visual_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor=?android:attr/colorAccent
android:maxLines="3"
android:gravity="center_vertical" />
I have specified an overlay directory and I'm attempting to overlay the layout file using a similar file except with some minor differences
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/visual_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor=?android:attr/colorPrimary
android:maxLines="3"
android:gravity="top" />
My application builds successfully, however, I'm seeing a NullPointerException during runtime when my Java code tries to set the text of the TextView
private void setVisualText(View someView) {
View view = getLayoutInflater().inflate(R.layout.myLayout, someView, false);
TextView myText = (TextView)view.findViewById(R.id.visual_text);
myText.setText(R.string.myString);
}
The exception happens only with the overlay file and the code runs fine without the overlay. Does anyone know why myText is null and why it's not finding the view correctly? My guess is it's related to the ID but I'm not sure why it's not resolving correctly.
So you have a TextView and an overlaying TextView?
If so, you should not use the same ids. Ids should be used only once.
Why are you calling?
getLayoutInflater().inflate(R.layout.myLayout, someView, false);
If you want to get a view from the main contentView, you can just use:
findViewById(id);
because if you want to attach the inflated view to the root you have to call:
getLayoutInflater().inflate(R.layout.myLayout, someView, true);
please try it with (..true).
by using the line above, you are getting the contentView view.

How do I avoid repetitive TextView code?

I want to include the below layout in a main layout file, at multiple points, but at each usage, I want to change ONLY the "android:text" attribute of the text view inside the relative layout (as seen below). How can I achieve that?
P.S. I know how to include it in the main layout. This includes the relative layout (as seen below), but the main purpose of creating another layout file is because the code (of the textview) is being repeatedly used in the main layout, and the only attribute that differs is "android:text" between these repeated text views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/order_id_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:fontFamily="sans-serif-light"
android:padding="20dp"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
in your another Layout file you can use this .
<include layout="#layout/main_layout"/>
And From your activity class you can set text by this.
TextView tv = (TextView) findViewById(R.id.order_id_label)
tv.setText("New Text");
This is the only way you can do this .
If all TextView element arguments are the same you could define this component in a separate file using <merge> </merge> directive and then <include layout="" />
Check here how to reuse
But if any of the TextView argument is changing, i.e. android:text attribute, the best way is to separate all other TextView attributes to custom style and reuse this custom style in different xml layout files
Check here how to use styles

How do i access a included layout in my code?

So I have included a layout in my xml file but I want to get that included layout in my code and then add to it.
You get Views using findViewByID()
But I don't think that can be used in this case. This is the part of my XML that I am referring
<include
android:layout_width="350dp"
android:layout_height="wrap_content"
layout="#layout/progress_bar"
android:layout_marginTop="20dp"
android:layout_below="#+id/instructions_label"
android:layout_centerHorizontal="true" />
You get views using 'findViewByID'
But i don't think that can be used in this case
Sure you can. Give your included layout an id
<include layout=#layout/someLayout
android:id="#+id/myId"
.../>
then retrieve it with that id
View myLayout = (View) findViewById(R.id.myId);
then you can use that reference to get sub views from someLayout.xml
Button btn = (Button) myLayout.findViewById(R.id.btn1)
assuming someLayout.xml has a Button with id of btn1
You can still add id to your included layout and get it with it's id. Or You can give ID to the parent element in that layout (inside it) and you can still call it with that id as well.
<include android:id=”#+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”#layout/title”/>

Android manifest xml file

After deleting an Android xml activity file, got problem in AndroidManifest.xml file.
What can be done to cure the issue?
In an activity created an EditText field and gave it a green background. And also gave Relative layout background a custom color, but nothing happened to the layout and the text field. So decided to delete the Relative layout(not main layout) to create a fresh one. And got the problem in Android manifest xml file.
Code:
layout seekbarlayout = (RelativeLayout) findViewById(R.id.layoutwithseekbar);
seekbarlayout.setBackgroundColor(Color.parseColor(xxxxxxxx));
text1 = (EditText) findViewById(R.id.textwithseekbar);
text1.setBackgroundColor(Color.parseColor(xxxxxxx));
Well, your RelativeLayout must had EditText in it, so when you removed RelativeLayout, the EditText should be removed with it.
Structure:
main_layout.xml
|--+ RelativeLayout (layoutwithseekbar)
|--> EditText (textwithseekbar)
try making new layout set id layoutwithseekbar on it and put EditText in it
<RelativeLayout android:id="#+id/layoutwithseekbar android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText android:id="#+id/textwithseekbar" android:layout_height="wrap_content"
android:layout_width="match_parent" />
</RelativeLayout>

Does LayoutInflater work with a layout xml file that uses includes?

Here's the rub: I have a layout that uses the include tag. The layout is simple ->
<LinearLayout>
<TextView ...>
<LinearLayout>
<include ...>
</LinearLayout>
</LinearLayout>
Now the included file is nothing more than:
<LinearLayout>
<TextView ... android:id="#+id/inner_text">
</LinearLayout>
Now, if I try to access the TextView inner_text in either the create or onStart call, it throws a NPE. I have tried using LayoutInflater to infate the included xml file and then access inner_text but to no avail - it always fails.
So, the question is : does LayoutInflater work with included xml files of the parent xml file? What I would like to do is grab the contents of the included xml file - and set the whole thing to be either visible or not visible based on preferences.
Nothing seems to allow me to grab the TextView object.
Now, when I include the xml as just a nested element in the parent file (not using the include tag) - then it accesses that LinearLayout just fine - but that defeats my purpose of trying to make the layout of the view dynamic - that is, I can change the contents of the child included layout at will - and not have to do any changes to the parent layout.
Any help, pointers, suggestions -> greatly appreciated.
As suggestion I think you can have in the main xml some Layout component left empty, them in runtime, inflate the main xml, then the included xml and add the content of the included in the space you left in the main.
But I think you example have to work, internally the Android uses the inflater to inflate resources. Can you edit and tell a bit more? Code examples can be helpfully.
well, very strange, when you inflate a view from any xml resource (if xml resources have or have not nested resources with include) the view must hold all the parsed xml. So, yes, LayoutInflater work with included xml files of the parent xml file.
i copied here the way i'm doing now...
my generic layout (a header with text, progress bar and image)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<ImageView
android:id="#+id/genericHeaderLogoPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/logo_pin"/>
<TextView
android:id="#+id/genericHeaderTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/genericHeaderLogoPin"
android:textSize="18dip"
android:textStyle="bold"
android:textColor="#FF0C9994"/>
<ProgressBar
android:id="#+id/genericHeaderProgressBar"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/genericHeaderLogoPin"
style="#android:style/Widget.ProgressBar.Inverse"
android:visibility="gone"/>
</RelativeLayout>
my parent layout... include the generic layout and a ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background_new_search_activity_1">
<include layout="#layout/generic_header_layout" android:id="#+id/listaBusquedasHeader"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
</LinearLayout>
my onCreate method that inflate the parent layout mentioned above:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.lista_busquedas_activity, null);
setContentView(view);
listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemClickListener(listItemClickListener);
((TextView)findViewById(R.id.genericHeaderTitle)).setText(R.string.messagesActivityTitle);
}
and that´s all, i hope this helps you.
Thanx for the help - I discovered what my problem was - I was trying to inflate the included file - not the parent container - so that's why it never found the elements. I wish the documentation would have mentioned that - logically I would think the parent xml file would load - but the included files may need inflating - guess not.
Works now thanks to your code examples you submitted.
Thanx.

Categories

Resources