Display a list in a Dialog Box in Sketchware

To display a list of items in a Dialog Box in Sketchware follow the steps given below.

1. In your Sketchware android project, add a Button button1 and a TextView textview1. The Button is for showing Dialog Box and TextView is for displaying the selected item.

2. Now go to LOGIC area, and add a List String list.
3. In onCreate event, add items to the list.
4. Create a new Dialog component dialog.

5. In Button onClick event use the Block Dialog setTitle, to set the title of the Dialog Box.

6. After that use an add source directly block and write following code in it.
dialog.setAdapter(
new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list),
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface _di, int _pos){
textview1.setText(list.get(_pos));
}} );

6. After the above code in Button onClick event, add Dialog show block.

7. Save and run the project. On clicking the button you will see a Dialog Box displaying the list of items.
8. If you want to open different pages (e.g. Page1Activity.java, Page2Activity.java, InfoActivity.java) on selecting the items from list, then create an Intent component i and instead of code above use following code onButtonClick.
dialog.setAdapter(
new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list),
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface _di, int _pos){
textview1.setText(list.get(_pos));
if (_pos == 0) {
i.setClass(getApplicationContext(), Page1Activity.class);
startActivity(i);
}
if (_pos == 1) {
i.setClass(getApplicationContext(), Page2Activity.class);
startActivity(i);
}
if (_pos == 2) {
i.setClass(getApplicationContext(), InfoActivity.class);
startActivity(i);
}
}} );

And after this use the Dialog show block.

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

TextInputLayout in Sketchware

How to find and​ highlight a word in a text field in Sketchware?

A Flash Light App in Sketchware

How to enable download in webview in Sketchware apps?

Intent - Open File By Type

Code for implementing Notifications in Sketchware

How to share an image from Drawable folder?

ActionBar back button

Animation Transtition Animation