Invoking widget's onDraw() from custom view's onDraw()

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

Invoking widget's onDraw() from custom view's onDraw()

Postby gsk123 » Fri Oct 03, 2008 11:16 am

Hello,

I want to draw the android widgets like edittext, progressbar or a textview in onDraw() method.

My Test activity sets a view by setContentView(xxx). The xxx class extends View class and it has a onDraw(Canvas canvas) method. So basically when i set, setContentView(xxx) this xxx-->onDraw() method should be invoked. Which is rightly happened and all i did in the onDraw() method is shown below.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ProgressBar progress;
  2.  
  3.  
  4.  
  5. protected void onDraw(Canvas canvas)
  6.  
  7. {
  8.  
  9.     int scrW  = getWidth();
  10.  
  11.     int scrH = getHeight();
  12.  
  13.     if(progress == null)
  14.  
  15.     {
  16.  
  17.          progress = new ProgressBar(test, null, android.R.attr.progressBarStyleHorizontal);
  18.  
  19.          progress.setLayoutParams(new AbsoluteLayout.LayoutParams(scrW, scrH, 0, 0));
  20.  
  21.          progress.setProgress(50);
  22.  
  23.          progress.setVisibility(View.VISIBLE);
  24.  
  25.     }
  26.  
  27.  
  28.  
  29.    //not only for progessbar also this case is applicable to edittext, textview and all type of widgets as well
  30.  
  31.     progress.draw(canvas);
  32.  
  33. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


What i expect to see is this above progress.draw(canvas) should invoke the ProgressBar.onDraw(canvas) method which they will be using to draw the progressbar. But it does not work as expected.

Can anyone help me to resolve this issue?

Thanks!
gsk123
Freshman
Freshman
 
Posts: 4
Joined: Tue May 13, 2008 5:12 am

Top

Re: Invoking widget's onDraw() from custom view's onDraw()

Postby ninor » Fri Oct 03, 2008 12:23 pm

gsk123 wrote:Hello,

I want to draw the android widgets like edittext, progressbar or a textview in onDraw() method.

My Test activity sets a view by setContentView(xxx). The xxx class extends View class and it has a onDraw(Canvas canvas) method. So basically when i set, setContentView(xxx) this xxx-->onDraw() method should be invoked. Which is rightly happened and all i did in the onDraw() method is shown below.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ProgressBar progress;
  2.  
  3. protected void onDraw(Canvas canvas)
  4. {
  5.     int scrW  = getWidth();
  6.     int scrH = getHeight();
  7.     if(progress == null)
  8.     {
  9.          progress = new ProgressBar(test, null, android.R.attr.progressBarStyleHorizontal);
  10.          progress.setLayoutParams(new AbsoluteLayout.LayoutParams(scrW, scrH, 0, 0));
  11.          progress.setProgress(50);
  12.          progress.setVisibility(View.VISIBLE);
  13.     }
  14.  
  15.    //not only for progessbar also this case is applicable to edittext, textview and all type of widgets as well
  16.     progress.draw(canvas);
  17. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


What i expect to see is this above progress.draw(canvas) should invoke the ProgressBar.onDraw(canvas) method which they will be using to draw the progressbar. But it does not work as expected.

Can anyone help me to resolve this issue?

Thanks!


You should call progress.invalidate(), or YourView.invalidate() at the end.
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby gsk123 » Fri Oct 03, 2008 12:34 pm

I tried to call progress.invalidate() and also invalidate from My Custom view (xxx) at the end of the onDraw() but it doesn't work.


Thanks for your help. Any other solutions?
gsk123
Freshman
Freshman
 
Posts: 4
Joined: Tue May 13, 2008 5:12 am

Postby ninor » Fri Oct 03, 2008 3:30 pm

Not right now... it should work with invalidate()...
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby irobot » Fri Oct 03, 2008 3:44 pm

invalidate() will force the widget/view to draw again. Right? but in this case we directly call the widget's onDraw() method by calling widget.draw(canvas) [progress.draw(Canvas)].

Atleast it should have painted once you know? Any help?
If at first you don't succeed, look in the trash for the instructions.
irobot
Freshman
Freshman
 
Posts: 5
Joined: Thu Mar 27, 2008 8:32 am
Location: Coimbatore, India

Postby gsk123 » Fri Oct 03, 2008 3:54 pm

Hi can you please try this.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestActivity extends Activity
  2.  
  3. {
  4.  
  5.         public void onCreate(Bundle savedInstanceState)
  6.  
  7.         {
  8.  
  9.                 super.onCreate(savedInstanceState);
  10.  
  11.                 setContentView(new TestView(this))             
  12.  
  13.         }
  14.  
  15. }
  16.  
  17.  
  18.  
  19. public class TestView extends View
  20.  
  21. {
  22.  
  23.         private TextView tv;
  24.  
  25.  
  26.  
  27.         TestView(Context context)
  28.  
  29.         {
  30.  
  31.                 super(context);
  32.  
  33.                 tv = new TextView(context);
  34.  
  35.                 tv.setText("Welcome to Android");
  36.  
  37.         }
  38.  
  39.  
  40.  
  41.         public void onDraw(Canvas canvas)
  42.  
  43.         {
  44.  
  45.                 super.onDraw(canvas);
  46.  
  47.                 //tv.draw(canvas);
  48.  
  49.                 tv.invalidate();
  50.  
  51.         }
  52.  
  53. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


I tried with tv.invalidate() also tv.draw(canvas), the TextView tv which has the text "Welcome to Android" is not shown when we run this code.

Thanks!
gsk123
Freshman
Freshman
 
Posts: 4
Joined: Tue May 13, 2008 5:12 am

Top

Postby Niketa » Mon Nov 24, 2008 9:36 am

Hi,
have u solved this problem?
if yes then please tell me. i also need to do this.

Thanks.
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Postby www.rzr.online.fr » Fri Apr 24, 2009 2:04 am

gsk123 wrote:
public class TestView extends View
{
private TextView tv;

public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
//tv.draw(canvas);
tv.invalidate();


what makes you think the TextView is inside the TestView ? how to group view ? layout ?
User avatar
www.rzr.online.fr
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 03, 2009 11:39 am
Location: fr/35/rennes

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests