Android Sensor Programming

Slides:



Advertisements
Similar presentations
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Advertisements

User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Layout and Control in UI The user interface (UI) is the graphical interface user can see and interact with your app comprising UI controls like textbox,
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Presented By: Muhammad Tariq Software Engineer Android Training course.
Import import android.graphics.Bitmap; import android.widget.ImageView;
로봇 모니터링 1/2 UNIT 20 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Message Queue Handler 2.
1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView.
Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Lab7 – Appendix.
Lab7 – Advanced.
Concurrency in Android
Android Programming - Features
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android N Amanquah.
GUI Programming Fundamentals
Android – Event Handling
Android External Resources
Android Widgets 1 7 August 2018
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android Introduction Hello Views Part 1.
ITEC535 – Mobile Programming
Android – Read/Write to External Storage
Picasso Revisted.
Android Introduction Hello Views Part 2.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Software Engineering for Internet Applications
CIS 470 Mobile App Development
Android Programming Lecture 6
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Sensor Programming
CIS 470 Mobile App Development
Building User Interfaces Basic Applications
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Android Developer Fundamentals V2
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Adding Components to Activity
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
User Interface Screen Elements
User Interface Screen Elements
CIS 470 Mobile App Development
Android Sensor Programming
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Android Sensor Programming
CIS 470 Mobile App Development
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Android Sensor Programming CIS 694/EEC 693 Android Sensor Programming Lecture 8 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University w.zhao1@csuohio.edu 11/6/2019 Android Sensor Programming

Android Sensor Programming User Interface Basic views: Commonly used views, such as the TextView, EditText, and Button views Picker views: Views that enable users to select from a list, such as the TimePicker and DatePicker views List views: Views that display a long list of items, such as the ListView and the SpinnerView views 11/6/2019 Android Sensor Programming

Android Sensor Programming Basic views TextView — to display text to the user EditText —A subclass of the TextView view, which allows users to edit its text content Button—Represents a push-button widget ImageButton—Similar to the Button view, except that it also displays an image CheckBox—A special type of button that has two states: checked or unchecked RadioGroup and RadioButton—The RadioButton has two states: either checked or unchecked. A RadioGroup is used to group one or moreRadioButtonviews, thereby allowing only one RadioButton to be checked within the RadioGroup ToggleButton—Displays checked/unchecked states using a light indicator 11/6/2019 Android Sensor Programming

Android Sensor Programming Basic views One thing that has been consistent throughout this example is that each view has the id attribute set to a particular value, such as in the case of the Button The id attribute is an identifier for a view, which allows it to be retrieved using the View.findViewById() or Activity.findViewById() methods <Button android:id="@+id/btnSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/save" /> 11/6/2019 Android Sensor Programming

Android Sensor Programming Using basic views Using Android Studio, create an Android project and name it BasicViews Replace the activity_main.xml with the following: <?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" > <Button android:id="@+id/btnSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="save" /> <Button android:id="@+id/btnOpen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open" /> <ImageButton android:id="@+id/btnImg1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <EditText android:id="@+id/txtName" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/chkAutosave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Autosave" /> <CheckBox android:id="@+id/star" style="?android:attr/starStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 11/6/2019 Android Sensor Programming

Android Sensor Programming <RadioGroup android:id="@+id/rdbGp1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/rdb1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/rdb2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Option 2" /> </RadioGroup> <ToggleButton android:id="@+id/toggle1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 11/6/2019 Android Sensor Programming

Android Sensor Programming Using basic views Modify MainActivity.java (add bolded lines): import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //---Button view--- Button btnOpen = (Button) findViewById(R.id.btnOpen); btnOpen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DisplayToast("You have clicked the Open button"); } }); 11/6/2019 Android Sensor Programming

Android Sensor Programming Using basic views Modify MainActivity.java (add bolded lines): //---Button view--- Button btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DisplayToast("You have clicked the Save button"); } }); //---CheckBox--- CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave); checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((CheckBox)v).isChecked()) DisplayToast("CheckBox is checked"); else DisplayToast("CheckBox is unchecked"); } }); Alternative way of adding an event handler <Button android:id="@+id/btnSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/save" android:onClick="btnSaved_clicked"/> 11/6/2019 Android Sensor Programming

