Thursday, 26 September 2013

Simple Example for extracting zip file in android

You can extract zip file by using
ZipInputStream
class in android.

MainActivity.java
package com.android.zipextracter;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  String zipFilePath = Environment.getExternalStorageDirectory()
    .getAbsolutePath()+"/";
  unpackZip(zipFilePath, "MyRar.zip");
 }

 private boolean unpackZip(String path, String zipname) {
  InputStream is;
  ZipInputStream zis;
  try {
   String filename;
   is = new FileInputStream(path + zipname);
   zis = new ZipInputStream(new BufferedInputStream(is));
   ZipEntry mZipEntry;
   byte[] buffer = new byte[1024];
   int count;

   while ((mZipEntry = zis.getNextEntry()) != null) {
    // zapis do souboru
    filename = mZipEntry.getName();

    // Need to create directories if not exists, or
    // it will generate an Exception...
    if (mZipEntry.isDirectory()) {
     File fmd = new File(path + filename);
     fmd.mkdirs();
     continue;
    }

    FileOutputStream fout = new FileOutputStream(path + filename);

    // cteni zipu a zapis
    while ((count = zis.read(buffer)) != -1) {
     fout.write(buffer, 0, count);
    }

    fout.close();
    zis.closeEntry();
    Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
   }

   zis.close();
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }

  return true;
 }
}


Don't forget to add following uses-permission in your manifest file


I hope this post is useful to you. kindly share your feedback as comment here.


Thank You



Friday, 20 September 2013

Custom DialogFragment Example in android

By this example you can access your DialogFragment throughout the application. You can send arguments to DialogFragment, and also you can retrieve values from DialogFragment.

Screenshot:


Code:

MyCustomDialog.java
package com.andoid.dialogfragment;

import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

public class MyCustomDialog extends DialogFragment {
 Button mButton;
 EditText mEditText;
 onSubmitListener mListener;
 String text = "";

 interface onSubmitListener {
  void setOnSubmitListener(String arg);
 }

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
  final Dialog dialog = new Dialog(getActivity());
  dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
  dialog.setContentView(R.layout.custom_dialog);
  dialog.getWindow().setBackgroundDrawable(
    new ColorDrawable(Color.TRANSPARENT));
  dialog.show();
  mButton = (Button) dialog.findViewById(R.id.button1);
  mEditText = (EditText) dialog.findViewById(R.id.editText1);
  mEditText.setText(text);
  mButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    mListener.setOnSubmitListener(mEditText.getText().toString());
    dismiss();
   }
  });
  return dialog;
 }
}


custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#efefef"
    android:gravity="center"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>


Code for access this DialogFragment from our Activity
MainActivity.java
package com.andoid.dialogfragment;

import com.andoid.dialogfragment.MyCustomDialog.onSubmitListener;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements onSubmitListener {
 TextView mTextView;
 Button mButton;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mTextView = (TextView) findViewById(R.id.textView1);
  mButton = (Button) findViewById(R.id.button1);
  mButton.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    MyCustomDialog fragment1 = new MyCustomDialog(); 
    fragment1.mListener = MainActivity.this;
    fragment1.text = mTextView.getText().toString();
    fragment1.show(getFragmentManager(), "");
   }
  });  
  
 }

 @Override
 public void setOnSubmitListener(String arg) {
  mTextView.setText(arg);
 }

}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:text="Change Text" />

</LinearLayout>



I hope this post is useful to you. kindly share your feedback as comment here.


Thank You



Thursday, 19 September 2013

Simple TextToSpeech Example in Android

Now possible to hear the spoken of words by Android.

Screenshot:

Code:

TextToSpeechActivity.java
package com.android.texttospeach;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class TextToSpeechActivity extends Activity implements
  TextToSpeech.OnInitListener {

 private static final String TAG = "MyTextToSpeechDemo";

 private TextToSpeech mTts;
 private Button mPlayButton;
 EditText mEditText;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.text_to_speech);

  // Initialize text-to-speech. This is an asynchronous operation.
  mTts = new TextToSpeech(this, this);

  mPlayButton = (Button) findViewById(R.id.button1);
  mEditText = (EditText) findViewById(R.id.editText1);
  mPlayButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    play();
   }
  });
 }

 @Override
 public void onDestroy() {
  // Don't forget to shutdown!
  if (mTts != null) {
   mTts.stop();
   mTts.shutdown();
  }

  super.onDestroy();
 }

 // Implements TextToSpeech.OnInitListener.
 public void onInit(int status) {
  // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
  if (status == TextToSpeech.SUCCESS) {
   int result = mTts.setLanguage(Locale.US);
   if (result == TextToSpeech.LANG_MISSING_DATA
     || result == TextToSpeech.LANG_NOT_SUPPORTED) {
    Log.e(TAG, "Language is not available.");
   } else {
    mPlayButton.setEnabled(true);
    play();
   }
  } else {
   Log.e(TAG, "Could not initialize TextToSpeech.");
  }
 }

 private void play() {
  String hello = mEditText.getText().toString();
  mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, null);
 }

}


text_to_speech.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:enabled="false"   
        android:ems="7"
        android:gravity="center"    
        android:text="Play" />

</LinearLayout>
I hope this post is useful to you. kindly share your feedback as comment here.



Thank You



Sunday, 1 September 2013

Spinner with multiple selection in Android

Video Output:



Source code link is added below.

Here we have a class called MultiSelectionSpinner.

In this class we have following methods.


  • public void setItems(String[] items)


    Used to set adapter items using array of strings to this spinner.
  • public void setItems(List items)


    Used to set adapter items using list of strings to this spinner.
  • public void setSelection(String[] selection)


    Used to set selected items on this adapter using array of strings to this spinner.
  • public void setSelection(List selection)


    Used to set selected items on this adapter using list of strings to this spinner.
  • public void setSelection(int index)


    Used to set single selected item using position to this spinner.
  • public void setSelection(int[] selectedIndices)


    Used to set selected items using array of integers to this spinner.
  • public List getSelectedStrings()


    Used to get the selected items as a string
  • public List getSelectedIndices()


    Used to get the selected indexes as a list of integers


  • I hope this post is useful to you. kindly share your feedback as comment here.



    Source code on GitHub



    Thank You