SlideShare uma empresa Scribd logo
1 de 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:

Mais conteúdo relacionado

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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.pdfUK Journal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
[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.pdfhans926745
 
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 2024The Digital Insurer
 

Último (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
[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
 
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
 

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: