Showing posts with label App development. Show all posts
Showing posts with label App development. Show all posts

How to create Android Clock Widget

December 25, 2014 Posted by WithU Technologies , , ,

Create an Android Clock Widget


Today we are going to create an Android clock widget with the help of Android Studio IDE. This is a beginner’s Android app development project. Though this is not an app, but to learn the process of android app development, a better start is with android widgets.

Assumptions


Before heading towards the widget development process, we need to ensure that you already completed certain steps which are required to start this project and they are:

  1. Android Studio IDE and SDK tools download and installation.

  2. Basic java knowledge, which can be easily gathered from our post named: Android App Development with hello world project (Though it is not mandatory as we also discuss those java things here)


(If you want to learn about the steps above, then just click on them and they will redirect you to their respective step by step description)

Step 1. Creating a .9png file



  • Open your Adobe Photoshop and create a new image background with following values:

  • Image Height: 1-1.5 CM

  • Image Width : 2-3 CM


And create a simple image like this

  • Go to Android Asset Studio

  • Choose your image and customize it according to your needs.

  • Download the zip file and extract it which contain a res


 

Step 2. Creating a new project in Android studio IDE



  • Open Android Studio IDE.


Quick start window

  • Click on New project and Name it. Select the project location and Click Next.


Minimum sdk

  • Select the minimum SDK. (I recommend to set it up to Android 4.0 Ice Cream sandwich).


add no activity

  • Select Add no activity in the next window and Click next.


Configure widget

  • Name your activity without any space and I recommend to name it with lowercase letters.

  • Click on Finish. Then the Android studio build the project and load the components of the project so it may take few seconds.


Let me remind you that today we are going to create a basic widget which will show you the current time on your Android device Home screen.

Step 3. Editing the Widget layout



  • To create a new widget java class, follow the path given:


Your application name > src > main > java(Right Click on java and Click on NEW) > Widget > app widget.

new widget

  • Name your app widget and Click on Finish. (It automatically creates all the required files which are able to run a widget on your Android device home screen. The limitations of default widget files are:


- They contain default values especially for the layout, which are needed to be recoded.

-  They did not feature the .9png backgrounds, as they are defaulted by some colour values for  background.

-  And Most importantly the widget does not have any function by default, to do. That is the java class is functionless. So, it only demonstrate in the home screen as a colour patch if started.

 

So, first of all we have to edit the layout file. You can edit it according to your wish by changing the text size, colour and also by changing the alignment, gravity and padding of the widget.

  • Open the layout file through the following path:


         ClockWidget\app\src\main\res\layout\clock_widget.xml

(The application name and activity name will vary in your case if you changed both of them during new project creation)

  • Change the android:background by adding the drawable .9png images

  • Now the .9png is to be needed, so we need the file which was downloaded in STEP 1, which contain res folder.

  • Open that res folder and copy all the drawable files.

  • Now, navigate to your project location and then go to the following path in your computer explorer:


                                       ClockWidget\app\src\main\res

 

drawable files

  • Paste all the drawable files inside the res folder of your project.

  • Save all the files in Android studio and re-open the project.


all drawable files

  • Now, you have all sizes of .9png files which are ready to be supported in any android device.

  • You can also edit the values from our project. The layout file code are given below.






 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/widget_margin"
android:background= "@drawable/clockwidget" ><TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/appwidget_text"
android:textColor="#ffffff"
android:textSize="40sp"
android:textStyle="bold|italic"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:shadowColor="#ff6a1aff" /></LinearLayout>

 





  • Now you have to edit the widget update provider file and it is generally named as Yourapplicationname_info

  • Navigate to the following path:


ClockWidget\app\src\main\res\xml\clock_widget_info.xml

  • Change the value of android:updatePeriodMillis="1"


Step 4. Editing the AndroidManifest.xml file

Moving on to our next step where we code the AndroidManifest.xml file, where we declare a broadcast receiver function which will process the Clock widget updates. By default the functions were already created by Android studio widget creator, but still you have to modify and add few items which are can be checked out from the following image or from the code given below.

android manifest
The code is given below which you have to type it by yourself if you really want to learn Android app development.




 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rjchakraborty.clockwidget" ><application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" ><receiver android:name=".ClockWidget"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter><meta-data
android:name="android.appwidget.provider"
android:resource="@xml/clock_widget_info" />
</receiver>
</application></manifest>

 




 

 Step 5. Adding functions to our widget


This is the most important function in the whole process, as by now you are only editing the pre setuped files with default values.But now you have to add functions to the widget to work it as a Digital clock and for that, you have to add a bunch of codes to your java file. Get started.

  • Navigate to your project java file from the following path:


ClockWidget\app\src\main\java\com\example\rjchakraborty\clockwidget\ClockWidget.java

(The application name and activity name will vary in your case if you changed both of them during new project creation)




 

java for clock widget

  •  The code for the java file are given below. So, it would be better if you learn first and type those codes by your own hand. Copy-pasting is a easier way out, but it is not the way to learn.






 


package com.example.rjchakraborty.clockwidget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
* Implementation of App Widget functionality.
*/
public class ClockWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

Timer timer = new Timer();
timer.scheduleAtFixedRate(new time(context, appWidgetManager), 1, 86400000 );
}

