adding a custom view to main layout

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

adding a custom view to main layout

Postby YoFrankie » Fri Aug 14, 2009 2:50 pm

Having created an own view extending textview for example - how can i add it to the layout defined in the main.xml?
YoFrankie
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Aug 03, 2009 4:52 pm

Top

Postby mangaluve » Fri Aug 14, 2009 9:33 pm

well you type something like

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <ViewName android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </ViewName>
  3.  
  4.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

where ViewName is the name of your custom view, _with the package name included_. For instance,
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <com.myapp.customviews.MyView android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </com.myapp.customviews.MyView>
  3.  
  4.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby qlimax » Tue Sep 15, 2009 8:45 pm

Starting from the same problem... and thanking mangaluve, I would say:

in addiction, your CustomView's constructor must be

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public CustomView(Context context, AttributeSet attrs){
  2.  
  3. ...
  4.  
  5. }
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


If that's was not obiouvs - for inner class the xml declaration is (example)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <com.myapp.MyActivity$InnerCustomView android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </com.myapp.MyActivity$InnerCustomView>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

How do I reference an Inner class?

Postby IcedDante » Wed Jan 20, 2010 9:00 am

If that's was not obiouvs - for inner class the xml declaration is (example)

XML:

<com.myapp.MyActivity$InnerCustomView android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </com.myapp.MyActivity$InnerCustomView>


Is that so? Well, it's not working for me. When I try to reference my InnerClass with a '$' character...

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.grafightscratch.ochemmer.MoleculeTablet$MoleculeTabletView
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/molecule_tablet_view"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. />
  7.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


I get an error.

Code: Select all
   - ERROR Error parsing XML: unbound prefix
   - Element type "com.grafightscratch.ochemmer.MoleculeTablet" must be followed by either
    attribute specifications, ">" or "/>".


This may be valid syntax, but Eclipse sure don't think so, and I can neither build my application nor deploy it to my emulator for testing. It makes me sad. :(

So how do I reference an inner class?
IcedDante
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jan 20, 2010 8:45 am
Location: San Diego

Postby qlimax » Wed Jan 20, 2010 3:18 pm

qlimax wrote:Starting from the same problem... and thanking mangaluve, I would say:

in addiction, your CustomView's constructor must be

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public CustomView(Context context, AttributeSet attrs){
  2. ...
  3. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


If that's was not obiouvs - for inner class the xml declaration is (example)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.myapp.MyActivity$InnerCustomView android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </com.myapp.MyActivity$InnerCustomView>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4



sorry, I was wrong, that was a pure speculation. and I didn t try that code ..sry

try this mode instead:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <View  class="com.abc.myclass$innerclass"   android:id="@+id/view1"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  
  2.  
  3. ...
  4. />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


let me know :> bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby IcedDante » Wed Jan 20, 2010 10:57 pm

Actually, I still get problems with your fix. Does that work for you? When I try it, it seems that Android just creates the Layout as a vanilla View class. Running this code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TestView mView = (TestView)findViewById(R.id.test_view);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Gives me an ClassCastException because the XML:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <View
  8.         class="com.grafightscratch.tst.ViewTest$TestView"  
  9.         android:id="@+id/test_view"
  10.    android:layout_width="fill_parent"
  11.    android:layout_height="wrap_content"
  12.    android:text="@string/hello"
  13.    />
  14. </FrameLayout>
  15.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


doesn't look at the class attribute. As you have it posted, class isn't part of the android namespace package, which I think is correct, but it does not seem to be considered at all here.

**EDIT**
It's not a ClassCastException, it's a NoSuchMethodException as the code is unable to find my TestView(Context, AttributeSet) constructor. Why? Not sure, but I'm looking into it now and will post an update soon.
IcedDante
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jan 20, 2010 8:45 am
Location: San Diego

Top

Postby IcedDante » Wed Jan 20, 2010 11:21 pm

OK, two things fixed this problem:

1) The "View" tag needs to be all lowercase as: "view"
Important because XML is case sensitive and both tags exist in the schema. The first will generate a View class, the second will be parsed as a view tag and will examine the class attribute to find out which type of View should be created:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <view
  2.         class="com.grafightscratch.tst.ViewTester$TestView"  
  3.         android:id="@+id/test_view"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="wrap_content"
  6.    android:text="@string/hello"
  7.    />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


2) The Inner class must be (?) static
By making the Inner class static the view worked. Unfortunately, that meant some heavy changes.

I made this view an Inner view because I wanted to allow it to access variable and parameters in the Activity class that houses it. As a result, I had to make all methods and fields in my Activity that the view accesses static as well. Is it me, or is that really bad design? Is there a way to do these Inner views without making everything static, or should I just forget all this and make my view a separate class?

btw- Here is the url that someone pointed me to that gave me a lot of this information.
IcedDante
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jan 20, 2010 8:45 am
Location: San Diego

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests