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(..);
Related
I came across this TextView class method() in some code for taking a screenshot. What does it do, what's its purpose?
textView.getRootView()
Actually, what is a root view?
It is a method of the base class View
It basically lets you find the topmost view in the current view hierarchy.
e.g
<LinearLayout>
<TextView/>
<RelativeLayout>
<...something...>
</RelativeLayout>
</LinearLayout>
In the above example, the root view would be the top most declared view ie the linear layout
First, read the documentation for getRootView().
Then the next question is, what is a View Hierarchy? Read that here.
So to be succinct, a root view is the top-most view in your current View Hierarchy that is connected to your object you are calling getRootView() from.
Looking at the above tree map, or a binary tree, it would be the root, or top-most node.
I notice that there is a tag <view>(not <View>) which can be used in Android layout xml. When I add such a tag in a layout xml, the app can be successfully compiled but crashes when running. So what is this <view> used for?
view is an alternative way for specifying a view to inflate. You need to combine it with class attribute to specify the class name.
This is "useful" if your view class is an inner class and you cannot use something with a $ in the tag name in XML. (Having inner classes as Views you inflate is not a very good idea though.)
For example:
<view class="your.app.full.package.Something$InnerClass" ... />
Reference: http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/view/LayoutInflater.java#696
View is widget of android that is use to add any kind of view.Like in problems when we use scrollView in a activity and at bottom of activity there is buttons ,That time we add view to looks better.We can set any kind of color to View .
for exp
This will make a View of 20dp height.
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>
In an Android application, I have created a custom view extending the View class. I want to declare an object of my class in the xml layout file but, even after many tries, the application unexpectedly stops when the setContentView call is executed (that's what the popup windows which appears says).
In my view class, which is declared in my MainActivity file as public, I have two constructors : one with only the Context as parameter and one with a Context and an AttributeSet parameters. And I have overridden the onDraw function.
This class is in my source package, named org.me.myapp.
In the layout file, I declare the object I want to insert like this :
<org.me.myApp.MainActivity.myView"
android:id="#+id/View"
android:layout_below="#id/toto"
android:layout_width="300dip"
android:layout_height="100dip"/>
Can anyone tell me what is wrong?
Thanks in advance for the time you will spend trying to help me.
What Yar said, you should extend View or ViewGroup to create custom layout element, and use that class name in your layout xml. Not member variable what it looks you're trying to do. After inflating your layout you can access your custom view with;
org.me.myapp.MyView myView = (org.me.myapp.MyView) findViewById(R.id.MYVIEWID);
I think you need to declare your custom layout as a separate class, for example: org.me.myapp.MyView and relate to it in your xml file like this:
<org.me.myapp.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
...
/>
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