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.

Watch the video to understand better.

Comments

  1. Please use Hacker Keyboard App, it's easy to edit, cut paste...etc data. Hurts me seeing you struggling to edit transcription errors
    🙈

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Why double list word in list dialog

    ReplyDelete

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity