Add ListView to Navigation drawer
In the latest version of Sketchware (v2.2.2), appcompat-v7 and design have been added, and we can now add a navigation drawer to our project. This navigation drawer uses a CustomView. But the CustomView in Sketchware doesn't have option to add a ListView.
In order to add a ListView to the drawer, we have to create the ListView programmatically and then add it to the drawer. To know how it can be done follow the steps given below.
1. In your Sketchware android project, go to Library manager and switch on AppCompat and Design.
2. In View manager, go to MainActivity.java and select Navigation Drawer Activity.
3. Now go to the CustomView drawer_main and add a LinearV linear1. Set it's width as MATCH_PARENT.
4. Create a new String list mylist.
5. Create a new Intent component i.
6. Now suppose you have five other activities SettingsActivity.java, NotesActivity.java, FeedbackActivity.java, PrivacyActivity.java, and AboutActivity.java.
Add a word for each of these activities to the String list mylist, in the onCreate event.
7. In the onCreate event, after adding items to the list, use an add source directly block and put following codes in it.
ListView listview1 = new ListView(this);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
listview1.setLayoutParams(lp1);
listview1.setAdapter(new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1, mylist));
_drawer_linear1.addView(listview1);
Note that in this code mylist is the name of the String list, _drawer_linear1 represents the linear1 added in the CustomView drawer_main.
This code creates a new ListView listview1, sets it's LayoutParams, sets it's Adapter, and adds it to the LinearLayout linear1 in CustomView drawer_main.
8. After this add another add source directly block and put codes for setting OnItemClickListener for the ListView.
listview1.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(
AdapterView _parent, View _view, final int _position, long _id) {
switch (_position){
case 0:
i.setClass(getApplicationContext(), SettingsActivity.class);
startActivity(i);
break;
case 1:
i.setClass(getApplicationContext(), NotesActivity.class);
startActivity(i);
break;
case 2:
i.setClass(getApplicationContext(), FeedbackActivity.class);
startActivity(i);
break;
case 3:
i.setClass(getApplicationContext(), PrivacyActivity.class);
startActivity(i);
break;
case 4:
i.setClass(getApplicationContext(), AboutActivity.class);
startActivity(i);
break;
}
}});
Note that here i is Intent component.
9. Save and run the project.
In order to add a ListView to the drawer, we have to create the ListView programmatically and then add it to the drawer. To know how it can be done follow the steps given below.
1. In your Sketchware android project, go to Library manager and switch on AppCompat and Design.
2. In View manager, go to MainActivity.java and select Navigation Drawer Activity.
3. Now go to the CustomView drawer_main and add a LinearV linear1. Set it's width as MATCH_PARENT.
4. Create a new String list mylist.
5. Create a new Intent component i.
6. Now suppose you have five other activities SettingsActivity.java, NotesActivity.java, FeedbackActivity.java, PrivacyActivity.java, and AboutActivity.java.
Add a word for each of these activities to the String list mylist, in the onCreate event.
7. In the onCreate event, after adding items to the list, use an add source directly block and put following codes in it.
ListView listview1 = new ListView(this);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
listview1.setLayoutParams(lp1);
listview1.setAdapter(new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1, mylist));
_drawer_linear1.addView(listview1);
Note that in this code mylist is the name of the String list, _drawer_linear1 represents the linear1 added in the CustomView drawer_main.
This code creates a new ListView listview1, sets it's LayoutParams, sets it's Adapter, and adds it to the LinearLayout linear1 in CustomView drawer_main.
8. After this add another add source directly block and put codes for setting OnItemClickListener for the ListView.
listview1.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(
AdapterView _parent, View _view, final int _position, long _id) {
switch (_position){
case 0:
i.setClass(getApplicationContext(), SettingsActivity.class);
startActivity(i);
break;
case 1:
i.setClass(getApplicationContext(), NotesActivity.class);
startActivity(i);
break;
case 2:
i.setClass(getApplicationContext(), FeedbackActivity.class);
startActivity(i);
break;
case 3:
i.setClass(getApplicationContext(), PrivacyActivity.class);
startActivity(i);
break;
case 4:
i.setClass(getApplicationContext(), AboutActivity.class);
startActivity(i);
break;
}
}});
Note that here i is Intent component.
9. Save and run the project.
Why complicate with codes if it can be done with blocks and without "list"
ReplyDeletewe can add listview to it by AIDE
ReplyDeleteThanks
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete1. Is it possible to change the direction of the drawer from right to left ?.
ReplyDelete2. Is it possible to add a submenu to drawer?
3.is it possible to add a switch to the listview?
bro sanjeev, I have any question: 1. how to change size linier drawer 2. how to change colour linier on drawer. thanks
ReplyDeleteHi Sanjeev, is it possible add custom listview in drawer with appCompact?
ReplyDeleteI love sketchware
ReplyDeleteHow to add listview with images in drawer....
ReplyDeleteHow can I change the font of a textview widget in drawer view?
ReplyDeleteVery nice! Thank you for the posts on Sketchware. Very informative.
ReplyDeleteHow to set OnBindCustomView to listview inside drawer layout
ReplyDelete