Tuesday, April 10, 2018

Android Splash Screen (Part-1)

Android Splash Screen is used to show the logo of your company or the apps logo. But it can also be used to acquire some background data or information regarding your app. It is also used to show user some kind of progress before the app loads completely. Unfortunately in android we don’t have any inbuilt mechanism to show splash screen compared to iOS. In this tutorial we are going to learn how to implement splash screen in your android application.

How can a splash screen be used while loading your apps data in background processes? Here are some of the ways using which you can use splash screen in background.
1.) Use it to showcase your app logo (can be dismissed using a timer) 
2.) Use it to make a network call and preparing app
3.) Use it to download the data and store it in Sqlite Database
4.) Use it to download required images
5.) Use it to fetch and parse json/xml data
6.) Use it to send device's information like device id and registering the device to your server



In this tutorial I’ll be covering implementation of splash screen in two different scenarios with two different styles. One of the scenario is showing splash screen using a timer and second is showing splash screen when making network http calls which takes some time to fetch required information. This both have one same way to show the splash screen. Another way of displaying splash screen it by using a theme style in android.
In order to implement splash screen we are going to create a separate activity for splash and once it closes we launch our main activity.

1. Android Splash Screen Using Timer

Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. (I use main activity's name as MainActivity.java).  For Splash Screen we will create a separate activity. Create a new class in your package and name it as SplashActivity.javaOpen your your AndroidManifest.xml file and make your splash screen activity as Launcher activity.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.splashscreendemo"

android:versionCode="1"

android:versionName="1.0" >

<application

    android:allowBackup="true"

    android:icon="@drawable/ic_launcher"

    android:label="@string/app_name"

    android:theme="@style/AppTheme" >

    <!-- Splash screen -->

    <activity

        android:name="com.android.splashscreendemo.SplashActivity"

        android:label="@string/app_name"

        android:screenOrientation="portrait"

        android:theme="@android:style/Theme.Black.NoTitleBar" >

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>



    <!-- Main activity -->

    <activity

        android:name="com.android.splashscreendemo.MainActivity"

        android:label="@string/app_name" >

    </activity>

 </application>

</manifest>

Create a layout file for splash screen under res ⇒ layout folder. I named the layout file as activity_splash.xml. This layout normally contains your app logo or company logo.

activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/gradient_bg" >

<ImageView

    android:id="@+id/logo"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerInParent="true"

    android:src="@drawable/app_logo" />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginBottom="10dp"

    android:textSize="16sp"

    android:textColor="#454545"

    android:gravity="center_horizontal"

    android:layout_alignParentBottom="true"

    android:text="Splash Screen Demo" />

    </RelativeLayout>

Add the following code in 
SplashActivity.java activity. In this following code a handler is used to wait for specific time and once the timer is out we launch main activity.
SplashActivity.java

package com.android.splashscreendemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends Activity {

    // Splash screen timer
    private static int TIME_OUT_SPLASH = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new Handler().postDelayed(new Runnable() {
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo
             */

            @Override
            public void run() {
                 // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
                // close this activity
                finish();
            }
        }, TIME_OUT_SPLASH);
    }
}

Run the application, you will see the splash screen for 3 sec and then your main activity will be launched. This was the first scenario for creating the Splash Screen in Android Studio. Now, the next scenario which is creating a Splash Screen with a Network call will be explained in the next post. 

Sunday, January 8, 2017

Android View Binding Library

Today, I'm going to post about view binding tutorial in android. So, first of all, what is view binding? View binding is a term which connects your code objects with all the views, and these views are displayed to the end user for showing the data. For example, you use

TextView title = (TextView) findViewById(R.id.tvSample);
title.setText("Hello !!!");

This is the basic view binding that we do in android. So far so good to work with it when you have only one or two views to display. But what, if you have a user profile filled with 100's of TextView's relating to users detailed info? You will have to write 100's of this lines of code, which is very time-consuming and very tiring work, plus developers are a bit lazy to do this kind of work. To overcome this issue, Mr. Jake Wharton has added a view binding library called Butter Knife. Most of you already know about it and use it in your code. Here's the link to this library.

Here is an example for binding the above TextView with your coding object.
@BindView(R.id.tvSample) TextView title;
Here, @BindView annotation is used to bind the tvSample to title object. With the latest update in the library, you will have to use an Unbinder Object to unbind or remove all the bound views when your app gets destroyed. unbinder.unbind() method to unbind all the views. For initializing it,

 @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    unbinder = ButterKnife.bind(this);
    // TODO Use fields...
  }
onClick listener object binding as follows:
@OnClick(R.id.submit)
public void submit(View view) {
  // TODO submit data to server...
}
This is the basic view binding tutorial to know about this library. Further, we will see how this library helps in recyclerview and all other lists views adapter classes to bind their row layout items with View Objects.

Saturday, February 20, 2016

Creating a Simple Example of using TextView, EditText and a Button

In this post, I'm going to start creating a small project by taking a TextView, EditText and a Button.
First up, you need to create the XML Layout for this three widgets to show in the app. Here is the XML code that you need to write in your activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="parthvora.com.myapplication.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:id="@+id/button"
        android:layout_below="@id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_below="@id/button"
        android:layout_marginTop="20dp" />

</RelativeLayout>

This is the basic layout that will show two things EditText to enter the text and Button to click, if you run the application in the Emulator. For configuring the Emulator please visit AVD Setup. This was just the designing portion, for the click of the button to work, we need to jump to the src folder and go to MainActivity.java file and edit it with the following code to make it click and do something when the button gets clicked.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText name;
    TextView text;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.editText);
        text = (TextView) findViewById(R.id.textView);
        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText(name.getText());
            }
        });
    }
}

So, this is the code file where we have declared the EditText, TextView and Button. In onCreate method we have initialized them using findViewById method and we have added a setOnClickListener method on the Button that is used to get the click event when user wants to click on the button in your app. Now, on click of the button we want to set the text from EditText and set it in the TextView which will be seen below the button in our app when the user will click on the button. setText is the method using which we can set the text and getText method is used to get the text from the EditText which will be entered by the user.

Tuesday, February 16, 2016

Keeping in Mind the Following Stuff in Android Studio

Take a look at this image. Here you will have to remember some things that will help you to create an Android Application.


 Here you can see there is a flow of files which is located in the app directory, wherein all of the important files are situated. The first of them is the Android Manifest file where all your android app settings are written and added. Also, you can specify Android Permissions and can change your Android Applications name here. Android Permission is the list of all available permission that you want to use in your app from the Android System. Next is the java folder in which all your java source code files will be located. And last is the res folder where all the resource linking to your android app is stored here. In res->layout, all your layouts are stored which are written in XML. In res->values you can add colors.xml for themes and string.xml for common strings that will be used in your app.

Monday, February 15, 2016

Understanding Android Activity Life Cycle

Firstly, to start creating your android app you must be clear with almost all of the basics that it takes for any developer to develop an android app. The basics to create your android app includes JAVA Programming, Understanding of Object Oriented Programming (OOPs) concepts, XML files and a good memory to remember these. If you want to become a good android developer for yourself, then these are the minimum required skills to take off. You can visit Programming Simplified also.

Lets first start with the Android Activity. Activity according to me is a web page as in a website. You can see the page and can interact with it to do something. Similarly, in android term, Activity is an application component that helps the users to interact with it using a screen for doing something. Examples: Take a photo, Call someone, Read Email, View a Map, etc.

How does it work actually? There is a life cycle attached to each and every activity through which it responds to and works accordingly. Let's say, for an example, you want to run your own business. For being able to run it, you will first of all start from somewhere. Maybe it could be like collecting money first up or getting a loan from a bank, joining your family business and learning it. Here in android, the activity starts running the same as to above mentioned lines. It initializes all of its components by running its onCreate() method.



 Before the activity is shown to the user it passes to three methods onCreate(), onStart(), onResume(). After the completion of onStart() and onResume() method, the activity is seen by the user but he/she can interact with it after onResume() method. At onPause(), the activity becomes invisible and the user cannot interact with it because another activity is working in the foreground. Then comes onStop() method where the activity becomes hidden from the user and it goes in the background where the code cannot execute for it. Last is the onDestroy() method where the activity is destroyed or stopped by the user.