0% found this document useful (0 votes)
19 views13 pages

Practical 14

The document outlines practical exercises for developing Android applications using various views such as List View, Grid View, Image View, and Scroll View. It includes Java code for MainActivity and XML layout files for each exercise, demonstrating how to implement user interactions and display content. The exercises cover displaying lists, changing images with buttons, creating a grid of buttons, and implementing scrollable text views.

Uploaded by

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

Practical 14

The document outlines practical exercises for developing Android applications using various views such as List View, Grid View, Image View, and Scroll View. It includes Java code for MainActivity and XML layout files for each exercise, demonstrating how to implement user interactions and display content. The exercises cover displaying lists, changing images with buttons, creating a grid of buttons, and implementing scrollable text views.

Uploaded by

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

Practical 14 : Develop a program to implement List View, Grid View, Image View

and Scroll View.

X. Exercise
1. Write a program to show the following output. Use appropriate view for the same.
MainActivity.java code:
package com.example.madpractical14.1;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView; import
android.widget.TextView; import
android.widget.Toast;

public class MainActivity extends AppCompatActivity {


ListView listView;
TextView textView; String[]
listItem;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = findViewById(R.id.listView);
textView = findViewById(R.id.textView);

listItem = getResources().getStringArray(R.array.array_technology); final

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


android.R.layout.simple_list_item_1, android.R.id.text1, listItem);

listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent" />

</LinearLayout>

String.xml code:
<resources>
<string name="app_name">MADpractical4.2</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
<item>Hadoop</item>
</string-array>
</resources>

activity_mylist.xml code:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="medium_text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:padding="5dp"
android:textColor="#FF5722"/>
Output:
2. Write a program to display an image using image view and a button name as
“change image”. Once you click on the button another image should get displayed.
MainActivity.java code:
package com.example.madpractical142;
import android.os.Bundle;
import java.util.Random;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.madpractical142.R;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final ImageButton dice = findViewById(R.id.imageButton2); final


TextView t = findViewById(R.id.textView);

dice.setOnClickListener(view -> { final


Random r = new Random(); int i =
r.nextInt(6) + 1;

switch (i) {
case 1:
dice.setImageResource(R.drawable.dice_six_faces_one);
t.setText("You get: 1");
break;
case 2:
dice.setImageResource(R.drawable.dice_six_faces_two);
t.setText("You get: 2");
break;
case 3:
dice.setImageResource(R.drawable.dice_six_faces_three);
t.setText("You get: 3");
break;
case 4:
dice.setImageResource(R.drawable.dice_six_faces_four);
t.setText("You get: 4");
break;
case 5:
dice.setImageResource(R.drawable.dice_six_faces_five);
t.setText("You get: 5");
break;
case 6:
dice.setImageResource(R.drawable.dice_six_faces_six);
t.setText("You get: 6");
break;
}
});
}
}

activity_main.xml code:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/dice_six_faces_one" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi" android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageButton2" />

</androidx.constraintlayout.widget.ConstraintLayout>
Output:
3. Write a program to Display 1 buttons using grid view.
MainActivity.java code:
package com.example.madpractical14.3;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {


GridView gridView;
String[] a = new String[15];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gridView = findViewById(R.id.gridview);

for (int i = 0; i < 15; i++) {


a[i] = "" + (i + 1);
}

ArrayAdapter<String> ad = new ArrayAdapter<>(this, R.layout.activity_mylist,


R.id.button, a);
gridView.setAdapter(ad);
}
}

activity_mylist.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

</LinearLayout>
activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" tools:context=".MainActivity"
android:columnWidth="90dp"
android:layout_gravity="center"
android:horizontalSpacing="10dp"
android:id="@+id/gridview"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp">
</GridView>

Output:
4. Write a program to Display a text view using scroll view.
MainActivity.java code:
package com.example.madpractical142;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

activity_main.xml code:
<?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">

<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="Vertical Scroll View"
android:textSize="30dp"
android:layout_gravity="center"
/>

<ScrollView android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 1"
android:textSize="30dp"
android:textColor="#A52A2A" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 2"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 3"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 4"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 5"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 6"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 7"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 8"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 9"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 10"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 11"
android:textSize="30dp"
android:textColor="#A52A2A" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:padding="20dp"
android:text="Textview 12"
android:textSize="30dp"
android:textColor="#A52A2A" />
</LinearLayout>
</ScrollView>
</LinearLayout>

Output:

You might also like