Randomizing popups

69 views
Skip to first unread message

Isaac Franks

unread,
Jun 28, 2018, 5:35:46 PM6/28/18
to DroidScript
How am I able to randomize a series of popups in a javascript program? I am going to have nearly 100 quotes that I want to random so that when I press a button it will display one of those quotes random. I am still super new to JS and am only getting somewhere with html thus far to give you an idea how new I am. I have been googling and arrays might be the way to go, but that is a bit confusing to me at this point. Can someone tell me how to randomize my quotes and be able to have them displayed as a popup (that lasts long enough to read or a display message that lasts so you can read it) when the button is pressed? Thanks!

Steve Garman

unread,
Jun 29, 2018, 3:03:25 AM6/29/18
to DroidScript
Ideally you would have the quotes in a text file in a JSON format. 

I have concentrated on choosing a quote in this sample and taken no interest in the app's appearence.

If you want to auto-dismiss the quote after a fixed time, you would need to use a custom dialog but this is a quick and dirty demo.

I've attached an spk file which should open DroidScriprt and install the app which will just run. I'll paste the code below but ir won't run without the file.

var quotes = [ ];
var quotesFile = app.GetAppPath()+"/quotes.json";


function OnStart()
{
 
//Create a layout with objects vertically centered.
 lay
= app.CreateLayout( "linear", "VCenter,FillXY" );


 
//Create a text label and add it to layout.
 txt
= app.CreateText( "Hello" );
 txt
.SetTextSize( 32 );
 lay
.AddChild( txt );
 
 
//create a button
 qotd
= app.CreateButton("Qotd");
 qotd
.SetOnTouch(qotd_OnTouch);
 lay
.AddChild(qotd, "Quote of the Day");
 
 
//Add layout to app.
 app
.AddLayout( lay );
 
 
var temp = app.ReadFile(quotesFile);
 quotes
= JSON.parse(temp)


}


function qotd_OnTouch()
{
   
var len = quotes.length;  
   
var pos = Math.floor(Math.random()*len);
    app
.Alert(quotes[pos])
}





QOTD.spk

Steve Garman

unread,
Jun 29, 2018, 4:55:52 AM6/29/18
to DroidScript
Heres another version with a simple custom dialog that displays the quotes for the number of seconds you choose.

It will still work with a JSON file quotes.jsn but I've given it three strings to choose from if it doesn't find the file to help with testing 

var quotes = ["This app has no quotes","Please add a quotes file","When you add a quotes file it should be in JSON format" ];
quotesFile
= "quotes.json";


function OnStart()
{
 
//Create a layout with objects vertically centered.
 lay
= app.CreateLayout( "linear", "VCenter,FillXY" );

 lay
.SetBackColor("#ddffdd")



 
//Create a text label and add it to layout.

 txt
= app.CreateText( "Please press button for quote" );
 txt
.SetTextSize( 18 );
 txt
.SetTextColor( "#224422" )

 lay
.AddChild( txt );
 
 
//create a button
 qotd
= app.CreateButton("Qotd");
 qotd
.SetOnTouch(qotd_OnTouch);

 qotd
.SetBackColor("#224422")
 qotd
.SetTextSize(24)

 lay
.AddChild(qotd, "Quote of the Day");
 
 
//Add layout to app.
 app
.AddLayout( lay );

 
 
if(app.FileExists(quotesFile))
 quotes
= JSON.parse(app.ReadFile(quotesFile))
 
 
// create a popup with size 24 text, background and text colors
 tempDisplay
= createPopup(24,"#446644", "#ddffdd" );

}


function qotd_OnTouch()
{
   
var len = quotes.length;  
   
var pos = Math.floor(Math.random()*len);

   
   
// display for at least 5 seconds
    tempDisplay
.Popup(quotes[pos], 5)
}


function createPopup(textSize, backColor, textColor)
{
   
var dlg = app.CreateDialog( "","noTitle,noDim,noCancel" );  
   
var lay = app.CreateLayout( "linear", "vertical,fillxy,left" );
    lay
.SetPadding( 0.02, 0, 0.02, 0.02 );
   
if (backColor) lay.SetBackColor( backColor )
    dlg
.AddLayout( lay );
   
var txt = app.CreateText( "",-1,-1,"multiLine");;
   
if(textSize) txt.SetTextSize( textSize );
   
if(textColor) txt.SetTextColor(textColor)
    lay
.AddChild( txt );
    dlg
.Popup = function(text, seconds)
   
{
        txt
.SetText( text );
        seconds
= seconds || 1;
        dlg
.Show();
        setTimeout
(dlg.Dismiss, seconds*1000);
   
}
   
return dlg;
}

