Week 6
Week 6
Lesson 6
Android
List-Based Selection Widgets
Victor Matos
Cleveland State University
Portions of this page are reproduced from work created and shared by Google and used according to terms
described in the Creative Commons 3.0 Attribution License.
2
Lesson 6
DATA
ADAPTER
p on a row to
Users can tap
make a selection.
4
Lesson 6
Parameters:
1. The current activity’s Context
2. The ListView showing the entire array (such as the built-in system resource :
android.R.layout.simple_list_item_1 ).
3. The TextView to which an individual row is written ( android.R.id.text1 ).
4. The actual data source (array or Java.List containing items to be shown).
5
<TextView
android:id="@android:id/empty" Used for empty lists
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff0000"
android:text="empty list" />
</LinearLayout>
6
Lesson 6
import ...
TextView txtMsg;
8
Lesson 6
Selection seen
by the listener
1
1. A ListActivity
Li tA ti it references
f it
its ViewGroup
Vi G with
ith the
th dedicated
d di t d name:
android:id/list
2. Later in the setting of the ArrayAdapter we use
android.R.layout.simple_list_item_1 (one row identified as text1 displayed
for each item supplied by the adapter)
3. The entry android.R.id.text1 names the destination field in the UI for a
single
g item.
Android SDK includes a number of predefined layouts/styles, they can be found in the folders
11
<TextView
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff00"
android:text="Using ListViews..."
android:textSize="16sp" />
<ListView
android:id="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
13
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v,
int position, long id) {
txtMsg.setText(text);
}
});
15
16
Lesson 6
1. Click here
3. Selected value
1. Click here
<TextView
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:background="#ff0033cc"
android:textSize "30sp"
android:textSize= 30sp
android:textStyle="bold" >
</TextView>
<Spinner
android:id="@+id/my_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</Spinner>
</LinearLayout>
19
implements AdapterView.OnItemSelectedListener {
TextView txtMsg;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
20
Lesson 6
}// onCreate
@Override
public void onNothingSelected(AdapterView<?> container) {
txtMsg.setText("");
}
}); 22
Lesson 6
}// onCreate
C t
}// class
23
24
Lesson 6
• android:numColumns indicates how many columns to show. When used with option
“auto_fit”, Android determines the number of columns based on available space and
the properties listed below.
25
Three columns would use 310 pixels (three columns of 100 pixels and two whitespaces of
5 pixels).
With android:stretchMode set to columnWidth, the three columns will each expand by
3-4 pixels to use up the remaining 10 pixels.
With android:stretchMode set to spacingWidth, the two internal whitespaces will each
grow by 5 pixels to consume the remaining 10 pixels.
26
Lesson 6
<GridView
android:id="@+id/grid"
android:background="#ffA52A2A"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:verticalSpacing="35dip"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:columnWidth="100dip"
android:stretchMode="spacingWidth" />
</LinearLayout> 27
TextView txtMsg;
28
Lesson 6
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public
bli voidid onItemClick(AdapterView<?>
It Cli k(Ad t Vi <?> container,
t i View
Vi v,
int position, long id) {
txtMsg.setText(items[position]);
}
});
}//onCreate
}// class
29
• The user can choose from the suggestion list or complete typing the word.
30
30
Lesson 6
Select this
31
32
Lesson 6
public
bli class
l ArrayAdapterDemo4
d extends
d Activity
i i i l
implements TextWatcher
h {
TextView txtMsg;
AutoCompleteTextView txtAutoComplete;
@
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
33
txtAutoComplete.setAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_single_choice,
items));
}//onCreate
}//class 34
Lesson 6
37
<ImageView
android:id="@+id/selected_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/my_bottom_layout"
android:layout_margin="10dip" />
<RelativeLayout
android:id="@+id/my_bottom_layout"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:layout_alignParentBottom="true"
android:background="@android:color/darker
android:background @android:color/darker_gray
gray" >
<TextView
android:id="@+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#ffffff00"
android:text="Scroll and tap to select image..."
android:textColor="#ff000000"
android:gravity="center"/>
38
Lesson 6
</RelativeLayout>
</RelativeLayout>
39
ImageView selectedImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bm = Bitmap.createScaledBitmap(bitmapDrawable.getBitmap(),
(int) (bitmapDrawable.getIntrinsicHeight() * 1.0),
(int)
( ) (bitmapDrawable.getIntrinsicWidth()
( p g () * 1.0),
), false);
);
selectedImage.setImageBitmap(bm);
selectedImage.setScaleType(ScaleType.FIT_XY);
//ABOVE SIMILAR TO ==> selectedImage.setImageResource(largeImages[position]);
}
});
}// onCreate
}//class
41
// item touched
gallery.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
// finger touches the screen
break;
case MotionEvent.ACTION_MOVE:
// finger moves on the screen
break;
case MotionEvent.ACTION_UP:
// finger leaves the screen
break;
}
txtMsg.append ( "\n Touched... " + event.getAction() );
return false;
}
});
43
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout height "fill
android:layout_height= parent"
fill_parent
android:layout_margin="10dip"
android:numColumns=“2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#ff777777" />
</LinearLayout>
45
ImageView imgSoloPhoto;
Button btnBack;
Bundle myOriginalMemoryBundle;
47
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myOriginalMemoryBundle = savedInstanceState;
setContentView(R.layout.main);
gridview.setAdapter(new MyImageAdapter(this));
gridview.setOnItemClickListener(this);
}// onCreate
48
Lesson 6
// create a new ImageView for each grid item mentioned by the ArrayAdapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
// new views - width 200, hwight 180, centered & cropped, 8dp padding
imageView.setLayoutParams(new GridView.LayoutParams(200, 180))
imageView.setScaleType(ImageView.ScaleType.CENTER CROP);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource( smallImages[position] );
return imageView;
}
}// ImageAdapter
49
imgSoloPhoto.setImageResource( largeImages[position] );
@Override
public void onClick(View v) {
// redraw the main screen beginning the whole app.
onCreate(myOriginalMemoryBundle);
}
});
}// showBigScreen
}// class
50
Lesson 6
Example 7 - Spinner
(again…)
(again )
51
52
Lesson 6
<Button
android:text="Back"
android:id="@+id/btnBack2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
53
Spinner spinnerCar;
Integer[] carImageArray;
TextView txtMsg2;
Button btnBack;
Bundle myState;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
myState = savedInstanceState;
54
Lesson 6
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerCar.setAdapter(adapter);
}//onCreate
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
if (position>0)
showScreen2(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// nothing TODO - stub is needed by the interface
}
55
setContentView(R.layout.screen2);
imgCar.setImageResource(carImageArray[position-1]);
}//showScreen2
}//ArrayAdapter7Spinner2
56
Lesson 6
Customized Lists
1. Not every row uses the same layout (e.g., some have one line of text,
others have two)
2
2. You need to configure the widgets in the rows (e
(e.g.,
g different icons for
different cases)
Example:
Each UI row will show an icon and text. Modify getView()
to accommodate different icons at different row positions
(odd rows show OK, even rows NOT-OK image).
58
Lesson 6
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:src="@drawable/not_ok" />
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#55ffff00"
android:textSize="40sp" />
</LinearLayout>
60
Lesson 6
TextView txtMsg;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new CustomIconLabelAdapter(this));
61
63
<LinearLayout
main.xml
main xml android:id= @+id/viewgroup
android:id="@+id/viewgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
Inflated
frames </HorizontalScrollView>
go here… </LinearLayout>
64
Lesson 6
<TextView
android:id="@+id/caption"
android:id= @+id/caption
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#55ffff00"
android:textSize="20sp" />
</LinearLayout>
65
TextView txtMsg;
ViewGroup scrollViewgroup;
ImageView icon;
TextView caption;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
66
Lesson 6
icon.setImageResource( thumbnails[i] );
caption.setText( items[i] );
3 scrollViewgroup.addView( frame );
frame.setId(i);
4 frame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "Selected position: " + frame.getId();
txtMsg.setText(text);
}
});// listener
}// for
}// onCreate
}// class
67
Comments
3.
3 After the current frame is filled with data,
data it is added to the
growing set of views hosted by the scrollViewgroup container
(scrollViewgroup is nested inside the horizontal scroller).
69
ViewGroup scrollViewgroup;
ImageView icon;
TextView caption;
TextView txtMsg;
int index;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
try {
File file;
icon.setImageBitmap(bitmap);
caption.setText("File-" + index);
71
scrollViewgroup.addView(frame);
frame.setId(index);
frame setId(index);
frame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "Selected position: " + frame.getId();
txtMsg.setText(text);
}
}); // listener
}// for
} catch (Exception e) {
} // onCreate
} // class
72
Lesson 6
BitmapFactory
Creates Bitmap objects from various sources, including files, streams, and byte
byte-arrays.
arrays.
inSampleSize Option
• If set to a value > 1, requests the decoder to subsample the original image,
returning a smaller image to save memory.
• The sample size is the number of pixels in either dimension that correspond to a
single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an
image that is 1/4 the width/height of the original, and 1/16 the number of pixels.
• Any value <= 1 is treated the same as 1.
Reference:
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
73
0 - Questions ?
1 - Questions
Q ti ?
2 – Questions ?
N – Questions ?
74
Lesson 6
<!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
75
Link: http://code.google.com/p/pdn-
slatedroid/source/browse/trunk/eclair/frameworks/base/core/res/res/layout/simple_spinner_dropdown_item.xml?r=44
76
Lesson 6
Java
editTextBox.setInputType(
dit t t t (
android.text.InputType.TYPE_CLASS_PHONE);
Knowing the inputType has an impact on virtual keyboards (the software can
expose the best layout for the current input class)
79
80
Lesson 6
Reference:
http://developer.android.com/r
eference/android/R.styleable.ht
ml#TextView_inputType
81
After tapping the EditText After first letter is typed the After entering space the
box to gain focus, a soft keyboard automatically keyboard repeats cycle
keyboard appears showing switches to LOWER case mode beginning with UPPER case,
CAPITAL letters then LOWER case letters. 83
English and Spanish are the You may speed up typing by Selected word is introduced in
user’s selected languages tapping on an option from the EditText box
in this device the list of suggested words
(bilingual choices) 84
Lesson 6
Soft keyboard
shows characters
used in email
addresses (such as
letters, @, dot).
Click on [?123 ]
key (lower-left) for
additional
characters
Additional symbols
87
88
Lesson 6
You
Y may change
h the
h bbutton’s
’ caption
i and d
set a listener to catch the click event on the
Auxiliary button. To do that add the
following entry to your XML specification of
the EditText box:
android:imeAction=“actionSend”
editTextBox
.setOnEditorActionListener()
to do something with the event.
89
are
90
Lesson 6
91
editTextBox.setInputType( InputType.TYPE_NULL );
92
Lesson 6
TextWatcher Control
Assume txtBox1 is an Editable box. A listener of the type onKeyListener could
be used to follow the actions made by the hardware keyboard; however it will
not properly work with the Virtual Keyboard.
93
IME suggestions
94
Lesson 6
<TextView
android:id="@+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textColor="#ffffffff"
android:background="#ff0000ff"
tools:context=".MainActivity" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="41dp“ />
</RelativeLayout> 95
editText1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable theWatchedText) {
String msg = "count: “ + editText1.getText().toString().length() + " "
+ theWatchedText.toString();
txtMsg.setText(msg);
}
96