0% found this document useful (0 votes)
41 views11 pages

Operation Sheet 3.1: Using List View in Building Mobile Application Menu

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Federal TVET Institute

Department of Information Communication Technology

Master of ICT Teachers Education

Operation Sheet 3.1: Using


List View in building mobile
application menu.

Dr. Patrick D. Cerna (Associate Professor)

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 1


Procedure:

Step 1. Create Android Application


The first step is to create a simple Android Application using Android studio. Search
for android in the program menu. When you click on Android studio icon, then click
“Start a new Android Studio Project”it will show screen as shown below:

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 2


Step 2: Type the application name

You can start your application development by calling start a new android studio
project. in a new installation frame should ask Application name, package
information and location of the project. Type the application name “GridView”

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 3


Step 3: Select Target Android Device

After entered application name, it going to be called select the form factors your
application runs on, here need to specify Minimum SDK, in our tutorial, I have
declared as API23: Android 6.0(Mashmallow) −

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 4


Step 4: Choose an Activity

The next level of installation should contain selecting the activity to mobile, it
specifies the default layout for Applications.

Step 5: Getting Ready for writing the application code

At the final stage it going to be open development tool to write the application code.

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 5


Step 6: After Click on a virtual device icon, it going to be shown by default virtual
devices which are present on your SDK, or else need to create a virtual device by
clicking Create new Virtual device button

If your AVD is created successfully it means your environment is ready for Android
application development.

Step 7: Design the User Interface

Before Writing a Hello word code, you must know about XML tags.To write hello
word code, you should redirect to App>res>layout>Activity_main.xml. Drag the
GridView on the Activity.

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 6


Step 8: Modify the MainActivity.java

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 7


A. Import necessary libraries

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

B. Declare and Create an Array. Add the following code to main function.

ListView simpleList;
String countryList[] = {"India", "China", "australia", "Portugle",
"America", "NewZealand"};
int flags[] = {R.drawable.india, R.drawable.china, R.drawable.australia,
R.drawable.portugle, R.drawable.america, R.drawable.new_zealand};

C. Add the code in the onCreate() Function

simpleList = (ListView) findViewById(R.id.simpleListView);


CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(),
countryList, flags);
simpleList.setAdapter(customAdapter);

Full MainActivity.java file code

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 8


Step 9: Add a new Java Activity File named it CustomAdapter.java and the following code
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.zip.Inflater;

public class CustomAdapter extends BaseAdapter {


Context context;
String countryList[];
int flags[];
LayoutInflater inflter;

public CustomAdapter(Context applicationContext, String[] countryList,


int[] flags) {
this.context = context;
this.countryList = countryList;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}

@Override

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 9


public int getCount() {
return countryList.length;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_listview, null);
TextView country = (TextView)
view.findViewById(R.id.textView);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
country.setText(countryList[i]);
icon.setImageResource(flags[i]);
return view;
}

Step 10: Running the Application.


Now run the App in Emulator and it will show you name of countries along with flags.
Below is the output screen:

Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 10


Operation Sheet 3.1 – GridView Dr. Patrick D. Cerna Page 11

You might also like