0% found this document useful (0 votes)
13 views3 pages

Unit 4

The document discusses different view components in Android like ListView, GridView and ImageView. It explains that ListView and GridView use an adapter to populate their items from a data source. It provides code samples to set an adapter and load images into an ImageView from resources, URLs and input streams.
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)
13 views3 pages

Unit 4

The document discusses different view components in Android like ListView, GridView and ImageView. It explains that ListView and GridView use an adapter to populate their items from a data source. It provides code samples to set an adapter and load images into an ImageView from resources, URLs and input streams.
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/ 3

UNIT 04

4.1 Displays a vertically-scrollable collection of views,


where each view is positioned immediatelybelow the
View Attributes type previous view in the list.
TextView text String
textSize Dimension xml
textColor Color <ListView
android:id="@+id/list_view"
backgroundTint Color android:layout_width="match_parent"
EditText text String android:layout_height="match_parent" />
hint String
textSize Dimension
textColor Color A list view is an adapter view that does not know the
inputType Integer details, such as type and contents, of the views it
Button text String contains. Instead list view requests views on demand
textAppearance Resource from a ListAdapter as needed, such as to display new
(resource reference views as the user scrolls up or down.
reference to (style)
java
style) public class CustomAdapter extends BaseAdapter {
backgroundTint Color
clickListener Interface private Context context;
private List<Item> items;
(interface)
ImageButton src (drawable Drawable public CustomAdapter(Context context, List<Item>
items) {
resource resource this.context = context;
reference) reference this.items = items;
backgroundTint Color }
@Override
clickListener Interface public View getView(int position, View
(interface) convertView, ViewGroup parent) {
ToggleButton text String if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
textOn String context.getSystemService(Context.LAYOUT_INFLATER_SER
textOff String VICE);
convertView =
checked Boolean inflater.inflate(R.layout.item_layout, parent,
RadioButton text String false);
buttonTint Color reference }

(color TextView titleTextView =


reference) convertView.findViewById(R.id.item_title);
TextView descriptionTextView =
checked Boolean convertView.findViewById(R.id.item_description);
RadioGroup Not Applicable Not Applicable
Checkbox text String Item item = items.get(position);
titleTextView.setText(item.getTitle());
buttonTint Color reference
(color descriptionTextView.setText(item.getDescription());
reference) return convertView;
checked Boolean }
ProgressBar style (resource Resource }
reference to reference
style) (style)
Adapter Requires to override the getView in order to
progress Float
return a View which can be displayed inside ListView
max Integer
as item.

4.2 To set adapter to our listView we’ve to use setAdapter


method
ListView
java
CustomAdapter adapter = new CustomAdapter(this,
items);
listView.setAdapter(adapter);

XML Attributes
UNIT 04
1. android:divider Drawable or color to draw xml
between list items. <ImageView
android:layout_width="wrap_content"
2. android:dividerHeight Height of the divider. android:layout_height="wrap_content"
3. android:entries Reference to an array resource android:src="@drawable/my_image"

that will populate the ListView. android:contentDescription="@string/my_image_descrip


4. android:footerDividersEnabled When set to tion"
/>
false, the ListView will not draw the divider
before each footer view.
To set an Image
5. android:headerDividersEnabled When set to
false, the ListView will not draw the divider
after each header view. java
ImageView simpleImageView=(ImageView)
GridView findViewById(R.id.simpleImageView);
simpleImageView.setImageResource(R.drawable.lion);

To set image from input stream


java
InputStream inputStream = new
FileInputStream(imagePath);
Drawable image = new BitmapDrawable(inputStream);
imageView.setImageDrawable(image);
GridView is very similar to ListView as it’s subclass of
same superclass
To load image from URL
A view that shows items in two-dimensional scrolling
grid. The items in the grid come from the ListAdapter java
Glide.with(this)
associated with this view. .load("https://www.example.com/image.jpg")
.into(imageView);
* Same Adapter Concepts applies on GridView
XML Attributes
xml
<GridView 1. android:adjustViewBounds Set this to true
android:id="@+id/idGVcourses" if you want the ImageView to adjust its bounds
android:layout_width="match_parent"
android:layout_height="match_parent" to preserve the aspect ratio of its drawable.
android:numColumns="2"/> 2. android:baseline The offset of the
baseline within this view.
3. android:baselineAlignBottom If true, the
XML Attributes
image view will be baseline aligned with based
1. android:columnWidth Specifies the fixed on its bottom edge.
width for each column. 4. android:cropToPadding If true, the image will
2. android:gravity Specifies the gravity within be cropped to fit within its padding.
each cell. 5. android:maxHeight An optional argument
3. android:horizontalSpacing Defines the to supply a maximum height for this view.
default horizontal spacing between columns. 6. android:maxWidth An optional argument
4. android:numColumns Defines how many to supply a maximum width for this view.
columns to show. 7. android:scaleType Controls how the
5. android:stretchMode Defines how columns image should be resized or moved to match the
should stretch to fill the available empty space, size of this ImageView.
if any. 8. android:src Sets a drawable as the content
6. android:verticalSpacing Defines the default of this ImageView.
vertical spacing between rows. 9. android:tint The tinting color for the image.
10. android:tintMode Blending mode used to
ImageView apply the image tint.
ScrollView
UNIT 04
A view group that allows the view hierarchy placed 2. android:hour (optional): Sets the initial hour
within it to be scrolled. Scroll view may have only one displayed when the TimePicker opens (0-23 for
direct child placed within it. 24-hour mode).
Scroll view supports vertical scrolling only. For 3. android:minute (optional): Sets the initial
horizontal scrolling, use HorizontalScrollView instead. minute displayed when the TimePicker opens
(0-59).
xml
<ScrollView
android:layout_width="match_parent" DatePicker
android:layout_height="match_parent">

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" Provides a widget for selecting a date.
android:text="This is some scrollable text
content." /> xml
<DatePicker
</LinearLayout> android:id="@+id/date_picker"
</ScrollView> android:layout_width="wrap_content"
android:layout_height="wrap_content" />
XML Attributes
1. android:fillViewport Defines whether the To listen that user has selected a date
scrollview should stretch its content to fill the java
viewport. DatePicker datePicker = (DatePicker)
4.3 findViewById(R.id.date_picker);
datePicker.setOnDateChangedListener((view, year,
TimePicker monthOfYear, dayOfMonth) -> {
// listen
});

XML attributes
1. android:maxDate The maximal date
shown by this calendar view in mm/dd/yyyy
A widget for selecting the time of day, in either 24- format.
hour or AM/PM mode. 2. android:minDate The minimal date
xml shown by this calendar view in mm/dd/yyyy
<TimePicker format.
android:id="@+id/time_picker" 3. android:datePickerMode Defines the
android:layout_width="wrap_content"
android:layout_height="wrap_content" look of the widget.
android:timePickerMode="clock" />

To get time after user has changed time * For Showing DatePicker & TimePicker use dialogs.
java
timePicker.setOnTimeChangedListener(
(view, hourOfDay, minute)->{
// Do something with the selected hourOfDay
and minute
});

XML attributes
1. android:timePickerMode: (optional) Sets the
mode for displaying time.
a. clock (default): Displays time using a
clock face with dials for hours and
minutes.
b. spinner: Displays time using separate
spinners for hours and minutes.

You might also like