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
Related
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.
Regarding this example -
http://android-coding.blogspot.com/2011/05/detect-multi-touch-event-test-on-custom.html
How can I add EditText to the view ?
I tried to add it to the layout but when I'm trying to update it I'm getting error message.
Thanks
I found a nice example here - http://www.kellbot.com/2009/06/android-hello-circle.
I added EditText to the layout and its working fine.
In order to use this example the code should be written as explained in Sebastien's comment, in which he uses parent as described in this line:
final Activity parent = this;
The MultiTouchView from the link extends a View. You cannot add other Views to a View. You will need the MultiTouchView to extend a ViewGroup. See the android doc on building custom components.
To be able to add views, I think replacing the following line in the sample code
public class MultiTouchView extends View {
with
public class MultiTouchView extends FrameLayout {
should work because FrameLayout is a subclass of ViewGroup. You may want to chose a different layout depending on your needs.
How to enbbed a GraphicsView into a layout? Or how do I add buttons and textViews to a GraphicsView? Could some one post a simple working example?
-Henry
I don't think you can add other Views inside a View component.
GraphicsView is not a default Android SDK class, so we can't know what you have there, sorry.
Make a Layout component to be able to add View components inside it.
To embed your GraphicsView in a layout in xml use something like:
<com.package.GraphicsView
id="#+id/graphics"
...
/>
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"
...
/>
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).