Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
31 views5 pages

Practical 14

The document outlines attributes of ImageView in Android, including adjustViewBounds, scaleType, and tint. It also describes the android:stretchMode attribute of GridView, which controls how columns stretch to fill space. Additionally, it provides example XML and Java code for implementing a GridView and an ImageView with a button to change images.

Uploaded by

dasmitag147
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Practical 14

The document outlines attributes of ImageView in Android, including adjustViewBounds, scaleType, and tint. It also describes the android:stretchMode attribute of GridView, which controls how columns stretch to fill space. Additionally, it provides example XML and Java code for implementing a GridView and an ImageView with a button to change images.

Uploaded by

dasmitag147
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 14:

List all attributes of Image View.

Ans 1) All the attributes of Image view are as follows:

1. android:adjustViewBounds

2. android:baseline

3. android:baselineAlignBottom

4. android:cropToPadding

5. android:maxHeight

6. android:maxWidth

7. android:scaleType

8. android:src.

9. android:tint

10.android:tintMode

Describe android:stretchMode attribute of Grid view in detail.

Ans 3) android:stretchMode : Defines how columns should stretch to fill the available empty space, if
any. This must be either of the values : none : Stretching is disabled. spacingWidth : The spacing
between each column is stretched.

Grid view program

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"

android:padding="16dp">

<GridView

android:id="@+id/gridView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:numColumns="5"
android:verticalSpacing="10dp"

android:horizontalSpacing="10dp"

android:stretchMode="columnWidth"

android:gravity="center"

android:background="#EEEEEE" />

</LinearLayout>

Javacode

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.GridView;

import android.widget.Toast;

public class MainActivity extends Activity {

GridView grid;

static final String[] letters = new String[]{

"A", "B", "C", "D", "E",

"F", "G", "H", "I", "J",

"K", "L", "M", "N", "O",

"P", "Q", "R", "S", "T",

"U", "V", "W", "X", "Y", "Z"

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main); // Ensure 'main.xml' has a GridView with id 'gridView'


grid = findViewById(R.id.gridView);

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,

android.R.layout.simple_list_item_1, letters);

grid.setAdapter(adapter);

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String selectedLetter = letters[position];

Toast.makeText(MainActivity.this, "You clicked: " + selectedLetter,


Toast.LENGTH_SHORT).show();

});

Write a program to display an image using Image View and a button named as “Change Image”.
Once you click on button another image

<?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"

android:gravity="center"

android:padding="20dp">

<ImageView

android:id="@+id/imageView"

android:layout_width="250dp"

android:layout_height="250dp"

android:src="@drawable/image1"
android:scaleType="fitCenter"/>

<Button

android:id="@+id/changeImageBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Change Image"

android:layout_marginTop="20dp"/>

</LinearLayout>

Java

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;

private Button changeImageBtn;

private boolean isImage1 = true;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);

changeImageBtn = findViewById(R.id.changeImageBtn);

changeImageBtn.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {

if (isImage1) {

imageView.setImageResource(R.drawable.image2); // Switch to second image

} else {

imageView.setImageResource(R.drawable.image1); // Switch back to first image

isImage1 = !isImage1; // Toggle the flag

});

You might also like