Android - Widgets Tutorial

Similar documents
Android - JSON Parser Tutorial

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

Time Picker trong Android

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Android Application Development. By : Shibaji Debnath

SD Module-1 Android Dvelopment

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


EMBEDDED SYSTEMS PROGRAMMING Android Services

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

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

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

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

Android Navigation Drawer for Sliding Menu / Sidebar

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

Mobile Software Development for Android - I397

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

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

Android Programs Day 5

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

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

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Introduction to Android Development

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

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

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

Mobile Technologies JULY 24, 2018

Produced by. Mobile Application Development. Eamonn de Leastar

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

ELET4133: Embedded Systems. Topic 15 Sensors

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

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

Intents. Your first app assignment

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Android HelloWorld - Example. Tushar B. Kute,

Multiple Activities. Many apps have multiple activities

ANDROID PROGRAMS DAY 3

Fragments. Lecture 11

Getting Started With Android Feature Flags

An Overview of the Android Programming

South Africa

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Topics of Discussion

Our First Android Application

Android UI Development

User Interface Development in Android Applications

Tutorial: Setup for Android Development

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Mobile Programming Lecture 1. Getting Started

CMSC 436 Lab 10. App Widgets and Supporting Different Devices

Welcome to Developing for Edge 04/06/2017

Chapter 8 Positioning with Layouts

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

Learning Android Canvas

ORACLE UNIVERSITY AUTHORISED EDUCATION PARTNER (WDP)

LifeStreet Media Android Publisher SDK Integration Guide

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

IPN-ESCOM Application Development for Mobile Devices. Extraordinary. A Web service, invoking the SOAP protocol, in an Android application.

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (Part 1-2) Hello-Goodbye App Tutorial

Upon completion of the second part of the lab the students will have:

Tutorial: Setup for Android Development

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

COMP61242: Task /04/18


CS 193A. Multiple Activities and Intents

Android Development Crash Course

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

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

Mobile Development Lecture 8: Intents and Animation

INTRODUCTION TO ANDROID

Android Apps Development for Mobile Game Lesson 5

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

Android Tutorial: Part 3

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

Data Persistence. Chapter 10

Android Application Development

Basic GUI elements - exercises

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

Notification mechanism

Mobile Health Sensing with Smart Phones

Android permissions Defining and using permissions Component permissions and related APIs

Android DP SDK Integration Guide

CS 4330/5390: Mobile Application Development Exam 1

android-espresso #androidespresso

XML Tutorial. NOTE: This course is for basic concepts of XML in line with our existing Android Studio project.

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

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

TomTom Mobile SDK QuickStart Guide

Android Workshop: Model View Controller ( MVC):

1. Implementation of Inheritance with objects, methods. 2. Implementing Interface in a simple java class. 3. To create java class with polymorphism

Creating a Custom ListView

Created By: Keith Acosta Instructor: Wei Zhong Courses: Senior Seminar Cryptography

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (1 &2 of 3) Hello-Goodbye App Tutorial

Android Basics. Android UI Architecture. Android UI 1

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

Arrays of Buttons. Inside Android

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Transcription:

Android - Widgets Tutorial A widget is a small gadget or control of your android application placed on the home screen. Widgets can be very handy as they allow you to put your favourite applications on your home screen in order to quickly access them. You have probably seen some common widgets, such as music widget, weather widget, clock widget e.t.c Widgets could be of many types such as information widgets, collection widgets, control widgets and hybrid widgets. Android provides us a complete framework to develop our own widgets. Widget - XML file In order to create an application widget, first thing you need is AppWidgetProviderInfo object, which you will define in a separate widget XML file. In order to do that, right click on your project and create a new folder called xml. Now right click on the newly created folder and create a new XML file. The resource type of the XML file should be set toappwidgetprovider. In the xml file, define some properties which are as follows <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minwidth="146dp" android:updateperiodmillis="0" android:minheight="146dp" android:initiallayout="@layout/activity_main"> </appwidget-provider> Widget - Layout file Now you have to define the layout of your widget in your default XML file. You can drag components to generate auto xml. Widget - Java file After defining layout, now create a new JAVA file or use existing one, and extend it withappwidgetprovider class and override its update method as follows. In the update method, you have to define the object of two classes which are PendingIntent and RemoteViews. Its syntax is PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);

In the end you have to call an update method updateappwidget() of the AppWidgetManager class. Its syntax is appwidgetmanager.updateappwidget(currentwidgetid,views); A part from the updateappwidget method, there are other methods defined in this class to manipulate widgets. They are as follows Stt Phương thức & Miêu tả 1 ondeleted(context context, int[] appwidgetids) This is called when an instance of AppWidgetProvider is deleted. 2 ondisabled(context context) This is called when the last instance of AppWidgetProvider is deleted 3 onenabled(context context) This is called when an instance of AppWidgetProvider is created. 4 onreceive(context context, Intent intent) It is used to dispatch calls to the various methods of the class Widget - Manifest file You also have to declare the AppWidgetProvider class in your manifest file as follows: <receiver android:name="exampleappwidgetprovider" > <intent-filter> <action android:name="android.appwidget.action.appwidget_update" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" /> </receiver> Ví dụ thuộc tính trong CSS Here is an example demonstrating the use of application Widget. It creates a basic widget applications that will open this current website in the browser. To experiment with this example, you need to run this on an actual device on which internet is running.

Steps Miêu tả 1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs. 2 Modify src/mainactivity.java file to add widget code. 3 Modify the res/layout/activity_main to add respective XML components 4 Create a new folder and xml file under res/xml/mywidget.xml to add respective XML components 5 Modify the AndroidManifest.xml to add the necessary permissions 6 Run the application and choose a running android device and install the application on it and verify the results. Và đây là nội dung của modified MainActivity.java. package com.example.sairamkrishna.myapplication; import android.app.pendingintent; import android.appwidget.appwidgetmanager; import android.appwidget.appwidgetprovider; import android.content.context; import android.content.intent; import android.net.uri; import android.widget.remoteviews; import android.widget.toast; public class MainActivity extends AppWidgetProvider{ public void onupdate(context context, AppWidgetManager appwidgetmanager,int[] appwidgetids) { for(int i=0; i<appwidgetids.length; i++){ int currentwidgetid = appwidgetids[i]; String url = "http://www.tutorialspoint.com"; Intent intent = new Intent(Intent.ACTION_VIEW); intent.addflags(intent.flag_activity_new_task); intent.setdata(uri.parse(url)); PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main); views.setonclickpendingintent(r.id.button, pending); appwidgetmanager.updateappwidget(currentwidgetid,views); Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show(); } } } Bạn sửa đổi nội dung của res/layout/activity_main.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" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity" android:transitiongroup="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tutorials point" android:id="@+id/textview" android:layout_centerhorizontal="true" android:textcolor="#ff3412ff" android:textsize="35dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="widget" android:id="@+id/button" android:layout_centerhorizontal="true" android:layout_margintop="61dp" android:layout_below="@+id/textview" /> &lr;/relativelayout> Và đây là nội dung của res/xml/mywidget.xml. <?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minwidth="146dp" android:updateperiodmillis="0" android:minheight="146dp" android:initiallayout="@layout/activity_main"> </appwidget-provider> Và đây là nội dung của res/values/string.xml. <resources> <string name="app_name">my Application</string> <string name="hello_world">hello world!</string> <string name="action_settings">settings</string> </resources> Tiếp theo là nội dung của AndroidManifest.xml file. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <receiver android:name=".mainactivity"> <intent-filter> <action android:name="android.appwidget.action.appwidget_update"></action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/mywidget"></meta-data> </receive> </application> </manifest> Chạy ứng dụng application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project's

activity files and click Run icon from the tool bar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application. Select your mobile device as an option and then check your mobile device which will display your default screen Go to your widget section and add your created widget to the desktop or home screen. It would look something like this

Now just tap on the widget button that appears, to launch the browser. But before that please make sure that you are connected to the internet. After pressing the button, the following screen would appear Note. By just changing the url in the java file, your widget will open your desired website in the browser.