implementing Android WebView

Hi In this tutorial i will give you instructions on implementing webview in Android. Sometimes you may need to load webpages inside your application itself rather than redirecting the user to browser. In this tutorial we will see the commands necessary to display webpages inside the application. First of all create a new project and compile it, Make sure you build.gradle file look similar to this

This application will need internet to download webpages, therefore add the internet permission to your manifest. Your manifest file must look similar to this.

Now Create a relative layout activity  and then add a web view inside the relative layout, your activity should look similar to this.

Then modify your mainActivity .java file, it should look similar to this. Look for the comments inside the code for explanation.

Now compile your project and run. You should be able to see a webpage loaded inside your webview. As shown Below.

Signal R in Android

Recently i developed a Android chatting application for the company called zupportdesk. I went through lot of difficulties since there is a very few online resources available for reference to implement signal R in Android, but somehow i figured it out by putting lot of time and effort on it. Now i thought of sharing what i have learned so that it will help some developers someday.

This tutorial will give you instruction on how to implement signal R in Android. Before getting started i expect you to have some knowledge on Android, Android Services and Signal R, therefore this tutorial is not for beginners. Ok lets get started

What is a signal R

SignalR is a new library used to add real-time web functionality to your applications. Signal R uses technology such as

  • web sockets
  • Event Source,
  • forever frame
  • long polling

Signal R is capable of selecting the best from those four technology depending on your internet connection and your application stability.

Signal R is used in application such as

  • Chatting application
  • Stock market application
  • Real-time gaming

It is also used in variety of sectors where ever the real-time communication is needed. to learn more about signal R visit signalr.net.

Required Libraries

  • gson-2.2.2 –  Download
  • signalr-client-sdk-android – Download
  • signalr-client-sdk – Download

Download these libraries and put it on your libs folder and then add it to your build.gradle as shown below.

Also we must ignore some files in the library

You app will work fine with out excluding these files, but when you upload it to the play store your application supported devices will be zero. Therefore its better to exclude the files now itself.

finally your build.gradle file will look some what similar to this

apply plugin: ‘com.android.application’

 

These type for application consumes huge amount of data and battery life of the device, therefore it is important to choose the best service that supports your application, I am going to use Android Bind service for this example. Because i want the application to run background task only when the application is open on the device.

To do the real time communication the services need to do three task.

  1. It should connect to Proxy hub and the keeps the connection continuously open.
  2. It should subscribe to the hub method , so that it can receive information from hub to the device, whenever the change or a new message is received to the hub.
  3. It should invoke the hub method when a device need to send messages to the hub

Example for hub method subscription are shown below

Example for invoke Hub methods are shown below

Finally You Service should look somewhat similar to this

In you Activity just call the Service

That’s it. if you do this properly you will have the connection between your signal R hub and your Android device. You can Check the connection information on ADT logs. From now on you just have to invoke and subscribe to hub method to send and receive messages.

 

Hope this helps. If you have any questions are suggestions, please feel free to ask. Happy Coding 🙂

Permissions in Android 6.0 API Level 23

One of the important change that was introduced in Android 6.0 (API level 23) is the users grant permissions to apps. With this change all the device that is running Android 6.0 and above will have to request the permissions while the app is running, not when they install the app.

This approach will help the users to decide which permissions are necessary for an app and to decide weather to give that permission or not. Now its all depends on the users hand. It also gives the user more control over the app’s functionality.

for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app’s Settings screen.

System permissions are divided into two sections, normal and dangerous:

  • Normal permissions do not directly risk the user’s privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
  • Dangerous permissions can give the app access to the user’s confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.

Below you can find the list of Normal and Dangerous permissions list.

Normal permissions

Dangerous permissions

 

For more information. Please check out this link.

https://developer.android.com/training/permissions/requesting.html

Android Loading images to Recycler View

In this tutorial i will give you instructions of loading images in a recycler view using Picasso library. One you complete this tutorial your application will look like this.
Screenshot_2016-06-17-10-26-06

This application will list image of the android versions and its version names. OK. lets get started.

1. Create a new project.

Open up your android studio and create a new project with the name Recyclerimage. Note you can give any name you like, but for this tutorial i suggest using the same name.

2.  Create a layout for the recycler view.

Inside your res/layout/ folder create a new file called row_layout.xml . This file will have a simple image view and a text view and copy and paste the below code.

Then add a recycler view to your activity_main.xml as shown in below code. Make sure to change your tools:contex property according to your project package name.

3. Add permissions to Android manifest.

This application need internet connection to download images from URL, therefore it is necessary to add request the internet permission through your AndroidManifest.xml file as shown in the below code.

4. Creating data adapter.

Now create a new java class called DataAdapter.java inside your package and then copy and paste the below code.

Make sure to change the package name according to your project.

Now create another class inside your package and name it as AndroidVersion.java and then copy and paste the blow code.

Make sure to change the package name.

Now lets modify the MainActivity.java class to get the list of images and strings to add it in to the recycler view.

5. Modify compile dependencies in your Build.gradle file.

That’s it. Save the project and run it on your device or emulator. It should work fine. If you have any comments or queries leave it in the comments section below.  Thank you. Happy coding 🙂

Android Https Post and Get

Yesterday, i have done an piratical test for an interview. The task provided is to post a username and password to an https url and also to  get the response results using an https connection in another url. I have successfully completed the task, but it took me around five hours. I wasted lot of time in researching on how to do an https post. Therefore i thought i could share some of the important information, so that it will we helpful for some one. please not i am not going to share the entire solution.

First create an custom socket factory inside your package. Use the below code.

Add this function inside your MainActivity class to create a httpClient using custom socket factory. As shown in the below code.

Then Run an Async task for https post like this.

And if you want to do an https Get request. See the code below

Hope this helps. Feel free to ask any question if you have in the comments section below.Tnx, Happy coding 🙂

Loading images from server in to Android Grid view

In this tutorial, we will be looking at how to load images from server in to android grid view. Once you complete this tutorial, your application will look like this.

Screenshot_2016-06-02-23-44-47

Lets call this application as a Movie Database. This will display list of movies and the name in a grid view.

 

1 . Creating Database

First of all lets create a database content_db. Create a new file named content_db.sql and copy and paste the below code and save it.

Now import the this file in to the database you have created.

2. Creating an API

Now you have the database ready, lets create an API to get all movies using PHP.

Copy the below code and save it in to file named content_db_getAllMovies.php in localhost location ( in my case its /var/www/html/). you can save it in your localhost location.

Make sure to change your database configuration as  per your database credentials, In the above file.

3. Creating an Android application

Now lets create a new android application called MovieDatabase, when you create an new application  in android studio it  will automatically create a new activity called MainActivity. You can  change it to anything you want, but i recommend to leave it as it is for this tutorial.

4. Creating Styles and Drawable

Copy and paste the below code inside /res/values/colors.xml file.

save and close it.

Now create a new file called grid_color_selector.xml inside /res/drawable/ folder and then copy and paste the below code.

save and close it.

5. Creating Layout files

Now create a new file called grid_item_layout.xml in your /res/layout/ folder and copy and paste the below code.

save and close it.

Now open your activity_main.xml which is located in /res/layout/  folder. open the file and copy and paste the below code.

Save and close it.

6. Java classes

create a new java file named GridViewAdapter.java inside your package and copy and paste the below code. make sure to change your package name.

save and close it.

Now create another new file called GridItem.java inside your package and then copy and paste the below code.

save and close it.

Now open you MainActivity.java class and make sure it looks like the below code.

save and close it.

7. Create a second activity

Now we need to create a new activity to be viewed when ever the movie is clicked. So lets create a new activity called DetailsActivity using your android studio.

8. Modify Manifest file.

This application need a internet connection to get data from the server, therefore it is necessary to add that permission to the manifest file. As shown in the below code.

9. Gradle Build

It is important to tell the gradle how to build the application. Note this application is using HttpClient package which is no more supported in android studio. Therefore its is very important how you set the build.gradle file.

Make sure your build.gradle file looks exactly as shown in the below code snippet.

make sure to set your applicationId. That’s it. compile the application and run it on your device or emulator. It should work fine.

10. Happy coding.

Feel free to tell me your comments and feedback’s below in the comments section. Thank you. 🙂

Unable to compile android project – dependency issue

Most of the time i see this issue on my android studio, while compling the application

It is because the android studio couldnot find some of the  libraries. Make sure you have them installed all the necessary libraries. 

Here are some dependencies requirement for Android sdk versions above 21. You can modify this in your build.gradle file.