Friday, 23 January 2015

Image Transaction in Android from Device To Server

Hi!

     Today lot of developer facing problem while  saving and retrieving images in Android from Device To Server and vice versa.

 This blog will be help you to solve this issue.

Step 1 :
call the gallery to select the image

Step 2 :
By using ActivityResultSet() we can receive the image

Step 3 :
Compress the image and convert into string

Code:

ImageView imv_tst_img;   // to set the output of the image
private static Bitmap Image = null; // Bitmap for handle the image compression
Button btn_rg_image;     // for action purpose
String Image_sv;           // to store the image as string

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rg__name);
imv_tst_img=(ImageView) findViewById(R.id.imv_tst_img);
btn_rg_image=(Button) findViewById(R.id.btn_rg_image);

btn_rg_image.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
imv_tst_img.setImageBitmap(null);
if (Image != null)
Image.recycle();
Intent intent = new Intent();
intent.setType("image/*");

// to call the Gallery intent
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"), GALLERY);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY && resultCode != 0) {
Uri mImageUri = data.getData();
try {
// create the Bitmap by using the selected image
Image = Media.getBitmap(this.getContentResolver(), mImageUri);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
Bitmap bit_map = Bitmap.createBitmap(Image, 0, 0,
Image.getWidth(), Image.getHeight());
//compress the bitmap
bit_map.compress(Bitmap.CompressFormat.JPEG, 75, bao);
//converted as byte[] array
byte[] byt_ara = bao.toByteArray();
// Encode by Base64 and save as string
String encoded_btmp = Base64
.encodeToString(byt_ara, Base64.DEFAULT);
// in mycode i pass the string for public use
Image_sv = encoded_btmp;
Log.d("Image_Save", Image_sv);
System.out.println("Image _1 : "+Image_sv);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

// Revers Process : method to convert the string into Bitmap and set it on the ImageViewer
public void set_Image() {
try {
byte[] decodedString = Base64.decode(Image_sv, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString,
0, decodedString.length);
imv_tst_img.setImageBitmap(decodedByte);
} catch (Exception e) {
// TODO: handle exception
Log.d("ERROR ON IMG", e.getMessage());
}
}

Thats All..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

Thursday, 22 January 2015

How To Make Launcher App in Android

Hi!

    In some situation while the employees are using android device, we need to develop application which should allow the user to use only one particular Enterprise / Business Application, it should block the other application and all activity of the device. It called as Launcher Application.

In this post i am going to give idea only about how to Restrict/Stop all other application while Android device is booting. Because if you try this on your real device all your stored data will be deleted and unable to launch any other applications except your currently launched application.


     In some Business Purpose we need to all employees should work on only one Business Application, We should stop the employee to use the official device for entertainment and other personal purposes.


Work Flow ;


1) In Android we have to stop the Actionbar , Notifications & Other Service.
2) Even if the device is booting we should run our Business application only.


Waning ;


This is not a fun, if you using this technique on your real device, your all data will be lose and this will be your permanent application..,


I have mention everything in this link download , please Download and use carefully..,


Thank You!


Please Leave Your Comment..,


Have A Happy Day..,

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..,