CS240: Advanced Programming Concepts

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Advertisements

Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Cosc 5/4730 Material Design Spec’s and Toolbar.. Material Design First and for most, Material Design is a spec used by android to have app look similar.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Mobile Programming Lecture 6
Social Media Apps Programming Min-Yuh Day, Ph.D. Assistant Professor Department of Information Management Tamkang University
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Create Navigation Drawer Team 2 Zhong Wang Jiaming Dong Philip Wu Lingduo Kong.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Android - Broadcast Receivers
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Styles, Dialog Boxes, and Menus. Styles Allow creation of a common format – placed in res/values/styles.xml – file name is incidental Can be applied.
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
ListView and ExpandableListView
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Cosc 4735 Activities, fragments, callbacks/listeners/interfaces.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Fragments and Menus Chapter 4 1. Objectives Learn three different types of menus: options, context, and popup Learn to configure the ActionBar and Toolbar.
Cosc 5/4730 RecyclerView And more..
Lab7 – Appendix.
Introduction to android
Android Programming - Features
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
CS499 – Mobile Application Development
several communicating screens
GUI Programming Fundamentals
Android – Event Handling
Mobile Computing With Android ACST 4550 Intro to Android, Part 1
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.
Mobile Application Development BSCS-7 Lecture # 11
ANDROID UI – FRAGMENTS UNIT II.
CIS 470 Mobile App Development
Android Programming Lecture 6
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CMPE419 Mobile Application Development
Chapter 1: Basics of Android, First App: HelloAndroid
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
Activities and Intents
Android Developer Fundamentals V2 Lesson 4
CIS 470 Mobile App Development
Android Developer Fundamentals V2
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Project Structure, App Resources and Event Handling
Adding Components to Activity
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
CMPE419 Mobile Application Development
CMPE419 Mobile Application Development
Activities and Fragments
CS 240 – Advanced Programming Concepts
Chapter 5 Your Second Activity.
Android Application Model I
Mobile Programming Broadcast Receivers.
Android Sensor Programming
Android Sensor Programming
CIS 470 Mobile App Development
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

CS240: Advanced Programming Concepts Week 15 Screencast 1

Topics Recyclerview (finish up) Expandable Recyclerview Toolbars ViewPager * Dialogs* *not discussed please see chapters 11 and 12 of our Android Text

RecyclerView 3 Principle Concerns Methods of Concern RecyclerView.ViewHolder - EXTEND RecyclerView.Adapter – EXTEND The “stuff” to be displayed Methods of Concern RecyclerView.ViewHolder Interface methods – i.e. onClick(View) Necessary custom methods RecyclerView.Adapter<MyType> Constructor? MyType onCreateViewHolder(ViewGroup, int) void onBindViewHolder(MyType, int) int getItemCount() *ViewHolder Possibly implements 1 or more interfaces. i.e. View.OnClickListener *MyType is a subclass of RecylerView.ViewHolder

Adding a RecyclerView

<. xml version="1. 0" encoding="utf-8". > <android. support <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/crime_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> First we will define our recyclerview in an xml layout file (this is fragment_crime_list.xml file from the crime fragment example project)

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_crime_list, container, false); mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view); mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); . . . return view; } Next we inflate that layout within the onCreateView method of the fragment that will host it. Here we have also set the layout manager to be an instance of the LinearLayoutManager which will give us a nice linear list of views on the screen…

Implementing the Adapter and ViewHolder

private class CrimeAdapter extends RecyclerView private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> { private List<Crime> mCrimes; public CrimeAdapter(List<Crime> crimes) { mCrimes = crimes; } @Override public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); View view = layoutInflater.inflate(R.layout.list_item_crime, parent, false); return new CrimeHolder(view); } @Override public void onBindViewHolder(CrimeHolder holder, int position) { Crime crime = mCrimes.get(position); holder.bindCrime(crime); } @Override public int getItemCount() { return mCrimes.size(); } } Finally the recyclerView requires us to extend the RecyclerView.Adapter class. Extending this generic class requires that we specify the type of ViewHolder that we are working with… *mention the list of crimes – this adapters method of accessing the data that must be bound to the ViewHolders at runtime *discuss the three methods overridden here…

<. xml version="1. 0" encoding="utf-8" <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools"> <CheckBox android:id="@+id/list_item_crime_solved_check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="4dp" android:textSize="40dp"> </CheckBox> <TextView android:id="@+id/list_item_crime_title_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/list_item_crime_solved_check_box" android:textStyle="bold" android:padding="4dp" tools:text="Crime Title" android:textSize="40dp"> </TextView> <TextView android:id="@+id/list_item_crime_date_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/list_item_crime_solved_check_box" android:layout_below="@id/list_item_crime_title_text_view" android:textStyle="bold" android:padding="4dp" tools:text="Crime Date" android:textSize="40dp"> </TextView> </RelativeLayout> This is the list_item_crime view layout… (i.e. the layout our viewHolders will work with)

