SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Welcome!
Today we are going
to create a new
Android Application
from scratch.
Create a new
Android Project with
a name ToDoList
If you have problems, look through
previous tutorial.
Modify the main layout (activity_main.xml in /res/layout)
to include ListView, Button and EditText within a
LinearLayout.
<?xml version= "1.0" encoding = "utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Add" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Its important to give Button, EditText and ListView
controls IDs so you can get reference to them in the
code.
<?xml version= "1.0" encoding = "utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<EditText
android:id = "@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item" />
<Button
android:id = "@+id/buttonAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Add" />
<ListView
android:id = "@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Now open your main Activity class (MainActivity.java at
/ src / com. … / ).
Default main activity prob looks like:
package com.example.todolist;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Takes layout from
resource folder we
have made recently.
We will work only with onCreate method. Firstly get
references to the ListView and EditText using
findViewById.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate activity view
setContentView(R.layout.activity_main);
// Get reference to UI
ListView myListView = (ListView)findViewById(R.id.listView);
EditText myEditText = (EditText)findViewById(R.id.editText);
Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
}
Now we need to create ArrayList of Strings to store
each to-do list item. (check out about array list if you are not sure)
And ArrayAdapter to store content in ListView.
(check out about array list if you are not sure)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate activity view
setContentView(R.layout.activity_main);
// Get reference to UI
ListView myListView = (ListView)findViewById(R.id.listView);
EditText myEditText = (EditText)findViewById(R.id.editText);
Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
// Create the array list of to-do items
ArrayList<String> todoItems = new ArrayList<String>();
// Create the array adapter to bind the array to the listview
ArrayAdapter<String> aa;
}
Next step to bind string array with array adapter and
with listView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate activity view
setContentView(R.layout.activity_main);
// Get reference to UI
ListView myListView = (ListView)findViewById(R.id.listView);
EditText myEditText = (EditText)findViewById(R.id.editText);
Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
// Create the array list of to-do items
ArrayList<String> todoItems = new ArrayList<String>();
// Create the array adapter to bind the array to the listview
ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the array adapter to the listView
myListView.setAdapter(aa);
}
Defining array
adapter through
current object,
layout item and
array list.
The final step is to make to-do list functional.
Add onKeyListener to the EditText that listens for a
“D-pad center button” and add item to string array list.
Add final label for
variables used inside
OnKeyListner to make
sure they won't going
to change
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate activity view
setContentView(R.layout.activity_main);
// Get reference to UI
ListView myListView = (ListView)findViewById(R.id.listView);
final EditText myEditText =
(EditText)findViewById(R.id.editText);
final Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
// Create the array list of to-do items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the array adapter to bind the array to the listview
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the array adapter to the listView
myListView.setAdapter(aa);
buttonAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
}
});
}
Run your application.
You should get something
like:
If you have problems check code on github:
(check out, if you are not sure about Github)
Or text me if you found any mistakes or you
want to add something to presentation:

Weitere ähnliche Inhalte

Andere mochten auch (20)

Yourprezi
YourpreziYourprezi
Yourprezi
 
Reliable Drupal Partner
Reliable Drupal PartnerReliable Drupal Partner
Reliable Drupal Partner
 
DOL model notice for exchange
DOL  model notice for exchangeDOL  model notice for exchange
DOL model notice for exchange
 
The asian section collections final
The asian section collections   finalThe asian section collections   final
The asian section collections final
 
Услуги стадиона «Динамо»
Услуги стадиона «Динамо»Услуги стадиона «Динамо»
Услуги стадиона «Динамо»
 
Git
GitGit
Git
 
68788 health care reform health plans overview 2 14-13
68788 health care reform health plans overview 2 14-1368788 health care reform health plans overview 2 14-13
68788 health care reform health plans overview 2 14-13
 
Ainu talk manuscripts - final
Ainu talk   manuscripts - finalAinu talk   manuscripts - final
Ainu talk manuscripts - final
 
HAINUTE BOTEZ
HAINUTE BOTEZ HAINUTE BOTEZ
HAINUTE BOTEZ
 
Dairyfree
DairyfreeDairyfree
Dairyfree
 
Uniforme scolare
Uniforme scolareUniforme scolare
Uniforme scolare
 
Cr chap 4
Cr chap 4Cr chap 4
Cr chap 4
 
Dairyfree
DairyfreeDairyfree
Dairyfree
 
Clink for Card Issuers
Clink for Card IssuersClink for Card Issuers
Clink for Card Issuers
 
TRUSOURI BOTEZ
TRUSOURI BOTEZTRUSOURI BOTEZ
TRUSOURI BOTEZ
 
Github tutorial1
Github tutorial1Github tutorial1
Github tutorial1
 
Wasicunwitkotko
WasicunwitkotkoWasicunwitkotko
Wasicunwitkotko
 
Classroom Management
Classroom ManagementClassroom Management
Classroom Management
 
Yourprezi
YourpreziYourprezi
Yourprezi
 
Foodcombining visuals
Foodcombining visualsFoodcombining visuals
Foodcombining visuals
 

Ähnlich wie Android tutorials8 todo_list

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 ListViewsAhsanul Karim
 
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 Mario Jorge Pereira
 
ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]Somkiat Khitwongwattana
 
Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Johan Nilsson
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in AndroidRobert Cooper
 
View groups containers
View groups containersView groups containers
View groups containersMani Selvaraj
 
How to Use ReorderableListView Widget in Flutter App Development.pptx
How to Use ReorderableListView Widget in Flutter App Development.pptxHow to Use ReorderableListView Widget in Flutter App Development.pptx
How to Use ReorderableListView Widget in Flutter App Development.pptxFlutter Agency
 
How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)Giuseppe Filograno
 
Adopting 3D Touch in your apps
Adopting 3D Touch in your appsAdopting 3D Touch in your apps
Adopting 3D Touch in your appsJuan C Catalan
 

Ähnlich wie Android tutorials8 todo_list (20)

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 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
 
ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]
 
Tutorial basicapp
Tutorial basicappTutorial basicapp
Tutorial basicapp
 
Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011
 
Android App Dev Manual-1.doc
Android App Dev Manual-1.docAndroid App Dev Manual-1.doc
Android App Dev Manual-1.doc
 
Layout
LayoutLayout
Layout
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
List Views
List ViewsList Views
List Views
 
View groups containers
View groups containersView groups containers
View groups containers
 
How to Use ReorderableListView Widget in Flutter App Development.pptx
How to Use ReorderableListView Widget in Flutter App Development.pptxHow to Use ReorderableListView Widget in Flutter App Development.pptx
How to Use ReorderableListView Widget in Flutter App Development.pptx
 
Android list view tutorial by Javatechig
Android list view tutorial by JavatechigAndroid list view tutorial by Javatechig
Android list view tutorial by Javatechig
 
Android 3
Android 3Android 3
Android 3
 
Android crashcourse
Android crashcourseAndroid crashcourse
Android crashcourse
 
List view languages
List view languagesList view languages
List view languages
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 
How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)
 
Android programming basics
Android programming basicsAndroid programming basics
Android programming basics
 
Adopting 3D Touch in your apps
Adopting 3D Touch in your appsAdopting 3D Touch in your apps
Adopting 3D Touch in your apps
 

Mehr von Vlad Kolesnyk

Mech eng presentation
Mech eng presentationMech eng presentation
Mech eng presentationVlad Kolesnyk
 
Android tutorials7 calculator_intro
Android tutorials7 calculator_introAndroid tutorials7 calculator_intro
Android tutorials7 calculator_introVlad Kolesnyk
 
Android tutorials6 run_your_app
Android tutorials6 run_your_appAndroid tutorials6 run_your_app
Android tutorials6 run_your_appVlad Kolesnyk
 
Android tutorials7 calculator
Android tutorials7 calculatorAndroid tutorials7 calculator
Android tutorials7 calculatorVlad Kolesnyk
 
Android tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipseAndroid tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipseVlad Kolesnyk
 
Android tutorials1 install_ide
Android tutorials1 install_ideAndroid tutorials1 install_ide
Android tutorials1 install_ideVlad Kolesnyk
 
Android tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirerAndroid tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirerVlad Kolesnyk
 
Android tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayoutAndroid tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayoutVlad Kolesnyk
 
Android tutorials7 calculator_javaprogramming
Android tutorials7 calculator_javaprogrammingAndroid tutorials7 calculator_javaprogramming
Android tutorials7 calculator_javaprogrammingVlad Kolesnyk
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improveVlad Kolesnyk
 

Mehr von Vlad Kolesnyk (11)

Mobile andwebapps
Mobile andwebappsMobile andwebapps
Mobile andwebapps
 
Mech eng presentation
Mech eng presentationMech eng presentation
Mech eng presentation
 
Android tutorials7 calculator_intro
Android tutorials7 calculator_introAndroid tutorials7 calculator_intro
Android tutorials7 calculator_intro
 
Android tutorials6 run_your_app
Android tutorials6 run_your_appAndroid tutorials6 run_your_app
Android tutorials6 run_your_app
 
Android tutorials7 calculator
Android tutorials7 calculatorAndroid tutorials7 calculator
Android tutorials7 calculator
 
Android tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipseAndroid tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipse
 
Android tutorials1 install_ide
Android tutorials1 install_ideAndroid tutorials1 install_ide
Android tutorials1 install_ide
 
Android tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirerAndroid tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirer
 
Android tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayoutAndroid tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayout
 
Android tutorials7 calculator_javaprogramming
Android tutorials7 calculator_javaprogrammingAndroid tutorials7 calculator_javaprogramming
Android tutorials7 calculator_javaprogramming
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improve
 

Kürzlich hochgeladen

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Kürzlich hochgeladen (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

Android tutorials8 todo_list

  • 2. Today we are going to create a new Android Application from scratch.
  • 3. Create a new Android Project with a name ToDoList If you have problems, look through previous tutorial.
  • 4. Modify the main layout (activity_main.xml in /res/layout) to include ListView, Button and EditText within a LinearLayout. <?xml version= "1.0" encoding = "utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="New To Do Item" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text = "Add" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
  • 5. Its important to give Button, EditText and ListView controls IDs so you can get reference to them in the code. <?xml version= "1.0" encoding = "utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <EditText android:id = "@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="New To Do Item" /> <Button android:id = "@+id/buttonAdd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text = "Add" /> <ListView android:id = "@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
  • 6. Now open your main Activity class (MainActivity.java at / src / com. … / ). Default main activity prob looks like: package com.example.todolist; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } Takes layout from resource folder we have made recently.
  • 7. We will work only with onCreate method. Firstly get references to the ListView and EditText using findViewById. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Inflate activity view setContentView(R.layout.activity_main); // Get reference to UI ListView myListView = (ListView)findViewById(R.id.listView); EditText myEditText = (EditText)findViewById(R.id.editText); Button buttonAdd = (Button)findViewById(R.id.buttonAdd); }
  • 8. Now we need to create ArrayList of Strings to store each to-do list item. (check out about array list if you are not sure) And ArrayAdapter to store content in ListView. (check out about array list if you are not sure) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Inflate activity view setContentView(R.layout.activity_main); // Get reference to UI ListView myListView = (ListView)findViewById(R.id.listView); EditText myEditText = (EditText)findViewById(R.id.editText); Button buttonAdd = (Button)findViewById(R.id.buttonAdd); // Create the array list of to-do items ArrayList<String> todoItems = new ArrayList<String>(); // Create the array adapter to bind the array to the listview ArrayAdapter<String> aa; }
  • 9. Next step to bind string array with array adapter and with listView @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Inflate activity view setContentView(R.layout.activity_main); // Get reference to UI ListView myListView = (ListView)findViewById(R.id.listView); EditText myEditText = (EditText)findViewById(R.id.editText); Button buttonAdd = (Button)findViewById(R.id.buttonAdd); // Create the array list of to-do items ArrayList<String> todoItems = new ArrayList<String>(); // Create the array adapter to bind the array to the listview ArrayAdapter<String> aa; aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); // Bind the array adapter to the listView myListView.setAdapter(aa); } Defining array adapter through current object, layout item and array list.
  • 10. The final step is to make to-do list functional. Add onKeyListener to the EditText that listens for a “D-pad center button” and add item to string array list. Add final label for variables used inside OnKeyListner to make sure they won't going to change @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Inflate activity view setContentView(R.layout.activity_main); // Get reference to UI ListView myListView = (ListView)findViewById(R.id.listView); final EditText myEditText = (EditText)findViewById(R.id.editText); final Button buttonAdd = (Button)findViewById(R.id.buttonAdd); // Create the array list of to-do items final ArrayList<String> todoItems = new ArrayList<String>(); // Create the array adapter to bind the array to the listview final ArrayAdapter<String> aa; aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); // Bind the array adapter to the listView myListView.setAdapter(aa); buttonAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub todoItems.add(0, myEditText.getText().toString()); aa.notifyDataSetChanged(); myEditText.setText(""); } }); }
  • 11. Run your application. You should get something like:
  • 12. If you have problems check code on github: (check out, if you are not sure about Github) Or text me if you found any mistakes or you want to add something to presentation: