Professional Documents
Culture Documents
Practical No 14
Practical No 14
Practical No 14
14
Practical No. 14: Develop a program to implement List View, Grid View, Image View
and Scroll View
I. Practical Significance
A View occupies a rectangular area on the screen and is responsible for drawing and event
handling. View is the base class for widgets, which are used to create interactive UI
components (buttons, text fields, etc.). The View Group subclass is the base class for layouts,
which are invisible containers that hold other Views (or other View Groups) and define their
layout properties.
.
II. Relevant Program Outcomes (POs)
PO 1. Basic knowledge
PO 2- Discipline knowledge
PO 3- Experiments and practice
PO 4- Engineering tools
Grid View
In android Grid View is a view group that display items in two dimensional scrolling grid (rows
and columns), the grid items are not necessarily predetermined but they are automatically
inserted to the layout using a List Adapter. Users can then select any grid item by clicking on
it. Grid View is default scrollable so we don’t need to use Scroll View or anything else
with Grid View.
Image View
In Android, Image View class is used to display an image file in application. Image file is easy
to use but hard to master in Android, because of the various screen sizes in Android devices.
An android is enriched with some of the best UI design widgets that allows us to build good
looking and attractive UI based application.
Scroll View
In android scroll View can hold only one direct child. This means that, if you have complex
layout with more views (Buttons, Text Views or any other view) then you must enclose them
inside another standard layout like Table Layout, Relative Layout or Linear Layout. You can
specify layout_width and layout_height to adjust width and height of screen. You can specify
height and width in dp (density pixel) or px (pixel). Then after enclosing them in a standard
layout, enclose the whole layout in scroll View to make all the element or views scrollable.
2.
Step 1. In activity_main.xml file, add <GridView> tag, give it some id, set number of columns
say 3.
Step 2. In MainActivity.java, refer the GridView added in activity_main.xml and create an
object.
Step 3. Create an ArrayAdapter object by using the String array given in question.
Step 4. Set the adapter in GridView by calling setAdapter() method.
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.os.Bundle;
simpleGrid.setAdapter(arrayAdapter);
}
3.
android:stretchMode
Defines how columns should stretch to fill the available empty space, if any.
Must be one of the following constant values.
Constant Value Description
columnWidth 2 Each column is stretched equally.
none 0 Stretching is disabled.
spacingWidth 1 The spacing between each column is stretched.
spacingWidthUniform 3 The spacing between each column is uniformly stretched.
X. Exercise
(Use blank space provide for answers or attached more pages if needed)
1. Write a program to show the following output. Use appropriate view for the same.
2. Write a program to display an image using Image View and a button named as “Change
Image”. Once you click on button another image should get displayed.
3. Write a program to display 15 buttons using grid view.
4. Write a program to display a text view using vertical scroll view.
Answers:
1.
//MainActivity.java
package com.jamiapolytechnic.experiment141;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.os.Bundle;
import android.widget.Toast;
}
});
}
}
//=====================================================================
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f73"
android:dividerHeight="1dp" />
</LinearLayout>
//=====================================================================
//activity_listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:textColor="#F000" />
</LinearLayout>
//=====================================================================
2.
package com.jpa.experiment14_2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
iv = findViewById(R.id.img);
btn = findViewById(R.id.btn);
String uri1 = "@drawable/img1";
String uri2 = "@drawable/img2";
int imageResource1 = getResources().getIdentifier(uri1, null, getPackageName());
int imageResource2 = getResources().getIdentifier(uri2, null, getPackageName());
res1 = getResources().getDrawable(imageResource1);
res2 = getResources().getDrawable(imageResource2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(iv.getDrawable() == res1)
iv.setImageDrawable(res2);
else
iv.setImageDrawable(res1);
}
});
}
}
//=====================================================================
//activity_main.xml
</LinearLayout>
//=====================================================================
3.
//Experiment14_3
//MainActivity.java
package com.jamiapolytechnic.experiment143;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//=====================================================================
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="110dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 14" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 15" />
</GridView>
</LinearLayout>
//=====================================================================
4.
//Experiment14_4
//MainActivity.java
package com.jamiapolytechnic.eperiment144;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//=====================================================================
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/data_types" />
</ScrollView>
//=====================================================================
//strings.xml
<resources>
<string name="app_name">Eperiment144</string>
<string name="data_types">
<b>Java Data Types: </b> \n
byte \n short \n int \n long \n
float \n double \n
char \n boolean \n
class \n interface \n\n
<b>Again: </b> \n
byte \n short \n int \n long \n
float \n double \n
char \n boolean \n
class \n interface \n\n
<b>Once more: </b> \n
byte \n short \n int \n long \n
float \n double \n
char \n boolean \n
class \n interface \n\n
<b>One last time: </b> \n
byte \n short \n int \n long \n
float \n double \n
char \n boolean \n
class \n interface
</string>
</resources>