Android Sensor Programming Using basic views Modify MainActivity.java (add bolded lines): //---RadioButton--- RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1); radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1); if (rb1.isChecked()) { DisplayToast("Option 1 checked!"); } else { DisplayToast("Option 2 checked!"); } } }); //---ToggleButton--- ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle1); toggleButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((ToggleButton)v).isChecked()) DisplayToast("Toggle button is On"); else DisplayToast("Toggle button is Off"); } }); } private void DisplayToast(String msg) { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); } } 11/6/2019 Android Sensor Programming

Android Sensor Programming Using basic views 11/6/2019 Android Sensor Programming

Android Sensor Programming ProgressBar view The ProgressBar view provides visual feedback about some ongoing tasks, such as when you are performing a task in the background Using Android Studio, create an Android project and name it ProgressBarTest Change 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" > <ProgressBar android:id="@+id/progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" /> </LinearLayout> 11/6/2019 Android Sensor Programming

Android Sensor Programming ProgressBar view Change MainActivity.java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity { static int progress; ProgressBar progressBar; int progressStatus = 0; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = 0; progressBar = (ProgressBar) findViewById(R.id.progressbar); progressBar.setMax(200); // default is 100 11/6/2019 Android Sensor Programming

Android Sensor Programming //---do some work in background thread--- new Thread(new Runnable() { public void run() { //---do some work here--- while (progressStatus < 100) { progressStatus = doSomeWork(); //---Update the progress bar--- handler.post(new Runnable() { public void run() { progressBar.setProgress(progressStatus); } }); } //---hides the progress bar--- handler.post(new Runnable() { public void run() { //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE--- progressBar.setVisibility(View.GONE); } }); } //---do some long running work here--- private int doSomeWork() { try { //---simulate doing some work--- Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return ++progress; } }).start(); } } 11/6/2019 Android Sensor Programming

Android Sensor Programming ProgressBar Styles Widget.ProgressBar.Horizontal Widget.ProgressBar.Small Widget.ProgressBar.Large Widget.ProgressBar.Inverse Widget.ProgressBar.Small.Inverse Widget.ProgressBar.Large.Inverse 11/6/2019 Android Sensor Programming

Android Sensor Programming Picker Views TimePicker and DatePicker views: select a data and time The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode Using Android Studio, create an Android project and name it TimePickerTest Modify 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" > <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am all set!" android:onClick="onClick" /> </LinearLayout> 11/6/2019 Android Sensor Programming

Android Sensor Programming TimePicker Views public void onClick(View view) { Toast.makeText(getBaseContext(), "Time selected:" + timePicker.getHour() + ":" + timePicker.getMinute(), Toast.LENGTH_SHORT).show(); } } Modify MainActivity.java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends AppCompatActivity { TimePicker timePicker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setIs24HourView(true); } 11/6/2019 Android Sensor Programming

Android Sensor Programming DatePicker Views Using Android Studio, create an Android project and name it DatePickerTest Modify 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" > <Button android:id="@+id/btnSet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am all set!" android:onClick="onClick" /> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 11/6/2019 Android Sensor Programming

Android Sensor Programming DatePicker Views Modify MainActivity.java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.TimePickerDialog; import android.icu.text.SimpleDateFormat; import android.view.View; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; import java.util.Date; public class MainActivity extends AppCompatActivity { TimePicker timePicker; DatePicker datePicker; int hour, minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setIs24HourView(true); datePicker = (DatePicker) findViewById(R.id.datePicker); } 11/6/2019 Android Sensor Programming

