Friday, 25 October 2013

Simple Spinner with Dynamic Data

Hi!


   Some time we may facing problem to load the dynamic data on android spinners.

I give you a very simple way to load the spinner with dynamic data by using normal ArrayAdapter<String>.

Create a Spinner on your xml file


  • For Static Data:



if you wants using static data you can use the  android:entries
and cal the array data from your string.xml like,


<string-array name="project">
Select
<item>Project_100</item>
<item>Project_101</item>
    </string-array>



<Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/project" />


  • For Dynamic Data:



if you wants to load the data dynamically fallow the code below,

Call the spinner normally in your xml


<Spinner
            android:id="@+id/spinner2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />




  1. In your Activity class declare an ArrayAdapter<String> for custom spinner,
  2. Create a List<String> for keep your date as list,
  3. Set the List on your ArrayAdapter<String>,
  4. set the Adapter on your Spinner
package dcs.raj.timesheet;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.app.Activity;

public class EditTimeSheet extends Activity {

Spinner pjt_spnr;
ArrayAdapter<String> pjt_sp_adoptr;
List<String> pjt_sp_list;

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

pjt_sp_list = new ArrayList<String>();
pjt_sp_list.add("Project_1");
pjt_sp_list.add("Project_2");
pjt_sp_list.add("Project_3");
pjt_sp_list.add("Project_4");
pjt_sp_list.add("Project_5");
pjt_spnr = (Spinner) findViewById(R.id.spinner2);
pjt_sp_adoptr = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, pjt_sp_list);
pjt_sp_adoptr
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
pjt_spnr.setAdapter(pjt_sp_adoptr);
}

}

Thats All..,

Thank You!


Please Leave Your Comment..,




Custom Tab Background Image

Hi!



          Just i am going to show a simple task how to change the background of Tab in android.


Create Your normal xml file for TabHost


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</TabHost>



  • create different images / background for on mouse over, click & normal and save it on drawable folder.
  • write selector for on click by using those images,




<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/tab_bl_cent" android:state_pressed="true"></item>
    <item android:drawable="@drawable/tab_wt_cntr" android:state_focused="true"></item>
    <item android:drawable="@drawable/tab_wt_cntr"></item>

</selector>




  • In your main activity class extend by using TabActivity, Then write your normal activity code 
  • Call the child position and set the background for it by using your selector of xml




package dcs.raj.timesheet;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class Main_Tab extends TabActivity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__tab);
// Show the Up button in the action bar.
TabHost thst = getTabHost();

TabSpec ts_project = thst.newTabSpec("Project");
ts_project.setIndicator("Project",getResources().getDrawable(R.drawable.ic_launcher));
Intent i_project = new Intent(this, Project.class);
ts_project.setContent(i_project);

TabSpec ts_Edit_ts = thst.newTabSpec("Edit Time Sheet");
ts_Edit_ts.setIndicator("Edit Time Sheet",getResources().getDrawable(R.drawable.ic_launcher));
Intent i_Time_sheet = new Intent(this, EditTimeSheet.class);
ts_Edit_ts.setContent(i_Time_sheet);

TabSpec ts_Time_sheet = thst.newTabSpec("View Time Sheet");
ts_Time_sheet.setIndicator("View Time Sheet", getResources().getDrawable(R.drawable.ic_launcher));
Intent i_Edit_ts = new Intent(this, ViewTimeSheet.class);
ts_Time_sheet.setContent(i_Edit_ts);

thst.addTab(ts_project);
thst.addTab(ts_Edit_ts);
thst.addTab(ts_Time_sheet);

thst.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_center_selector);
thst.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_center_selector);
thst.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.tab_center_selector);
}

}


Now you can check with your device..,


Thank You!




Wednesday, 23 October 2013

WebView

Hi!

     We can write both Android Standalone & Web applications,in this blogger i am going to explain about the web application only especially by calling the URL directly.

There are two ways to create the web application in android

  1. Directly Call the URL By using WebView ,
  2. By using HTML5, PhoneGap we can create the Web Applications.


In android we are using WebView to make executable Android application for our websites.

It is very simple to do it.


  • Create an Object for WebView,
  • Assign setting for like JavaScript Enable,Zooming Controlling  ,etc..,
  • load your url to WebView object
  • keep to manage the history if their is any previous page,
  • in your XML create the WebView,

Java Code:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Pets extends Activity {
WebView webview;

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webview = new WebView(this);
setContentView(webview);
setProgressBarVisibility(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setUseWideViewPort(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 100);
}
});

webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description,
Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://petsfacebook.in/index.php");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webview.canGoBack() == true) {
webview.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}


XML Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Pets" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 In Your Android Manifest don't forget to give permission for Internet connection "android.permission.INTERNET"



Thank You!

Generate the Google Map API key for Release Too

Hi!


       Some of us facing problem to receive the Google map API key for accessing the Google map in Android.

I have explain below in step by step for Windows users!


  1. we have to generate the Google API Account,
  2. In "Services" have to switch on the Google Maps Android API v2
  3. Create a Client ID for installed applications 
  4. While try to create an Android Key for API Project it will ask SHA1 (Fingerprint),
    Do same as what i am going to do for receiving the Finger Print

Method - 1
  • Open Your Debug Key Store From Eclipse >> Windows >> Preferences >> Android >> Build >> (Copy Your) Default Debug Key Store
  • Open Your keytool path from "C:\Program Files\Java\jdk1.6.0_23\bin\keytool.exe"
  • Open Your Windows (Console) CMD.exe
  • Now place the below formate to get your Fingerprint 
C:\Users\Administrator>"Your keytool path" -list -v -alias androiddebugkey -keystore "Your Debug Key Store" -storepass android -keypass android

ex:

C:\Users\Administrator>"C:\Program Files\Java\jdk1.6.0_23\bin\keytool.exe" -list
 -v -alias androiddebugkey -keystore "C:\Users\Administrator\.android\debug.keys
tore" -storepass android -keypass android

now you will receive your Unique Fingerprint like

Certificate fingerprints:

MD5 : 00:00:0:00:00:00:00:00:00:00:00:00:00:00:00:00         SHA1: 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
Signature algorithm name: SHA1withRSA

Method - 2

The another one method is

Step -1

open this java path in your console / cmd (should mention your java version correctly)
C:\Program Files\Java\jdk1.6.0_43\bin>
press enter
Step - 2

the andindir is your local dir / folder name , copy and past the below code and press enter

keytool -list -v -keystore C:\Users\andindir\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android

Process - II - (Release Key)

Step -1

open this java path in your console / cmd (should mention your java version correctly)
C:\Program Files\Java\jdk1.6.0_43\bin>
press enter
Step - 2

the andindir is your local xxxxx.jks located path name & xyxyxyxy is your alias, copy and past the below code and press enter

keytool -list -v -keystore C:\ andindir \xxxxx.jks -alias xyxyxyxy

eg: (C:\Program Files\Java\jdk1.8.0_40\bin>keytool -list -v -keystore D:\Android_Raj_
xxxx\SW\Raj_xxx\xxx_Xxxxxxx\xxxxxxx.jks -alias xyxyxyxy
Enter keystore password:
)


Now You can get your Unique API key by placing the Fingerprint..,


Thank You!