tag:blogger.com,1999:blog-51972778846341957382024-03-15T18:09:39.704-07:00Android CodesCodes for Android developers with examplesKeerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-5197277884634195738.post-40713311516628590722012-08-28T21:54:00.000-07:002020-07-12T01:50:04.564-07:00Seek Bar or Progress Bar or Slide Bar or Slider<div dir="ltr" style="text-align: left;" trbidi="on">
This bar will be like this, the name for this bar is seek bar<br />
<br />
<a href="http://1.bp.blogspot.com/-HDNFnyRU2Cw/TcuMbBaL70I/AAAAAAAAAGc/7eWN1qnZbAw/s1600/seek.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" src="http://1.bp.blogspot.com/-HDNFnyRU2Cw/TcuMbBaL70I/AAAAAAAAAGc/7eWN1qnZbAw/s320/seek.JPG" id="BLOGGER_PHOTO_ID_5605728557055340354" style="cursor: pointer; display: block; height: 167px; margin: 0px auto 10px; text-align: center; width: 320px;" /></a><br />
<br />
In the xml file we need to mention like this to call the seek bar<br />
<br />
<div class="innerblock">
<br />
<strong>xml</strong><br />
<pre class="innertextblock">
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
/> </pre>
</div>
<br />
<br />
The java class file will be like this<br />
<br />
<div class="innerblock">
<br />
<strong>Seek.java</strong><br />
<pre class="innertextblock">
package com.android.seek;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class Seek extends Activity {
/** Called when the activity is first created. */
AudioManager audioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView reading = (TextView) findViewById(R.id.reading);
SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
seekBar.setMax(100);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
reading.setText("Processing "+progress+"% ");
}
});
}
}</pre>
</div>
<br />
<br />
<br />
The code is fulling checked so you can run the code after downloading it itself.<br />
<br />
You can download the full source code<br />
<br />
<div class="downloadblock">
<br />
Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNd3psVXQ1T2xhczQ/edit?usp=drive_web">source</a></div>
<br />
<br />
Have a good day.</div>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com93tag:blogger.com,1999:blog-5197277884634195738.post-75178626846248315102011-11-21T22:27:00.000-08:002015-02-20T02:44:33.921-08:00Running a service in background on Android<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<br /></div>
Here the code is generate to run some process in background through service. For example we are running the time in background and displaying it in the UI. The service coding will be in a seperate class MyService.java<br />
<div>
<br />
<br />
<div class="innerblock">
<strong>MyService.java</strong><br />
<pre class="innertextblock">package com.services.demo;
import java.util.Date;
<div style="text-align: center;">
</div>
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
public class MyService extends Service {
public static final String BROADCAST_ACTION = "com.services.demo.ServiceDemoActivity";
private final Handler handler = new Handler();
Intent intent;<pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
int counter = 0;
@Override
public void onCreate() {
// Called on service created
intent = new Intent(BROADCAST_ACTION);
}
@Override<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
public void onDestroy() {
// Called on service stopped
}
<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
@Override
public void onStart(Intent intent, int startid) {
int i = 0;
while (i < 101) { if (i > 100) {
this.onDestroy();
} else {
counter = i;
i++;
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1 * 1000); // 1 sec
}<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
}
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 1 * 1000); // 1 sec
}<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
};
private void DisplayLoggingInfo() {
intent.putExtra("time", new Date().toLocaleString());
intent.putExtra("counter", String.valueOf(counter));
sendBroadcast(intent);
}<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
public static boolean isRunning() {
return true;
}
@Override<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
public IBinder onBind(Intent intent) {
return null;
}
}
</pre>
</div>
<div>
<br /></div>
So for starting the service and stopping the service we will be using<br />
<div>
<br />
<div class="innerblock">
<br />
<strong>Java</strong><br />
<pre class="innertextblock">startService(new Intent(ServiceDemoActivity.this, MyService.class));
stopService(new Intent(ServiceDemoActivity.this, MyService.class));</pre>
</div>
<br />
For updating the UI we need to use the broadcast service. In that BroadcastReceiver we will update the UI<br />
<div class="innerblock">
<br />
<strong>Java</strong><br />
<pre class="innertextblock">private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateDate(intent);
}<pre class="innertextblock"><pre class="innertextblock"></pre>
<div style="text-align: center;">
</div>
</pre>
};
private void updateDate(Intent intent) {
String time = intent.getStringExtra("time");
TextView date = (TextView) findViewById(R.id.date);
date.setText(time);
}</pre>
</div>
<pre class="innertextblock"><div style="text-align: left;">
<span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px; white-space: normal;">
</span></div>
<div style="text-align: left;">
<span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px; white-space: normal;">In the MyService.java the method DisplayLoggingInfo() will send the broadcast to the activity to update the UI, the UI xml code is</span></div>
<div style="text-align: left;">
<span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px; white-space: normal;">
</span></div>
</pre>
<div class="innerblock">
<strong>main.xml</strong><br />
<pre class="innertextblock"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/notification"
/>
<TextView
android:layout_width="fill_parent" android:id="@+id/date"
android:layout_height="wrap_content" android:text="" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
<Button android:id="@+id/stop" android:text="Stop Service" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</pre>
</div>
<br />
If we are opening the application on the first time means, it will be like this<br />
<a href="http://2.bp.blogspot.com/-KI0nLL3B3BY/TstBaYH798I/AAAAAAAAAJM/-gLClIZM-4o/s1600/service-started-android.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5677703676638787522" src="http://2.bp.blogspot.com/-KI0nLL3B3BY/TstBaYH798I/AAAAAAAAAJM/-gLClIZM-4o/s320/service-started-android.JPG" style="cursor: hand; cursor: pointer; display: block; height: 162px; margin: 0px auto 10px; text-align: center; width: 320px;" /></a><br />
and if we go out and come back to the page means, it will be showing a message like this<br />
<br />
<img alt="" border="0" id="BLOGGER_PHOTO_ID_5677703675478963906" src="http://1.bp.blogspot.com/-BJrXLhkUTNQ/TstBaTzadsI/AAAAAAAAAJE/4kplQGAG6rM/s320/service-continuing-android.JPG" style="cursor: pointer; display: block; height: 137px; margin-bottom: 10px; margin-left: auto; margin-right: auto; margin-top: 0px; text-align: center; width: 320px;" />and if we stop the service means, it will be showing a message like this<br />
<div>
<br />
<a href="http://3.bp.blogspot.com/-52zP5FyZAks/TstBanwjboI/AAAAAAAAAJU/ltoKDiSI7Ik/s1600/service-stopped-android.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5677703680835677826" src="http://3.bp.blogspot.com/-52zP5FyZAks/TstBanwjboI/AAAAAAAAAJU/ltoKDiSI7Ik/s320/service-stopped-android.JPG" style="cursor: hand; cursor: pointer; display: block; height: 150px; margin: 0px auto 10px; text-align: center; width: 320px;" /></a><br />
<pre class="innertextblock" style="font-size: 13px;"><pre class="innertextblock" style="text-align: center;"></pre>
</pre>
You can download the full source code<br />
<div>
<br />
<br />
<div class="downloadblock">
<br />
Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNVTVmb1gwVTBGUVk/edit">source</a></div>
<br />
<br />
Have a good day.</div>
</div>
</div>
</div>
</div>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com18tag:blogger.com,1999:blog-5197277884634195738.post-73380839372109803042011-11-07T23:23:00.000-08:002020-07-12T01:50:13.538-07:00Show or Hide soft keyboard on opening a dialog or activity in androidFor showing the soft keyboard on entering into the activity or to the dialog we can use this coding<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br /><br />InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);<br />imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);<br /><br /></pre> </div><br /><br />and for hiding the keyboard<br /><br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);<br />imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0); <br /><br /></pre> </div><br /><br />Have a nice day!Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com9tag:blogger.com,1999:blog-5197277884634195738.post-77943543151299202392011-11-01T23:14:00.001-07:002020-07-12T01:50:17.498-07:00Animation for view and activities with and without viewflipper in AndroidNow we are going to see some animations in android, three types of animation are given on this pack<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-XbAdIgjE9yI/TrDgG6iUMcI/AAAAAAAAAIc/DF2oR5WXTC8/s1600/animation_viewflipper_layout_hide_show.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 318px; height: 320px;" src="http://2.bp.blogspot.com/-XbAdIgjE9yI/TrDgG6iUMcI/AAAAAAAAAIc/DF2oR5WXTC8/s320/animation_viewflipper_layout_hide_show.JPG" alt="" id="BLOGGER_PHOTO_ID_5670278340256477634" border="0" /></a><br /><br />Yes as shown in the image we are going to see all these types.<br />First we can see about Animation using ViewFlipper<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-0kRcsKG4glA/TrDgGBygDII/AAAAAAAAAIQ/D6_a-0qm2Z8/s1600/animation_viewflipper.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 138px;" src="http://3.bp.blogspot.com/-0kRcsKG4glA/TrDgGBygDII/AAAAAAAAAIQ/D6_a-0qm2Z8/s320/animation_viewflipper.JPG" alt="" id="BLOGGER_PHOTO_ID_5670278325023542402" border="0" /></a><br /><br />Here i had added three views in the viewflipper and i had called the animation changes on scrolling using touchevent, the xml section will be like this<br /><br /><div class="innerblock"> <br /><strong>viewer.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><br /><ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_margin="5dip"<br /> android:id="@+id/layoutswitcher"<br /> android:layout_width="fill_parent"<br /> android:layout_height="500dp"><br /> <LinearLayout<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:padding="20dip"<br /> android:background="@android:color/background_light"<br /> android:text="Currently you are in Page 1. Scroll right or left to see next page"<br /> android:layout_width="fill_parent"<br /> android:layout_weight="1"<br /> android:textStyle="bold" ><br /> </TextView><br /> </LinearLayout><br /> <LinearLayout<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:padding="20dip"<br /> android:background="@android:color/darker_gray"<br /> android:text="Currently you are in Page 2. Scroll right or left to see next page"<br /> android:layout_width="fill_parent"<br /> android:layout_weight="1"<br /> android:textStyle="bold" ><br /> </TextView><br /> </LinearLayout><br /> <LinearLayout<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:padding="20dip"<br /> android:background="@android:color/white"<br /> android:text="Currently you are in Page 3. Scroll right or left to see next page"<br /> android:layout_width="fill_parent"<br /> android:layout_weight="1"<br /> android:textStyle="bold" ><br /> </TextView><br /> </LinearLayout><br /></ViewFlipper><br /><br /></pre> </div><br /><br />and in the java we need to apply the animation on touchevent as <br /><br /><br /><div class="innerblock"> <br /><strong>ViewerEffect.java</strong><br /><pre class="innertextblock" ><br />package com.animmer;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.MotionEvent;<br />import android.view.animation.AccelerateInterpolator;<br />import android.view.animation.Animation;<br />import android.view.animation.TranslateAnimation;<br />import android.widget.ViewFlipper;<br /><br />public class ViewerEffect extends Activity {<br /> <br /> private ViewFlipper vf; <br /> private float oldTouchValue;<br /> <br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.viewer);<br /> <br /> vf = (ViewFlipper) findViewById(R.id.layoutswitcher);<br /> }<br /> <br /> //for the previous movement<br /> public static Animation inFromRightAnimation() {<br /><br /> Animation inFromRight = new TranslateAnimation(<br /> Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,<br /> Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f<br /> );<br /> inFromRight.setDuration(400);<br /> inFromRight.setInterpolator(new AccelerateInterpolator());<br /> return inFromRight;<br /> }<br /> public static Animation outToLeftAnimation() {<br /> Animation outtoLeft = new TranslateAnimation(<br /> Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,<br /> Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f<br /> );<br /> outtoLeft.setDuration(400);<br /> outtoLeft.setInterpolator(new AccelerateInterpolator());<br /> return outtoLeft;<br /> } <br /> // for the next movement<br /> public static Animation inFromLeftAnimation() {<br /> Animation inFromLeft = new TranslateAnimation(<br /> Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,<br /> Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f<br /> );<br /> inFromLeft.setDuration(400);<br /> inFromLeft.setInterpolator(new AccelerateInterpolator());<br /> return inFromLeft;<br /> }<br /> public static Animation outToRightAnimation() {<br /> Animation outtoRight = new TranslateAnimation(<br /> Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f,<br /> Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f<br /> );<br /> outtoRight.setDuration(400);<br /> outtoRight.setInterpolator(new AccelerateInterpolator());<br /> return outtoRight;<br /> }<br /> <br /> @Override<br /> public boolean onTouchEvent(MotionEvent touchevent) {<br /> switch (touchevent.getAction())<br /> {<br /> case MotionEvent.ACTION_DOWN:<br /> {<br /> oldTouchValue = touchevent.getX();<br /> break;<br /> }<br /> case MotionEvent.ACTION_UP:<br /> {<br /> float currentX = touchevent.getX();<br /> if (oldTouchValue < currentX)<br /> {<br /> vf.setInAnimation(inFromLeftAnimation());<br /> vf.setOutAnimation(outToRightAnimation());<br /> vf.showNext();<br /> }<br /> if (oldTouchValue > currentX)<br /> {<br /> vf.setInAnimation(inFromRightAnimation());<br /> vf.setOutAnimation(outToLeftAnimation());<br /> vf.showPrevious();<br /> }<br /> break;<br /> }<br /> }<br /> return false;<br /> }<br />}<br /><br /></pre> </div><br /><br /><br />Likewise the next is animation between activities <br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-IhcX3dRIwGg/TrDgEkXaXNI/AAAAAAAAAH4/FDE78GUK7wA/s1600/animation_between_activities.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 123px;" src="http://3.bp.blogspot.com/-IhcX3dRIwGg/TrDgEkXaXNI/AAAAAAAAAH4/FDE78GUK7wA/s320/animation_between_activities.JPG" alt="" id="BLOGGER_PHOTO_ID_5670278299945426130" border="0" /></a><br /><br />Yes we here we will be giving animation effect between activities<br />For animation effects we need to put four files in the anim folder, they are slide_in_left, slide_out_left, slide_in_right and slide_out_right.<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );<br /></pre> </div><br /><br />The above code is used to move left to right<br /><br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );<br /></pre> </div><br /><br />The above code is used to move right to left<br /><br />The final animation technique is to hide and show the views<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-QoDVl5m5OPE/TrDgFBxaTaI/AAAAAAAAAII/mOMXMPrq5Ks/s1600/animation_show_hide_view.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 164px;" src="http://1.bp.blogspot.com/-QoDVl5m5OPE/TrDgFBxaTaI/AAAAAAAAAII/mOMXMPrq5Ks/s320/animation_show_hide_view.JPG" alt="" id="BLOGGER_PHOTO_ID_5670278307839102370" border="0" /></a><br /><br />Here we will be using touch event to assign the animation part, the animation objects will be assigned as shown below<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />mAnimShow = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);<br />mAnimHide = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);<br /><br />mAnimShow.setAnimationListener(new AnimationListener() {<br /><br /> @Override<br /> public void onAnimationRepeat(Animation animation) {<br /> // TODO Auto-generated method stub<br /><br /> }<br /><br /> @Override<br /> public void onAnimationStart(Animation animation) {<br /> overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );<br /><br /> }<br /><br /> @Override<br /> public void onAnimationEnd(Animation animation) {<br /> <br /> <br /> }<br /><br /> });<br /><br />mAnimHide.setAnimationListener(new AnimationListener() {<br /><br /> @Override<br /> public void onAnimationRepeat(Animation animation) {<br /> // TODO Auto-generated method stub<br /><br /> }<br /><br /> @Override<br /> public void onAnimationStart(Animation animation) {<br /> overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );<br /><br /> }<br /><br /> @Override<br /> public void onAnimationEnd(Animation animation) {<br /> <br /> <br /> }<br /><br /> });<br /></pre> </div><br /><br />The full source code is provided below, you can apply these animation for any other purpose also, the animation technique is same, we need to put this in our logic.<br /><br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNNWI2QUxZTlRDV00/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com8tag:blogger.com,1999:blog-5197277884634195738.post-26164586051311444662011-10-18T00:29:00.000-07:002020-07-12T01:50:21.753-07:00Check whether service is running or not in android applicationUsing the below given code we can check whether a particular service in our application is running or not<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />private boolean isMyServiceRunning() {<br /> ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);<br /> for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {<br /> if ("com.dir.pack.DemoService".equals(service.service.getClassName())) {<br /> return true;<br /> }<br /> }<br /> return false;<br />}<br /></pre> </div><br /><br />Here in the if condition we need to give the service file name with the package. So on the checking area you can simply call this function in the if statement like<br /><br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />if(isMyServiceRunning()) {<br /> System.out.println("Service is running");<br />} else {<br /> System.out.println("Service is not running");<br />}<br /></pre> </div><br /><br />Have a nice dayKeerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com4tag:blogger.com,1999:blog-5197277884634195738.post-23992575935710618412011-09-23T22:05:00.001-07:002020-07-12T01:50:27.254-07:00Image and content is populated from Sqlite in AndroidIn this below application we had populated the listview's data and image from the sqlite<div><br /></div><br /><a href="http://1.bp.blogspot.com/-Ioqc_gFkmyw/TngnjgEsiyI/AAAAAAAAAHY/OWYTYdWsC_M/s1600/listview_from_sqlite_populate.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 152px;" src="http://1.bp.blogspot.com/-Ioqc_gFkmyw/TngnjgEsiyI/AAAAAAAAAHY/OWYTYdWsC_M/s320/listview_from_sqlite_populate.JPG" border="0" alt="" id="BLOGGER_PHOTO_ID_5654312823022193442" /></a><div><br />Here we are using a java class for creating the table<br /><br /><div class="innerblock"> <br /><strong>PlaceDataSQL.java</strong><br /><pre class="innertextblock">package com.data.pack;<br /><br />package com.data.pack;<br /><br />import android.content.Context;<br />import android.database.SQLException;<br />import android.database.sqlite.SQLiteDatabase;<br />import android.database.sqlite.SQLiteException;<br />import android.database.sqlite.SQLiteOpenHelper;<br />import android.util.Log;<br /><br />/** Helper to the database, manages versions and creation */<br />public class PlaceDataSQL extends SQLiteOpenHelper {<br /> private static final String DATABASE_NAME = "sample.db";<br /> private static final int DATABASE_VERSION = 1;<br /><br /> private Context context;<br /><br /> public PlaceDataSQL(Context context) {<br /> super(context, DATABASE_NAME, null, DATABASE_VERSION);<br /> this.context = context;<br /> }<br /><br /> @Override<br /> public void onCreate(SQLiteDatabase db) {<br /> db.execSQL("CREATE TABLE gallery (id varchar(20), image BLOB,caption varchar(160),description varchar(200))");<br /> }<br /><br /> private void versionUpdation(SQLiteDatabase db) {<br /><br /> }<br /><br /> /**<br /> * Check if the database already exist to avoid re-copying the file each<br /> * time you open the application.<br /> * <br /> * @return true if it exists, false if it doesn't<br /> */<br /> public boolean checkDataBase(String db) {<br /><br /> SQLiteDatabase checkDB = null;<br /><br /> try {<br /> String myPath = "data/data/com.data.pack/databases/" + db;<br /> checkDB = SQLiteDatabase.openDatabase(myPath, null,<br /> SQLiteDatabase.OPEN_READONLY);<br /><br /> } catch (SQLiteException e) {<br /><br /> // database does't exist yet.<br /><br /> } catch (Exception e) {<br /><br /> }<br /><br /> if (checkDB != null) {<br /><br /> checkDB.close();<br /><br /> }<br /><br /> return checkDB != null ? true : false;<br /> }<br /><br /> @Override<br /> public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br /> if (oldVersion >= newVersion)<br /> return;<br /><br /> if (oldVersion == 1) {<br /> Log.d("New Version", "Datas can be upgraded");<br /> }<br /><br /> Log.d("Sample Data", "onUpgrade : " + newVersion);<br /> }<br /><br />}<br /><br /></pre> </div><br /><br />Here in the onCreate function the table is created, here for image we are using blog, so that we can insert the image inside the table itself and the onUpgrade function is used to upgrade the table on the next release, for example you had create an application and published it in market and a guy is using your application, now suddenly you like to add another field on your table means you need to change the " DATABASE_VERSION " to 2 and in the onUpgrade funtion you need to write the alter query there. Likewise i had using a funtion checkDataBase(), this is to check whether the database is exist or not.<br /><br />Then in the main activity we are calling delete to delete the table content, likewise we will be using a function insertData, inside this function we will be inserting data to the table, like that we will be using getDataAndPopulate(), here we will be retrieving data from the table and displaying in the list.<br /><br />The main thing is we inserting the image to database for this we are using HttpClient and on the retriving time it is very easy to assign the data, so while calling on the next time we can assign and there is no need of url call for showing image.<br /><br /><div class="innerblock"> <br /><strong>SqliteDBActivity.java</strong><br /><pre class="innertextblock">package com.data.pack;<br /><br /><br />package com.data.pack;<br /><br /><br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.net.HttpURLConnection;<br />import java.net.MalformedURLException;<br />import java.net.URL;<br />import java.util.ArrayList;<br /><br />import org.apache.http.HttpEntity;<br />import org.apache.http.HttpResponse;<br />import org.apache.http.HttpStatus;<br />import org.apache.http.client.ClientProtocolException;<br />import org.apache.http.client.methods.HttpGet;<br />import org.apache.http.impl.client.DefaultHttpClient;<br />import org.apache.http.util.EntityUtils;<br /><br />import android.app.ListActivity;<br />import android.content.ContentValues;<br />import android.content.Context;<br />import android.database.Cursor;<br />import android.database.sqlite.SQLiteDatabase;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapFactory;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.BaseAdapter;<br />import android.widget.Button;<br />import android.widget.ImageView;<br />import android.widget.TextView;<br /><br />public class SqliteDBActivity extends ListActivity {<br /> private static PlaceDataSQL placeData;<br /> private ArrayList<String> id = new ArrayList<String>();<br /> private ArrayList<byte[]> image = new ArrayList<byte[]>();<br /> private ArrayList<String> caption = new ArrayList<String>();<br /> private ArrayList<String> description = new ArrayList<String>();<br /> private Button populate;<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> populate = (Button) findViewById(R.id.populate);<br /> placeData = new PlaceDataSQL(this);<br /> <br /> SQLiteDatabase db = placeData.getWritableDatabase();<br /> <br /> Cursor cursors = getRawEvents("select * from gallery");<br /><br /> if (cursors.moveToNext()) {<br /> populate.setVisibility(View.GONE);<br /> getDataAndPopulate();<br /> } else {<br /> populate.setVisibility(View.VISIBLE);<br /> }<br /> <br /> <br /> db.delete("gallery", "id=?", new String[] {<br /> "12" });<br /> populate.setOnClickListener(new View.OnClickListener() {<br /> <br /> @Override<br /> public void onClick(View v) {<br /> try {<br /> callInsertion("1","http://2.bp.blogspot.com/-bkmnlZUKXPs/TjZeTVCgp9I/AAAAAAAAAHI/SPnWJYqq4uQ/s1600/twitter_follow.gif","First","This is the first item");<br /> callInsertion("2","http://1.bp.blogspot.com/-HDNFnyRU2Cw/TcuMbBaL70I/AAAAAAAAAGc/7eWN1qnZbAw/s320/seek.JPG","Second","This is the second item");<br /> } catch (ClientProtocolException e) {<br /> // TODO Auto-generated catch block<br /> e.printStackTrace();<br /> } catch (IOException e) {<br /> // TODO Auto-generated catch block<br /> e.printStackTrace();<br /> } <br /> <br /> getDataAndPopulate();<br /> }<br /> });<br /> <br /> }<br /> <br /> private void callInsertion(String id, String url, String caption, String description) throws ClientProtocolException, IOException {<br /> DefaultHttpClient mHttpClient = new DefaultHttpClient();<br /> HttpGet mHttpGet = new HttpGet(url);<br /> HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);<br /> if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {<br /> HttpEntity entity = mHttpResponse.getEntity();<br /> if ( entity != null) {<br /> // insert to database <br /> insertData(id,EntityUtils.toByteArray(entity),caption,description);<br /> }<br /> }<br /> }<br /> <br /> private void insertData(String id, byte[] image, String caption, String description) {<br /> SQLiteDatabase db = placeData.getWritableDatabase();<br /> ContentValues values;<br /> values = new ContentValues();<br /> values.put("id", id);<br /> values.put("image", image);<br /> values.put("caption", caption);<br /> values.put("description", description);<br /> <br /> db.insert("gallery", null, values);<br /> }<br /> <br /> private void getDataAndPopulate() {<br /> id = new ArrayList<String>();<br /> image = new ArrayList<byte[]>();<br /> caption = new ArrayList<String>();<br /> description = new ArrayList<String>();<br /> Cursor cursor = getEvents("gallery");<br /> while (cursor.moveToNext()) {<br /> String temp_id = cursor.getString(0);<br /> byte[] temp_image = cursor.getBlob(1);<br /> String temp_caption = cursor.getString(2);<br /> String temp_description = cursor.getString(3);<br /> id.add(temp_id);<br /> image.add(temp_image);<br /> caption.add(temp_caption);<br /> description.add(temp_description);<br /> }<br /> String[] captionArray = (String[]) caption.toArray(<br /> new String[caption.size()]);<br /> <br /> ItemsAdapter itemsAdapter = new ItemsAdapter(<br /> SqliteDBActivity.this, R.layout.item,<br /> captionArray);<br /> setListAdapter(itemsAdapter);<br /> populate.setVisibility(View.GONE);<br /> }<br /> <br /> <br /> private class ItemsAdapter extends BaseAdapter {<br /> String[] items;<br /><br /> public ItemsAdapter(Context context, int textViewResourceId,<br /> String[] items) {<br /> this.items = items;<br /> }<br /><br /> @Override<br /> public View getView(final int POSITION, View convertView,<br /> ViewGroup parent) {<br /> TextView desc;<br /> TextView cap;<br /> View view = convertView;<br /> ImageView img;<br /> if (view == null) {<br /> LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br /> view = vi.inflate(R.layout.item, null);<br /> <br /> } <br /> img = (ImageView) view.findViewById(R.id.image);<br /> cap = (TextView) view.findViewById(R.id.caption);<br /> <br /> desc = (TextView) view.findViewById(R.id.description);<br /> <br /> cap.setText(caption.get(POSITION));<br /> desc.setText(description.get(POSITION));<br /> img.setImageBitmap(BitmapFactory.decodeByteArray(image.get(POSITION), 0, image.get(POSITION).length));<br /> <br /><br /> return view;<br /> }<br /><br /> public int getCount() {<br /> return items.length;<br /> }<br /><br /> public Object getItem(int position) {<br /> return position;<br /> }<br /><br /> public long getItemId(int position) {<br /> return position;<br /> }<br /> }<br /> <br /> private Cursor getRawEvents(String sql) {<br /> SQLiteDatabase db = (placeData).getReadableDatabase();<br /> Cursor cursor = db.rawQuery(sql, null);<br /><br /> startManagingCursor(cursor);<br /> return cursor;<br /> }<br /><br /> private Cursor getEvents(String table) {<br /> SQLiteDatabase db = (placeData).getReadableDatabase();<br /> Cursor cursor = db.query(table, null, null, null, null, null, null);<br /><br /> startManagingCursor(cursor);<br /> return cursor;<br /> }<br />}</pre> </div><br /><br />In this example i had wrote two types of retrieving methods from the table, by writing the query and specifying the table name itself.<br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNRDFNZGhrckpwTHM/edit">source</a><br /></div><br /><br />Have a good day.</div>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com18tag:blogger.com,1999:blog-5197277884634195738.post-80691605123806446592011-09-19T23:08:00.001-07:002020-07-12T01:50:30.796-07:00Customize the tab in AndroidI had attached two tab examples<br /><div style="clear:both;"></div><br /><div style="clear:both;"></div><br /><a href="http://3.bp.blogspot.com/-PT9h4bmpfec/TnguDwiRvWI/AAAAAAAAAHo/aDNQzjr7qhY/s1600/custom_tab_designs.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 228px; height: 320px;" src="http://3.bp.blogspot.com/-PT9h4bmpfec/TnguDwiRvWI/AAAAAAAAAHo/aDNQzjr7qhY/s320/custom_tab_designs.JPG" border="0" alt="" id="BLOGGER_PHOTO_ID_5654319974266813794" /></a><br /><a href="http://4.bp.blogspot.com/-1vqlWTniEFo/TnguAU9ETOI/AAAAAAAAAHg/EeTDhMk5vpE/s1600/custom_tab_design.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 221px; height: 320px;" src="http://4.bp.blogspot.com/-1vqlWTniEFo/TnguAU9ETOI/AAAAAAAAAHg/EeTDhMk5vpE/s320/custom_tab_design.JPG" border="0" alt="" id="BLOGGER_PHOTO_ID_5654319915323378914" /></a><br /><div style="clear:both;"></div><br /><div style="clear:both;"></div><br />First we are going to see about First example<br /><br /><div class="innerblock"> <br /><strong>CustomTabActivity.java</strong><br /><pre class="innertextblock" ><br />package com.customs.tabs;<br /><br />import com.customs.tabs.R;<br />import android.app.TabActivity;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.widget.ImageView;<br />import android.widget.TabHost;<br />import android.widget.TextView;<br /><br />public class CustomTabActivity extends TabActivity{<br /> private TabHost tabHost;<br /> <br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> setTabs();<br /> }<br /> <br /> private void setTabs() {<br /> tabHost = getTabHost();<br /> <br /> addTab(R.string.tab_1, R.drawable.info);<br /> addTab(R.string.tab_2, R.drawable.info);<br /> addTab(R.string.tab_3, R.drawable.info);<br /> }<br /> <br /> private void addTab(int labelId, int drawableId) {<br /> Intent intent = new Intent(this, TabAct.class);<br /> TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); <br /> <br /> View tabIndicator = LayoutInflater.from(this).inflate(R.layout.indicator, getTabWidget(), false);<br /> <br /> TextView title = (TextView) tabIndicator.findViewById(R.id.title);<br /> title.setText(labelId);<br /> ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);<br /> icon.setImageResource(drawableId);<br /> <br /> spec.setIndicator(tabIndicator);<br /> spec.setContent(intent);<br /> tabHost.addTab(spec);<br /> <br /> }<br />}<br /></pre> </div><br /><br />The class extends TabActivity and the header of the tab is provided in seperate activity which is called on the addTab method. The selection / unselection / active colors are mentioned in the indicator xml file in the layout<br /><br /><br /><div class="innerblock"> <br /><strong>indicator.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="0dip"<br /> android:layout_height="64dip" <br /> android:layout_weight="1"<br /> android:orientation="vertical"<br /> android:background="@drawable/indicator"<br /> android:padding="5dp"><br /><br /> <ImageView android:id="@+id/icon"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_centerHorizontal="true"<br /> /><br /><br /> <TextView android:id="@+id/title"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_alignParentBottom="true"<br /> android:layout_centerHorizontal="true"<br /> style="?android:attr/tabWidgetStyle"<br /> /> <br /></RelativeLayout><br /></pre> </div><br /><br />Now we can see the Second example of Tab<br /><br />Here we are not going to extend the tabactivity, instead of that we can set the tab headers through setting background images or colors using xml and without using xml<br /><br /><div class="innerblock"> <br /><strong>CustomTabs.java</strong><br /><pre class="innertextblock" ><br />package com.customized.tabs;<br /><br />import com.customized.tabs.R;<br /><br />import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.widget.TabHost;<br />import android.widget.TextView;<br />import android.widget.TabHost.TabContentFactory;<br />import android.widget.TabHost.TabSpec;<br /><br />public class CustomTabs extends Activity {<br /><br /> private TabHost mTabHost;<br /> private TextView mTabLayout;<br /><br /> private void setupTabHost() {<br /> mTabHost = (TabHost) findViewById(android.R.id.tabhost);<br /> mTabHost.setup();<br /> mTabLayout = (TextView) findViewById(R.id.taber);<br /> mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {<br /> <br /> @Override<br /> public void onTabChanged(String tabId) {<br /> mTabHost.getTabWidget().getChildAt(0)<br /> .setBackgroundResource(R.drawable.m2);<br /> if ("Tab 1".equals(tabId)) {<br /> mTabHost.getTabWidget().getChildAt(0)<br /> .setBackgroundResource(R.drawable.m1);<br /> mTabLayout.setText("This is Tab 1");<br /> } else if("Tab 2".equals(tabId)) {<br /> mTabLayout.setText("This is Tab 2");<br /> } else {<br /> mTabLayout.setText("This is Tab 3");<br /> }<br /> }<br /> });<br /> }<br /><br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> // construct the tabhost<br /> setContentView(R.layout.main);<br /><br /> setupTabHost();<br /> <br /> //mTabHost.getTabWidget().setDividerDrawable(R.drawable.divider);<br /><br /> setupTab(new TextView(this), "Tab 1");<br /> setupTab(new TextView(this), "Tab 2");<br /> setupTab(new TextView(this), "Tab 3");<br /> }<br /><br /> private void setupTab(final View view, final String tag) {<br /> View tabview = createTabView(mTabHost.getContext(), tag);<br /> <br /> TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {<br /> public View createTabContent(String tag) {return view;}<br /> });<br /> mTabHost.addTab(setContent);<br /> if ("Tab 1".equals(tag))<br /> mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(<br /> R.drawable.m1);<br /> <br /><br /> }<br /><br /> private static View createTabView(final Context context, final String text) {<br /> View view = LayoutInflater.from(context).inflate(R.layout.bg, null);<br /> TextView tv = (TextView) view.findViewById(R.id.tabsText);<br /> tv.setText(text);<br /> return view;<br /> }<br />}<br /></pre> </div><br /><br />In the above code i had commented the divider between each header, if you need means you can uncomment and i had set a different image for the Tab 1 alone, if you need you can set different images for each tab like that. Then in the bg_selector.xml i had commented the xml files for background color and i had set images for that<br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNYk1ZMHBvOGtpR3M/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com8tag:blogger.com,1999:blog-5197277884634195738.post-56422602223739767892011-09-19T22:40:00.000-07:002020-07-12T01:50:34.678-07:00Using Sqlite to populate a ListView in AndroidIn this below application we had populated the listview using the data from the sqlite<div><br /></div><br /><a href="http://1.bp.blogspot.com/-Ioqc_gFkmyw/TngnjgEsiyI/AAAAAAAAAHY/OWYTYdWsC_M/s1600/listview_from_sqlite_populate.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 152px;" src="http://1.bp.blogspot.com/-Ioqc_gFkmyw/TngnjgEsiyI/AAAAAAAAAHY/OWYTYdWsC_M/s320/listview_from_sqlite_populate.JPG" border="0" alt="" id="BLOGGER_PHOTO_ID_5654312823022193442" /></a><div><br />Here we are using a java class for creating the table<br /><br /><div class="innerblock"> <br /><strong>PlaceDataSQL.java</strong><br /><pre class="innertextblock">package com.data.pack;<br /><br />import android.content.Context;<br />import android.database.SQLException;<br />import android.database.sqlite.SQLiteDatabase;<br />import android.database.sqlite.SQLiteException;<br />import android.database.sqlite.SQLiteOpenHelper;<br />import android.util.Log;<br /><br />/** Helper to the database, manages versions and creation */<br />public class PlaceDataSQL extends SQLiteOpenHelper {<br /> private static final String DATABASE_NAME = "sample.db";<br /> private static final int DATABASE_VERSION = 1;<br /><br /> private Context context;<br /><br /> public PlaceDataSQL(Context context) {<br /> super(context, DATABASE_NAME, null, DATABASE_VERSION);<br /> this.context = context;<br /> }<br /><br /> @Override<br /> public void onCreate(SQLiteDatabase db) {<br /> db.execSQL("CREATE TABLE gallery (id varchar(20), image varchar(2000),caption varchar(160),description varchar(200))");<br /> }<br /><br /> private void versionUpdation(SQLiteDatabase db) {<br /><br /> }<br /><br /> /**<br /> * Check if the database already exist to avoid re-copying the file each<br /> * time you open the application.<br /> *<br /> * @return true if it exists, false if it doesn't<br /> */<br /> public boolean checkDataBase(String db) {<br /><br /> SQLiteDatabase checkDB = null;<br /><br /> try {<br /> String myPath = "data/data/com.data.pack/databases/" + db;<br /> checkDB = SQLiteDatabase.openDatabase(myPath, null,<br /> SQLiteDatabase.OPEN_READONLY);<br /><br /> } catch (SQLiteException e) {<br /><br /> // database does't exist yet.<br /><br /> } catch (Exception e) {<br /><br /> }<br /><br /> if (checkDB != null) {<br /><br /> checkDB.close();<br /><br /> }<br /><br /> return checkDB != null ? true : false;<br /> }<br /><br /> @Override<br /> public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br /> if (oldVersion >= newVersion)<br /> return;<br /><br /> if (oldVersion == 1) {<br /> Log.d("New Version", "Datas can be upgraded");<br /> }<br /><br /> Log.d("Sample Data", "onUpgrade : " + newVersion);<br /> }<br /><br />}<br /></pre> </div><br /><br />Here in the onCreate function the table is created and the onUpgrade function is used to upgrade the table on the next release, for example you had create an application and published it in market and a guy is using your application, now suddenly you like to add another field on your table means you need to change the " DATABASE_VERSION " to 2 and in the onUpgrade funtion you need to write the alter query there. Likewise i had using a funtion checkDataBase(), this is to check whether the database is exist or not.<br /><br />Then in the main activity we are calling delete to delete the table content, likewise we will be using a function insertData, inside this function we will be inserting data to the table, like that we will be using getDataAndPopulate(), here we will be retrieving data from the table and displaying in the list.<br /><br /><br /><div class="innerblock"> <br /><strong>SqliteDBActivity.java</strong><br /><pre class="innertextblock">package com.data.pack;<br /><br /><br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.net.HttpURLConnection;<br />import java.net.MalformedURLException;<br />import java.net.URL;<br />import java.util.ArrayList;<br /><br />import android.app.ListActivity;<br />import android.content.ContentValues;<br />import android.content.Context;<br />import android.database.Cursor;<br />import android.database.sqlite.SQLiteDatabase;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapFactory;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.BaseAdapter;<br />import android.widget.Button;<br />import android.widget.ImageView;<br />import android.widget.TextView;<br /><br />public class SqliteDBActivity extends ListActivity {<br /> private static PlaceDataSQL placeData;<br /> private ArrayList<string> id = new ArrayList<string>();<br /> private ArrayList<string> image = new ArrayList<string>();<br /> private ArrayList<string> caption = new ArrayList<string>();<br /> private ArrayList<string> description = new ArrayList<string>();<br /> private Button populate;<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> populate = (Button) findViewById(R.id.populate);<br /> placeData = new PlaceDataSQL(this);<br /> <br /> SQLiteDatabase db = placeData.getWritableDatabase();<br /> <br /> Cursor cursors = getRawEvents("select * from gallery");<br /><br /> if (cursors.moveToNext()) {<br /> populate.setVisibility(View.GONE);<br /> getDataAndPopulate();<br /> } else {<br /> populate.setVisibility(View.VISIBLE);<br /> }<br /> <br /> <br /> db.delete("gallery", "id=?", new String[] {<br /> "12" });<br /> populate.setOnClickListener(new View.OnClickListener() {<br /> <br /> @Override<br /> public void onClick(View v) {<br /> insertData("1","http://2.bp.blogspot.com/-bkmnlZUKXPs/TjZeTVCgp9I/AAAAAAAAAHI/SPnWJYqq4uQ/s1600/twitter_follow.gif","First","This is the first item");<br /> insertData("2","http://1.bp.blogspot.com/-HDNFnyRU2Cw/TcuMbBaL70I/AAAAAAAAAGc/7eWN1qnZbAw/s320/seek.JPG","Second","This is the second item");<br /> getDataAndPopulate();<br /> }<br /> });<br /> <br /> }<br /> <br /> private void insertData(String id, String image, String caption, String description) {<br /> SQLiteDatabase db = placeData.getWritableDatabase();<br /> ContentValues values;<br /> values = new ContentValues();<br /> values.put("id", id);<br /> values.put("image", image);<br /> values.put("caption", caption);<br /> values.put("description", description);<br /> <br /> db.insert("gallery", null, values);<br /> }<br /> <br /> private void getDataAndPopulate() {<br /> id = new ArrayList<string>();<br /> image = new ArrayList<string>();<br /> caption = new ArrayList<string>();<br /> description = new ArrayList<string>();<br /> Cursor cursor = getEvents("gallery");<br /> while (cursor.moveToNext()) {<br /> String temp_id = cursor.getString(0);<br /> String temp_image = cursor.getString(1);<br /> String temp_caption = cursor.getString(2);<br /> String temp_description = cursor.getString(3);<br /> id.add(temp_id);<br /> image.add(temp_image);<br /> caption.add(temp_caption);<br /> description.add(temp_description);<br /> }<br /> String[] captionArray = (String[]) caption.toArray(<br /> new String[caption.size()]);<br /> <br /> ItemsAdapter itemsAdapter = new ItemsAdapter(<br /> SqliteDBActivity.this, R.layout.item,<br /> captionArray);<br /> setListAdapter(itemsAdapter);<br /> populate.setVisibility(View.GONE);<br /> }<br /> <br /> <br /> private class ItemsAdapter extends BaseAdapter {<br /> String[] items;<br /><br /> public ItemsAdapter(Context context, int textViewResourceId,<br /> String[] items) {<br /> this.items = items;<br /> }<br /><br /> @Override<br /> public View getView(final int POSITION, View convertView,<br /> ViewGroup parent) {<br /> TextView desc;<br /> TextView cap;<br /> View view = convertView;<br /> ImageView img;<br /> if (view == null) {<br /> LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br /> view = vi.inflate(R.layout.item, null);<br /> <br /> }<br /> img = (ImageView) view.findViewById(R.id.image);<br /> cap = (TextView) view.findViewById(R.id.caption);<br /> <br /> desc = (TextView) view.findViewById(R.id.description);<br /> <br /> cap.setText(caption.get(POSITION));<br /> desc.setText(description.get(POSITION));<br /> try {<br /> URL url = new URL(image.get(POSITION));<br /> HttpURLConnection connection = (HttpURLConnection) url<br /> .openConnection();<br /> connection.setDoInput(true);<br /> connection.connect();<br /> InputStream input = connection.getInputStream();<br /> BitmapFactory.Options bfOptions = new BitmapFactory.Options();<br /> bfOptions.inDither = false; // Disable Dithering<br /> // mode<br /> bfOptions.inPurgeable = true; // Tell to gc that<br /> // whether it needs<br /> // free memory, the<br /> // Bitmap can be<br /> // cleared<br /> bfOptions.inInputShareable = true; // Which kind of<br /> // reference<br /> // will be used<br /> // to recover<br /> // the Bitmap<br /> // data after<br /> // being clear,<br /> // when it will<br /> // be used in<br /> // the future<br /> bfOptions.inTempStorage = new byte[16 * 1024];<br /> Bitmap bmp = null;<br /> bmp = BitmapFactory.decodeStream(input, null,<br /> bfOptions);<br /> img.setImageBitmap(bmp);<br /> connection.disconnect();<br /> input.close();<br /><br /> } catch (MalformedURLException e) {<br /> } catch (IOException e) {<br /> } catch (IllegalAccessError e) {<br /> } catch (NullPointerException e) {<br /> }<br /><br /> return view;<br /> }<br /><br /> public int getCount() {<br /> return items.length;<br /> }<br /><br /> public Object getItem(int position) {<br /> return position;<br /> }<br /><br /> public long getItemId(int position) {<br /> return position;<br /> }<br /> }<br /> <br /> private Cursor getRawEvents(String sql) {<br /> SQLiteDatabase db = (placeData).getReadableDatabase();<br /> Cursor cursor = db.rawQuery(sql, null);<br /><br /> startManagingCursor(cursor);<br /> return cursor;<br /> }<br /><br /> private Cursor getEvents(String table) {<br /> SQLiteDatabase db = (placeData).getReadableDatabase();<br /> Cursor cursor = db.query(table, null, null, null, null, null, null);<br /><br /> startManagingCursor(cursor);<br /> return cursor;<br /> }<br />}<br /></string></string></string></string></string></string></string></string></string></string></string></string></pre> </div><br /><br />In this example i had wrote two types of retrieving methods from the table, by writing the query and specifying the table name itself.<br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNU1VXci1jdktDMk0/edit">source</a><br /></div><br /><br />Have a good day.</div>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com11tag:blogger.com,1999:blog-5197277884634195738.post-27573362324203783652011-07-31T22:19:00.000-07:002020-07-12T01:50:38.578-07:00Creating keystore and google api key for android google mapsOpen the command prompt and follow the steps<br /><br />D:\android-sdk-windows-1.6_r1\tools><span style="font-weight: bold;">keytool -genkey -v -keystore projectkey.keystore -alias aliasname -keyalg RSA -keysize 2048 -validity 15000</span><br />Enter keystore password: ------------<br />What is your first and last name?<br /> [Unknown]: ------------<br />What is the name of your organizational unit?<br /> [Unknown]: ------------<br />What is the name of your organization?<br /> [Unknown]: ------------<br />What is the name of your City or Locality?<br /> [Unknown]: ------------<br />What is the name of your State or Province?<br /> [Unknown]: ------------<br />What is the two-letter country code for this unit?<br /> [Unknown]: ------------<br /><br />D:\android-sdk-windows-1.6_r1\tools><span style="font-weight: bold;">keytool -list -alias aliasname -keystore projectkey.keystore</span><br />Enter keystore password:<br /><span style="font-weight: bold;">aliasname</span>, Dec 7, 2010, PrivateKeyEntry,<br />Certificate fingerprint (MD5): CA:CF:AA:0E:5A:2B:88:C8:64:F1:FA:F7:29:21:50:FF<br /><br />Using your Certificate fingerprint (MD5) get google api key from this site<br />http://code.google.com/android/maps-api-signup.htmlKeerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com5tag:blogger.com,1999:blog-5197277884634195738.post-3657533010681253502011-07-30T01:52:00.001-07:002020-07-12T01:50:44.235-07:00How to Install APK Files on Android Emulator using command prompt (cmd) without eclipseHere we are going to see how to run (install) the apk file to the Android emulator without using eclipse.<br /><br />Installing in different OS:<br /><br />1. a Windows, Mac OS X (intel) or Linux (i386) powered computer<br />2. the Android Software Development Kit (SDK).<br /><br /><br />You do not need Eclipse or Eclipse-plugin the Android Development Tools (ADT). The last two are required for the software development for Android, but are not necessary for application evaluation on emulator.<br /><br />First download the Android SDK, which may be obtained here: http://code.google.com/android/download.html<br /><br />Follow the instruction, described in the topic "Installing SDK" from the Google manual, which is located here: http://code.google.com/android/intro/installing.html#installingsdk or you can go with <a href="http://android-codes-examples.blogspot.com/search/label/installation">http://android-codes-examples.blogspot.com/search/label/installation</a><br /><br />Steps need to be followed:<br /><br /> Installing the SDK<br /><br /> After downloading the SDK, unpack the .zip archive to a suitable location on your machine. For the rest of this document, we will refer to the directory where you installed the SDK as $SDK_ROOT.<br /><br /> Optionally, you can add $SDK_ROOT/tools and $SDK_ROOT/platform-tools to your path:<br /><br /> On Linux, edit your ~/.bash_profile or ~/.bashrc file. Look for a line that sets the PATH environment variable and add the full path to your $SDK_ROOT/tools to it. If you don't see a line setting the path, you can add one:<br /><br /> export PATH=${PATH}: <path to="" your="" sdk_root="" tools=""><br /> On a Mac, look in your home directory for .bash_profile and proceed as for Linux. You can create the .bash_profile, if you haven't already set one up on your machine.<br /> On Windows, right click on My Computer, and select Properties. Under the Advanced tab, hit the Environment Variables button, and in the dialog that comes up, double-click on Path under System Variables, and add the full path to the tools/ directory under $SDK_ROOT to it.<br /><br /> Adding $SDK_ROOT/tools to your path lets you run Android Debug Bridge (adb) and the other command line tools without needing to supply the full path to the tools directory. Note that, if you update your SDK, you should remember to update your PATH settings to point to the new location, if different.<br /><br /><br />-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-<br /><br />Now navigate to the $SDK_ROOT/tools directory and execute the file 'emulator.exe'. This is the Android emulator itself. Wait couple of minutes until it loads. Now you must see the home screen of Android - wallpapers with snowy mountains on the background and the navigation bar below.<br /><br />Enjoyed? Ok! Let's continue. Download and save locally a APK-file which you want to install/evaluate on the emulator. We recommend to save the APK file directly in the $SDK_ROOT/tools directory.<br /><br />Note: APK probably stands for "Android package". It's an application distribution unit in the Android environment. If you are coming from the Windows Mobile world, think of APK as of CAB-files.<br /><br />Ok, now start the console ("Start -> Run... -> type 'cmd'" for Windows computers). Type-in the following command: <span style="font-weight: bold;">adb install $APK</span> where $APK is the name of the APK file. For example: <span style="font-weight: bold;">adb install Snake.apk</span><br /><br />If you receive a "path not found" error, then you probably either didn't add path to the $SDK_ROOT/tools directory to your system PATH settings or the application you are trying to install is not in the <span style="font-weight: bold;">$SDK_ROOT/tools or $SDK_ROOT/platform-tools</span> directory.<br /><br />If all went without errors then you should be seeing your newly installed APK on the emulator.</path>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com4tag:blogger.com,1999:blog-5197277884634195738.post-21410579598725739982011-07-11T20:30:00.000-07:002020-07-12T01:50:48.778-07:00Design a linearlayout or textview and any component in android with border,corner radius and gradientWe can design a linearlayout or relativelayout or framelayout... or even we can design a text view with this coding. The sample is given for linear layout, the result will be like this<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-G7w_hm-F8fA/ThvA4bzncDI/AAAAAAAAAGo/Iyhwa88UqNo/s1600/border_corner_layout_design.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 257px;" src="http://2.bp.blogspot.com/-G7w_hm-F8fA/ThvA4bzncDI/AAAAAAAAAGo/Iyhwa88UqNo/s320/border_corner_layout_design.JPG" alt="" id="BLOGGER_PHOTO_ID_5628304235099025458" border="0" /></a>here i had given corner radius for left top corner and left bottom corner for the linear layout, and i applied border for whole linearlayout.<br /><br />For design the layout we need to use this xml file<br /><br /><div class="innerblock"><br /><strong>layout_border.xml</strong><br /><pre class="innertextblock"><?xml version="1.0" encoding="utf-8"?><br /><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><br /><item><br /> <shape android:shape="rectangle"><br /> <stroke android:width="2dp" android:height="2dp"<br /> android:color="#FF0000" /><br /> <solid android:color="#000000" /><br /> <padding android:left="1dp" android:top="1dp" android:right="1dp"<br /> android:bottom="1dp" /><br /><br /> <corners android:radius="1dp" android:bottomRightRadius="5dp"<br /> android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"<br /> android:topRightRadius="0dp" /><br /> </shape><br /></item><br /><br /></layer-list><br /></pre> </div><br /><br />and we need to set the xml file as background for the linear layout as<br /><br /><div class="innerblock"><br /><strong>In the main xml</strong><br /><pre class="innertextblock"><LinearLayout android:layout_gravity="center"<br />android:layout_width="200dp" android:layout_height="200dp" android:background="@drawable/layout_border" /><br /></pre> </div><br /><br />For gradient effect you can use the below xml, this will not be in the source, you can put this file and set this as background for gradient effect<br /><br /><div class="innerblock"> <br /><strong>layout_gradient.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><br /> <gradient<br /> android:startColor="#ffa5ba"<br /> android:endColor="#ff92af"<br /> android:angle="150"/><br /><br /></shape><br /></pre> </div><br /><br />The code is fulling checked so you can run the code after downloading it itself.<br /><br />You can download the full source code<br /><br /><div class="downloadblock"><br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNM1p2UjNJbUxXUFU/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com4tag:blogger.com,1999:blog-5197277884634195738.post-18976733158838966122011-04-21T02:58:00.001-07:002020-07-12T01:50:52.496-07:00Google map example in android with info window (Popup) and multiple markers and zoom controlsThrough this application we can populate a google map with the info window, zoom controls and marker features. The image of the running application is shown below<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-PrYuYQMcGcc/Ta__3fCcw7I/AAAAAAAAAGU/xCxjK3slU4k/s1600/google_map_marker_tooltip_popup_android.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 222px; height: 320px;" src="http://3.bp.blogspot.com/-PrYuYQMcGcc/Ta__3fCcw7I/AAAAAAAAAGU/xCxjK3slU4k/s320/google_map_marker_tooltip_popup_android.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5597974190534476722" /></a><br /><br />First of all, we can run the application with the google api sdk only, we can use from API level 3, but the Google api sdk should be used to run the application.<br /><br /><H3> Custom Linear layout is used on this application </H3><br /><br /><div class="innerblock"> <br /><strong>LocationViewers.java</strong><br /><pre class="innertextblock" ><br />package com.icons.draw.view;<br /><br />import java.util.ArrayList;<br />import java.util.List;<br />import android.content.Context;<br />import android.util.AttributeSet;<br />import android.view.View;<br />import android.widget.LinearLayout;<br />import android.widget.ZoomControls;<br /><br />import com.google.android.maps.MapController;<br />import com.google.android.maps.MapView;<br />import com.icons.draw.R;<br /><br />public class LocationViewers extends LinearLayout {<br /><br /> private MapLocationOverlay overlay;<br /> <br /> // Known lat/long coordinates that we'll be using.<br /> private List<MapLocation> mapLocations;<br /> <br /> public static MapView mapView;<br /> <br /> public LocationViewers(Context context, AttributeSet attrs) {<br /> super(context, attrs);<br /> init();<br /> }<br /><br /> public LocationViewers(Context context) {<br /> super(context);<br /> init();<br /> }<br /> <br /> public void init() { <br /><br /> setOrientation(VERTICAL);<br /> setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));<br /> String api = getResources().getString(R.string.map_api_key);<br /> mapView = new MapView(getContext(),api);<br /> mapView.setEnabled(true);<br /> mapView.setClickable(true);<br /> addView(mapView);<br /> overlay = new MapLocationOverlay(this);<br /> mapView.getOverlays().add(overlay);<br /> mapView.getController().setZoom(5);<br /> mapView.getController().setCenter(getMapLocations().get(0).getPoint());<br /> }<br /> <br /> <br /> public List<MapLocation> getMapLocations() {<br /> if (mapLocations == null) {<br /> mapLocations = new ArrayList<MapLocation>();<br /> mapLocations.add(new MapLocation("Avinashi road, Coimbatore",11.0138,76.9871));<br /> mapLocations.add(new MapLocation("Marina Beach, Chennai",13.0548,80.2830));<br /> mapLocations.add(new MapLocation("Taj Mahal, New Delhi",28.6353,77.2250));<br /> mapLocations.add(new MapLocation("Meenakshi Temple, Madurai",9.9195,78.1208));<br /> }<br /> return mapLocations;<br /> }<br /><br /> public MapView getMapView() {<br /> return mapView;<br /> }<br />}<br /><br /></pre> </div><br /><br />In this LocationViewers.java only we are using the MapView, then this file will be called in the map.xml<br /><br /><div class="innerblock"> <br /><strong>map.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:id="@+id/home_container" android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /><br /> <com.icons.draw.view.LocationViewers<br /> android:id="@+id/map_location_viewer" android:layout_width="fill_parent"<br /> android:layout_height="fill_parent" ><br /><br /> <br /></com.icons.draw.view.LocationViewers><br /><br /><ZoomControls android:id="@+id/zoomcontrols"<br /> android:gravity="bottom"<br /> android:layout_width="wrap_content" android:layout_height="wrap_content" /><br /></FrameLayout><br /></pre> </div><br /><br />In the above mentioned xml file we had used the ZoomControls also, this will be given the zoom listener on the java file<br /><br /><div class="innerblock"> <br /><strong>DrawIcons.java</strong><br /><pre class="innertextblock" ><br />package com.icons.draw;<br /><br />import com.google.android.maps.MapActivity;<br /><br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.LinearLayout;<br />import android.widget.ZoomControls;<br />import com.icons.draw.view.LocationViewers;<br /><br />public class DrawIcons extends MapActivity {<br /> @Override<br /> public void onCreate(Bundle icicle) {<br /> <br /> super.onCreate(icicle);<br /><br /> setContentView(R.layout.map);<br /> <br /> <br /> ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);<br /> zoomControls.setOnZoomInClickListener(new View.OnClickListener() {<br /> @Override<br /> public void onClick(View v) { <br /> LocationViewers.mapView.getController().zoomIn(); <br /> }<br /> });<br /> zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {<br /> @Override<br /> public void onClick(View v) {<br /> LocationViewers.mapView.getController().zoomOut();<br /> }<br /> });<br /> }<br /><br /> /**<br /> * Must let Google know that a route will not be displayed<br /> */<br /> @Override<br /> protected boolean isRouteDisplayed() {<br /> return false;<br /> } <br />}<br /><br /></pre> </div><br /><br />While pressing the zoom controls plus or minus, this listener will be called and the zoom functionality will be occurs. Then we are going to see two files <b>MapLocation.java</b> and <b>MapLocationOverlay.java</b>, where in the MapLocationOverlay file we will be drawing the marker and the info window for each marker, you can get the code from the zip file below.<br /><br />The main thing to run the application is adding the internet permission and google library in the AndroidManifest.xml file<br /><br /><div class="innerblock"> <br /><strong>AndroidManifest.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.icons.draw"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".DrawIcons"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> <uses-library android:name="com.google.android.maps" /><br /> </application><br /> <uses-permission android:name="android.permission.INTERNET" /><br /> <uses-sdk android:minSdkVersion="4" /><br /><br /></manifest> <br /><br /></pre> </div><br /><br />The code is fulling checked so you can run the code after downloading it itself.<br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNV0dfSzN6eTlTZjg/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com26tag:blogger.com,1999:blog-5197277884634195738.post-41773834246250875852011-04-05T02:03:00.000-07:002020-07-12T01:50:57.106-07:00Animated customized dialog or popup transparent layout with Button and Linear layout gradient effect in androidThe image shown below is an example of animated customized popup with transparent layout for button and linearlayout, and the linearlayout and button is given gradient effect<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-X0MlgmWVXe0/TZra7WYoboI/AAAAAAAAAGM/iT16gG7oChE/s1600/animated_popup_gradient_button_linearlayout.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 224px; height: 320px;" src="http://2.bp.blogspot.com/-X0MlgmWVXe0/TZra7WYoboI/AAAAAAAAAGM/iT16gG7oChE/s320/animated_popup_gradient_button_linearlayout.JPG" alt="" id="BLOGGER_PHOTO_ID_5592022600489922178" border="0" /></a><br /><br />How to get a transparent layout? For that we need to use a transparent class<br /><br /><div class="innerblock"> <br /><strong>TransparentPanel.java</strong><br /><pre class="innertextblock">package com.popup.design.layers;<br /><br />import android.content.Context;<br />import android.graphics.Canvas;<br />import android.graphics.Paint;<br />import android.graphics.RectF;<br />import android.graphics.Paint.Style;<br />import android.util.AttributeSet;<br />import android.widget.LinearLayout;<br /><br />public class TransparentPanel extends LinearLayout<br />{<br />private Paint innerPaint, borderPaint ;<br /> <br />public TransparentPanel(Context context, AttributeSet attrs) {<br /> super(context, attrs);<br /> init();<br />}<br /><br />public TransparentPanel(Context context) {<br /> super(context);<br /> init();<br />}<br /><br />private void init() {<br /> innerPaint = new Paint();<br /> innerPaint.setARGB(225, 0, 0, 0);<br /> innerPaint.setAntiAlias(true);<br /><br /> borderPaint = new Paint();<br /> borderPaint.setARGB(255, 0, 0, 0);<br /> borderPaint.setAntiAlias(true);<br /> borderPaint.setStyle(Style.STROKE);<br /> borderPaint.setStrokeWidth(2);<br />}<br /><br />public void setInnerPaint(Paint innerPaint) {<br /> this.innerPaint = innerPaint;<br />}<br /><br />public void setBorderPaint(Paint borderPaint) {<br /> this.borderPaint = borderPaint;<br />}<br /><br /> @Override<br /> protected void dispatchDraw(Canvas canvas) {<br /> <br /> RectF drawRect = new RectF();<br /> drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());<br /> <br /> canvas.drawRoundRect(drawRect, 5, 5, innerPaint);<br /> canvas.drawRoundRect(drawRect, 5, 5, borderPaint);<br /><br /> super.dispatchDraw(canvas);<br /> }<br />}<br /></pre> </div><br /><br />We need to use this transparentPanel class in the xml file, where we need the transparent layout, in the popup.xml we are using the com.popup.design.layers.TransparentPanel instead of LinearLayout, see in the code<br /><br /><div class="innerblock"> <br /><strong>popup.xml</strong><br /><pre class="innertextblock"><br /><?xml version="1.0" encoding="utf-8"?><br /><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:orientation="vertical"<br />android:background="@drawable/white"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"><br /><br /><Button android:id="@+id/show_popup_button" android:layout_gravity="bottom"<br /> android:layout_width="wrap_content" android:layout_height="wrap_content"<br /> android:layout_marginBottom="25px" android:text="Show Popup" /><br /><br /><com.popup.design.layers.TransparentPanel<br /> android:id="@+id/popup_window" android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" android:orientation="vertical"<br /> android:layout_gravity="bottom" android:gravity="left" android:padding="1px"<br /> android:background="@drawable/white"><br /><br /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical" android:gravity="right"<br /> android:layout_width="fill_parent" android:layout_height="fill_parent"<br /> android:background="@drawable/button_bar_gradient"><br /><br /> <Button android:id="@+id/hide_popup_button"<br /> android:layout_width="wrap_content" android:layout_height="wrap_content"<br /> android:layout_alignParentRight="true" android:layout_marginTop="5px"<br /> android:layout_marginRight="10px" android:paddingLeft="5px"<br /> android:paddingRight="5px" style="?android:attr/buttonStyleSmall"<br /> android:textStyle="bold" android:textColor="@drawable/white"<br /> android:textSize="12px" android:text=" Close " android:background="@drawable/button_close"/><br /><br /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical" android:layout_width="fill_parent"<br /> android:layout_height="1px" android:layout_marginTop="5px"<br /> android:layout_below="@+id/hide_popup_button" android:background="@drawable/white" /><br /> </RelativeLayout><br /><br /> <TextView android:id="@+id/location_name"<br /> android:layout_width="wrap_content" android:layout_height="wrap_content"<br /> android:textStyle="bold" android:textSize="16px" android:textColor="@drawable/white"<br /> android:layout_marginTop="5px" android:layout_marginLeft="5px" /><br /><br /> <TextView android:id="@+id/location_description"<br /> android:layout_width="wrap_content" android:layout_height="wrap_content"<br /> android:textColor="@drawable/white" android:layout_margin="5px" /><br /><br /></com.popup.design.layers.TransparentPanel><br /><br /></FrameLayout><br /></pre> </div><br /><br />Customized Popup is the com.popup.design.layers.TransparentPanel, initially we will hide this layout, and on the button click will show this layout. The java code is as follows<br /><br /><div class="innerblock"> <br /><strong>AnimatePopup.java</strong><br /><pre class="innertextblock">package com.popup.design;<br /><br />import com.popup.design.layers.TransparentPanel;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.animation.Animation;<br />import android.view.animation.AnimationUtils;<br />import android.widget.Button;<br />import android.widget.TextView;<br /><br />/**<br />*<br />* @author http://www.android-codes-examples.blogspot.com/<br />*<br />*/<br />public class AnimatePopup extends Activity {<br /><br />private Animation animShow, animHide;<br /><br />@Override<br />public void onCreate(Bundle icicle) {<br /> <br /> super.onCreate(icicle);<br /><br /> setContentView(R.layout.popup);<br /> <br /> initPopup();<br /> }<br /> <br /> private void initPopup() {<br /> <br /> final TransparentPanel popup = (TransparentPanel) findViewById(R.id.popup_window);<br /><br /> // Start out with the popup initially hidden.<br /> popup.setVisibility(View.GONE);<br /> <br /> <br /> animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);<br /> animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);<br /> <br /> final Button showButton = (Button) findViewById(R.id.show_popup_button);<br /> final Button hideButton = (Button) findViewById(R.id.hide_popup_button);<br /> showButton.setOnClickListener(new View.OnClickListener() {<br /> public void onClick(View view) {<br /> popup.setVisibility(View.VISIBLE);<br /> popup.startAnimation( animShow );<br /> showButton.setEnabled(false);<br /> hideButton.setEnabled(true);<br /> }});<br /> <br /> hideButton.setOnClickListener(new View.OnClickListener() {<br /> public void onClick(View view) {<br /> popup.startAnimation( animHide );<br /> showButton.setEnabled(true);<br /> hideButton.setEnabled(false);<br /> popup.setVisibility(View.GONE);<br /> }});<br /><br /><br /> final TextView locationName = (TextView) findViewById(R.id.location_name);<br /> final TextView locationDescription = (TextView) findViewById(R.id.location_description);<br /> <br /> locationName.setText("Animated Popup");<br /> locationDescription.setText("Animated popup is created by http://www.android-codes-examples.blogspot.com/"<br /> + " Transparent layout is used on this example, and animation xml is also used"<br /> + " on this example. Have a Good day guys.");<br />}<br />}<br /></pre> </div><br /><br />For giving gradient effect we need to use this xml file as the background for the linearlayout or button. The xml file is<br /><br /><div class="innerblock"> <br /><strong>button_bar_gradient.xml</strong><br /><pre class="innertextblock"><?xml version="1.0" encoding="utf-8"?><br /><shape xmlns:android="http://schemas.android.com/apk/res/android"><br /><gradient android:startColor="#C0C0C0"<br /> android:endColor="#505050"<br /> android:angle="90"/> <br /> <corners android:radius="2px" /><br /></shape><br /><br /></pre> </div><br /><br />In the popup.xml we had used the button_bar_gradient as a background. This button_bar_gradient.xml will be in drawable folder.<br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNaG9BWkVtMTRfRFU/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com17tag:blogger.com,1999:blog-5197277884634195738.post-10112011690808394512011-04-04T20:28:00.000-07:002020-07-12T01:51:01.511-07:00ListActivity with remembering the last clicks of the radio button in androidWe are going to see about using the ListActivity with radio button example and we are going to see, how to remember the last clicked on this listactivity before going to another activity. The view will be like this<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-B6VP-5MYj8k/TZqMbCTv0kI/AAAAAAAAAGE/vukWUVL3v_k/s1600/listview_radio_click_data.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 224px; height: 320px;" src="http://4.bp.blogspot.com/-B6VP-5MYj8k/TZqMbCTv0kI/AAAAAAAAAGE/vukWUVL3v_k/s320/listview_radio_click_data.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5591936283437945410" /></a><br />The ListActivity with radio button can be done by using this xml code in the layout<br /><br /><div class="innerblock"> <br /><strong>XML</strong><br /><pre class="innertextblock" ><br /><ListView<br />android:id="@android:id/list"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent"<br />android:background="#2c0bf9"<br />android:cacheColorHint="#2c0bf9"<br />android:drawSelectorOnTop="false"<br />/><br /></pre> </div><br /><br />In the above mentioned xml, we used the android:id as @android:id/list, it is because we are going to use ListActivity. After that we need to insert the code in the java file as <br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />setListAdapter(new ArrayAdapter<String>(this,<br /> android.R.layout.simple_list_item_single_choice, items));<br /></pre> </div><br /><br />and for check operation we need to call the selector function, in that itemClicked is a int array where we will be storing whether the row is clicked or not, this will be maintained of item click, the selector and item click functions are follows<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />private void selector(int first,int total) {<br /> for (int i = 0; i < total; i++) {<br /> View v = getListView().getChildAt(i);<br /> CheckedTextView check = (CheckedTextView) v;<br /> if(itemClicked[first+i]==1)<br /> check.setChecked(true);<br /> else<br /> check.setChecked(false); <br /> }<br />}<br /><br />@Override<br />protected void onListItemClick(ListView l, View v, int position, long id) {<br /> selectFlag = true;<br /> CheckedTextView check = (CheckedTextView) v;<br /> boolean click = !check.isChecked();<br /> check.setChecked(click);<br /> if (click) {<br /> itemClicked[position] = 1;<br /> } else {<br /> itemClicked[position] = 0;<br /> } <br />}<br /></pre> </div><br /><br />We need the scroller listener, so that the last checked can be checked while scrolling, for implementing the scroll first we need to implement scroll listener as<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />public class StoreClicks extends ListActivity implements ListView.OnScrollListener<br /></pre> </div><br /><br />then in the onscroll we need to call the selector method<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />@Override<br />public void onScroll(AbsListView view, int firstVisibleItem,<br /> int visibleItemCount, int totalItemCount) {<br /> <br /> if(visibleItemCount == 6 && !firstTimeFlag) {<br /> selector(0,6);<br /> firstTimeFlag = true;<br /> }<br />}<br /><br />@Override<br />public void onScrollStateChanged(AbsListView view, int scrollState) {<br /> switch (scrollState) {<br /> case OnScrollListener.SCROLL_STATE_IDLE:<br /> int first = view.getFirstVisiblePosition();<br /> int last = view.getLastVisiblePosition();<br /> int childCount = view.getChildCount();<br /> selector(first,childCount);<br /> break;<br /><br /> case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:<br /> break;<br /><br /> case OnScrollListener.SCROLL_STATE_FLING:<br /> break;<br /> } <br /><br />}<br /></pre> </div><br /><br />In the onScroll method the selector will be called only one time, after that onScrollStateChanged selector method will be called, this is why means, for the first page only the items will be visible, we can check or uncheck, so only we are doing like this. The full StoreClicks.java file will be like this<br /><br /><div class="innerblock"> <br /><strong>StoreClicks.java</strong><br /><pre class="innertextblock" ><br />package com.clicks.list;<br /><br />import android.app.ListActivity;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.AbsListView;<br />import android.widget.ArrayAdapter;<br />import android.widget.Button;<br />import android.widget.CheckedTextView;<br />import android.widget.ListView;<br />import android.widget.Toast;<br />import android.widget.AbsListView.OnScrollListener;<br /><br />/**<br /> * <br /> * @author http://www.android-codes-examples.blogspot.com/<br /> * <br /> */<br /><br />public class StoreClicks extends ListActivity implements<br /> ListView.OnScrollListener {<br /> private static String[] items = { "Joseph", "George", "Mary", "Antony",<br /> "Albert", "Michel", "John", "Abraham", "Mark", "Savior",<br /> "Kristopher", "Thomas", "Williams", "Assisi", "Sebastian",<br /> "Aloysius", "Alex", "Daniel", "Anto", "Alexandar", "Brito",<br /> "Robert", "Jose", "Paul", "Peter" };<br /> private int[] itemClicked = new int[25];<br /> private boolean selectFlag = false; // this will be changed to true if a<br /> private boolean firstTimeFlag = false;<br /> // item is clicked<br /><br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> Button click = (Button) findViewById(R.id.click);<br /> <br /> Bundle extras = getIntent().getExtras();<br /> try{<br /> itemClicked = (int[])extras.getIntArray("clicks");<br /> }catch(NullPointerException e){}<br /> if(null == itemClicked)<br /> {<br /> itemClicked = new int[25];<br /> for (int i = 0; i < 25; i++) {<br /> itemClicked[i] = 0;<br /> }<br /> }<br /> click.setOnClickListener(new View.OnClickListener() {<br /><br /> @Override<br /> public void onClick(View v) {<br /> if (selectFlag) {<br /> Intent intent = new Intent(StoreClicks.this, TabPage.class);<br /> Bundle extras = new Bundle();<br /> extras.putIntArray("clicks", itemClicked);<br /> intent.putExtras(extras);<br /> startActivity(intent);<br /> finish();<br /> } else {<br /> Toast.makeText(StoreClicks.this,<br /> "You must select atleast an item",<br /> Toast.LENGTH_SHORT).show();<br /> }<br /> }<br /> });<br /> setListAdapter(new ArrayAdapter<String>(this,<br /> android.R.layout.simple_list_item_single_choice,<br /> items));<br /> getListView().setOnScrollListener(this);<br /> }<br /><br /> private void selector(int first,int total) {<br /> for (int i = 0; i < total; i++) {<br /> View v = getListView().getChildAt(i);<br /> CheckedTextView check = (CheckedTextView) v;<br /> if(itemClicked[first+i]==1)<br /> check.setChecked(true);<br /> else<br /> check.setChecked(false); <br /> }<br /> }<br /><br /> @Override<br /> protected void onListItemClick(ListView l, View v, int position, long id) {<br /> selectFlag = true;<br /> CheckedTextView check = (CheckedTextView) v;<br /> boolean click = !check.isChecked();<br /> check.setChecked(click);<br /> if (click) {<br /> itemClicked[position] = 1;<br /> } else {<br /> itemClicked[position] = 0;<br /> } <br /> }<br /><br /> @Override<br /> public void onScroll(AbsListView view, int firstVisibleItem,<br /> int visibleItemCount, int totalItemCount) {<br /> <br /> if(visibleItemCount == 6 && !firstTimeFlag) {<br /> selector(0,6);<br /> firstTimeFlag = true;<br /> }<br /> }<br /><br /> @Override<br /> public void onScrollStateChanged(AbsListView view, int scrollState) {<br /> switch (scrollState) {<br /> case OnScrollListener.SCROLL_STATE_IDLE:<br /> int first = view.getFirstVisiblePosition();<br /> int last = view.getLastVisiblePosition();<br /> int childCount = view.getChildCount();<br /> selector(first,childCount);<br /> break;<br /><br /> case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:<br /> break;<br /><br /> case OnScrollListener.SCROLL_STATE_FLING:<br /> break;<br /> } <br /><br /> }<br /><br />}<br /><br /></pre> </div><br /><br />The itemClicked array should be passed in the bundle to the next activity and vice verse to this activity, so that the clicks will be maintained and we can check it explicitly<br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNcy1idzB3TkdIUjQ/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com3tag:blogger.com,1999:blog-5197277884634195738.post-59991381768135086062011-03-29T21:28:00.000-07:002020-07-12T01:51:06.595-07:00ListActivity with checkbox using AsyncTask with sleep time example in androidWe are going to see about using the ListActivity with checkbox example and we are going to see, how to using the AsyncTask with a good example, here we will be using sleep call, it is optional. The view will be like this<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-AHogGzUh-pk/TZKxfQLIE9I/AAAAAAAAAF8/k44ZWUgxhY8/s1600/asyncTask_listActivity_simple_list_item_checked.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 223px; height: 320px;" src="http://2.bp.blogspot.com/-AHogGzUh-pk/TZKxfQLIE9I/AAAAAAAAAF8/k44ZWUgxhY8/s320/asyncTask_listActivity_simple_list_item_checked.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5589725237995901906" /></a><br /><br />First we can see about using sleep option in android, in our code we used this <br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br /> for (String item : items) {<br /> publishProgress(item);<br /> SystemClock.sleep(200);<br /> }<br /></pre> </div><br /><br />Here SystemClock.sleep(200); is used to sleep for a while, for this ListActivity with checkBox or AsyncTask this is not compulsory, this is used in this example for making to know about this sleep option.<br /><br />The ListActivity with check box can be done by using this xml code in the layout<br /><br /><div class="innerblock"> <br /><strong>XML</strong><br /><pre class="innertextblock" ><br /><ListView<br />xmlns:android="http://schemas.android.com/apk/res/android"<br />android:id="@android:id/list"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent"<br />android:drawSelectorOnTop="false"<br />/><br /></pre> </div><br /><br />like that we need to insert the code in the java file as <br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />setListAdapter(new ArrayAdapter<String>(this,<br /> android.R.layout.simple_list_item_checked, new ArrayList()));<br /></pre> </div><br /><br />and for check operation we need to override the function as<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />@Override<br />protected void onListItemClick(ListView l, View v, int position, long id) {<br /> CheckedTextView check = (CheckedTextView)v;<br /> check.setChecked(!check.isChecked());<br />}<br /></pre> </div><br /><br />That will do for showing a List view using the ListActivity, for using the AsyncTask, we need to call the AsyncTask class like<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />new AddStringTask().execute();<br /></pre> </div><br /><br />On this call the AsyncTask class will be called <br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />class AddStringTask extends AsyncTask<Void, String, Void> {<br /> @Override<br /> protected Void doInBackground(Void... unused) {<br /> for (String item : items) {<br /> publishProgress(item);<br /> SystemClock.sleep(200);<br /> }<br /><br /> return (null);<br /> }<br /><br /> @Override<br /> protected void onProgressUpdate(String... item) {<br /> ((ArrayAdapter) getListAdapter()).add(item[0]);<br /> }<br /><br /> @Override<br /> protected void onPostExecute(Void unused) {<br /> setSelection(3);<br /> Toast.makeText(AsyncTaskDemo.this, "Done!", Toast.LENGTH_SHORT).show();<br /> }<br />}<br /></pre> </div><br /><br />The full AsyncTaskDemo.java file will be like this<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />package com.async.demo;<br /><br />import android.app.ListActivity;<br />import android.os.AsyncTask;<br />import android.os.Bundle;<br />import android.os.SystemClock;<br />import android.view.View;<br />import android.widget.ArrayAdapter;<br />import android.widget.CheckedTextView;<br />import android.widget.ListView;<br />import android.widget.Toast;<br />import java.util.ArrayList;<br /><br />/**<br /> * <br /> * @author http://www.android-codes-examples.blogspot.com/<br /> *<br /> */<br /><br />public class AsyncTaskDemo extends ListActivity {<br /> private static String[] items = { "Joseph", "George", "Mary", "Antony", "Albert",<br /> "Michel", "John", "Abraham", "Mark", "Savior", "Kristopher",<br /> "Thomas", "Williams", "Assisi", "Sebastian", "Aloysius", "Alex", "Daniel",<br /> "Anto", "Alexandar", "Brito", "Robert", "Jose",<br /> "Paul", "Peter" };<br /><br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /><br /> setListAdapter(new ArrayAdapter<String>(this,<br /> android.R.layout.simple_list_item_checked, new ArrayList()));<br /><br /> new AddStringTask().execute();<br /> }<br /><br /> class AddStringTask extends AsyncTask<Void, String, Void> {<br /> @Override<br /> protected Void doInBackground(Void... unused) {<br /> for (String item : items) {<br /> publishProgress(item);<br /> SystemClock.sleep(200);<br /> }<br /><br /> return (null);<br /> }<br /><br /> @Override<br /> protected void onProgressUpdate(String... item) {<br /> ((ArrayAdapter) getListAdapter()).add(item[0]);<br /> }<br /><br /> @Override<br /> protected void onPostExecute(Void unused) {<br /> setSelection(3);<br /> Toast.makeText(AsyncTaskDemo.this, "Done!", Toast.LENGTH_SHORT).show();<br /> }<br /> }<br /> <br /> @Override<br /> protected void onListItemClick(ListView l, View v, int position, long id) {<br /> CheckedTextView check = (CheckedTextView)v;<br /> check.setChecked(!check.isChecked());<br /> }<br /><br /> <br />}<br /></pre> </div><br /><br />You can download the full source code<br /><br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNVGJnUG16RkFVXzg/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com7tag:blogger.com,1999:blog-5197277884634195738.post-49721045819629298562011-03-29T02:57:00.000-07:002020-07-12T01:51:11.367-07:00Example code for OnKeyDown listener in AndroidIn this example Toast is shown for the key presses.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-yJn03DDaWVc/TZGtOb7byKI/AAAAAAAAAF0/vYKg_QMr6EE/s1600/onKeyDown_press.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 252px; height: 138px;" src="http://4.bp.blogspot.com/-yJn03DDaWVc/TZGtOb7byKI/AAAAAAAAAF0/vYKg_QMr6EE/s320/onKeyDown_press.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5589439076070312098" /></a><br /><br />The approach is simple, just we need to put this code, thats all<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />@Override<br /> public boolean onKeyDown(int keyCode, KeyEvent event) {<br /> super.onKeyDown(keyCode, event);<br /> switch(keyCode)<br /> {<br /> case KeyEvent.KEYCODE_CAMERA:<br /> Toast.makeText(KeyActions.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();<br /> return true;<br /> case KeyEvent.KEYCODE_1:<br /> Toast.makeText(KeyActions.this, "Pressed 1", Toast.LENGTH_SHORT).show();<br /> return true;<br /> case KeyEvent.KEYCODE_HOME:<br /> Toast.makeText(KeyActions.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();<br /> return true;<br /> <br /> case KeyEvent.KEYCODE_BACK:<br /> Toast.makeText(KeyActions.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();<br /> Intent result = new Intent("Complete");<br /> setResult(Activity.RESULT_OK, result);<br /> finish();<br /> return true;<br /> }<br /><br /> return false;<br /> }<br /></pre> </div><br /><br />For every key there is the code, after entering the KeyEvent. the eclipse will show the list of codes available.<br /><br />If you have doubts means <br /><br />The full source code<br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNcjJJRlBDUnVOSXc/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com2tag:blogger.com,1999:blog-5197277884634195738.post-69846581517667822082011-03-23T20:52:00.001-07:002020-07-12T01:51:15.514-07:00Customized ListView item's selection color can be changed on touching or selecting the item in the listOn selection, on focus, on touch of the items we can change the color of the item, by default android will show a color, we can change it as we like. For that we need this xml to be stored inside the color folder, here only we need mention the colors for each listeners<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-MaZmwHgvT60/TYrACvkGxmI/AAAAAAAAAFs/H7srlDlA_Ag/s1600/listview_item_selected_color.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 242px; height: 320px;" src="http://1.bp.blogspot.com/-MaZmwHgvT60/TYrACvkGxmI/AAAAAAAAAFs/H7srlDlA_Ag/s320/listview_item_selected_color.JPG" alt="" id="BLOGGER_PHOTO_ID_5587489441066698338" border="0" /></a><br /><br /><div class="innerblock"> <br /><strong>list_bg.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><selector xmlns:android="http://schemas.android.com/apk/res/android"><br /><item <br /> android:state_selected="false"<br /> android:state_pressed="false" <br /> android:drawable="@color/grey" /><br /><item android:state_pressed="true" <br /> android:drawable="@color/blue" /><br /><item android:state_selected="true"<br /> android:state_pressed="false" <br /> android:drawable="@color/blue" /><br /></selector><br /><br /></pre> </div><br /><br />That list_bg.xml will be called in the customized list xml as background<br /><br /><div class="innerblock"> <br /><strong>list.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent" android:layout_height="wrap_content"<br /> android:layout_margin="2dp" android:background="@color/list_bg"><br /><br /> <TextView android:id="@+id/post" android:gravity="center_vertical"<br /> android:layout_width="wrap_content" android:layout_height="50dp"<br /> android:textSize="20sp" android:textColor="#D0640D" <br /> android:layout_toRightOf="@+id/bite_image" /> <br /><br /></RelativeLayout><br /></pre> </div><br /><br />This one is the main xml for the Lister.java<br /><br /><div class="innerblock"> <br /><strong>main.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent" android:layout_height="fill_parent"<br /> android:orientation="vertical"><br /><br /> <ListView android:id="@android:id/list" android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" android:smoothScrollbar="true"<br /> android:background="#fff" android:cacheColorHint="#fff"<br /> android:fastScrollEnabled="false" android:clickable="true"<br /> android:layout_marginBottom="36dp" /><br /><br /></LinearLayout><br /></pre> </div><br /><br /><br />The Lister.java will be like this, the main thing is the list_bg.xml only<br /><br /><div class="innerblock"> <br /><strong>Lister.java</strong><br /><pre class="innertextblock" ><br />package com.list.viewer;<br /><br />import android.app.ListActivity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.BaseAdapter;<br />import android.widget.TextView;<br /><br />public class Lister extends ListActivity{<br /><br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> String[] captionArray = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"};<br /> ItemsAdapter ItemsAdapter = new ItemsAdapter(<br /> Lister.this, R.layout.list,<br /> captionArray);<br /> setListAdapter(ItemsAdapter);<br /> }<br /> <br /> <br /> private class ItemsAdapter extends BaseAdapter {<br /> String[] items;<br /><br /> public ItemsAdapter(Context context, int textViewResourceId,<br /> String[] items) {<br /> // super(context, textViewResourceId, items);<br /> this.items = items;<br /> }<br /><br /> // @Override<br /> public View getView(int position, View convertView, ViewGroup parent) {<br /> <br /> View v = convertView;<br /> if (v == null) {<br /> LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br /> v = vi.inflate(R.layout.list, null);<br /> } <br /> TextView post = (TextView) v<br /> .findViewById(R.id.post);<br /> post.setText(items[position]);<br /> <br /> return v;<br /> }<br /><br /> public int getCount() {<br /> return items.length;<br /> }<br /><br /> public Object getItem(int position) {<br /> return position;<br /> }<br /><br /> public long getItemId(int position) {<br /> return position;<br /> }<br /> }<br />}<br /><br /></pre> </div><br /><br /><br /><br />The full source code<br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNN2dkQnlXNFBJcUU/edit">source</a><br /></div><br /><!--http://www.ziddu.com/download/14315845/ListViewExample.zip.html--><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com13tag:blogger.com,1999:blog-5197277884634195738.post-19404325190851241632011-03-21T04:32:00.000-07:002020-07-12T01:51:20.235-07:00Multiple ListView and custom listview example with click listener functionsMultiple listview can be placed inside a Activity. Here i am having two listviews<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-LhJhV6H0BaE/TYc3X55MQLI/AAAAAAAAAFk/W6DYpif8s6c/s1600/multi_listview_example.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 226px; height: 320px;" src="http://1.bp.blogspot.com/-LhJhV6H0BaE/TYc3X55MQLI/AAAAAAAAAFk/W6DYpif8s6c/s320/multi_listview_example.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5586494746593607858" /></a><br /><br />As shown in the above image, the left side displaying list view is customized listview, it that listview we will be showing a imageview and a textview, On clicking the item of that listview, the second listview will be generated.<br /><br />The xml layout for that page will be like this<br /><br /><div class="innerblock"> <br /><strong>main.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical" android:layout_width="fill_parent" android:gravity="center"<br /> android:layout_height="fill_parent" android:background="#cccccc"><br /> <TextView android:layout_width="fill_parent" android:textColor="#000"<br /> android:layout_height="wrap_content" android:text="Multiple ListView Example" /><br /> <LinearLayout android:orientation="horizontal" android:layout_marginTop="10dp" android:gravity="center_horizontal"<br /> android:layout_width="fill_parent" android:layout_height="wrap_content"><br /> <ListView android:id="@+id/ListView1" android:layout_width="130dp"<br /> android:background="#fff" android:cacheColorHint="#fff"<br /> android:layout_height="wrap_content" /><br /> <TextView android:text="==>" android:layout_height="wrap_content" android:textColor="#000"<br /> android:layout_width="wrap_content"></TextView><br /> <ListView android:id="@+id/ListView2" android:layout_width="130dp"<br /> android:background="#4c6f7e" android:cacheColorHint="#4c6f7e"<br /> android:layout_height="wrap_content" /><br /> </LinearLayout><br /><br /></LinearLayout><br /></pre> </div><br /><br />For the customized listview the coding will be like this, it will show the customized listview with a imageview and textview for each item<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />private String[] list1 = {"Icon", "Icon Creator", "Image", "Image Creator"}; <br />private ListView lister1; <br />lister1=(ListView)findViewById(R.id.ListView1);<br />ItemsAdapter itemsAdapter = new ItemsAdapter(<br /> ListViewExample.this, R.layout.list,<br /> list1);<br />lister1.setAdapter(itemsAdapter);<br /></pre> </div><br /><br />In the above example we need to explicitly use a layout, the layout will be<br /><br /><div class="innerblock"> <br /><strong>list.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="horizontal"<br /> android:layout_width="100dp"<br /> android:layout_height="fill_parent"<br /> ><br /><ImageView<br /> android:layout_width="wrap_content" android:layout_height="wrap_content"<br /> android:src="@drawable/icon" android:id="@+id/image"/><br /><TextView android:textColor="#000"<br /> android:layout_width="fill_parent" <br /> android:layout_height="wrap_content" <br /> android:text="" android:id="@+id/desc"<br /> /><br /></LinearLayout><br /></pre> </div><br /><br /><br />For the second listview, is normal listview, which is used with the pre defined layout android.R.layout.simple_list_item_1<br /><br />The full ListViewExample.java file will be like this<br /><br /><div class="innerblock"> <br /><strong>ListViewExample.java</strong><br /><pre class="innertextblock" ><br />package com.list.viewer;<br /><br />import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.AdapterView;<br />import android.widget.ArrayAdapter;<br />import android.widget.BaseAdapter;<br />import android.widget.ImageView;<br />import android.widget.ListView;<br />import android.widget.TextView;<br />import android.widget.AdapterView.OnItemClickListener;<br /><br />/** http://www.android-codes-examples.blogspot.com/ */<br />public class ListViewExample extends Activity {<br /><br /> private String[] list1 = { "Icon", "Icon Creator", "Image", "Image Creator" };<br /> private String[] list21 = { "Design", "Creation", "Selection", "Caller" };<br /> private String[] list22 = { "Investor", "Producer", "Controller",<br /> "Publisher" };<br /> private String[] list23 = { "Designer", "Maker", "Creator", "Asignment" };<br /> private String[] list24 = { "Arranger", "Caller", "Solution", "Result" };<br /><br /> private ListView lister1;<br /> private ListView lister2;<br /><br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> lister1 = (ListView) findViewById(R.id.ListView1);<br /> lister2 = (ListView) findViewById(R.id.ListView2);<br /> ItemsAdapter itemsAdapter = new ItemsAdapter(ListViewExample.this,<br /> R.layout.list, list1);<br /> lister1.setAdapter(itemsAdapter);<br /> lister2.setAdapter(new ArrayAdapter<String>(ListViewExample.this,<br /> android.R.layout.simple_list_item_1, list21));<br /> lister1.setOnItemClickListener(new OnItemClickListener() {<br /><br /> @Override<br /> public void onItemClick(AdapterView<?> arg0, View arg1,<br /> int positon, long arg3) {<br /> String[] lister = null;<br /> switch (positon) {<br /> case 0:<br /> lister = list21;<br /> break;<br /> case 1:<br /> lister = list22;<br /> break;<br /> case 2:<br /> lister = list23;<br /> break;<br /> case 3:<br /> lister = list24;<br /> break;<br /> default:<br /> lister = list21;<br /> }<br /> lister2.setAdapter(new ArrayAdapter<String>(<br /> ListViewExample.this,<br /> android.R.layout.simple_list_item_1, lister));<br /> }<br /> });<br /> }<br /><br /> private class ItemsAdapter extends BaseAdapter {<br /> String[] items;<br /><br /> public ItemsAdapter(Context context, int textViewResourceId,<br /> String[] items) {<br /> this.items = items;<br /> }<br /><br /> @Override<br /> public View getView(final int position, View convertView,<br /> ViewGroup parent) {<br /> TextView mDescription;<br /> View view = convertView;<br /> ImageView image;<br /> if (view == null) {<br /> LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br /> view = vi.inflate(R.layout.list, null);<br /> }<br /> image = (ImageView) view.findViewById(R.id.image);<br /> mDescription = (TextView) view.findViewById(R.id.desc);<br /> mDescription.setText(items[position]);<br /> image.setBackgroundResource(R.id.image);<br /> return view;<br /> }<br /><br /> public int getCount() {<br /> return items.length;<br /> }<br /><br /> public Object getItem(int position) {<br /> return position;<br /> }<br /><br /> public long getItemId(int position) {<br /> return position;<br /> }<br /> }<br />}<br /></pre> </div><br /><br /><br /><br />The full source code<br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNUC1LWHREdUNzTGc/edit">source</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com26tag:blogger.com,1999:blog-5197277884634195738.post-35652289052298355452011-03-16T21:15:00.000-07:002020-07-12T01:51:24.635-07:00How to display Custom Dialog and designing a page without xml layout is given with example?Custom Dialog appears like this<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-N6qhm3BlRT8/TYGK1OBXFII/AAAAAAAAAFc/dSL8Oy4oa7w/s1600/custom_dialog_without_xml_example.JPG"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 227px; height: 320px;" src="http://3.bp.blogspot.com/-N6qhm3BlRT8/TYGK1OBXFII/AAAAAAAAAFc/dSL8Oy4oa7w/s320/custom_dialog_without_xml_example.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5584897659817628802" /></a><br /><br />First we can see how we are going to display a button without using the xml layout,<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />Button show = new Button(this);<br />show.setText("Click to view a Custom Dialog");<br />/** Setting FILL_PARENT for the width and height of the button */<br />show.setLayoutParams(new Gallery.LayoutParams(<br /> LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));<br />/** Without the layout xml we are going to add the button to the screen */<br />setContentView(show);<br /></pre> </div><br /><br />Instead of using the xml layout in setContentView() we are using the new created Button's variable, likewise if we are using the linearlayout's variable means, we can display the component without using the layout xml.<br /><br />In this example we used the layout params<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />show.setLayoutParams(new Gallery.LayoutParams(<br /> LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));<br /></pre> </div><br /><br />Actually this is used for setting the layout width and height for the button.<br /><br />Now we can come to showing Custom Dialog, for that we need to create a class which exetends Dialog, the class will be like this<br /><br /><div class="innerblock"> <br /><strong>CustomizeDialog.java</strong><br /><pre class="innertextblock" ><br />package com.custom.dialog;<br /><br />import android.app.Dialog;<br />import android.content.Context;<br />import android.view.View;<br />import android.view.Window;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br /><br />public class CustomizeDialog extends Dialog implements OnClickListener {<br /> Button close;<br /><br /> public CustomizeDialog(Context context) {<br /> super(context);<br /> /** It will hide the title */<br /> requestWindowFeature(Window.FEATURE_NO_TITLE); <br /> setContentView(R.layout.main);<br /> <br /> close = (Button) findViewById(R.id.close);<br /> close.setOnClickListener(this);<br /> }<br /><br /> @Override<br /> public void onClick(View v) { <br /> if (v == close)<br /> dismiss();<br /> }<br /><br />}<br /></pre> </div><br /><br />Then in the main class CustomDialog.java we need to call the CustomizeDialog.java class to show the custom dialog, like this<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this);<br />customizeDialog.show();<br /></pre> </div><br /><br />The full code of the CustomDialog.java will be like this<br /><br /><div class="innerblock"> <br /><strong>CustomDialog.java</strong><br /><pre class="innertextblock" ><br />package com.custom.dialog;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.Gallery;<br />import android.view.WindowManager.LayoutParams;<br /><br />public class CustomDialog extends Activity {<br /> /** Called when the activity is first created. */<br /> private Button show;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> <br /> show = new Button(this);<br /> show.setText("Click to view a Custom Dialog");<br /> /** Setting FILL_PARENT for the width and height of the button */<br /> show.setLayoutParams(new Gallery.LayoutParams(<br /> LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));<br /> /** Without the layout xml we are going to add the button to the screen */<br /> setContentView(show);<br /> show.setOnClickListener(new View.OnClickListener() {<br /> <br /> @Override<br /> public void onClick(View v) {<br /> CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this);<br /> customizeDialog.show();<br /> }<br /> });<br /> <br /> <br /> }<br />}<br /></pre> </div><br /><br /><br /><br />The full source code<br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNT2tTNGxfb2FWNzQ/edit">source</a><br /></div><br /><br />For AlertDialog you can see this blog <a href="http://android-codes-examples.blogspot.com/2011/03/how-to-display-alertdialog-and.html">http://android-codes-examples.blogspot.com/2011/03/how-to-display-alertdialog-and.html</a><br /><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com1tag:blogger.com,1999:blog-5197277884634195738.post-23163079188265666432011-03-15T02:46:00.000-07:002020-07-12T01:51:28.161-07:00How to display AlertDialog and selection in Alert Dialog and selection with radio buttons with example?<div dir="ltr" style="text-align: left;" trbidi="on">
We are going to see the Alert Dialog in detail.<br />
<br />
First we going to see how to use a normal AlertDialog<br />
<br />
The below code is enough to call AlertDialog,<br />
<br />
<div class="innerblock">
<br />
<strong>Java</strong><br />
<pre class="innertextblock">
AlertDialog.Builder alert = new AlertDialog.Builder(ShowDialog.this);
alert.setTitle("This is Alert Dialog");
alert.setMessage("Sample alert dialog from http://www.android-codes-examples.blogspot.com");
alert.setIcon(R.drawable.icon);
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
alert.show();</pre>
</div>
<br />
<br />
Next we are going to see how to call a AlertDialog with ListView for selection purpose<br />
<br />
<div class="innerblock">
<br />
<strong>Java</strong><br />
<pre class="innertextblock">
final CharSequence[] items = {"India", "US", "UK", "Australia"};
AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView");
builder.setIcon(R.drawable.icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();</pre>
</div>
<br />
<br />
Next we are going to see how to call a AlertDialog with ListView with Radio button for selection purpose<br />
<br />
<div class="innerblock">
<br />
<strong>Java</strong><br />
<pre class="innertextblock">
final CharSequence[] items = {"India", "US", "UK", "Australia"};
AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView and Radio button");
builder.setIcon(R.drawable.icon);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
</pre>
</div>
<br />
<br />
A sample with all the three examples of alertdialog is follows:<br />
<br />
<div class="innerblock">
<br />
<strong>ShowDialog.java</strong><br />
<pre class="innertextblock">
package com.display.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ShowDialog extends Activity {
/** http://www.android-codes-examples.blogspot.com/ */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button alert = (Button) findViewById(R.id.alert);
Button customList = (Button) findViewById(R.id.customList);
Button customListRadio = (Button) findViewById(R.id.customListRadio);
alert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(ShowDialog.this);
alert.setTitle("This is Alert Dialog");
alert
.setMessage("Sample alert dialog from http://www.android-codes-examples.blogspot.com");
alert.setIcon(R.drawable.icon);
alert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
});
customList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items = {"India", "US", "UK", "Australia"};
AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView");
builder.setIcon(R.drawable.icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
customListRadio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items = {"India", "US", "UK", "Australia"};
AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView and Radio button");
builder.setIcon(R.drawable.icon);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}</pre>
</div>
<br />
<br />
Likewise for multiple choice in the alertdialog we need to use this line<br />
<br />
<div class="innerblock">
<br />
<strong>Java</strong><br />
<pre class="innertextblock">
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
/** ... */
}
});</pre>
</div>
<br />
<br />
<br />
<br />
The full source code<br />
<div class="downloadblock">
<br />
Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNaHZLOEJ1alM1SkE/edit">source</a></div>
<br />
<br />
The different types of AlertDialog images will be like this<br />
<br />
<a href="http://4.bp.blogspot.com/-a8WH7GbnPRU/TX81noCoqHI/AAAAAAAAAFU/J_PYqC9UlBU/s1600/alertdialog_listview_selection_with_radio_button.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5584241017842346098" src="http://4.bp.blogspot.com/-a8WH7GbnPRU/TX81noCoqHI/AAAAAAAAAFU/J_PYqC9UlBU/s320/alertdialog_listview_selection_with_radio_button.JPG" style="cursor: pointer; display: block; height: 320px; margin: 0px auto 10px; text-align: center; width: 243px;" /></a><br />
<a href="http://4.bp.blogspot.com/-GJfz45Aldh0/TX81nq50YMI/AAAAAAAAAFM/clY3BqA_q-o/s1600/alertdialog_listview_selection.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5584241018610671810" src="http://4.bp.blogspot.com/-GJfz45Aldh0/TX81nq50YMI/AAAAAAAAAFM/clY3BqA_q-o/s320/alertdialog_listview_selection.JPG" style="cursor: pointer; display: block; height: 320px; margin: 0px auto 10px; text-align: center; width: 281px;" /></a><br />
<a href="http://3.bp.blogspot.com/-5hVUkqtoMgI/TX81nTdgSVI/AAAAAAAAAFE/J8yU48fCB00/s1600/alertdialog_example.JPG" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5584241012317899090" src="http://3.bp.blogspot.com/-5hVUkqtoMgI/TX81nTdgSVI/AAAAAAAAAFE/J8yU48fCB00/s320/alertdialog_example.JPG" style="cursor: pointer; display: block; height: 320px; margin: 0px auto 10px; text-align: center; width: 288px;" /></a><br />
<br />
For Custom Dialog you can see this blog <a href="http://android-codes-examples.blogspot.com/2011/03/how-to-display-custom-dialog-and.html">http://android-codes-examples.blogspot.com/2011/03/how-to-display-custom-dialog-and.html</a><br />
<br />
Have a good day.</div>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com5tag:blogger.com,1999:blog-5197277884634195738.post-66942186568104255482011-03-14T04:06:00.001-07:002020-07-12T01:51:34.645-07:00Make a phone call using Android code in application?We can write code to make a call from the android application that we created. <br /><br />The below code is enough to make a call from the application,<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />Intent sIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:919894643826"));<br />startActivity(sIntent);<br /></pre> </div><br /><br />We can put this code in a click event, so that on clicking a button or textview the call be done.<br /><br />Likewise we need to add this line inside the Manifest.xml file<br /><br /><div class="innerblock"> <br /><strong>XML</strong><br /><pre class="innertextblock" ><br /><uses-permission android:name="android.permission.CALL_PHONE"></uses><br /></pre> </div><br /><br />The java code will be like this<br /><div class="innerblock"> <br /><strong>Caller.java</strong><br /><pre class="innertextblock" ><br />package com.phone.calller;<br /><br />import android.app.Activity;<br />import android.content.Intent;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.Button;<br /><br />public class Callers extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> Button Phone = (Button) findViewById(R.id.call);<br /> Phone.setOnClickListener(new View.OnClickListener() {<br /> @Override<br /> public void onClick(View v) {<br /> Intent sIntent = new Intent(Intent.ACTION_CALL, Uri<br /> .parse("tel:919894643826"));<br /> startActivity(sIntent);<br /> }<br /> });<br /> }<br />}<br /></pre> </div><br /><br />and the manifest.xml file will be like this<br /><br /><div class="innerblock"> <br /><strong>AndroidManifest.xml</strong><br /><pre class="innertextblock" ><br /><?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.phone.calller"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".Callers"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /><br /> </application><br /><uses-permission android:name="android.permission.CALL_PHONE" /><br /><br /></manifest> <br /></pre> </div><br />Have a good day.<br /><br />The full source code<br /><div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNS2hVdldsOEQ5bkk/edit">source</a><br /></div>Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com7tag:blogger.com,1999:blog-5197277884634195738.post-23559280485791014562011-03-07T22:05:00.001-08:002011-08-05T23:05:51.635-07:00How to run a Runnable thread or UI thread in Android?On the launching time of the application itselef the system creates a thread called "main" in the application. This main thread is also called as UI thread, it will only interact with the components of Android UI toolkit.<br /><br />Here is an example for using the runnable thread in a click event, while clicking the image will be loaded, as it will take time to load the image, we can use the thread as shown below : <br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />public void onClick(View v) {<br /> new Thread(new Runnable() {<br /> public void run() {<br /> final Bitmap bm = loadImage();<br /> mImage.post(new Runnable() {<br /> public void run() {<br /> mImage.setImageBitmap(bm);<br /> }<br /> });<br /> }<br /> }).start();<br />}<br /></pre> </div><br /><br /><br /><H3>Using Handler in Runnable thread</H3><br />Here we are using the runnable thread using the handler. When we send a message to the Handler it will get saved into a queue and get executed by the UI thread as soon as possible.<br /><br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />public class ProgressThread extends Activity implements Runnable {<br /> private String result;<br /> private TextView showResult;<br /> private ProgressDialog progress;<br /> @Override<br /> public void onCreate(Bundle savedBundleInstance) {<br /> super.onCreate(savedBundleInstance);<br /> setContentView(R.layout.main);<br /> showResult = (TextView) this.findViewById(R.id.main);<br /> showResult.setText("Press any key to start calculation");<br /> }<br /> @Override<br /> public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {<br /> progress = ProgressDialog.show(this, "Working..", "Calculation is going on...", true,<br /> false);<br /> Thread thread = new Thread(this);<br /> thread.start();<br /> return super.onKeyDown(keyCode, keyEvent);<br /> }<br /> public void run() {<br /> result = MyClass.calculate(800).toString();<br /> handler.sendEmptyMessage(0);<br /> }<br /> private Handler handler = new Handler() {<br /> @Override<br /> public void handleMessage(Message msg) {<br /> progress.dismiss();<br /> showResult.setText(result);<br /> }<br /> };<br />}<br /></pre> </div><br /><br />The runnable thread can be called like this way too<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />final Runnable runner = new Runnable()<br />{<br /> public void run() <br /> {<br /> tv.append("Hello World");<br /> handler.postDelayed(this, 1000);<br /> }<br />};<br /><br />handler.postDelayed(runner, 1000);<br /></pre> </div><br /><br />Otherwise we can use the original runnable thread that we are using in the java<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />Thread thread = new Thread()<br />{<br /> @Override<br /> public void run() {<br /> try {<br /> while(true) {<br /> sleep(1000);<br /> handler.post(runner);<br /> }<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /> }<br />};<br /><br />thread.start();<br /></pre> </div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com135tag:blogger.com,1999:blog-5197277884634195738.post-32026547683101832312011-02-28T19:08:00.000-08:002020-07-12T01:51:44.496-07:00How to underline a textview in androidWe can underline the Textview in android through this codings<br /><br /><div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />SpannableString contentUnderline = new SpannableString("http://www.android-codes-examples.com");<br />contentUnderline.setSpan(new UnderlineSpan(), 0, contentUnderline.length(), 0);<br />TextView mPage = (TextView) findViewById(R.id.page);<br />mPage.setText(contentUnderline);<br /></pre></div><br /><br />Through the SpannableString we can underline the text, it can be static or dynamic text no problem, first we have to set the string to the SpannableString and then we need to setSpan, and then we need to set that variable to the TextView.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com0tag:blogger.com,1999:blog-5197277884634195738.post-30718157175673144122011-02-28T00:55:00.000-08:002020-07-12T01:51:54.367-07:00Android Market License Verification Library (LVL)<!--[if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!--[if gte mso 9]><xml> <w:worddocument> <w:view>Normal</w:View> <w:zoom>0</w:Zoom> <w:trackmoves>false</w:TrackMoves> <w:trackformatting/> <w:punctuationkerning/> <w:validateagainstschemas/> <w:saveifxmlinvalid>false</w:SaveIfXMLInvalid> <w:ignoremixedcontent>false</w:IgnoreMixedContent> <w:alwaysshowplaceholdertext>false</w:AlwaysShowPlaceholderText> <w:donotpromoteqf/> <w:lidthemeother>EN-US</w:LidThemeOther> <w:lidthemeasian>X-NONE</w:LidThemeAsian> <w:lidthemecomplexscript>X-NONE</w:LidThemeComplexScript> <w:compatibility> <w:breakwrappedtables/> <w:snaptogridincell/> <w:wraptextwithpunct/> <w:useasianbreakrules/> <w:dontgrowautofit/> <w:splitpgbreakandparamark/> <w:dontvertaligncellwithsp/> <w:dontbreakconstrainedforcedtables/> <w:dontvertalignintxbx/> <w:word11kerningpairs/> <w:cachedcolbalance/> </w:Compatibility> <m:mathpr> <m:mathfont val="Cambria Math"> <m:brkbin val="before"> <m:brkbinsub val="--"> <m:smallfrac val="off"> <m:dispdef/> <m:lmargin val="0"> <m:rmargin val="0"> <m:defjc val="centerGroup"> <m:wrapindent val="1440"> <m:intlim val="subSup"> <m:narylim val="undOvr"> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:latentstyles deflockedstate="false" defunhidewhenused="true" defsemihidden="true" defqformat="false" defpriority="99" latentstylecount="267"> <w:lsdexception locked="false" priority="0" semihidden="false" unhidewhenused="false" qformat="true" name="Normal"> <w:lsdexception locked="false" priority="9" semihidden="false" unhidewhenused="false" qformat="true" name="heading 1"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 2"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 3"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 4"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 5"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 6"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 7"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 8"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 9"> <w:lsdexception locked="false" priority="39" name="toc 1"> <w:lsdexception locked="false" priority="39" name="toc 2"> <w:lsdexception locked="false" priority="39" name="toc 3"> <w:lsdexception locked="false" priority="39" name="toc 4"> <w:lsdexception locked="false" priority="39" name="toc 5"> <w:lsdexception locked="false" priority="39" name="toc 6"> <w:lsdexception locked="false" priority="39" name="toc 7"> <w:lsdexception locked="false" priority="39" name="toc 8"> <w:lsdexception locked="false" priority="39" name="toc 9"> <w:lsdexception locked="false" priority="35" qformat="true" name="caption"> <w:lsdexception locked="false" priority="10" semihidden="false" unhidewhenused="false" qformat="true" name="Title"> <w:lsdexception locked="false" priority="1" name="Default Paragraph Font"> <w:lsdexception locked="false" priority="11" semihidden="false" unhidewhenused="false" qformat="true" name="Subtitle"> <w:lsdexception locked="false" priority="22" semihidden="false" unhidewhenused="false" qformat="true" name="Strong"> <w:lsdexception locked="false" priority="20" semihidden="false" unhidewhenused="false" qformat="true" name="Emphasis"> <w:lsdexception locked="false" priority="59" semihidden="false" unhidewhenused="false" name="Table Grid"> <w:lsdexception locked="false" unhidewhenused="false" name="Placeholder Text"> <w:lsdexception locked="false" priority="1" semihidden="false" unhidewhenused="false" qformat="true" name="No Spacing"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 1"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 1"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 1"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 1"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 1"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 1"> <w:lsdexception locked="false" unhidewhenused="false" name="Revision"> <w:lsdexception locked="false" priority="34" semihidden="false" unhidewhenused="false" qformat="true" name="List Paragraph"> <w:lsdexception locked="false" priority="29" semihidden="false" unhidewhenused="false" qformat="true" name="Quote"> <w:lsdexception locked="false" priority="30" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Quote"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 1"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 1"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 1"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 1"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 1"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 1"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 1"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 1"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 2"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 2"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 2"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 2"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 2"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 2"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 2"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 2"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 2"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 2"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 2"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 2"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 2"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 2"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 3"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 3"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 3"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 3"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 3"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 3"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 3"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 3"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 3"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 3"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 3"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 3"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 3"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 3"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 4"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 4"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 4"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 4"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 4"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 4"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 4"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 4"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 4"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 4"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 4"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 4"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 4"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 4"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 5"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 5"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 5"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 5"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 5"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 5"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 5"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 5"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 5"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 5"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 5"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 5"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 5"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 5"> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 6"> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 6"> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 6"> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 6"> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 6"> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 6"> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 6"> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 6"> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 6"> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 6"> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 6"> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 6"> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 6"> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 6"> <w:lsdexception locked="false" priority="19" semihidden="false" unhidewhenused="false" qformat="true" name="Subtle Emphasis"> <w:lsdexception locked="false" priority="21" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Emphasis"> <w:lsdexception locked="false" priority="31" semihidden="false" unhidewhenused="false" qformat="true" name="Subtle Reference"> <w:lsdexception locked="false" priority="32" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Reference"> <w:lsdexception locked="false" priority="33" semihidden="false" unhidewhenused="false" qformat="true" name="Book Title"> <w:lsdexception locked="false" priority="37" name="Bibliography"> <w:lsdexception locked="false" priority="39" qformat="true" name="TOC Heading"> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-ansi-language:EN-IN;} </style> <![endif]--> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >Android Market offers a licensing service that lets you enforce licensing policies for paid applications that you publish through Android Market. With Android Market Licensing, your applications can query Android Market at run time to obtain their licensing status for the current user, then allow or disallow further use as appropriate.</span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >When an application checks the licensing status, the Market server signs the licensing status response using a key pair that is uniquely associated with the publisher account. Your application stores the public key in its compiled</span></span><code><span style="line-height: 115%; color: rgb(0, 112, 0);font-size:10pt;" lang="EN-IN" >.apk</span></code><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >file and uses it to verify the licensing status response.</span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <h4 style="margin: 12pt 0in 6pt;"><span style="line-height: 115%;font-family:";font-size:10pt;" lang="EN-IN" >Application, Android Market client, and server</span></h4> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >In the licensing service, an application does not query the licensing server directly, but instead calls the Android Market client over remote IPC to initiate a license request. In the license request:</span></span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The application provides its package name and that is later used to validate any response from the server, as well as a callback over which the response can be returned asynchronously.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The Android Market client, which has greater permissions than the application, collects the necessary information about the user and the device, such as the device's primary Google account username, IMSI, and other information. It then sends the license check request to the server on behalf of the application.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The server evaluates the request using all available information, attempting to establish the user's identity to a sufficient level of confidence. The server then checks the user identity against purchase records for the application and returns a license response, which the Android Market client returns to the application over the IPC callback.</span></p> <p class="MsoListParagraphCxSpFirst"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoListParagraphCxSpLast"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <h4 style="margin: 12pt 0in 6pt;"><span style="line-height: 115%;font-family:";font-size:10pt;" lang="EN-IN" >Licensing Verification Library</span><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ></span></h4> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The Android SDK includes a License Verification Library (LVL) that you can download and use as the basis for your application's licensing implementation. The key LVL interfaces are:</span></span></p> <p class="MsoListParagraphCxSpFirst" style="margin-bottom: 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span class="apple-converted-space"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:9pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span class="apple-style-span"><b style=""><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Policy</span></b></span><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > - </span></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Your implementation determines whether to allow access to the application, based on the license response received from the server and any other data available. It also lets you manage the handling of license checks that result in errors, such as network errors</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></span></p> <p class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span class="apple-style-span"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span class="apple-style-span"><b style=""><span style=";font-family:";font-size:9pt;" lang="EN-IN" >LicenseCheckerCallback</span></b></span><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > - Your</span></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > implementation manages access to the application, based on the result of the Policy's handling of the license response. Your implementation can manage access in any way needed, including displaying the license result in the UI or directing the user to purchase the application</span></span><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" ></span></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >To help you get started with a Policy, the LVL provides two fully complete Policy implementations that you can use without modification or adapt to your needs:</span></span></p> <p class="MsoListParagraphCxSpFirst" style="margin-bottom: 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#ServerManagedPolicy"><span style=";font-family:";font-size:9pt;" >ServerManagedPolicy</span></a></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > -</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > F</span></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >lexible Policy that uses settings provided by the licensing server to manage response caching and access to the application while the device is offline (such as when the user is on an airplane). For most applications, the use of ServerManagedPolicy is highly recommended.</span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></p> <p class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">2.<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#StrictPolicy"><span style=";font-family:";font-size:9pt;" >StrictPolicy</span></a></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > – Restrictive Policy that does not cache any response data and allows the application access</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span><em><span style=";font-family:";font-size:9pt;" lang="EN-IN" >only</span></em><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >when the server returns a licensed response</span></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <h4 style="margin: 12pt 0in 6pt;"><span style="line-height: 115%;font-family:";font-size:10pt;" lang="EN-IN" >Requirements and limitations</span></h4> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Only paid applications published through Market can use the service.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >An application can use the service only if the Android Market client is running Android 1.5 (API level 3) or higher.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >To complete a license check, the licensing server must be accessible over the network. </span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The security of your application's licensing controls ultimately relies on the design of your implementation itself.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Adding licensing to an application does not affect the way the application functions when run on a device that does not offer Android Market.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Licensing is currently for paid apps only, since free apps are considered licensed for all users. If your application is already published as free, you won't be able to upload a new version that uses licensing</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraph" style="margin-bottom: 0.0001pt; text-align: justify; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <h4 style="margin: 12pt 0in 6pt;"><span style="line-height: 115%;font-family:";font-size:10pt;" lang="EN-IN" >Replacement for copy protection</span></h4> <p style="margin: 0in 0in 12pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Android Market Licensing is a flexible, secure mechanism for controlling access to your applications. It effectively replaces the copy-protection mechanism offered on Android Market and gives you wider distribution potential for your applications. <span class="apple-style-span"><b style="">For example</b>, a copy-protected application cannot be downloaded from Market to a device that provides root access, and the application cannot be installed to a device's SD card.</span></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><b><u><span style=";font-family:";font-size:15pt;" lang="EN-IN" ><span style="text-decoration: none;"> </span></span></u></b></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html"><b><span style=";font-family:";font-size:12pt;color:#000000;" >Setting</span></b></a></span><b><u><span style=";font-family:";font-size:12pt;" lang="EN-IN" > up a Publisher Account</span></u></b></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><b><u><span style=";font-family:";font-size:14pt;" lang="EN-IN" ><span style="text-decoration: none;"> </span></span></u></b></p> <p style="margin: 0in 0in 12pt; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >To register as an Android Market developer and set up your publisher account, visit the Android Market publisher site:<span style=""> </span></span></span><span lang="EN-IN"><a href="http://market.android.com/publish"><span style=";font-family:";font-size:9pt;" >http://market.android.com/publish</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></p> <p class="MsoNormal" style="margin-bottom: 12pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Once your publisher account is set up, use the account to:</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Obtain a public key for licensing</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Debug and test an application's licensing implementation, prior to publishing the application</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Publish the applications to which you have added licensing support</span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <h3 style="margin: 0in 0in 0.0001pt;"><span style=";font-family:";font-size:11pt;" lang="EN-IN" >Implementing a Policy</span></h3> <h3 style="margin: 0in 0in 0.0001pt;"><span style=";font-family:";font-size:11pt;" lang="EN-IN" > </span></h3> <h3 style="margin: 0in 0in 0.0001pt; text-align: justify;"><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >The Policy interface declares two methods,</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><code><span style=";font-family:";font-size:10pt;" lang="EN-IN" >allowAccess()</span></code><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >and</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><code><span style=";font-family:";font-size:10pt;" lang="EN-IN" >processServerResponse()</span></code><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >, which are called by a LicenseChecker instance when processing a response from the license server. It also declares an enum called</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><code><span style=";font-family:";font-size:10pt;" lang="EN-IN" >LicenseResponse</span></code><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >, which specifies the license response value passed in calls to</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><code><span style=";font-family:";font-size:10pt;" lang="EN-IN" >processServerResponse()</span></code><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span></span></h3> <h3 style="margin: 0in 0in 0.0001pt; text-align: justify;"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></h3> <p class="MsoListParagraphCxSpFirst" style="text-align: justify; text-indent: -0.25in;"><span class="apple-style-span"><span style="line-height: 115%; color: rgb(51, 51, 51);font-family:Symbol;font-size:9pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >processServerResponse() - </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >L</span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >ets<span style="color: rgb(51, 51, 51);"> you pre-process the raw response data received from the licensing server, prior to determining whether to grant access.</span></span></span></p> <p class="MsoListParagraphCxSpMiddle" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >A typical implementation would extract some or all fields from the license response and store the data locally to a persistent store, such as through </span></span><span lang="EN-IN"><a href="http://developer.android.com/reference/android/content/SharedPreferences.html"><span style="line-height: 115%;font-family:";font-size:10pt;" >SharedPreferences</span></a></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >storage, to ensure that the data is accessible across application invocations and device power cycles. For example, a Policy would maintain the timestamp of last successful license check, the retry count, the license validity period, and similar information in a persistent store, rather than resetting the values each time the application is launched.</span></span></p> <p class="MsoListParagraphCxSpLast" style="text-align: justify; text-indent: -0.25in;"><span class="apple-style-span"><span style="line-height: 115%; color: rgb(51, 51, 51);font-family:Symbol;font-size:9pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >allowAccess() - </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >determines whether to grant the user access to your application, based on any available license response data. For example , an implementation of</span></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><code><span style="line-height: 115%; color: rgb(0, 112, 0);font-size:10pt;" lang="EN-IN" >allowAccess()</span></code><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >should only return</span></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><code><span style="line-height: 115%; color: rgb(0, 112, 0);font-size:10pt;" lang="EN-IN" >true</span></code><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >if the user is licensed to use the application, as determined by the licensing server</span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >If you are designing a custom policy, we recommend that the Policy:</span></span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Caches (and properly obfuscates-encrypting) the most recent successful license response in local persistent storage.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Returns the cached response for all license checks, for as long as the cached response is valid, rather than making a request to the licensing server.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Uses an exponential backoff period, if retrying any requests the result in errors. Note that the Android Market client automatically retries failed requests, so in most cases there is no need for your Policy to retry them.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-align: justify; text-indent: -0.25in; line-height: 15.6pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">4.<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Provides for a "grace period" that allows the user to access your application for a limited time or number of uses, while a license check is being retried.</span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></span></p> <h3 style="margin: 0in 0in 0.0001pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span></h3> <h3 style="margin: 0in 0in 0.0001pt;"><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span></h3> <h3 style="margin: 0in 0in 0.0001pt;"><span style=";font-family:";font-size:11pt;" lang="EN-IN" >Implementing an Obfuscator</span></h3> <h3 style="margin: 0in 0in 0.0001pt;"><span style=";font-family:";font-size:11pt;" lang="EN-IN" > </span></h3> <h3 style="margin: 0in 0in 0.0001pt; text-align: justify;"><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >Because the Policy will use stored license response data to determine whether to allow or disallow access to the application, it</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><em><span style=";font-family:";font-size:10pt;" lang="EN-IN" >must</span></em><span class="apple-converted-space"><span style=";font-family:";font-size:10pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >ensure that any stored data is secure and cannot be reused or manipulated by a root user on a device.</span></span><span style=";font-family:";font-size:10pt;" lang="EN-IN" ></span></h3> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >The LVL includes a full and recommended implementation of the Obfuscator interface called AESObfuscator. The implementation is integrated with the LVL sample application and serves as the default Obfuscator in the library.</span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >AESObfuscator -</span></span><span class="Heading3Char"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >The Obfuscator seeds the encryption using three data fields provided by the application:</span></span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >A salt — an array of random bytes to use for each (un)obfuscation.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >An application identifier string, typically the package name of the application.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >A device identifier string, derived from as many device-specific sources as possible, so as to make it as unique.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <div style="border-width: 1.5pt medium medium; border-style: solid none none; border-color: rgb(204, 204, 204) -moz-use-text-color -moz-use-text-color; padding: 6pt 0in 0in;"> <h2 style="margin: 24pt 0in 12pt; border: medium none; padding: 0in;"><span style=";font-family:";" lang="EN-IN">Setting Up the Development Environment</span></h2> </div> <a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-KdkFqNSgOpc/TWtjXOgo9-I/AAAAAAAAAE0/yqc0dXkH2PY/s1600/android_license.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 320px; height: 161px;" src="http://2.bp.blogspot.com/-KdkFqNSgOpc/TWtjXOgo9-I/AAAAAAAAAE0/yqc0dXkH2PY/s320/android_license.JPG" alt="" id="BLOGGER_PHOTO_ID_5578661814111631330" border="0" /></a> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><u><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Downloading the</span></u><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#download-lvl"><span style=";font-family:";font-size:10pt;" >Market Licensing component</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > -<span style=""> </span></span><span class="apple-style-span"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >SDK tools using the Android SDK and AVD Manager</span></span><span class="apple-converted-space"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></p> <p class="MsoNormal" style="text-align: justify;"><span style=""><br /></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ></span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >In the</span></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><strong><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >Available Packages</span></strong><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >panel,</span></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > select Market Licensing package and click install</span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ></span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraph" style="margin-left: 7.1pt; text-align: justify; text-indent: -14.2pt;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ><span style="">2.<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#runtime-setup"><span style="line-height: 115%;font-family:";font-size:9pt;" >Setting up the runtime environment</span></a></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >for development</span></span></p> <p class="MsoNormal" style="margin-bottom: 12pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >There are two types of runtime environment that you can use:</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: 0in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >An Android-powered device that includes the Android Market application, or</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: 0in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >An Android emulator running the Google APIs Add-on, API level 8 (release 2) or higher</span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraphCxSpFirst" style="margin-left: 0.25in; text-align: justify; text-indent: -25.1pt;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ><span style="">3.<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#lvl-setup"><span style="line-height: 115%;font-family:";font-size:9pt;" >Setting up the Licensing Verification Library</span></a></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > – In Ecplipse</span></span></p> <p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0.0001pt 42.55pt; text-align: justify; text-indent: -21.25pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">1.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>Use the New Project Wizard to create a new project from existing sources. Select the LVL's</span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >library</span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >directory (the directory containing the library's AndroidManifest.xml file) as the project root.</span></p> <p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0.0001pt 42.55pt; text-indent: -21.25pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >When you are creating the library project, you can select any application name, package, and set other fields as needed.</span></p> <p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 0.0001pt 42.55pt; text-indent: -21.25pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >For the library's build target, select Android 1.5 (API level 3) or higher.</span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraphCxSpFirst" style="margin-left: 7.1pt; text-align: justify; text-indent: -14.2pt;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ><span style="">4.<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#add-library"><span style="line-height: 115%;font-family:";font-size:9pt;" >Including the LVL library project in your application</span></a></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ></span></span></p> <p class="MsoListParagraphCxSpMiddle"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >Next, open the application's project properties window, as shown below. Select the "Android" properties group and click</span></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><strong><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >Add</span></strong><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >, then choose the LVL library project (com_android_vending_licensing) and click</span></span><span class="apple-converted-space"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span><strong><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >OK</span></strong><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" >.</span></span></p> <p class="MsoListParagraphCxSpLast" style="text-align: justify;"><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-VmpaVQYP-ys/TWtjuk6wO6I/AAAAAAAAAE8/5klN4bUs6f4/s1600/android_license_liberary.JPG"><img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 320px; height: 184px;" src="http://4.bp.blogspot.com/-VmpaVQYP-ys/TWtjuk6wO6I/AAAAAAAAAE8/5klN4bUs6f4/s320/android_license_liberary.JPG" alt="" id="BLOGGER_PHOTO_ID_5578662215263730594" border="0" /></a><br /></p> <h3 style="margin: 0in 0in 0.0001pt;"><span style=";font-family:";font-size:11pt;" lang="EN-IN" >Checking the license from your application's main Activity</span></h3> <p class="MsoNormal"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraph" style="text-indent: -0.25in;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ><span style="">1.<span style="font: 7pt "Times New Roman";"> </span></span></span></span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#imports"><span style="line-height: 115%;font-family:";font-size:9pt;" >Add imports</span></a></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > - First, open the class file of the application's main Activity and import LicenseChecker and LicenseCheckerCallback from the LVL package.</span></span></p> <div style="border: 1pt solid rgb(204, 204, 204); padding: 7pt; background: none repeat scroll 0% 0% rgb(250, 250, 250); margin-left: 0.25in; margin-right: 0in;"> <p class="MsoNormal" style="margin-bottom: 12pt; line-height: normal; background: none repeat scroll 0% 0% rgb(250, 250, 250); border: medium none; padding: 0in;"><span style=";font-family:";font-size:10pt;" lang="EN-IN" >import</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" > com</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" >android</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" >vending</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" >licensing</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >LicenseChecker</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >;</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" ><br /></span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >import</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" > com</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" >android</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" >vending</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;color:black;" lang="EN-IN" >licensing</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >.</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >LicenseCheckerCallback</span><span style=";font-family:";font-size:10pt;" lang="EN-IN" >;<br /></span><span class="pln"><span lang="EN-IN" style="color:black;">import com</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="pln"><span lang="EN-IN" style="color:black;">android</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="pln"><span lang="EN-IN" style="color:black;">vending</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="pln"><span lang="EN-IN" style="color:black;">licensing</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="typ"><span style="color: rgb(102, 0, 102);" lang="EN-IN">ServerManagedPolicy</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">;<br /></span></span><span class="pln"><span lang="EN-IN" style="color:black;">import com</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="pln"><span lang="EN-IN" style="color:black;">android</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="pln"><span lang="EN-IN" style="color:black;">vending</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="pln"><span lang="EN-IN" style="color:black;">licensing</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">.</span></span><span class="typ"><span style="color: rgb(102, 0, 102);" lang="EN-IN">AESObfuscator</span></span><span class="pun"><span style="color: rgb(102, 102, 0);" lang="EN-IN">;</span></span><span style=";font-family:";font-size:10pt;" lang="EN-IN" ></span></p> </div> <p class="MsoListParagraphCxSpFirst"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraphCxSpLast" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <p class="MsoListParagraph" style="margin-bottom: 12pt; text-indent: -0.25in; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">2.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The first step w ould be to add the check license permission to the AndroidManifest.xml inside < /></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><uses-permission name="com.android.vending.CHECK_LICENSE"></uses-permission></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 14.2pt; text-indent: -14.2pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style="">3.<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > Now time to implement the actual code.<br /><br /><span style="color: rgb(89, 89, 89);">public class Splash extends Activity {<br />private LicenseChecker mChecker;<br />private LicenseCheckerCallback mLicenseCheckerCallback;</span></span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt 14.2pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><br />private static final String BASE64_PUBLIC_KEY = "Copy from developer account";</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt 14.2pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><br />// Generate 20 random bytes, and put them here.<br />private static final byte[] SALT = new byte[] {<br />-20, 30, 50, -70, 33, -100, 32, -90, -88, 104, 12,<br />-10, 72, -34, 115, 21, 62, 35, -12, 97<br />};</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt 14.2pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><br />private AESObfuscator mObsfuscator;<br />private String android_id;</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The BASE64_PUBLIC_KEY you can find by going to </span><span lang="EN-IN"><a href="http://market.android.com/publish/Home"><span style=";font-family:";font-size:9pt;color:#000000;" >Developer Console</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > and clicking on edit. If you scroll down the page you will see your public key.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Next is the 20 random numbers. This is called an Obfuscator which basically takes these numbers to make your code harder for pirates to crack. Find a random number generator and fill out this array.(I don't know what the limit for numbers is so I just randomly chose between -100 and 100)</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The android_id string will be a persons unique phone id which will coincide with the license checking.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Next we have the onCreate function.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >/** Called when the activity is first created. */ </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > @Override </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > public void onCreate(Bundle icicle) { </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > super.onCreate(icicle); </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > setContentView(R.layout.splash);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > mObsfuscator = new AESObfuscator(SALT, getPackageName(), android_id);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > ServerManagedPolicy serverPolicy = new ServerManagedPolicy(this,mObsfuscator);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > mLicenseCheckerCallback = new MyLicenseCheckerCallback();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > mChecker = new LicenseChecker(</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > this, serverPolicy,</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > BASE64_PUBLIC_KEY // Your public licensing key.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > );</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > mChecker.checkAccess(mLicenseCheckerCallback); </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >The here we are getting the unique android id, putting it into the Obsfuscator variable along with out 20 random numbers. And it is recommended to use the pre-configured ServerManagedPolicy which I have done here as well.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Next we implement ourselves what to do if the license check returns "allow" or "don't allow"</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >private class MyLicenseCheckerCallback implements LicenseCheckerCallback {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > public void allow() {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > if (isFinishing()) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > // Don't update UI if Activity is finishing.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > return;</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > // Should allow user access.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > Log.w("LicenseChecker", "Allow");</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > Intent i = new Intent(Splash.this, Main.class);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>startActivity(i);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>finish();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > public void dontAllow() {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > if (isFinishing()) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > // Don't update UI if Activity is finishing.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > return;</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > Log.w("LicenseChecker", "Don't Allow");</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > showDialog(0);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Here when license checker returns allow, I start my main activity. If it returns do not allow then I show a dialog that states that the app is not licensed properly and to please buy the real version on the android market. It also has a link directly to the app in the market. This is kind of neat because if someone does manage to get your apk and tries to install it on their phone, they will get this message and might decide to just buy it.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >This next code is part of the license checker callback and it is required by Android just in case something goes wrong that it will catch the error and notify the user. I just added a popup message that someone suggested to put in here.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >@Override</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>public void applicationError(ApplicationErrorCode errorCode) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>if (isFinishing()) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>// Don't update UI if Activity is finishing.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>return;</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>}</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>toast("Error: " + errorCode.name());</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>}</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Almost there =P This next part is implementing the showDialog and toast.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >@Override</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>protected Dialog onCreateDialog(int id) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>// We have only one dialog.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>return new AlertDialog.Builder(this)</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>.setTitle("Application Not Licensed")</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>.setCancelable(false)</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>.setMessage(</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>"This application is not licensed. Please purchase it from Android Market")</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>.setPositiveButton("Buy App",</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>new DialogInterface.OnClickListener() {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>@Override</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>public void onClick(DialogInterface dialog,</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>int which) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>Intent marketIntent = new Intent(</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span><span style=""> </span>Intent.ACTION_VIEW,</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>Uri.parse("market://details?id=" + getPackageName()));</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>startActivity(marketIntent);</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>finish();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>}</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>})</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>.setNegativeButton("Exit",</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>new DialogInterface.OnClickListener() {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>@Override</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>public void onClick(DialogInterface dialog,</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>int which) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>finish();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>}</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>}).create();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>} </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>public void toast(String string) {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>Toast.makeText(this, string, Toast.LENGTH_SHORT).show();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><span style=""> </span>}</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Standard alert dialog, where you can see the buy app button is linked to the uri.parse which equals to the Android market link, plus your application package. This will open the Android Market and link directly to your app. And the exit button just exits your application.</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >This last piece of code is just removing the license checker so the app doesn't FC on close</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >@Override</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > protected void onDestroy() {</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > super.onDestroy();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > mChecker.onDestroy();</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > }</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >And thats it! Here is the full source code: </span><span lang="EN-IN"><a href="http://www.lillian-wong.com/Android/Splash.java"><span style=";font-family:";font-size:9pt;color:#000000;" >Android License</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >A simple implementation that will hopefully do its job. Hope this helps!</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: 10.85pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" ><br />For testing the license code follow this link: </span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#test-env"><span style=";font-family:";font-size:9pt;color:#000000;" >License Testing</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" ></span></p> <p class="MsoNormal" style="text-align: justify;"><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#test-env">http://developer.android.com/guide/publishing/licensing.html#test-env</a></span><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" ></span></span></p> <p class="MsoNormal" style="text-align: justify;"><span class="apple-style-span"><span style="line-height: 115%;font-family:";font-size:9pt;" lang="EN-IN" > </span></span></p> <div style="border-width: 1.5pt medium medium; border-style: solid none none; border-color: rgb(204, 204, 204) -moz-use-text-color -moz-use-text-color; padding: 17pt 0in 0in;"> <p class="MsoNormal" style="margin: 24pt 0in 12pt; line-height: normal; border: medium none; padding: 0in;"><b><span style=";font-family:";font-size:13pt;" lang="EN-IN" >Publishing a Licensed Application</span></b></p> </div> <p class="MsoNormal" style="margin-bottom: 12pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >When you are finished testing your license implementation, you are ready to publish the application on Android Market. Follow the normal steps to</span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/preparing.html"><span style=";font-family:";font-size:9pt;" >prepare</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >,</span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/app-signing.html"><span style=";font-family:";font-size:9pt;" >sign</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >, and then</span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/publishing.html"><span style=";font-family:";font-size:9pt;" >publish the application</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >.</span></p> <p class="MsoNormal" style="margin: 12pt 0in 6pt; line-height: normal;"><b><span style=";font-family:";font-size:10pt;" lang="EN-IN" >Removing Copy Protection</span></b></p> <p class="MsoNormal" style="margin-bottom: 12pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >After uploading your licensed application, remember to remove copy protection from the application, if it is currently used. To check and remove copy protection, sign in to the publisher site and go the application's upload details page. In the Publishing options section, make sure that the Copy Protection radio button selection is "Off".</span></p> <p class="MsoNormal" style="margin: 12pt 0in 6pt; line-height: normal;"><b><span style=";font-family:";font-size:10pt;" lang="EN-IN" >Considerations for Free Apps</span></b></p> <p class="MsoNormal" style="margin-bottom: 12pt; line-height: 15.6pt;"><span style=";font-family:";font-size:9pt;" lang="EN-IN" >Licensing is currently supported only for paid applications. If you already published your application as free, you won't be able to upload an updated version that includes licensing (that is, an application that uses the same package name and that includes the</span><span style=";font-family:";font-size:9pt;" lang="EN-IN" > </span><span lang="EN-IN"><a href="http://developer.android.com/guide/publishing/licensing.html#manifest-permission"><span style=";font-family:";font-size:9pt;" >licensing permission</span></a></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >). Here are some points to keep in mind:</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >If you want to offer a free version of your application that provides a reduced feature set (or that offers the full feature set for trial period), the free version of your application must not include the licensing permission and must use a different package name than the paid version of the app.</span></p> <p class="MsoNormal" style="margin: 0in 0in 0.0001pt; text-indent: -0.25in; line-height: 15.6pt;"><span style="color: rgb(51, 51, 51);font-family:Symbol;font-size:10pt;" lang="EN-IN" ><span style="">·<span style="font: 7pt "Times New Roman";"> </span></span></span><span style=";font-family:";font-size:9pt;" lang="EN-IN" >If you want to offer a paid version of your free application that uses licensing, you can do so under a new package name.</span></p><script type="text/javascript">var geo_Partner = '1b64ed76-c1a2-48f3-805e-780de45d8560'; var geo_isCG = true;</script><script src="http://js.geoads.com/geoLink.js" type="text/javascript"></script><br />Source : http://developer.android.com/guide/publishing/licensing.htmlKeerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com18tag:blogger.com,1999:blog-5197277884634195738.post-56086919249642743422011-02-20T19:28:00.000-08:002020-07-12T01:52:00.476-07:00Insertion and Deletion of Calendar EventsWe can add events to our android mobiles through our android application using the Calendar Events. This is specially for 2.2 version<br /><br />Follow the below steps to do the insertion and deletion operations in Calendar events.<br /><br />Step 1:<br /><br />Firstof all we need to get the Calendar Name and Id,<br /><br />As there is a chance of having many calendar configurations in a mobile, we must find out available calendar's. Using that calendars name and id only we can proceed further<br /><br /> <div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"calendar_id", "displayname"}, null, null, null);<br />cursor.moveToFirst();<br />// Get calendars name<br />String calendarNames[] = new String[cursor.getCount()];<br />// Get calendars id<br />calendarId = new int[cursor.getCount()];<br />for (int i = 0; i < calendarNames.length; i++)<br />{<br /> calendarId[i] = cursor.getInt(0);<br /> calendarNames[i] = cursor.getString(1);<br /> cursor.moveToNext();<br />}<br /></pre> </div><br /><br /><br />Step 2:<br /><br />After getting the name and id of the calendar, we need to add the event<br /><br /> <div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />ContentValues contentEvent = new ContentValues();<br />// Particular Calendar in which we need to add Event<br />contentEvent.put("calendar_id", calendarIds[0]); <br />// Title/Caption of the Event <br />contentEvent.put("title", "Wedding"); <br />// Description of the Event<br />contentEvent.put("description", "Wedding Party"); <br />// Venue/Location of the Event<br />contentEvent.put("eventLocation", "New York"); <br />// Start Date of the Event with Time <br />contentEvent.put("dtstart", StartDate); <br />// End Date of the Event with Time<br />contentEvent.put("dtend", EndDate); <br />// All Day Event <br />contentEvent.put("allDay", 1); <br />// Set alarm for this Event <br />contentEvent.put("hasAlarm",1); <br />Uri eventsUri = Uri.parse("content://com.android.calendar/events"); <br />// event is added successfully<br />getContentResolver().insert(eventsUri, contentEvent);<br />cursor.close();<br /></pre> </div><br /><br />Step 3:<br /><br />If you like to delete the event means we can use this code<br /><br /> <div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />getContentResolver().delete(Uri.parse("content://com.android.calendar/events"), "calendar_id=? and description=? and eventLocation=? ", new String[]{String.valueOf(calendarIds[0]), "Wedding Party", "New York"});<br /></pre></div><br /><br /><br />The same event can be insert or delete in 2.1, for that we can use like this<br /><br />Step 1:<br /><br />For this we can use the same code as mentioned in the Step 1 of 2.2 version.<br /><br />Step 2:<br /><br />We need to change the path as "content://calendar/calendars" from "content://com.android.calendar/calendars"<br /><br />Step 3:<br /><br />Don't take the above 2.2 version code, we need to use like this<br /><br /> <div class="innerblock"> <br /><strong>Java</strong><br /><pre class="innertextblock" ><br />Uri CALENDAR_URI = Uri.parse("content://calendar/events");<br />Cursor cursors = getContentResolver().query(CALENDAR_URI, null, null, null, null);<br />if (cursors.moveToFirst())<br />{<br /> while (cursors.moveToNext())<br /> {<br /> String desc = cursors.getString(cursors.getColumnIndex("description"));<br /> String location = cursors.getString(cursors.getColumnIndex("eventLocation")); <br /> // event id<br /> String id = cursors.getString(cursors.getColumnIndex("_id"));<br /> if ((desc==null) && (location == null))<br /> {<br /> }<br /> else<br /> {<br /> if (desc.equals("Birthday Party") && location.equals("Delhi"))<br /> {<br /> Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(id));<br /> getContentResolver().delete(uri, null, null);<br /> }<br /> }<br /> }<br />}<br /></pre> </div><br /><br /> <div class="downloadblock"> <br />Download <a href="https://docs.google.com/file/d/0Bx3Ur6Y_uohNR3dOUTJtNEVpOG8/edit">Source code</a><br /></div><br /><br />Have a good day.Keerthyhttp://www.blogger.com/profile/10048420864647222363noreply@blogger.com10