private class time extends TimerTask {

RemoteViews remoteviews;
AppWidgetManager appWidgetManager;
ComponentName clockWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

public time(Context context, AppWidgetManager appwidgetmanager) {
this.appWidgetManager = appwidgetmanager;
remoteviews = new RemoteViews(context.getPackageName(), R.layout.clock_widget);
clockWidget = new ComponentName(context, ClockWidget.class);
}

@Override
public void run() {
remoteviews.setTextViewText(R.id.appwidget_text, "TIME  " +format.format(new Date()));
appWidgetManager.updateAppWidget(clockWidget, remoteviews);
}
}}


 




Though the layout used by me is not that good as the app is only created for experimental purpose. You can change the look of the widget by editing the layout file ad also by changing the .9png backgrounds.

 

Clock widget

Optional



  • Navigate to the following path in Android studio:


             ClockWidget\app\src\main\res\values\strings.xml

strings

  • Generally the string file is auto created by default but you can change the names as per your wish, but that was not necessary.


That's solve for this project. Try it and made it more stylish then the clock widget shown here.Feel free to ask us questions and we will be more than happy to help you as much as we can.

 

 

How to download and install Android Studio and SDK tools.

December 22, 2014 Posted by WithU Technologies , , , ,
What is Android?

Basically android is an operating system which generally runs on smartphone and other mobile devices.

Android is a Linux based operating system developed by Google. Android is Unique as Google developed it as a free Operating system and manufactures used them on their devices without any cost. Even anyone can develop android and also the Google play application helps to host your app on Google play and android users will download it from there.

  • Every day more than 1 million new Android devices are activated worldwide.

  • Easily optimize a single binary for phones, tablets, and other devices.

  • 5 billion Downloads a month and growing.

Nowadays Google also developing Android Wear devices which are very portable like Android glasses and watches.



Now let’s come to the topic which is to be discussed here, i.e. Android application development.

  • First of all to install Android studio, you need to install the latest version of Java Development Kit.

For Android application development we need a package which is easily downloadable from the Google Android developer website. The link is given below.

Download android studio

Android Studio package contains:

  • Android Studio IDE

  • Android SDK tools

  • Android 6.0 (Marshmallow) Platform or the latest version which android updated later.

  • Android 6.0 emulator system image with Google APIs and many other system image which you need to run the Android virtual device on your PC.

But even after downloading and installing this package, you need to download other platforms, build tools, images etc.

Installing Android studio IDE



  • First of all you need to unzip the Android studio downloaded package.

  • Following files are to be there inside the package, check them out.

File contains

  • Open the Android Studio folder and double click on android-studio-bundle-(version) setup file.

  • Then follow the setup instructions.

  • Changing the destination directory instructions:

Make a new folder in other than your root directory of operating system and rename it to android studio (Use lowercase letters).

[As in case your computer Operating system crashed and it needed to be formatted, then also the files were still safe with a huge contents on any other directory]

  • Choose that folder during destination folder selection in setup wizard and Click Next.

Extracting files

  • Click Install and setup will start to extract the bundle files.

  • Uncheck the Start Android Studio box and Click Finish


Downloading and Installing the Android SDK tools and Platforms


Before we start the process let me know you that Android SDK is something like a download manager which collects all the required tools and platforms direct from the Google and make the downloading procedure with an ease. You did not have to download each and every files shown in that sdk manager as it depends on the app development motive of the developer. For example if you want to test your app on the older version of android to improve the platform compatibility then you can download the older platform packages. So it depends on you. So, it solely depends on you, but still I made a checklist of downloads which you really needed for a basic app development process.

       Packages needed to be downloaded:

  • Download the latest version of Android SDK tools along with build tools and platform tools.

  • Download Android 4.2.2 – Android 6.0

  • On Extras – Download Android Play services, Google USB Driver, Google Web Driver and Intel x86 Emulator Accelerator, Google Repository, Google Play services.

       Steps to be followed:

As we know that we change the directory path during Android Studio installation. So make sure that the sdk path is also to that directory of android studio.

Quick start window

  • Launch the Android studio IDE and Click on Configure and then Click on SDK manager to open it.

Android sdk manager

  • Check the files which you need for your Android development package as per above suggestions.

  • For emulator it is advisable to download the Intel Atom System image. Intel atom System Image emulator is faster than the ARM based emulator.

  • In case of platforms, there is no need to download the Android TV and Android Wear system image, if you are not going to develop apps for TV and wearable Bands supported by Android OS.

  • In case of build tools, latest version is recommended to install.

Downloading package

  • Click on install Packages after the selection. Accept the license agreement and click Install.

  • One by one the packages were downloaded to their respective directory. This process may take some time, depending upon your Internet speed.

  • After the completion of the download task close the SDK Manager window.

  • Restart the Android studio IDE.

And your Android studio is ready for app development. So enjoy developing new apps and for more detailed instructions on app development.