I am trying to creating a custom view extends RelativeLayout.
To avoid adding view by coding. I prepare a xml with my custom relativelayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
...
</RelativeLayout>
So in my custom view class, I would inflate the xml by
View.inflate(context, R.layout.custom_view, this);
My question is as my class is already a RelativeLayout, if I doing so, I would have two levels of RelativeLayout. Of course, I can solve it by removing the outer RelativeLayout in xml. But if I am doing so, I cannot see preview in xml editor in eclipse which is the reason I want my custom view inflate from xml.
Even my custom view class extends FrameLayout, there would be one layer more in the view hierachy. How to solve this problem?
To reduce the unnecessary extra layer, you need to use merge tags. Here is a good example on how to use it.
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
<merge xmlns:android="http://schemas.android.com/apk/res/android">
...
</merge>
Related
I have a custom view called CustomImageView.
I would like to create an xml file with this view so I inflate the view and add it programmatically to my current layout. (depending on orientation , I will decide where to add it)
Is it possible to so something like this in the xml without a containing layout
<?xml version="1.0" encoding="utf-8"?>
<CustomImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="wrap_content"
android:height="wrap_content"
android:level="2"
android:anotherattr="adsad" />
After that I will inflate it. or should I put it in a Layout like LinearLayout? I just want an xml with my view so I can inflate it and add to my existing layout.. I am not sure what the containing layout would be!
This is perfectly legitimate. You can do it with built-in or custom views. Here is an example from the Android framework. It's an XML layout file containing a single TextView, representing a default ListView row layout. The TextView has several attributes assigned to it in XML, just as your custom view will.
Is it possible to so something like this in the xml
Yes you can inflate an XML that just contains one view. But why not just create the view programmatically? new CustomImageView(...)
depending on orientation , I will decide where to add it
Consider using the layout directories such as "layout-land" to automatically inflate different layouts based on orientation - that's what those are for. Read more about this Supporting Different Screens
I am not sure what the containing layout would be
Use the layout directories and it removes all doubt
I have a ViewGroup inside a layout inside my XML. It has children defined under it. In the ViewGroup, I can easily use getChildAt(..) to go through the children defined in XML.
activity_main.xml:
<Layout>
<ViewGroup>
<Child1> ...
</ViewGroup>
</Layout>
I want to replace that ViewGroup with a Compound Control that has a ViewGroup and some other controls. But I still need to be able to access its children.
I.e.
activity_main.xml:
<Layout>
<CompoundControl>
<Child1>...
</CompoundControl>
</Layout>
compound_control.xml:
<Layout>
<ViewGroup>
<Buttons or something>
</Layout>
But new if I call getChildAt(..) in the class for the compound control, of course it looks for children in compoun_control.xml, not children in activity_main.xml. Is there a way around this? Thank you.
You have to get the custom compound_control viewgroup and its children accessible in the activity class corresponding to the main_activity.xml. So you can give id to your compound control when replacing it with the viewgroup in main_activity . Later on if you want to access custom viewgroup you can get reference from the activity class. like
<Layout>
<CompoundControl id="xyz">
<Child1>...
</CompoundControl>
</Layout>
This id can be fetched in its activity class like.
CompoundControl a = (CompoundControl)findViewById(R.id.xyz);
a.getChildAt(..);
I am developing an Android 2.1 app.
I have defined a LinearLayout class:
public class MyTopBar extends LinearLayout {
...
}
Then, I have a layout xml file (content.xml):
<LinearLayout>
...
</LienarLayout>
I have a RootActivity.java , I would like to set MyTopBar as content in this RootActivity.
Then I have MyActivity which extends RootActivity:
public class MyActivity extends RootActivity{
//set xml layout as content here
}
I would like to set the content.xml as content of MyActivity.
As a whole, I would like to use the above way to achieve the layout that MyTopBar should be located on top of the screen always. The other Activities which extend RootActivity will have its content below MyTopBar. How to achieve this??
1 You could add your custom LinearLayout directly to the xml layout of the MyActivity class like this:
<LinearLayout>
<com.full.package.MyTopBar
attributes here like on any other xml views
/>
...
</LinearLayout>
or you could use the include tag to include the layout with the custom view:
<LinearLayout>
<include layout="#layout/xml_file_containing_mytopbar"
/>
...
</LinearLayout>
2 Use :
setContentView(R.layout.other_content);
Have a Layout vacant for the TopBar and add Your Topbar in it by using layout.addView(topbarObject);
Regarding your second question the setContentView can be called only once, as far as I know. You can however have those two xml files inflated using View.inflate(other_content.xml) and added in the parent xml layout whenever you need it. You can removeView() on parent layout and addView() with the new layout file.
Edit:
For the solution of both the question, you can have a parent Layout for eg. like the following:
//Omitting the obvious tags
//parent.xml
<RelativeLayout
android:id="#+id/parentLayout">
<RelativeLayout
android:id="#+id/topLayout">
</RelativeLayout>
<RelativeLayout
android:id="#+id/contentLayout">
</RelativeLayout>
</RelativeLayout>
Now in your code set the parent layout as content view,make an object of your TopBar layout and add it to the topLayout.
setContentView(R.layout.parent);
MyTopBar topBar=new MyTopBar(this);
RelativeLayout toplayout=(RelativeLayout)findViewByid(R.id.topLayout);
topLayout.addView(topBar); //or you can directly add it to the parentLayout, but it won't work for the first question. So better stick to it.
Now inflate the required xml layout. and add it to contentLayout.
RelativeLayout layout=(RelativeLayout)View.inflate(R.layout.content,null);
contentLayout.addView(layout);//Assuming you've done the findViewById on this.
and when you need to show the other content xml, just call the following code.
contentLayout.removeAllView();
RelativeLayout layout2=(RelativeLayout)View.inflate(R.layout.other_content,null);
contentLayout.addView(layout2);
I'm coming from the world of GWT and UIBinder, where I'm used to defining custom components by extending Composite, and then putting all the UI layout code in a MyComponent.ui.xml file. This is a very nice way to build up components from smaller pieces.
I'm trying to achieve the same effect on Android with custom Views. I've been able to programmatically extend View, and then add objects by calling addView(textView). I'd like to be able to do that in XML, but I don't see how to associate an xml layout file with the view (apart the primary res/layout/main.xml file, which provides the primary layout for the app.
How can I layout my custom views in XML?
Edit: My question was unclear. What I'm looking to do is associate a my_widget.xml file with my customized view. Then in my_widget.xml I'd like to define various TextViews, etc, and plug them into my View class.
Use the fully qualified name of your custom view class in place of one of the built-in views. Here's a layout, for instance, that fills the window with your class:
<?xml version="1.0" encoding="utf-8"?>
<my.package.MyCustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
To construct Views from XML, use LayoutInflater.
You can use findViewById (int id) with an id defined in XML. See the example in the android doc here (scroll down to IDs).
Is there a way to link inherit class to xml file.
I am trying to connect extended class to widget in the xml file.
Is it possible ?
Thanks in advance.
You must have noticed that all the nodes that we specify in the layout XMLs are actually either View classes(for e.g: TextView, EditView) or view containers/layout managers(e.g: LinearLayout, RelativeLayout etc.). Android allows you to create custom views and containers by extending the View class and one of the layout managers, respectively. You can then choose to inflate such views directly from code or specify them as nodes in your layout XMLs.
For instance, assuming you create a View class such as:
public class com.views.MyView extends View{}
then you can include this class directly in you layout XML by saying:
<LinearLayout ..>
<com.views.MyView .. />
</LinearLayout>
Note that when you specify your View class directly in XML there are a few important subtleties to consider such as:
When inflating the custom view, the framework will call different constructor of the view. The arguments would be a context object and AttributeSet(containing attributes you set in the XML).
For more details refer this