SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
Navigation Drawer
by Eakapong Kattiya
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Monday, July 15, 13
by Eakapong Kattiya
Navigation Drawer :Add ActionbarSherlock Library
Source : MyNavigationDrawer.zip
Monday, July 15, 13
by Eakapong Kattiya
Navigation Drawer :Add ActionbarSherlock Library
Monday, July 15, 13
by Eakapong Kattiya
(1)Theme.Sherlock : styles.xml
<resources>
<style name="AppBaseTheme" parent="Theme.Sherlock.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
Change AppBaseTheme in styles.xml
Monday, July 15, 13
by Eakapong Kattiya
(2)Theme.Sherlock :AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
//..
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mydrawerlayout.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Monday, July 15, 13
by Eakapong Kattiya
Navigation Drawer : activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
//..
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF00FF" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Monday, July 15, 13
by Eakapong Kattiya
Navigation Drawer : main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_websearch"
android:icon="@drawable/action_search"
android:showAsAction="ifRoom|withText"
android:title="Search"/>
</menu>
Monday, July 15, 13
import com.actionbarsherlock.app.SherlockFragmentActivity; //import android.app.Activity;
public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{
	 List<Integer> images = new ArrayList<Integer>();
	 List<String> items = new ArrayList<String>();
	 private ListView mDrawerList;
	 private DrawerLayout mDrawerLayout;
	 @Override
	 protected void onCreate(Bundle savedInstanceState) {
	 	 super.onCreate(savedInstanceState);
	 	 setContentView(R.layout.activity_main);
	 	 images.add(android.R.drawable.ic_menu_gallery);
	 	 images.add(android.R.drawable.ic_menu_camera);
	 	 items.add("Earth");
	 	 items.add("Jupiter");
	 	 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
	 	 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // Set Shadow
	 	 mDrawerList = (ListView) findViewById(R.id.left_drawer);
	 	 ListAdapter adapter = new CustomArrayAdapter(this, items, images);
	 	 // Set the adapter for the list view
	 	 mDrawerList.setAdapter(adapter);
	 	 mDrawerList.setOnItemClickListener(this);
if (savedInstanceState == null) {
selectItem(0);
}
	 }
}
by Eakapong Kattiya
Navigation Drawer : MainActivity.java
Monday, July 15, 13
import com.actionbarsherlock.view.Menu; //import android.view.Menu;
public class MainActivity extends SherlockFragmentActivity
implements OnItemClickListener{
//..
@Override
	 public boolean onCreateOptionsMenu(Menu menu) {
	 	 //getMenuInflater().inflate(R.menu.main, menu);
	 	 getSupportMenuInflater().inflate(R.menu.main, menu);
	 	 return true;
	 }
}
by Eakapong Kattiya
Navigation Drawer : MainActivity.java
Monday, July 15, 13
import com.actionbarsherlock.view.Menu; //import android.view.Menu;
public class MainActivity extends SherlockFragmentActivity
implements OnItemClickListener{
//..
	 @Override
	 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	 	 selectItem(arg2);
	 }
	 private void selectItem(int arg0) {
	 	 // update the main content by replacing fragments
	 	 PlanetFragment fragment = new PlanetFragment();
	 	 fragment.setPlanet(items.get(arg0));
	 	
	 	 FragmentManager fragmentManager = getSupportFragmentManager();
	 	 fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
	 	 // update selected item and title, then close the drawer
	 	 mDrawerList.setItemChecked(position, true);
	 	 mDrawerLayout.closeDrawer(mDrawerList);
	 }
}
by Eakapong Kattiya
Navigation Drawer : MainActivity.java
Monday, July 15, 13
import com.actionbarsherlock.view.Menu; //import android.view.Menu;
public class MainActivity extends SherlockFragmentActivity
implements OnItemClickListener{
//..
	 @Override
	 public boolean onOptionsItemSelected(final MenuItem item) {
	 	 // Handle action buttons
	 	 switch (item.getItemId()) {
	 	 case R.id.action_websearch:
	 	 	 // create intent to perform web search for this planet
	 	 	 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
	 	 	 intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
	 	 	 startActivity(intent);
	 	 	 return true;
	 	 default:
	 	 	 return super.onOptionsItemSelected(item);
	 	 }
	 }
}
by Eakapong Kattiya
Navigation Drawer : MainActivity.java
Monday, July 15, 13
public class CustomArrayAdapter extends ArrayAdapter<String> {
	 private List<Integer> images;
	 private List<String> items;
	 private Context context;
	 public CustomArrayAdapter(Context context, List<String> items, List<Integer> images) {
	 	 super(context, R.layout.drawer_list_item, items);
	 	 this.context = context;
	 	 this.images = images;
	 	 this.items = items ;
	 }
	 @Override
	 public View getView(int arg0, View convertView, ViewGroup parent) {
	 	 /* create a new view of my layout and inflate it in the row */
	 	 LayoutInflater inflator = (LayoutInflater) context
	 	 	 	 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 	 View view = inflator.inflate(R.layout.drawer_list_item, null);
	 	
	 	 TextView textView = (TextView) view.findViewById(R.id.cityName);
	 	 textView.setText(items.get(arg0));
	 	
	 	 ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity);
	 	 String packageName = context.getPackageName();
	 	 String imageName = items.get(position).toLowerCase();
	 	 int resId = context.getResources().getIdentifier(imageName,"drawable", packageName);
	 	 imageCity.setImageResource(resId);
	 	 return view;
	 }
}
by Eakapong Kattiya
Navigation Drawer : CustomArrayAdapter.java
Monday, July 15, 13
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dip"
android:background="#222222" >
<ImageView
android:id="@+id/imageCity"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/imageCity"
android:orientation="vertical"
android:paddingLeft="10dp" >
<TextView
android:id="@+id/cityName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="18dp" />
<TextView
android:id="@+id/cityLink"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:textColor="#FFFFFF"
android:textSize="14dp" />
</LinearLayout>
</RelativeLayout>
by Eakapong Kattiya
Navigation Drawer : drawer_list_item.xml
Monday, July 15, 13
by Eakapong Kattiya
List vs ArrayList vs LinkedList
List is interface , not a concrete class.
List cannot be instantiated.
List<String> items = new List<String>() ;
ArrayList is an implementation of List.
List<String> items = new ArrayList<String>();
items.add("Earth");
items.add("Jupiter");
LinkedList is an implementation of List.
List<String> items = new LinkedList<String>();
items.add("Earth");
items.add("Jupiter");
Monday, July 15, 13
by Eakapong Kattiya
ArrayList vs LinkedList
- ส่วนใหญ่แล้วใช้ ArrayList
- ใช้ LinkedList เมื่อข้อมูลใหญ่มากเช่น เกิน 1 ล้านแถวจะทํางานเร็วกว่า
(Big-Oh น้อยกว่าเมื่อมีการเพิ่มข้อมูล)
http://leepoint.net/notes-java/algorithms/big-oh/bigoh.html
Algorithm
array
ArrayList
LinkedList
access front O(1) O(1)
access back O(1) O(1)
access middle O(1) O(N)
insert at front O(N) O(1)
insert at back O(1) O(1)
insert in middle O(N) O(1)
Monday, July 15, 13
by Eakapong Kattiya
Sort ArrayList
List<String> items = new ArrayList<String>();
items.add("Earth");
items.add("Jupiter");
Collections.sort(items); //Ascending Sort
Comparator<String> comparator = Collections.reverseOrder();
Collections.sort(items,comparator); //Descending Sort
Monday, July 15, 13
-Fragment is a chunk of user interface with its own life cycle.
-Fragment must exist within an Activity.
-Interaction with fragments is done through FragmentManager.
-Fragment API was introduced in API 11
-Use SherlockFragmentActivity , SherlockFragment
instead of native Fragment API
by Eakapong Kattiya
Fragment (>API 11)
Monday, July 15, 13
by Eakapong Kattiya
Fragment (>API 11)
Monday, July 15, 13
by Eakapong Kattiya
Navigation Drawer : fragment_planet.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
</RelativeLayout>
Monday, July 15, 13
public class PlanetFragment extends SherlockFragment {
	 private String planet ;
	 public PlanetFragment() {
	
	 }
	 public String getPlanet() {
	 	 return planet;
	 }
	 public void setPlanet(String planet) {
	 	 this.planet = planet;
	 }
	 @Override
	 public View onCreateView(LayoutInflater inflater, ViewGroup container,
	 	 	 Bundle savedInstanceState) {
	 	 View view = inflater.inflate(R.layout.fragment_planet, container, false);
	 	 String packageName = getActivity().getPackageName();
	 	 String imageName = planet.toLowerCase();
	 	 int imageId = getResources().getIdentifier(imageName,"drawable", packageName);
	 	 ImageView imageView = (ImageView)view.findViewById(R.id.image);
	 	 imageView.setImageResource(imageId);
	 	 getActivity().setTitle(planet);
	 	 return view;
	 }
}
by Eakapong Kattiya
Navigation Drawer : PlanetFragment.java
Monday, July 15, 13
ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity);
Drawable image = context.getResources().getDrawable(android.R.drawable.ic_dialog_email);
imageCity.setImageDrawable(image);
by Eakapong Kattiya
ImageView
List<Integer> images = new ArrayList<Integer>();
images.add(android.R.drawable.ic_menu_gallery);
images.add(android.R.drawable.ic_menu_camera);
ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity);
Drawable image = context.getResources().getDrawable(images.get(0));
imageCity.setImageDrawable(image);
ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity);
String packageName = context.getPackageName();
String imageName = items.get(position).toLowerCase();
int resId = context.getResources().getIdentifier(imageName,"drawable", packageName);
imageCity.setImageResource(resId);
Fixed Resource id
Dynamic Resource id
Choose image name
Monday, July 15, 13
by Eakapong Kattiya
Nine-patch Image
Monday, July 15, 13
by Eakapong Kattiya
Nine-patch Image
Monday, July 15, 13
by Eakapong Kattiya
Nine-patch Image
res/drawable/drawer_shadow.9.png
Monday, July 15, 13
by Eakapong Kattiya
Navigation Drawer with ActionBarDrawerToggle
Source : MyNavigationDrawerToggle.zip
Monday, July 15, 13

