How to replace the exising layout of #include programmatically? - android

In my xml file I have include layout using #inlcude and wants to replace that layout using id programmatically.For example in my layout I have the following xml files
<include android:id="#+id/promo1" android:layout_width="fill_parent"
android:layout_height="fill_parent" layout="#layout/one_promo">
</include>
in java code I want like below code to replace the existing layout with new.
View v = findViewById(R.id.promo1);
v.setView(R.layout.new_promo);
Is there any facility to change like this after including with #include within xml file.

You should see at the inflate method. And there are lots of similar questions at stackoverflow, eg this one.

Related

Can we alter the Views present in the layout xml file for the Views present inside the include tag.

I am trying to re-use an already created layout xml file in another xml file by means of
My current XML file is as below:
<?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">
<include
android:id="#+id/insertTask"
layout="#layout/activity_edit_screen">
</include>
</RelativeLayout>
The layout file activity_edit_screen has a button named "Update", I want to change that button to "Add". That is the only change between the two xml files. How can I achevie this? I am trying to re-use one xml in another. Please provide suggestions. Thanks in advance.
Try to rename the button in code behind.
Button button=(Button)findViewByID(R.id.button1);
button.setText("Add");
Try to rename the button text in onCreate() after initializing view.
Button sample_btn=(Button)findViewById(R.id.sample_btn);
sample_btn.setText("Add")
;

How to reference a View in another XML without including it?

My code currently requires a reference to a ListView in another XML file and activity. I have tried using the include keyword as so:
<include layout="#layout/cleareditems"/>
However, when I do this it merges the two layouts and you can see everything in clearedItems. What would I do just to reference that View and change it?
Thanks!
here is an example
<PATH_TO_YOUR_CLASS.YOUR_CLASS
android:id="#+id/search"//we give some name to it
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and you should use merge tag, so the layout are not duplicated
<merge xmlns:android="http://schemas.android.com/apk/res/android">
//YOUR CUSTOM VIEW ELEMENTS, in this case its called search from above
</merge>

How to make the custom xml layout for this android project?

I am trying make application from this project: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
But here, there is a Layout that generating dynamicly. I want to create my own layout in xml file. So what should i have to do for it.
Please anyone can help me to make the xml layout from this dynamic layout ??
Thanks.
That example is not creating a "dynamic layout". The layout, which is the part you'd be defining in XML, consists of only one View object, MyView.
What I assume you are referring to by "dynamic layout" is the MyView class, which is a custom View object which accepts touch input and draws on the screen. This cannot be defined in XML... you must write the Java code to handle the logic necessary, since the regular View class (which MyView is extending) does not support such functionality.
What you would need to do is create a Java file defining the MyView class. Say for example, com.example.MyView. Then, in XML, you can include this custom view in your layout by referring to the full name, including the package name. For example...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<com.example.MyView>
android:layout_height="fill_parent"
android:layout_width="fill_parent"
</com.example.MyView>
</LinearLayout>
You can use this layout in an activity as usual using setContentView.

Android xml layout and adding a custom view written in xml

I've created a custom view XML layout resource MyView.xml. Can I refer to MyView by the XML layout file inside a second XML layout such as main.xml? If so, how?
(To be clear, I'm not looking for replies that explain how to inflate views from within my Java code, or that explain introducing Java classes in XML layout resources with <com.MyDomain.App.MyView ...> unless this helps here or to clarify that I have no other option.)
If your XML file is like /res/layout/MyView.xml you can refer to in main.xml with
<?xml version=”1.0” encoding=”utf-8”?>
...
<include layout="#layout/MyView.xml" />
...

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