Saturday, 13 December 2014

Stop Refreshing on List View With Check Box / Radio Button

Stop Refreshing on List View With Check Box / Radio Button

Hi!

                 In our Development, Mostly Developers facing one common problem while developing listview with CheckBox / Radio Buttons.

       After you load your data on Listview, when you scroll the listview the selected items will disappear and some items will selected automatically. this problem is because of we are try to figure out the item by position. But when the listview is scrolled up / down the position will be reset.

The solution is we have to take the components tag position by getTag(), and we can identify by this tag position. But should set the Tag before set the value on the Item.

On Business Class

boolean P_is_checked;

i have create a boolean value to carry the state.

On The Adapter Class
{
// radio button on checkedchangelistener
rb_p.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//  Step - 1 get the tag position while it is checked
int getPosition = (Integer) buttonView.getTag(); 
                               // Step - 2 On The Business Class Object set the boolean value state
A_list.get(getPosition).setP_is_checked(buttonView.isChecked());
}
});
// Step - 3 set the Tag position
rb_p.setTag(position);
//Step - 4 Set the value of the item
rb_p.setChecked(A_list.get(position).P_is_checked);
}

Thats all, no your listview the CheckBox / RadioButton will never check or unchecked automatically while scrolling..,


Enjoy..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

Monday, 1 December 2014

Set Demo Period Expired

Demo Period Expired

Today i am going to show the tips about, how to set the demo period in Android Application?

There are several ways to do it, But very simple and perfect way is to use SharedPreferences,

1) Step

we have to create a SharedPreferences to keep the mandatory details about the application,

2) Step 

To save the current date (Installation Date) on it.

3) Step 

Check the condition for every wakeup of the application for expire period days with the current date,


This is the method to check the validation of 10 Days Demo

private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private final long oneday = 24 * 60 * 60 * 1000;

public void checkExpireDate() throws ParseException {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String installDate = preferences.getString("InstallDate", null);
if (installDate == null) {
SharedPreferences.Editor editor = preferences.edit();
Date now = new Date();
String dateString = formatter.format(now);
editor.putString("InstallDate", dateString);
editor.commit();
} else {
Date before = (Date) formatter.parse(installDate);
Date now = new Date();
long diff = now.getTime() - before.getTime();
long days = diff / oneday;
if (days > 10) {
AlertDialog.Builder ad= new AlertDialog.Builder(new ContextThemeWrapper(ReaderB.this, R.style.popup_theme));
ad.setTitle("Your App Is Exceeded the Trial Version");
ad.setMessage("Do You Want To Purchase?..,");
ad.setInverseBackgroundForced(false);
ad.setCancelable(false);
ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// Direct to our site if the application is expired
@Override
public void onClick(DialogInterface dialog, int which) {
Intent fina=new Intent(ReaderB.this,DcsSite.class);
startActivity(fina);
finish();
}
});
ad.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
ad.show();
}
}
}

Style. xml file for Popup Theme 

 <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <color name="back_color">#ffffffff</color>

    <style name="popup_theme" parent="@android:style/Theme.Dialog">
        <item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
        <item name="android:windowBackground">@color/back_color</item>
        <item name="android:colorBackground">@color/back_color</item>
    </style>

Thats All..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

Friday, 14 November 2014

How To Play YouTube Video

How To Play YouTube Videos

HI!
        Today i am going to show you "How To Play YouTube Videos"

You need to prepare your system for do this. First you have to download YouTube Android Player API , Then Go to your console and activate your YouTube Data API v3Finally. Activate your Project API key from your Credential . Now You can start the coding part.

Add the YouTube Android Player API into your project. Create a new Activity with extend of  YouTubePlayer.OnInitializedListener.

On the activity_my__tube.XML file call the YouTube Player View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/you_player"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

On the Activity Replace the extend class 

import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class My_Tube extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
public static final String key_user = "__________(Your project api key)_______________";

public static final String key_video = "_________(Video Id _ what you want to play)____________";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my__tube);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.you_player);
youTubePlayerView.initialize(key_user, this);
}