Más contenido relacionado

La actualidad más candente

Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
Mathias Seguy
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
Jussi Pohjolainen
 

La actualidad más candente (20)

Material Design and Backwards Compatibility
Material Design and Backwards CompatibilityMaterial Design and Backwards Compatibility
Material Design and Backwards Compatibility
 
Implementing cast in android
Implementing cast in androidImplementing cast in android
Implementing cast in android
 
Introduction toandroid
Introduction toandroidIntroduction toandroid
Introduction toandroid
 
Eddystone beacons demo
Eddystone beacons demoEddystone beacons demo
Eddystone beacons demo
 
droidparts
droidpartsdroidparts
droidparts
 
Advancing the UI — Part 1: Look, Motion, and Gestures
Advancing the UI — Part 1: Look, Motion, and GesturesAdvancing the UI — Part 1: Look, Motion, and Gestures
Advancing the UI — Part 1: Look, Motion, and Gestures
 
Android - Working with Fragments
Android - Working with FragmentsAndroid - Working with Fragments
Android - Working with Fragments
 
Navigation Architecture Component
Navigation Architecture ComponentNavigation Architecture Component
Navigation Architecture Component
 
Embracing YUI3 and Frontend Perf
Embracing YUI3 and Frontend PerfEmbracing YUI3 and Frontend Perf
Embracing YUI3 and Frontend Perf
 