Isaac Franks

unread,
Jul 2, 2018, 2:27:35 PM7/2/18
to DroidScript
Thank you so much! I used the second one and now I can build off of it. Is there any way to make separate buttons so that I could separate quotes by the person who said them? Seriously, thanks a ton!

alex.symbroson

unread,
Jul 2, 2018, 4:21:06 PM7/2/18
to DroidScript
You can give each author an id starting from zero and make a two-dimensional list of quotes. You can save the author names in a separate list on their id-position if you need them.

Steve Garman

unread,
Jul 2, 2018, 5:09:36 PM7/2/18
to DroidScript
Isaac,
I was playing with enhancing this the other day.

This doesn't really answer your question because I don't really undetstand your question.

However, this version uses a JSON database I found in github and shows one way to deal with storing authors along with quotes.

qotd.spk

Isaac Franks

unread,
Sep 20, 2018, 3:16:06 PM9/20/18
to DroidScript
I don't know if I should be posting in another thread (let me know if I should), but can you apply this same set of arrays (using the JSON database) be applied to texttospeech? What I am asking is whether when you press your button for the quote you could have it speak to you instead of being displayed on screen. I have looked at the two examples on droidscript and feel like I might just be missing the right callback method?

Steve Garman

unread,
Sep 20, 2018, 3:39:46 PM9/20/18
to DroidScript
I suggest you find the line
txt.SetText( text );
and add the line
app.TextToSpeech( text );
after it.

Once you have worked out what that does, you may find there is a great chunk of code you can you can dispense with.

Isaac Franks

unread,
Sep 25, 2018, 4:07:54 PM9/25/18
to DroidScript
And that change relates specifically to the older array we had talked about?

Isaac Franks

unread,
Sep 25, 2018, 10:50:58 PM9/25/18
to DroidScript
Sorry, I mean there is no text that I set, I just used the example you had shown with the quotes stored in Json files. Do I set it under the button that normally displays the Popup?

Steve Garman

unread,
Sep 26, 2018, 1:09:23 AM9/26/18
to DroidScript
I don't know Isaac.

Believe it or not, I don't keep every line of code I write as answers for for other people.

Have you tried it?

Isaac Franks

unread,
Sep 26, 2018, 3:01:59 PM9/26/18
to DroidScript
lol No, of course. I added your original spk for context. Like I said, I am trying to make it so that when I press the button you get the quote spoken to you instead of having it displayed as a popup.

I tried your suggestion, yes, but I am trying to link it from stored quotes, not have it say just one statement. I was not sure how to link it and tried a bunch of methods that seem illogical now. I just need to understand what you would link it with.

Steve Garman

unread,
Sep 27, 2018, 12:40:29 AM9/27/18
to DroidScript
I don't know what you mean by "I added your original spk for context"

The idea of using other people's code to help with writing your app is that you study it to learn how it works.

It can't be that difficult to find the code that displays the quote.

Once you have done that, you can add app.TextToSpeech() so that you have both the display and the speech working.

Then, if required, you can gradually remove the code that displays the quote, a bit at a time so that you know what the problem is if something gies wrong..

Jared

unread,
Sep 28, 2018, 10:35:50 AM9/28/18
to DroidScript
I like this Steve! I made a slightly modded version that generates random programmer/programming quotes it's kind of fun I think :D

@Isaac, take a look at how I processed the json file, it may provide some minor hints of how to manipulate json.

RandProgQuotes.spk

Jared

unread,
Sep 28, 2018, 10:43:54 AM9/28/18
to DroidScript
added this little code to copy to clipboard

put it after: "lay.AddChild(txt)"

code :

var btnCopy = app.CreateButton( "[fa-copy]" , -1,-1,"fontawesome");

btnCopy.SetOnTouch( function (){
app.SetClipboardText( txt.GetText() );
app.ShowPopup( "Copied to Clipboard . . . " );
});
lay.AddChild( btnCopy );

Reply all
Reply to author
Forward
0 new messages