@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(this, "Error While Playing!", Toast.LENGTH_SHORT).show();
}

@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
player.setPlayerStateChangeListener(state_Change_Listener);
player.setPlaybackEventListener(play_Back_Listener);
if (!wasRestored) {
player.cueVideo(key_video);
}
}

private PlaybackEventListener play_Back_Listener = new PlaybackEventListener() {

@Override
public void onBuffering(boolean arg0) {

}

@Override
public void onPaused() {

}

@Override
public void onPlaying() {

}

@Override
public void onSeekTo(int arg0) {

}

@Override
public void onStopped() {

}

};

private PlayerStateChangeListener state_Change_Listener = new PlayerStateChangeListener() {

@Override
public void onAdStarted() {

}

@Override
public void onError(ErrorReason arg0) {

}

@Override
public void onLoaded(String arg0) {

}

@Override
public void onLoading() {
}

@Override
public void onVideoEnded() {

}

@Override
public void onVideoStarted() {

}
};

}

Now you can ENJOY by playing your YouTube Videos on your Application..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

Wednesday, 12 November 2014

Simple Date Picker

Simple Date Picker

Hi!

In android it is easy to call the default date picker

we have to custom the DialogFragment

// declare year,month date as integer

        private int mYear;
private int mMonth;
private int mDay;

Button btn_dob;

btn_dob = (Button) findViewById(R.id.btn_reg_dob);

btn_dob.setOnClickListener(this);

switch (v.getId()) {
case R.id.btn_reg_dob:
                       // call the custom DialogFragment Class on click event
DialogFragment fragments = new DOBSelectDateFragment();
fragments.show(getFragmentManager(), "Date Picker");
break;
}

// customize the DialogeFragment for Date Picker
class DOBSelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

public Dialog onCreateDialog(Bundle savedInstanceState) {

return new DatePickerDialog(getActivity(), this, mYear, mMonth,
mDay);
}

@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;

onPopulateSet(year, monthOfYear + 1, dayOfMonth);

}

private void onPopulateSet(int year, int i, int dayOfMonth) {
                        // Set the result wherever you want
et_reg_dob.setText(dayOfMonth + "/" + i + "/" + year);
}
}

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

Friday, 18 July 2014

Custom Spinner Design

Hi!

In android we can custom spinner background and text align very  easily.

In your XML file create the spinner normally as fallow


main.xml

<Spinner
                android:id="@+id/sp_cal_doc"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="#40000000"
                android:popupBackground="#000000" />


the " popupBackground " is used to drop down background and #40000000 is the color range for black transparent.

Now have to create a TextView in your XML file

spiner_tv.xml


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:gravity="center"
    android:text="TextView"
    android:textColor="@color/wit"
    android:textSize="18sp"
    android:typeface="normal" >

</TextView>

Now you can declare and create your own ArrayAdapter<String> & ArrayList<String> as fallow. But the layout should be your custom TextView Layout only.

Main.Java

Spinner sp_doctr;
sp_doctr = (Spinner) findViewById(R.id.sp_cal_doc);
ArrayList<String> lst_doctr = new ArrayList<String>();

lst_doctr.add("Doctors");
lst_doctr.add("Dr.Raj");
lst_doctr.add("Dr.Lee");
lst_doctr.add("Dr.Selva");
lst_doctr.add("Dr.Saranya");

ArrayAdapter<String> adr_doctr=adr_doctr = new ArrayAdapter<String>(this, R.layout.spiner_tv,
lst_doctr);

sp_doctr.setAdapter(adr_doctr);





Chang the font style on Spinner :


If you want to change the font style on your Spinner view fallow the below.

Custom ArrayAdapter insted of default ArrayAdapter:


package dcs.raj.dcspharma.adoptr;