Simplified Android Development with Simple-Stack
Simplified Android Development with Simple-StackSimplified Android Development with Simple-Stack
Simplified Android Development with Simple-Stack
 
Google I/O 2021 Recap
Google I/O 2021 RecapGoogle I/O 2021 Recap
Google I/O 2021 Recap
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender android
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
Handling action bar in Android
Handling action bar in AndroidHandling action bar in Android
Handling action bar in Android
 
Mini curso Android
Mini curso AndroidMini curso Android
Mini curso Android
 
Minicurso Android
Minicurso AndroidMinicurso Android
Minicurso Android
 
Android por onde começar? Mini Curso Erbase 2015
Android por onde começar? Mini Curso Erbase 2015 Android por onde começar? Mini Curso Erbase 2015
Android por onde começar? Mini Curso Erbase 2015
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 

Similar a Android basic 4 Navigation Drawer

Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
Alexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
C.T.Co
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahoda
Droidcon Berlin
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 

Similar a Android basic 4 Navigation Drawer (20)

Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
React Native Androidはなぜ動くのか
React Native Androidはなぜ動くのかReact Native Androidはなぜ動くのか
React Native Androidはなぜ動くのか
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developers
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahoda
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and Smartphones
 
Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257
 
Building an app with Google's new suites
Building an app with Google's new suitesBuilding an app with Google's new suites
Building an app with Google's new suites
 
