Friday, 28 June 2013

TabHost Tutorial(Android)

Here i am going to see about simple tabhost example. To use TabHost in android we need to extends the main class with TabActivity.
To display a Tab Bar, we need 3 things.


  1. TabHost ( main container of tab view )
  2. TabWidget ( used to navigate between tabs )
  3. FrameLayout ( for tab content )
Files Used:-
  1. TabBarExample.java ( A simple TabHost contains 2 tabs )
  2. FirstTab.java ( first tab bar content )
  3. SecondTab.java ( second tab bar content )
  4. tab.xml ( tabhost design in xml file )
tab.xml
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost">
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
</LinearLayout>
</TabHost>
[/sourcecode]
TabBarExample.java
This is main activity class this should extends with TabActivity to use TabHost.
package com.android.tab;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabBarExample extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
/** TabHost will have Tabs */
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
/** TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab. */
/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
/** TabSpec setIndicator() is used to set name for the tab. */
/** TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator("First Tab Name").setContent(new Intent(this,FirstTab.class));
secondTabSpec.setIndicator("Second Tab Name").setContent(new Intent(this,SecondTab.class));
/** Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
}
}
FirstTab.java
This class contains content for First Tab.
package com.android.tab;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class FirstTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
}
}
SecondTab.java
This class contains content for Second Tab.

package com.android.tab;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondTab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Second Tab");
setContentView(textView);
}
}
AndroidManifest.xml
You need to add the below lines in your AndroidManifest.xml inside the application tag.
<activity android:name=".FirstTab" />
<activity android:name=".SecondTab" />
That's it :)

Thursday, 27 June 2013

Loading Welcome Splash / Spash Screen Example(Android)

In android, when we build an application we may need to display a splash screen ( welcome screen ) for users to intimate some thing & do some other process in background ( like fetching data from DB, Parsing XML , etc.. ).
Splash screen is mainly used in Game Applications. Game application may need to show a splash screen that contains App logo, App name & App author of the game to be displayed for few seconds.
Here we are going to see about a simple splash screen example that displays a textview as splash screen & then move to main activity after the splash screen times up.
splash.xml
Design a splash screen to display.
Splash.xml file used to show as splash screen. It contains a textview with text as "Splash Screen".
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:background="#6B8AAD">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18sp"
android:textStyle="bold" android:textColor="#fff" android:text="Splash Screen"></TextView>
</LinearLayout>
[/sourcecode]
SplashScreenActivity.java
Splash screen activity is used to show splash . Here we use thread to show splash for particular time and then disappear automatically.

package com.android.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashScreenActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/** set time to splash out */
final int welcomeScreenDisplay = 3000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
/**
* Called after splash times up. Do some action after splash
* times up. Here we moved to another main activity class
*/
startActivity(new Intent(SplashScreenActivity.this,
MainScreenActivity.class));
finish();
}
}
};
welcomeThread.start();
}
}
MainScreenActivity.java
This main activity is used to show after the splash times up. From here your Apps start up.

public class MainScreenActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Main Activity");
setContentView(textView);
}
}
That's it.. 

How to handle screen orientation change issue?(android)

As a android developer when we where newbie to development we use to face a big issue
"Handle Screen Orientation Change"
What can this issue cause?
Well it cause hell lot of problem let me list it.
  1. It creates a new activity when ever we change the orientation.
  2. Consider playing audio file,This will reload the player twice and this will play two same song simultaneously.
  3. Causes memory management problem.
  4. Mess up continuity completely.Consider you have a 20 form field and  you change the orientation filling 15 fields,This issue will cause the form to loose all those 15 field data.
So how to solve this issue. After analysis various code we came to know that google has given the solution to problem with a single line code in the Manifest.xml
Add this snippet to the activity attribute to solve this issue.

android:configChanges="orientation"
As that of like this in activity attribute

<activity android:label="@string/app_name" android:configChanges="orientation"
android:name=".com.android">