Android Sensor Programming DatePicker Views Modify MainActivity.java private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet( TimePicker view, int hourOfDay, int minuteOfHour) { hour = hourOfDay; minute = minuteOfHour; SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa"); Date date = new Date(); String strDate = timeFormat.format(date); Toast.makeText(getBaseContext(), "You have selected " + strDate, Toast.LENGTH_SHORT).show(); } }; public void onClick(View view) { Toast.makeText(getBaseContext(), "Date selected:" + (datePicker.getMonth() + 1) + "/" + datePicker.getDayOfMonth() + "/" + datePicker.getYear() + "\n" + "Time selected:" + timePicker.getHour() + ":" + timePicker.getMinute(), Toast.LENGTH_SHORT).show(); } } 11/6/2019 Android Sensor Programming

Android Sensor Programming DatePicker Views Homework #10: If you look closely, the TimePicker is not entirely displayed. Fix the problem using a layout we learned in lecture 7 so that nothing is cut off from the screen. 11/6/2019 Android Sensor Programming

Android Sensor Programming ListView List views are views that enable you to display a long list of items. In Android, there are two types of list views: ListView and SpinnerView. Using Android Studio, create an Android project and name it ListViewTest Modify 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" > <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show selected items" android:onClick="onClick"/> <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 11/6/2019 Android Sensor Programming

Android Sensor Programming ListView Modify strings.xml under res/values <resources> <string name="app_name">ListViewTest</string> <string-array name="presidents_array"> <item>Dwight D. Eisenhower</item> <item>John F. Kennedy</item> <item>Lyndon B. Johnson</item> <item>Richard Nixon</item> <item>Gerald Ford</item> <item>Jimmy Carter</item> <item>Ronald Reagan</item> <item>George H. W. Bush</item> <item>Bill Clinton</item> <item>George W. Bush</item> <item>Barack Obama</item> <item>Donald Trump</item> </string-array> </resources> 11/6/2019 Android Sensor Programming

Android Sensor Programming ListView Modify MainActivity.java import android.os.Bundle; import android.app.ListActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { String[] presidents; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lstView = getListView(); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lstView.setTextFilterEnabled(true); presidents = getResources().getStringArray(R.array.presidents_array); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, presidents)); } 11/6/2019 Android Sensor Programming

Android Sensor Programming ListView Modify MainActivity.java public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, "You have selected " + presidents[position], Toast.LENGTH_SHORT).show(); } public void onClick(View view) { ListView lstView = getListView(); String itemsSelected = "Selected items: \n"; for (int i=0; i<lstView.getCount(); i++) { if (lstView.isItemChecked(i)) { itemsSelected += lstView.getItemAtPosition(i) + "\n"; } } Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show(); } 11/6/2019 Android Sensor Programming

Android Sensor Programming SpinnerView The SpinnerView displays one item at a time from a list and enables users to choose from them Create an Android project and name it SpinnerViewTest Modify 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" > <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawSelectorOnTop="true" /> </LinearLayout> 11/6/2019 Android Sensor Programming

Android Sensor Programming SpinnerView Modify strings.xml <resources> <string name="app_name">SpinnerViewTest</string> <string-array name="presidents_array"> <item>Dwight D. Eisenhower</item> <item>John F. Kennedy</item> <item>Lyndon B. Johnson</item> <item>Richard Nixon</item> <item>Gerald Ford</item> <item>Jimmy Carter</item> <item>Ronald Reagan</item> <item>George H. W. Bush</item> <item>Bill Clinton</item> <item>George W. Bush</item> <item>Barack Obama</item> <item>Donald Trump</item> </string-array> </resources> 11/6/2019 Android Sensor Programming

Android Sensor Programming SpinnerView Modify MainActivity.java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity { String[] presidents; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); presidents = getResources().getStringArray(R.array.presidents_array); Spinner s1 = (Spinner) findViewById(R.id.spinner1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, presidents); s1.setAdapter(adapter); 11/6/2019 Android Sensor Programming

Android Sensor Programming SpinnerView Modify MainActivity.java s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { int index = arg0.getSelectedItemPosition(); Toast.makeText(getBaseContext(), "You have selected item : " + presidents[index], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } } 11/6/2019 Android Sensor Programming

Android Sensor Programming Homework #11 Further develop the contact management app you did in homework #2 When there is at least one contact, the main activity could display a list of contacts identified by the full name of each contact. Make sure you use the ListView for the contacts In the main activity, a user could click each row of the contact, a third activity would open to display the contact details 11/6/2019 Android Sensor Programming