Slightly Advanced Android Wear ;)
Slightly Advanced Android Wear ;)Slightly Advanced Android Wear ;)
Slightly Advanced Android Wear ;)
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
Android crashcourse
Android crashcourseAndroid crashcourse
Android crashcourse
 
List adapter with multiple objects
List adapter with multiple objectsList adapter with multiple objects
List adapter with multiple objects
 
How to React Native
How to React NativeHow to React Native
How to React Native
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Android Bootstrap
Android BootstrapAndroid Bootstrap
Android Bootstrap
 
"Android Data Binding в массы" Михаил Анохин
"Android Data Binding в массы" Михаил Анохин"Android Data Binding в массы" Михаил Анохин
"Android Data Binding в массы" Михаил Анохин
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
 

Más de Eakapong Kattiya (8)

(31 July 2013) iOS Basic Development Day 2 Human interface design
(31 July 2013) iOS Basic Development Day 2 Human interface design (31 July 2013) iOS Basic Development Day 2 Human interface design
(31 July 2013) iOS Basic Development Day 2 Human interface design
 
Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )
 
Android Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTAndroid Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADT
 
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
 
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
(1 July 2013) iOS Basic Development Day 5 - Submit to App Store
 
Iphone developer advance twitter
Iphone developer advance   twitterIphone developer advance   twitter
Iphone developer advance twitter
 
iOS Advance Development - Social Media
iOS Advance Development - Social MediaiOS Advance Development - Social Media
iOS Advance Development - Social Media
 
