Listview with example in Android
Now we will see how we can design a practical list view stepwise.
- So for this we have created a new project by giving it's name as 'list' , and now we will see how to design its content.
- So it has xml file inside the ‘src’ folder which is inside the ‘app’ folder which is default generated.
- After that we will design its layout, for this we will open our 'res' folder.
- And after that we will see xml file, in which the linear layout is the default, so inside it we will now design a list view tag, and we will set the properties like its width and height.
- Now we have to design a second layout file which we will give the name xml. Now inside this we have to design a text view, in which we will set basic properties, such as width, height, padding, textStyle, textSize.
- Now we will see our main activity, for that we will now open java in the folder of 'java' which is inside our 'main' folder. We can see a default code there.
- First we will create an array of strings. We will specify the name of array as 'mobilearray', and then we will specify whatever content we need inside that array. All these contents will be displayed in our list.
- After this we have to create an ArrayAdapter. So after creating it, we'll keep its instance as 'adapter'. And after that its content work will be specified.
- After that we have to pass the arguments in which we will first pass 'localinstance', and then we have to set its layout, then we will use our 'res' folder to set the layout.
- Now we have to identify our list view here with the help of given Id.
- After that we have to set the adapter of List view, then we can use its instance here.
- Now we will see it by executing, going to the option of run in the menu, as soon as we click on the run app, our code will be displayed, we will be able to see the list that we had designed, and we will see all the contents that we had given inside the array.