private class CrimeHolder extends RecyclerView private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private Crime mCrime; private TextView mTitleTextView; private TextView mDateTextView; private CheckBox mSolvedCheckBox; public CrimeHolder(View itemView) { super(itemView); mTitleTextView = (TextView)itemView.findViewById(R.id.list_item_crime_title_text_view); mDateTextView = (TextView)itemView.findViewById(R.id.list_item_crime_date_text_view); mSolvedCheckBox = (CheckBox)itemView.findViewById(R.id.list_item_crime_solved_check_box); itemView.setOnClickListener(this); } public void bindCrime(Crime crime) { mCrime = crime; mTitleTextView.setText(mCrime.getTitle()); mDateTextView.setText(mCrime.getDate().toString()); mSolvedCheckBox.setChecked(mCrime.isSolved()); } @Override public void onClick(View v) { Intent intent = CrimePagerActivity.newIntent(getActivity(),mCrime.getId()); startActivityForResult(intent, REQUEST_CRIME); } } As noted earlier, our recyclerview needs ViewHolders to display, and it is our responsibility to define these viewholders… In this example, our ViewHolder class (named CrimeHolder) also implements View.OnClickListener – so it must override the onClick() method, allowing our ViewHolder subclass to respond to click events…

Expandable Recyclerview https://www.bignerdranch.com/blog/expand-a-recyclerview-in-four-steps/ This is a fantastic demo – it would be tough to do a better job. Please walk yourself through this demo, it is an extension of the CriminalIntent application. Enjoy!

Toolbars Implemented in Android 5.0 and above, but the AppCompat library enables us to support toolbars back as far as Android 2.1 AppCompatActivity extends FragmentActivity… *Made pretty easy for us* … so we can use this class pretty broadly, including in places where we need to manage fragments.

Enabling the Toolbar 1) Add the AppCompat library as a dependency to your app… (check) 2) Utilize an application theme that displays the action bar Theme.AppCompat.Light.DarkActionBar 3) Make all activities a subclass of AppCompatActivity

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".CrimeListActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".CrimePagerActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" android:parentActivityName=".CrimeListActivity"> </activity> From AndroidManifest.xml and styles.xml (within the values folder) <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style> <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar"></style> </resources>

Define the menu… Right-click on res, select New->Android resource file. Change type to ‘Menu’, give it a name… Edit the xml file to populate the menu… … this will generate an xml file containing an empty menu

fragment_crime_list.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item_new_crime" android:icon="@drawable/ic_menu_add" android:title="@string/new_crime" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/menu_item_show_subtitle" android:title="@string/show_subtitle" app:showAsAction="ifRoom"/> </menu> See chapter 13 for more detail concerning drawable assets…

Create the menu in the app @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_crime_list, menu); . . } First we need to let the fragment manager know that our fragment has an options menu and needs to respond to the onCreatOptionsMenu() method (when called on the activity) – i.e. we are propagating this method call… Within onCreateOptionsMenu we inflate the menu and perform any other setup logic that is required

@Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.menu_item_new_crime: Crime crime = new Crime(); CrimeLab.get(getActivity()).addCrime(crime); Intent intent = CrimePagerActivity.newIntent(getActivity(), crime.getId()); startActivity(intent); return true; case R.id.menu_item_show_subtitle: mSubtitleVisible = !mSubtitleVisible; getActivity().invalidateOptionsMenu();//trigger a redraw of the menu... updateSubtitle(); return true; default: return super.onOptionsItemSelected(item); } } Responding to touch events in the menu is made easy by simply overriding the onOptionsItemSelected() method. Within this method we can determine which item was selected and respond accordingly. In this case we are retrieving the item’s resource ID number and using this value to drive a switch statement. In the case of the ‘new item’ option being selected we are creating a new crime object and then starting the CrimePagerActivity (An advancement of the simple CrimeActivity) to populate the details of the crime entry.

A note on “hierarchical navigation” Easy to enable, simply add a parentActivityName attribute in the AndroidManifest.xml file Allows us to “jump” to a particular activity within the application – overriding the behavior of the activity stack Adjusts the stack as appropriate: if the desired activity exists on the stack then all activities up to and including that activity will be popped of the stack and A NEW INSTANCE of the activity will be created… <activity android:name=".CrimePagerActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" android:parentActivityName=".CrimeListActivity"> </activity>

Hierarchical Navigation Figure 13.13 from: Phillips, Bill; Stewart, Chris. Android Programming: The Big Nerd Ranch Guide (2nd Edition) (p. 249). Pearson Education. Kindle Edition.