Friday, 14 February 2014

Splash Screen


Android splash screen are normally used to show  some kind of progress before the application loads completely.That progress can be getting data from Sqlite database, data from internet or it can be any background process. We can use splash screen for just showing our Application logo

splashscreen.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
  // here background image should be kept under res/drawable folder
    android:background="@drawable/bishop"
    tools:context=".SplashscreenActivity" >

    <TextView
        android:id="@+id/splashtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="16dp"
        android:text="Bishop Heber"
        android:textColor="#FFFAFA"
        android:textSize="40dp" />
</RelativeLayout>

Splash Screen Activity:

package com.solomon.splashscreenexample;

import android.R.color;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.TextView;

public class SplashscreenActivity extends Activity {
//In AndroidMainfest.xml this file should be declared as launcher Activity
//Splashscreen timer is for showing splashscreen for particular time.
private static int SPLASH_TIME_OUT=3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
TextView splashtxt = (TextView)findViewById(R.id.splashtextview);
//Getting ttf(True type font) font style.
//Before we have to copy ttf file in assets folder
Typeface type = Typeface.createFromAsset(getAssets(), "epohistory.ttf");
// setting style to textview
splashtxt.setTypeface(type);
  new Handler().postDelayed(new Runnable(){
     /* Inside this anonymous inner class we are creating a runnable thread and 
      * overriding run method. This anonymous inner class is subclass to Handler class                               */

@Override
public void run() {
/* Creating an Intent to start another activity
 * After starting MainpageActivity  i am closing this (SplashscreenActivity) 
        * Activity.
         */
Intent i = new Intent(SplashscreenActivity.this,MainpageActivity.class);
startActivity(i);
finish();
}
      },SPLASH_TIME_OUT);
   }
}

Main Page Activity:


package com.solomon.splashscreenexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainpageActivity extends Activity{
// In AndroidMainfest.xml file we should declare this Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Creating an linearlayout and setting its orientation as Vertical
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
//Creating an Textview 
TextView mainpagetxt = new TextView(this);
mainpagetxt.setText("Main page TextView");
mainpagetxt.setGravity(Gravity.CENTER);
//setting textview to Linearlayout
ll.addView(mainpagetxt);
this.setContentView(ll);
}

}

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solomon.splashscreenexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solomon.splashscreenexample.SplashscreenActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  // Here we are declaring MainpageActivity       
   <activity
            android:name="com.solomon.splashscreenexample.MainpageActivity">
          
        </activity>
    </application>
</manifest>

Output:

     This screen will appear up to 3 seconds after it will close and another Activity will open




Wednesday, 12 February 2014

Creating ScrollView, Layout, Textview , EditText, Button and CheckBox without using XML file

Dynamic Layouts in Android:

No xml file in this post. we can create everything using Java code except AndroidManifest.xml file. This way of creating Layout  is good comparing to xml file.

Program:

package com.solomon.imagewhex1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this); // creating a ScrollView 
LinearLayout ll = new LinearLayout(this);//creating a LinearLayout
ll.setOrientation(LinearLayout.VERTICAL);// setting LinearLayout as Vertical                                                                                                             orientation
sv.addView(ll);// setting view to scrollview
TextView tv1 = new TextView(this);// creating Textview
tv1.setText("Dynamic TextView");// Setting text to Textview
ll.addView(tv1);
EditText ed1 = new EditText(this);// creating EditText
ed1.setText("Dynamic EditText");
ll.addView(ed1);
Button b1 = new Button(this);//creating Button
b1.setText("Dynamic Buttton");//setting Text to Button
ll.addView(b1);
for(int i=0; i<20; i++){
                 //For every iteration creating a Checkbox and setting text for that Checkbox
CheckBox cb = new CheckBox(this);
cb.setText("Dynamic CheckBox");
ll.addView(cb);
}
this.setContentView(sv);// Finally adding every view to our Activity
}
}

Output:


      

        

Sunday, 8 December 2013

Android Life Cycle of Activity


What is Activity?

 Activities are our application’s presentation layer. The UI of  your application is built around
one or more extensions of the Activity class. Activities use Fragments and Views to layout
and display information, and to respond to user actions. Compared to desktop development,
Activities are equivalent to Forms.

Life Cycle methods of Activity are used to control and manage the resource.
Following methods are available android.app.Activity class.
  •   OnCreate: called when activity is first created.
  •  OnStart: called when activity is becoming visible to the user.
  •   OnResume: called when activity will start interacting with the user.
  •    OnPause: called when activity is not visible to the user.
  •   OnStop: called when activity is no longer visible to the user.
  •   OnRestart: called after your activity is stopped, prior to start.
  •   OnDestroy: called before the activity is destroyed.


Life Cycle Activity XML file:

<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=".Activity_life_cycle" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="106dp"
        android:layout_marginTop="110dp"
        android:text="Finish" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="58dp"
        android:ems="10" />

</RelativeLayout>

Life Cycle Activity .Java File:

package com.solomon.lifecycleactivity;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Activity_life_cycle extends Activity {

public static final String TAG="LifeCycleActivity";
Button finishButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_life_cycle);
finishButton = (Button)findViewById(R.id.button1);
finishButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
finish();
/*When we click this button our program will end and
                                    onPause,OnStop,OnDestroy methods will be called.*/
}
});
 Toast.makeText(getApplicationContext(),"Oncreate",Toast.LENGTH_LONG).show();

//Using Toast we can display some Pop-up messages.
        /*In this program whenever oncreate or other methods are called
                    corresponding Toast message will appear on Emulator.*/
Log.d(TAG,"In Oncreate");

//This string will be displayed  in LogCat.
}

@Override
protected void onDestroy() {
   super.onDestroy();
  Toast.makeText(getApplicationContext(),"OnDestroy",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnDestroy");
}


@Override
protected void onPause() {
   super.onPause();
  Toast.makeText(getApplicationContext(),"OnPause",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnPause");
}


@Override
protected void onRestart() {
     super.onRestart();
     Toast.makeText(getApplicationContext(),"OnRestart",Toast.LENGTH_LONG).show();
      Log.d(TAG,"In OnRestart");
}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
 super.onRestoreInstanceState(savedInstanceState);
   Toast.makeText(getApplicationContext(),"OnRestoreInstanceState",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnRestoreInstanceState");
         // called when the activity is started after it is destroyed by the system.
}

@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"OnResume",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnResume");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

/* When user change orientation of mobile device or Emulator
     this method is called. Current state of Activity is saved when this 
                     method is called. By Pressing Ctrl+F11 we can change the 
                     orientation of Emulator.  
*/
 Toast.makeText(getApplicationContext(),"OnSaveInstanceState",
         Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnSaveInstanceState");
}

@Override
protected void onStart() {
      super.onStart();
     Toast.makeText(getApplicationContext(),"OnStart",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnStart");
}
@Override
protected void onStop() {
  super.onStop();
  Toast.makeText(getApplicationContext(),"OnStop",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnStop");
}

}

AndroidManifest.Xml File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solomon.lifecycleactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solomon.lifecycleactivity.Activity_life_cycle"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Output:








//Like this for every methods of life cycle Activity ,Toast message will appear.

LogCat Message:







   /*If we change the orientation of mobile or Emulator
       OnSaveInstanceState and OnRestoreInstanceState will be called.*/
















































































Saturday, 7 December 2013

Hello World Example

Hello World Example:

1.Layout XML File:

<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=".HelloWorldExample_MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="26dp"
        android:text="Enter Name" />

    <EditText
        android:id="@+id/name_editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name_editText1"
        android:layout_below="@+id/name_editText1"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="30dp"
        android:onClick="welcome"
        android:text="Submit" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="28dp"
        android:text="Welcome" />

</RelativeLayout>
  
   After writing this much code you will get Following UI screen:
   // We dont have to write everything by ourself ,we can do just drag and drop from Palette.


2. .JAVA file for our Activity :

package com.solomon.HelloWorldExample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class HelloWorldExample_MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_example__main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world_example__main, menu);
return true;
}
public void welcome(View view) {
//While clicking button1 this welcome method will call of onclick.
String name;
TextView welcomeTextView;
EditText nameEditText;
nameEditText=(EditText)findViewById(R.id.name_editText1);

//Using EditText we can get values from user
//Here,I am getting a name and storing in nameEditText

welcomeTextView=(TextView)findViewById(R.id.textView2);
//TextView is more like a Label.

name=nameEditText.getText().toString();
//Getting Text from EditText and storing it in name(local variable).

welcomeTextView.setText("Hello"+name+".Welcome to Android World");
//whatever value stored in name is added with 2 more string and stored in WelcomeTextView.
}

}

3.AndroidMainfest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solomon.HelloWorldExample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solomon.HelloWorldExample.HelloWorldExample_MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.Output: