How to make canvas write text

zxxdaro

Member
Licensed User
Longtime User
I am trying to make some text appear on the screen.
This is the code. My secondScreen appears but the code after it does not.
The bits of code i am using here come from the various bits of documentation available. Can someone please help a complete newbie.
How do I make a piece of text, which will eventually come from a variable, on the screen?

Sub Show_Click
secondScreen.Visible=True
Dim Canvas1 As Canvas
Canvas1.Initialize(Activity) 'this canvas will draw on the activity background
Canvas1.DrawText("This is a nice sentence.", 200dip, 200dip, Typeface.DEFAULT_BOLD, 30, Colors.Blue, "LEFT")

End Sub
 
Last edited:

klaus

Expert
Licensed User
Longtime User
What is secondScreen ?
Perhaps it is covering the text you draw onto the avtivity.
Or try to add Activity.Invalidate after the DrawText line.
You could have a look at chapter 14 Graphics / Drawing in the Beginner's Guide.

Best regards.
 
Upvote 0

zxxdaro

Member
Licensed User
Longtime User
Thanks both of you. I have tried your suggestions, but they don't solve the problem. I had already read that chapter and I commented out the display of the second screen in case that was causing the problem. It is commented out in the code below.

I realise that I am probably making lots of mistakes, but If I can get this stuff working I think that my perspective as a complete beginner may be useful at some point and I will be able to contribute to the wiki.

You might as well have more of the code. This is all of it.

What it is aimed at doing is testing me on a learning process.
I have a file with questions and answers on a single line separated by a comma and there are two screens. I am doing nothing with my file yet but just testing the displays. I have a screen where nothing is diplayed at the moment as the first screen. This will hold the question and it has a button on it to show the second screen which has the answer. At the moment of course I am just trying to get an example of text printed on the screen which I will then replace with something extracted from my strings of questions and answers.


'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim firstScreen, secondScreen As Panel
Dim mainList As List
mainList = File.ReadList(File.DirAssets, "testandroid.txt")
Dim vCounter As Int
Dim holdString As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("firstdisplay")
secondScreen.Initialize("")

secondScreen.LoadLayout("knownscreen")
Activity.AddView(secondScreen,0,0,100%x,100%y)
secondScreen.Visible =False
End Sub
Sub Show_Click
'secondScreen.Visible=True
Dim Canvas1 As Canvas
Canvas1.Initialize(Activity) 'this canvas will draw on the activity background
Canvas1.DrawText("This is a nice sentence.", 200dip, 200dip, Typeface.DEFAULT_BOLD, 30, Colors.Blue, "LEFT")
Activity.Invalidate

End Sub
 
Upvote 0

Jim

Member
Licensed User
Longtime User
At a quick look over, I noticed theres no dim for Show. I'm assuming its a button or something or other. But perhaps thats not the problem because you were saying the code in Show_click was still being executed (panel became visible)
 
Upvote 0

zxxdaro

Member
Licensed User
Longtime User
The most extraodrinary thing has happened. I carried on experimenting and messed up the project completely so I reconstructed it from the available information. It now behaves differently. I can by commenting out the line :-
secondScreen.Visible=True
with an apostrophe get the text to appear on the first screen which it did not before but putting it back in does not result in it appearing on the second screen. Most odd. The reconstructed project is attached.
 

Attachments

  • forforum.zip
    7.1 KB · Views: 336
Upvote 0

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do, could you explain it more in details.

The problem in your code is that you draw the text on the activity and when you set secondScreen.Visible=True, the secondScreen panel covers everything in the activity.

Depending on what you want to do there are probably other possibilities to do it.

Best regards.
 
Upvote 0

zxxdaro

Member
Licensed User
Longtime User
Think of it as a learning exercise.
The first screen shows you a question. You think "Do I know the answer?".
You then press the button on the first screen which activates the second which has the answer on it.

That is what I am working on at the moment.
when I have that working the second screen will have "Right" and "Wrong" buttons on it that will let me record the answers.


What I have at the moment IS NO TEXT on the first screen , although I have worked out how to do that and NO TEXT on the second.

I am just at the point where I am using some dummy text to find out how to get text to display before I start using the questions and answers.

If there are better says to do it please let me know.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Instead of drawing the text onto the activity I'd suggest you to work with label to display the texts.
One on top for the question and a second one below with the answer.
The answer label with the two buttons should be on a small panel you can set to visible or hidden.
Changing the text in the labels is much easier than drawing it onto the activity.

Best regards.
 
Upvote 0
Top