Iphone developer advance location based
Iphone developer advance location basedIphone developer advance location based
Iphone developer advance location based
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Android basic 4 Navigation Drawer

  • 1. Navigation Drawer by Eakapong Kattiya http://developer.android.com/training/implementing-navigation/nav-drawer.html Monday, July 15, 13
  • 2. by Eakapong Kattiya Navigation Drawer :Add ActionbarSherlock Library Source : MyNavigationDrawer.zip Monday, July 15, 13
  • 3. by Eakapong Kattiya Navigation Drawer :Add ActionbarSherlock Library Monday, July 15, 13
  • 4. by Eakapong Kattiya (1)Theme.Sherlock : styles.xml <resources> <style name="AppBaseTheme" parent="Theme.Sherlock.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> </resources> Change AppBaseTheme in styles.xml Monday, July 15, 13
  • 5. by Eakapong Kattiya (2)Theme.Sherlock :AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" //.. <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mydrawerlayout.MainActivity" android:label="@string/app_name" android:theme="@style/Theme.Sherlock" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> Monday, July 15, 13
  • 6. by Eakapong Kattiya Navigation Drawer : activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" //.. <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF00FF" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#FFFFFF" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout> Monday, July 15, 13
  • 7. by Eakapong Kattiya Navigation Drawer : main.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_settings"/> <item android:id="@+id/action_websearch" android:icon="@drawable/action_search" android:showAsAction="ifRoom|withText" android:title="Search"/> </menu> Monday, July 15, 13
  • 8. import com.actionbarsherlock.app.SherlockFragmentActivity; //import android.app.Activity; public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{ List<Integer> images = new ArrayList<Integer>(); List<String> items = new ArrayList<String>(); private ListView mDrawerList; private DrawerLayout mDrawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); images.add(android.R.drawable.ic_menu_gallery); images.add(android.R.drawable.ic_menu_camera); items.add("Earth"); items.add("Jupiter"); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // Set Shadow mDrawerList = (ListView) findViewById(R.id.left_drawer); ListAdapter adapter = new CustomArrayAdapter(this, items, images); // Set the adapter for the list view mDrawerList.setAdapter(adapter); mDrawerList.setOnItemClickListener(this); if (savedInstanceState == null) { selectItem(0); } } } by Eakapong Kattiya Navigation Drawer : MainActivity.java Monday, July 15, 13
  • 9. import com.actionbarsherlock.view.Menu; //import android.view.Menu; public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{ //.. @Override public boolean onCreateOptionsMenu(Menu menu) { //getMenuInflater().inflate(R.menu.main, menu); getSupportMenuInflater().inflate(R.menu.main, menu); return true; } } by Eakapong Kattiya Navigation Drawer : MainActivity.java Monday, July 15, 13
  • 10. import com.actionbarsherlock.view.Menu; //import android.view.Menu; public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{ //.. @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { selectItem(arg2); } private void selectItem(int arg0) { // update the main content by replacing fragments PlanetFragment fragment = new PlanetFragment(); fragment.setPlanet(items.get(arg0)); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerLayout.closeDrawer(mDrawerList); } } by Eakapong Kattiya Navigation Drawer : MainActivity.java Monday, July 15, 13
  • 11. import com.actionbarsherlock.view.Menu; //import android.view.Menu; public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{ //.. @Override public boolean onOptionsItemSelected(final MenuItem item) { // Handle action buttons switch (item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } } by Eakapong Kattiya Navigation Drawer : MainActivity.java Monday, July 15, 13
  • 12. public class CustomArrayAdapter extends ArrayAdapter<String> { private List<Integer> images; private List<String> items; private Context context; public CustomArrayAdapter(Context context, List<String> items, List<Integer> images) { super(context, R.layout.drawer_list_item, items); this.context = context; this.images = images; this.items = items ; } @Override public View getView(int arg0, View convertView, ViewGroup parent) { /* create a new view of my layout and inflate it in the row */ LayoutInflater inflator = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflator.inflate(R.layout.drawer_list_item, null); TextView textView = (TextView) view.findViewById(R.id.cityName); textView.setText(items.get(arg0)); ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity); String packageName = context.getPackageName(); String imageName = items.get(position).toLowerCase(); int resId = context.getResources().getIdentifier(imageName,"drawable", packageName); imageCity.setImageResource(resId); return view; } } by Eakapong Kattiya Navigation Drawer : CustomArrayAdapter.java Monday, July 15, 13
  • 13. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="80dip" android:background="#222222" > <ImageView android:id="@+id/imageCity" android:layout_width="50dp" android:layout_height="50dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_toRightOf="@id/imageCity" android:orientation="vertical" android:paddingLeft="10dp" > <TextView android:id="@+id/cityName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="18dp" /> <TextView android:id="@+id/cityLink" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="web" android:textColor="#FFFFFF" android:textSize="14dp" /> </LinearLayout> </RelativeLayout> by Eakapong Kattiya Navigation Drawer : drawer_list_item.xml Monday, July 15, 13
  • 14. by Eakapong Kattiya List vs ArrayList vs LinkedList List is interface , not a concrete class. List cannot be instantiated. List<String> items = new List<String>() ; ArrayList is an implementation of List. List<String> items = new ArrayList<String>(); items.add("Earth"); items.add("Jupiter"); LinkedList is an implementation of List. List<String> items = new LinkedList<String>(); items.add("Earth"); items.add("Jupiter"); Monday, July 15, 13
  • 15. by Eakapong Kattiya ArrayList vs LinkedList - ส่วนใหญ่แล้วใช้ ArrayList - ใช้ LinkedList เมื่อข้อมูลใหญ่มากเช่น เกิน 1 ล้านแถวจะทํางานเร็วกว่า (Big-Oh น้อยกว่าเมื่อมีการเพิ่มข้อมูล) http://leepoint.net/notes-java/algorithms/big-oh/bigoh.html Algorithm array ArrayList LinkedList access front O(1) O(1) access back O(1) O(1) access middle O(1) O(N) insert at front O(N) O(1) insert at back O(1) O(1) insert in middle O(N) O(1) Monday, July 15, 13
  • 16. by Eakapong Kattiya Sort ArrayList List<String> items = new ArrayList<String>(); items.add("Earth"); items.add("Jupiter"); Collections.sort(items); //Ascending Sort Comparator<String> comparator = Collections.reverseOrder(); Collections.sort(items,comparator); //Descending Sort Monday, July 15, 13
  • 17. -Fragment is a chunk of user interface with its own life cycle. -Fragment must exist within an Activity. -Interaction with fragments is done through FragmentManager. -Fragment API was introduced in API 11 -Use SherlockFragmentActivity , SherlockFragment instead of native Fragment API by Eakapong Kattiya Fragment (>API 11) Monday, July 15, 13
  • 18. by Eakapong Kattiya Fragment (>API 11) Monday, July 15, 13
  • 19. by Eakapong Kattiya Navigation Drawer : fragment_planet.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:gravity="center" android:padding="32dp" /> </RelativeLayout> Monday, July 15, 13
  • 20. public class PlanetFragment extends SherlockFragment { private String planet ; public PlanetFragment() { } public String getPlanet() { return planet; } public void setPlanet(String planet) { this.planet = planet; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_planet, container, false); String packageName = getActivity().getPackageName(); String imageName = planet.toLowerCase(); int imageId = getResources().getIdentifier(imageName,"drawable", packageName); ImageView imageView = (ImageView)view.findViewById(R.id.image); imageView.setImageResource(imageId); getActivity().setTitle(planet); return view; } } by Eakapong Kattiya Navigation Drawer : PlanetFragment.java Monday, July 15, 13
  • 21. ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity); Drawable image = context.getResources().getDrawable(android.R.drawable.ic_dialog_email); imageCity.setImageDrawable(image); by Eakapong Kattiya ImageView List<Integer> images = new ArrayList<Integer>(); images.add(android.R.drawable.ic_menu_gallery); images.add(android.R.drawable.ic_menu_camera); ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity); Drawable image = context.getResources().getDrawable(images.get(0)); imageCity.setImageDrawable(image); ImageView imageCity = (ImageView) view.findViewById(R.id.imageCity); String packageName = context.getPackageName(); String imageName = items.get(position).toLowerCase(); int resId = context.getResources().getIdentifier(imageName,"drawable", packageName); imageCity.setImageResource(resId); Fixed Resource id Dynamic Resource id Choose image name Monday, July 15, 13
  • 22. by Eakapong Kattiya Nine-patch Image Monday, July 15, 13
  • 23. by Eakapong Kattiya Nine-patch Image Monday, July 15, 13
  • 24. by Eakapong Kattiya Nine-patch Image res/drawable/drawer_shadow.9.png Monday, July 15, 13
  • 25. by Eakapong Kattiya Navigation Drawer with ActionBarDrawerToggle Source : MyNavigationDrawerToggle.zip Monday, July 15, 13