Android xml layout and adding a custom view written in xml - android

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" />
...

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>

android double layout objects in compound view

When you create a compound view and inflate an xml layout file for it like this:
public class CompundLayout extends LinearLayout{...}
this inflates an xml with root like this:
<LinearLayout ... />
you end up with a layout hierarchy with a LinearLayout inside a LinearLayout (or so I concluded when defining a tag string to the layout object in the xml cased my app to crash).
Am I wrong? is there a better way to do this and prevent this double layout?
There is a better way to avoid the double layout, alter your xml layout to replace the LinearLayout container with a "merge" container. Your xml layout will look something like this afterwards:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView ... />
<EditText ... />
...
</merge>

How to replace the exising layout of #include programmatically?

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.

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.

Categories

Resources