XML attributes not read

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

XML attributes not read

Postby xplosive » Tue Oct 14, 2008 6:08 pm

Hi,

It seems that my attributes are not being read. I have the following main.xml:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/layMain"
    >
   
    <android.happer.Mens
       android:id="@+id/myView"
       android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="#000000FF"
          android:visibility="invisible"
       />
       
   
</LinearLayout>



It goes wrong at the point where i want to give my custum View a backgground color. The visibilty doesn't do his work either.

But, when i put the following lines in my Mens class (which extends the View class):

Code: Select all
setBackgroundColor(Color.BLUE );
// setVisibility(INVISIBLE);


It works. Thus, when i invoke the methods in my class file it works fina, but when use the lines in the xml file, it doesn't work. How is that?

Thnx in advance :)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Top

Postby MrSnowflake » Tue Oct 14, 2008 6:15 pm

Probably because you override onDraw().
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Tue Oct 14, 2008 6:21 pm

Ain't that strange? I mean, when i omit onDraw() how can i put a drawing on the screen? In this case i draw a simple line, but this should be a player. So if i omit onDraw() how do i solve this?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Tue Oct 14, 2008 6:30 pm

you can call super.onDraw()
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Tue Oct 14, 2008 6:36 pm

So, on my Mens class i call super.onDraw(); (which point at the View class right?)

But if i do that, where do i draw my object?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Tue Oct 14, 2008 6:38 pm

in onDraw() of course
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby xplosive » Tue Oct 14, 2008 6:40 pm

I don't get it.

This is my entire class:

Code: Select all
package android.happer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.View;

public class Mens extends View {
   
      public Mens(Context context, AttributeSet attrs)
      {
         super(context);
      }
   
   protected void onDraw(Canvas canvas)
    {
      Paint p = new Paint();
      p.setColor(Color.RED);
      
      canvas.drawLine(15, 15, 25, 5, p);
      
      //setBackgroundColor(Color.BLUE );
      // setVisibility(INVISIBLE);
      
    }
}



What do i have to do, to make the arttributes in the XML file work?(background and visibility)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Tue Oct 14, 2008 6:43 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    protected void onDraw(Canvas canvas)
  2.  
  3.     {
  4.  
  5.       super.onDraw(canvas);
  6.  
  7.       Paint p = new Paint();
  8.  
  9.       p.setColor(Color.RED);
  10.  
  11.        
  12.  
  13.       canvas.drawLine(15, 15, 25, 5, p);
  14.  
  15.        
  16.  
  17.       //setBackgroundColor(Color.BLUE );
  18.  
  19.       // setVisibility(INVISIBLE);
  20.  
  21.        
  22.  
  23.     }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Tue Oct 14, 2008 6:46 pm

It didn't work. My background is still default and i can still see my line.

main.xml:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/layMain"
    >
   
    <android.happer.Mens
       android:id="@+id/myView"
       android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="#FF000000"
          android:visibility="invisible"
       />
       
   
</LinearLayout>

xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Tue Oct 14, 2008 6:47 pm

Hmm, k, then I don't know, sorry.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Tue Oct 14, 2008 6:48 pm

Okee, thnx anyway :)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests