ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

Similar documents
Database Development In Android Applications

User Interface Development in Android Applications

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

Basic GUI elements - exercises

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Data Persistence. Chapter 10

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

else if(rb2.ischecked()) {

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Create Parent Activity and pass its information to Child Activity using Intents.

Starting Another Activity Preferences

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Manifest.xml. Activity.java

M.A.D Assignment # 1

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar


Simple Currency Converter

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

<uses-permission android:name="android.permission.internet"/>


EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Dynamically Create Admob Banner and Interstitial Ads

Press project on the left toolbar if it doesn t show an overview of the app yet.

ANDROID PROGRAMS DAY 3

Android Programs Day 5

StoppUhr. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1"

android:layout_margintop="5dp" > <EditText android:layout_width="210dp"

Vienos veiklos būsena. Theory

COMP61242: Task /04/18

Fragment Example Create the following files and test the application on emulator or device.

Android Beginners Workshop

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Intents. Your first app assignment

Arrays of Buttons. Inside Android

Android UI Development

Android HelloWorld - Example. Tushar B. Kute,

Introduction to Android Development

Android Application Development. By : Shibaji Debnath

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

Saving application preferences

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

Produced by. Mobile Application Development. Eamonn de Leastar

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

APPENDIX CODE TO STORE THE BUTTON MENU AND MOVE THE PAGE

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

TPCT s College of Engineering, Osmanabad. Laboratory Manual SDL-II. Mobile Application Development (Android) For. Third Year Students (CSE)

Android Data Storage

Practical 1.ListView example

Eng. Jaffer M. El-Agha Android Programing Discussion Islamic University of Gaza. Data persistence

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

Computer Science E-76 Building Mobile Applications

Text Properties Data Validation Styles/Themes Material Design

Tabel mysql. Kode di PHP. Config.php. Service.php

THE CATHOLIC UNIVERSITY OF EASTERN AFRICA A. M. E. C. E. A

Lecture 14. Android Application Development

Android Layout Types

COMP4521 EMBEDDED SYSTEMS SOFTWARE


Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Adapter.

Our First Android Application

Learn about Android Content Providers and SQLite

Android/Java Lightning Tutorial JULY 30, 2018

EMBEDDED SYSTEMS PROGRAMMING Android Services

Mobile Software Development for Android - I397

A-1. Listing Program. CariKata.java. package com.indah.kmp;

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Android Workshop: Model View Controller ( MVC):

android-espresso #androidespresso

Mobila applikationer och trådlösa nät, HI1033, HT2012

LAB MANUAL SITTTR KALAMASSERY SMART DEVICE PROGRAMMING LAB. SITTTR Smart Device Programming Manual Page 1

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Laying Out Controls in Containers

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Accelerating Information Technology Innovation

Android Tutorial: Part 3

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

INTENTS android.content.intent

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Hangman Requirements

Android - JSON Parser Tutorial

// MainActivity.java ; Noah Spenser; Senior Design; Diabetic Breathalyzer

Adaptation of materials: dr Tomasz Xięski. Based on presentations made available by Victor Matos, Cleveland State University.

Meniu. Create a project:

LAMPIRAN PROGRAM. public class Listdata_adiktif extends ArrayAdapter<ModelData_adiktif> {

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

App Development for Smart Devices. Lec #18: Advanced Topics

Time Picker trong Android

EMBEDDED SYSTEMS PROGRAMMING Application Basics

使用 TensorFlow 設計矩陣乘法計算並轉移執行在 Android 上 建國科技大學資管系 饒瑞佶 2017/8

Interaction with Android

Transcription:

ITU- FAO- DOA- TRCSL Training on Innovation & Application Development for E- Agriculture Shared Preferences 11 th - 15 th December 2017 Peradeniya, Sri Lanka Shahryar Khan & Imran Tanveer, ITU Experts

Shared Preferences Agenda Storing Data Retrieving Data Practical

Shared Preferences The SharedPreferences class is used to store primitive data types in a key-value pair. Data stored in this SharedPreferences persists application sessions. Data gets deleted when The application gets uninstalled. User clears the data from application settings Data doesn t get deleted when application is killed or closed. The shared preferences file is stored in shared_pref folder under data/data/{application_package_directory/ Storing Data Following are the steps to write data in preferences. Access the preferences Open preferences file in edit mode Write primitive data in key-value pair form Save the preference file. Retrieving Data Following are the steps to retrieve data from preferences. Access the preferences Call one of the get methods to retrieve data

Create a new Android application project with name SharedPreference. Open the strings.xml file from the values folder of the newly created project and following strings in this file. <string name="title">shared Preferences</string> <string name="save_data">save</string> <string name="load_data">retrieve</string> <string name="saved_name">your name</string> <string name="enter_name">enter your name</string> <string name="save">save</string> <string name="show">show</string> Now open the activity_main.xml file and enter the following code in this file. <?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="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="16dp" android:text="@string/title" android:textsize="20sp" android:textstyle="bold" /> <TextView android:id="@+id/data_to_save" android:layout_alignparentleft="true" android:layout_below="@+id/title" android:layout_marginleft="23dp" android:layout_margintop="63dp" android:textsize="20sp" android:text="@string/save_data" /> <EditText android:id="@+id/enter_data"

android:layout_width="fill_parent" android:layout_below="@+id/title" android:layout_marginleft="20dp" android:layout_marginright="15dp" android:layout_margintop="55dp" android:gravity="left top" android:layout_torightof="@id/data_to_save" android:hint="@string/enter_name" /> <Button android:id="@+id/save" android:layout_below="@+id/data_to_save" android:layout_marginleft="25dp" android:layout_margintop="66dp" android:text="@string/save" /> <TextView android:id="@+id/data_to_load" android:layout_alignparentleft="true" android:layout_below="@+id/save" android:layout_marginleft="23dp" android:layout_margintop="63dp" android:text="@string/load_data" android:textsize="20sp"/> <Button android:id="@+id/show" android:layout_below="@+id/data_to_load" android:layout_marginleft="25dp" android:layout_margintop="36dp" android:text="@string/show" /> <EditText android:id="@+id/load_data" android:layout_width="fill_parent" android:layout_below="@+id/save" android:hint="@string/saved_name" android:layout_torightof="@+id/data_to_load" android:layout_alignbottom="@+id/data_to_load" android:layout_marginleft="20dp" android:layout_margintop="40dp" />

</RelativeLayout> This was the designing part of your application. Now you will write code to add functionality to this design. To do this open the MainActivity.java from the package folder that lies inside the src folder of your project. Paste the following code inside this file. import android.app.activity; import android.content.sharedpreferences; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class MainActivity extends Activity implements View.OnClickListener { private EditText savedata; private TextView retrievedata; private Button savebtn; private Button showbtn; /** Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); retrievedata = (TextView)findViewById(R.id.load_data); savedata = (EditText)findViewById(R.id.enter_data); savebtn = (Button)findViewById(R.id.save); showbtn = (Button)findViewById(R.id.show); savebtn.setonclicklistener(this); showbtn.setonclicklistener(this); showbtn.setenabled(false); if(retrievename().length()>0) showbtn.setenabled(true); @Override public void onclick(view v) { switch(v.getid()){ case R.id.save:

savename("name", savedata.gettext().tostring()); showbtn.setenabled(true); break; case R.id.show: retrievedata.settext(retrievename()); break; private void savename(string key, String value){ SharedPreferences sharedpreferences = getpreferences(mode_private); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putstring(key, value); editor.commit(); private String retrievename(){ SharedPreferences sharedpreferences = getpreferences(mode_private); String saveddata = sharedpreferences.getstring("name", ""); return saveddata; Now run this application and you should get the screen like