Now you are completely free from this orientation change problem.

Portrait,Landscape Differeent Layouts(Android)

Today i am going to see about, How to use different layout when screen orientation changes from Portrait to Landscape or Landscape to Portrait.
For example :-
In portrait mode , the option page button will looks like nice when buttons are in one by one position.
But in landscape if we use the same process we need to have scrollview to see all the option button in the page.
To overcome this problem here is a easy solution.
In your res folder create two folders for Landscape 1) drawable-land 2) layout-land
drawable-land folder images is used when the phone orientation in landscape mode.
layout-land folder layout files is used when the phone orientation in landscape mode.

main.xml fopr portrait
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="center">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
</LinearLayout>
main.xml fopr Landscape

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="center">
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button"></Button>
</LinearLayout>
</LinearLayout>


How to set image as wallpaper?(Android)

Today i am going to see about how to set a image as wallpaper in 2 line of code in android.
we can easily set any image from your res folder as a wallpaper in a 2 line of code.
First step is you need to change your image as Bitmap, then use getApplicationContext() to set your wallpaper.That's it!
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ExampleApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ImageView imageView = (ImageView) findViewById(R.id.ImageView01);
imageView.setBackgroundResource(R.drawable.gall_2);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.gall_2));
try {
getApplicationContext().setWallpaper(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
test.xml
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Set as Wallpaper"></Button>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
Add permission in AndroidManifest.xml
<uses-permission android:name="android.permission.SET_WALLPAPER" />



How to make a call to a phone number?(Android)

n Android, we can call any phone number by using Intent.ACTION_CALL. To call a phone number we need to access uses permission.
Here is the code that shows you how to call a particular phone number
public class ExampleApp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:8304600889" )));
}
}
Here the sample phone number is '8304600889'. You can give any phone number here.
Add the below code in your AndroidManifest.XML file below to close the application tag.
<uses-permission android:name="android.permission.CALL_PHONE" />
By using "android.permission.CALL_PHONE" only we can place a call.
The output will look like

Custom Toast Tutorial(Android)

Here i am going to see about how to display a textview in toast using setView() and some more properties of Toast.
Toast is used to display a short message or notification in quick time without any button ( like OK, Cancel ).
Simple Toast
To display a simple toast, we need to makeText for that toast and then call show() to display it.
To use makeText(), we need to send 3 arguments.
1) Context
2) Text to display in toast
3) Duration How much time its need to be show
At last call show() to display toast.
Toast setDuration()
setDuration(int) is used to set display duration of a toast. You need to set duration in int type.
Toast have 2 default integers for duration
1) Toast.LENGTH_SHORT - Display the toast for short time. Used when the notification text is small.
2) Toast.LENGTH_LONG - Display the toast for long time. Used when the notification text is big.
Custom Toast Notification
Custom toast notification is used when we need to display a custom View as toast by using setView().
create a toast and set any view to display in setView().
Here we created a textview with some UI design to display in toast.
/** Creating TextView to display in toast.
* set Background Color, Text Color, Padding & Text for TextView. */
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.GRAY);
textView.setTextColor(Color.BLUE);
textView.setPadding(10,10,10,10);
textView.setText("Textview as Toast");
/** Create a Toast to display a View.
* Here we are going to display a TextView.
* Toast setView() is used to display a View.
* Toast Display Duration is Long. So it will display for long time.
* Toast setGravity() is used to set position to display the toast. */
Toast toastView = new Toast(this);
toastView.setView(textView);
toastView.setDuration(Toast.LENGTH_LONG);
toastView.setGravity(Gravity.CENTER, 0,0);
toastView.show();
Toast setView()
Used to display a view in toast. we can create a view with our won UI & then set it to setView() to display in view.
Toast cancel()
cancel() is used to stop displaying the toast if the toast is displaying.
Toast setGravity()
setGravity() is used to set position to display the toast