import java.util.ArrayList;
import dcs.raj.dcspharma.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Spinner_A extends ArrayAdapter<String> {

Context context;
ArrayList<String> a_list;

public Spinner_A(Context context, int textViewResourceId,
ArrayList<String> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.a_list = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater lf = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = lf.inflate(R.layout.spiner_tv, null);
}
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"font/fontsfile.otf");
TextView tv = (TextView) convertView.findViewById(R.id.tv);
tv.setText(a_list.get(position));
tv.setTypeface(tf);

return convertView;
}

}



Replace the ArrayAdapter:

Spinner_A adr_doctr = new Spinner_A(this, R.layout.spiner_tv, lst_doctr);


Thats all..,

Now you can enjoy with your custom spinner design..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,


Saturday, 17 May 2014

How To Send Sms & Types Of Possibilities

Hi!

  In android there are two way to send a SMS,

  The very 1st one is by calling the SmsManager with out intent send message as  programatically, without displaying as message box,

  The another one is by calling the intent, user can edit the message before sending it like a normal message

  Optionally we can set the delivered report too

// i call the default "Intent.ACTION_GET_CONTENT" intent to pick the number to send sms.

@Override

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Way1:
Intent intent_1 = new Intent(Intent.ACTION_GET_CONTENT);
intent_1.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent_1, 1);

break;
case R.id.Way2:
Intent intent_2 = new Intent(Intent.ACTION_GET_CONTENT);
intent_2.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent_2, 2);

break;
case R.id.Way3:
Intent intent_3 = new Intent(Intent.ACTION_GET_CONTENT);
intent_3.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent_3, 3);

break;
default:
break;
}
}

// By calling the OnActivityResult() method we can split the action by based on the RequestCode


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver()
.query(uri,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);

if (c != null && c.moveToFirst()) {
String number = c.getString(0);
if (requestCode == 1) {
way_1(number);
}
if (requestCode == 2) {
way_2(number);
}
if (requestCode == 3) {
way_3(number);
}

}
} finally {
if (c != null) {
c.close();
}
}

}

}
}

// By calling the SmsManager


private void way_1(String number) {
// TODO Auto-generated method stub
Toast.makeText(this,
"Please wait! \n Message Sending to " + ": " + number,
Toast.LENGTH_LONG).show();

String phoneNo = "+91" + number;
String message = "Hi! " + "\n" + "This is Test Message";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "Message Sent Successfuly",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Message Sending faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

//  By Calling the Intent with Delivery report


private void way_2(String number) {
Toast.makeText(this,
"Please wait! \n Message Sending to " + ": " + number,
Toast.LENGTH_LONG).show();

String phoneNo = "+91" + number;
String message = "Hi! " + "\n" + "This is Test Message";
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);

registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(
getBaseContext(),
"SMS sent! \n Please wait till SMS Reach Your Partner ..,",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(
getBaseContext(),
"SMS Not sent! \n  Generic failure Please Try Again",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),
"SMS Not sent! \n  No service Please Try Again",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),
"SMS Not sent! \n  Null PDU Please Try Again",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),
"SMS Not sent! \n  Radio off Please Try Again",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),
"SMS delivered Successfuly", Toast.LENGTH_SHORT)
.show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS is Not delivered",
Toast.LENGTH_SHORT).show();

break;
}
}
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);
}


// By Calling the intent without Delevery Report


private void way_3(String number) {
Toast.makeText(this,
"Please wait! \n Message Sending to " + ": " + number,
Toast.LENGTH_LONG).show();
String phoneNo = "+91" + number;
String message = "Hi! " + "\n" + "This is Test Message";
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");

smsIntent.putExtra("address", new String(phoneNo));
smsIntent.putExtra("sms_body", message);
try {
startActivity(smsIntent);
Log.i("Message Sent Successfuly..,", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Welcome.this,
"Message Sending faild, please try again..,",
Toast.LENGTH_SHORT).show();
}

}



call the default "Intent.ACTION_GET_CONTENT" intent



By calling the SmsManager



By Calling the intent



Thank You!

Please Leave Your Comment..,


Have A Happy Day..,