Confusion about Custom Views, especially in XML layout file

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

Confusion about Custom Views, especially in XML layout file

Postby illiniwatcher » Sat Nov 15, 2008 8:39 am

I'm confused about Custom Views. I have noticed that when I define an Android application this way:


----- Whatever.java -----

public class Whatever extends Activity {

private SecondView mySecondView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mySecondView = new SecondView(this.getApplicationContext());
}



----- SecondView.java -----

public class SecondView extends View {

public EditText txt1;

public SecondView(Context context) {
super(context);

// doing text assignment did nothing here.
txt1 = (EditText) findViewById(R.id.EditText01);

txt1.setWidth(200);

}

}


When I run this or anything like this, the SecondView doesn't do anything. Once I had an onDraw event defined and that did nothing either.

Instead of this line:

mySecondView = new SecondView(this.getApplicationContext());

I've tried this line:

mySecondView = (SecondView) findViewById(R.id.SecondView)

with "SecondView" being defined in the XML file with a tag like the following:

<my.package.SecondView ... ... />

but that seems to cause two problems:

1) The application hangs, causing a Force close situation
2) In the XML Outline editor, there's a Null reference exception where the layout normally appears.


My questions are:

Q1) What's the best way to have an Activity in one java file and a View in another file? Why would you want to? Is this a custom View situation?

Q2) Why is there no way to have the package-qualified Custom View tag in the XML Outline editor? (Currently, you have to type it in source.)

Q3) In which situations would you want to have multiple Custom Views?
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Re: Confusion about Custom Views, especially in XML layout f

Postby illiniwatcher » Sat Nov 15, 2008 9:52 am

I found an answer, right on this forum!

creating_custom_views_-_the_togglebutton-t310.html
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests