Add a view without xml

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

Add a view without xml

Postby sudheer1437 » Mon Oct 05, 2009 12:08 pm

I need a little help from people with expirience. I hope it's easy. I just want to show new View (creating it without XML layouts) above main program's view :

I wrote such code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. DummyView dView = new DummyView(context);
  3.  
  4. dview.layout(10, 10, 100, 100);
  5.  
  6. dview.setVisibility(View.VISIBLE);
  7.  
  8. dview.bringToFront();
  9.  
  10. dview.requestFocus();
  11.  
  12. dview.invalidate();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


As you see - nothing helped, no window visible
What do I make incorrectly? Thanks!
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Top

Postby Sowmya » Mon Oct 05, 2009 1:12 pm

Did you call setContentView(dview)?
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Postby sudheer1437 » Mon Oct 05, 2009 2:21 pm

Thanks for ur reply...
Since i m writing the above code in a normal class... means to say.. my class doesnt extend Activity.

So, i cant use setContentView();
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby blackgin » Mon Oct 05, 2009 7:37 pm

I think if you want to show a gui you should do it from an activity..
Maybe you want to create the gui into another class, but if you do so, you need to call the non-activity class from an activity and get the view..
User avatar
blackgin
Developer
Developer
 
Posts: 33
Joined: Tue Apr 21, 2009 1:21 pm

Postby tatyana.maximovskaya » Mon Oct 05, 2009 7:42 pm

I suppose views cannot be added and displayed inside a class that doesn't exteds the Activity. Maybe what you need is creating SubActivity with smaller than screen size and show it over the parent activity. http://developerlife.com/tutorials/?p=302
tatyana.maximovskaya
Freshman
Freshman
 
Posts: 2
Joined: Sun Oct 04, 2009 9:12 pm

Postby azraeal » Wed Oct 07, 2009 10:21 pm

You would need to pass a your parent view/viewgroup of which you're attaching your custom view down to your new DummyView(context) call. And, you're not attaching your DummyView to a parent with a parent.addView().

There might be another issue because you can't manipulate UI outside of the UI thread, so your actual parent.addView call must be appended as a runnable to the UI thread.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Top

Postby sudheer1437 » Thu Oct 08, 2009 6:21 am

azraeal wrote:You would need to pass a your parent view/viewgroup of which you're attaching your custom view down to your new DummyView(context) call. And, you're not attaching your DummyView to a parent with a parent.addView().

There might be another issue because you can't manipulate UI outside of the UI thread, so your actual parent.addView call must be appended as a runnable to the UI thread.



Thanks for the reply....
Can u explain me clearly...
With a piece of example please....
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby azraeal » Thu Oct 08, 2009 7:05 am

Which part, the first or the second?

First part's self explanatory. So you've created DummyView. But that only exists in memory. You need to attach that to your UI. You can use setContentView, which replaces the entire UI with that one view, but that's limited to the activity. Or, you can call on an existing, on screen UI element(layout), and add this DummyView as a child view.

In your activity, you probably have:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.something);
  2.  
  3. ViewGroup parent = findViewById(R.id.parentView); //all layouts extend viewgroup
Parsed in 0.069 seconds, using GeSHi 1.0.8.4

Then, in your logic:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. DummyView dv = new DummyView(context);
  2.  
  3. ...
  4.  
  5. parent.addView(dv);
  6.  
  7.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Second part: If, when you defined DummyView, is in another thread, you've need to attach the logic from the previous part to the UI thread. You can do that with message handlers or, with the same parent view(group), you can call postRunnable().
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parent.postRunnable(new Runnable(){
  2.  
  3.     public void run(){
  4.  
  5.         DummyView dv...
  6.  
  7.         ...
  8.  
  9.         parent.addView(dv);
  10.  
  11.     }
  12.  
  13. });
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

Something to that effect.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby sudheer1437 » Thu Oct 08, 2009 8:27 am

Really heartful Thanks for the reply..

Let me explain my need...

I wanna create jar package, and give it to user.
The user just calls only one of my methods.

in that method he is passing his Activity's context object as parameter.


Now, i have to use dis context object and create a view which consists of a some data which i am parsing from a remote xml and dynamically add this to View and show dis to user.

this is my requirement.

shall i need to take any more inputs from the user so that i can make this happen.

plz help me in this matter....

Thanks in advance...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby azraeal » Thu Oct 08, 2009 5:46 pm

I'll repeat this for the third time, and hopefully you'll understand.

If you want to add a view, you'd need some reference to a parent view. Since you're only passing in context, you have no control over the UI. So, to answer what really should have been a rhetorical question: Yes, you need to pass in something else. And that something else should be a ViewGroup, so the caller can pass in any layout or custom ViewGroup of their choosing (because all layouts extend ViewGroup). Then you simply add your view to this ViewGroup (which I've been calling parent).
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby sudheer1437 » Fri Oct 09, 2009 5:14 am

Thanks for the reply....

I already solved the problem...
Thanks a lot once again...

Now one more doubt...
I am unable call the Activity which i have written in my jar file...
it is saying.. package must be declared...

so wad to do...???
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby azraeal » Fri Oct 09, 2009 5:57 pm

...


Add the <activity> declarations in your manifest.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby sudheer1437 » Sat Oct 10, 2009 6:09 am

ya.. i did... but the problem is ... my activity class which I wanna call is in different package than the the activity i am presently using.. manifest file is allowing me to add only one package...

so how can i add the activity which is in another package...

Thanks in advance...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests