Pr.Pg Next Pg

Toast Tutorials

  • A toast provides simple feedback about an operation in a small popup.

  • It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later.

  • Toasts automatically disappear after a timeout.

 

 

Application Example

  • We will have a XML layout whose code looks like this.

 

ext_storage.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">

 

<Spinner

android:id="@+id/spinner1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

 

<EditText

android:id="@+id/etFileName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

 

<requestFocus />

</EditText>

 

<Button

android:id="@+id/bSave"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:gravity="center"

android:text="Save" />

 

</LinearLayout>

 

  • Below is the java file of the activity.

 

ExternalData.java

 

package com.example.helloandroid;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Spinner;

import android.widget.Toast;

 

public class ExternalData extends Activity implements OnItemSelectedListener, OnClickListener {

Button bSave;

EditText etFileName;

Spinner spinner;

String state;

boolean read=false,write=false;

String path[] = {"Images","Sound","Video"};

File folder=null;

File destination=null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.ext_storage);

bSave = (Button)findViewById(R.id.bSave);

etFileName = (EditText)findViewById(R.id.etFileName);

Spinner spinner = (Spinner)findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,path);

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(this);

bSave.setOnClickListener(this);

state= Environment.getExternalStorageState();

checkState();

}

 

 

private void checkState() {

// TODO Auto-generated method stub

if(state.equals(Environment.MEDIA_MOUNTED)){

read=write=true;

}else if(state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)){

read=true;

write=false;

}else{

read=write=false;

}

}

 

@Override

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

// TODO Auto-generated method stub

switch(arg2){

case 0:

folder = new File(Environment.getExternalStorageDirectory()+"/Images");

break;

case 1:

folder = new File(Environment.getExternalStorageDirectory()+"/Sound");

break;

case 2:

folder = new File(Environment.getExternalStorageDirectory()+"/Video");

break;

}

}

 

@Override

public void onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub

}

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

String fname = etFileName.getText().toString();

destination = new File(folder,fname);

checkState();

if(read=write=true){

try {

folder.mkdirs();

InputStream is = getResources().openRawResource(R.drawable.background); OutputStream os = new FileOutputStream(destination);

byte [] data = newbyte[is.available()];

is.read(data);

os.write(data);

is.close();

os.close();

Toast t = Toast.makeText(ExternalData.this,"File has been saved", Toast.LENGTH_LONG);

t.show();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.print("Error");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

 

System.out.print("Error");

}

}

}

 

}

 

  • In this class basically we have created a Spinner which looks like a Drop down box.

  • We then have set up an adapter for the spinner in which we store the String array which will contain the name of the folder within the SD card we want to store in.

  • We also have a method which will check the state of the External Storage.

  • The boolean values namely read and write are used which will indicate the state of the storage.

  • Within the item in the Spinner is selected we create a folder of the name clicked in the Spinner inside the SD card.

  • Inside the onClick() method of the button we read the resource which we ant to store in the SD card using the InputStream and then we make an OutputStream Object with the destination file name.

  • We then perform the operation of reading the data and then writing into the file.

  • We also have a toast which will pop up when the operation is finished indicating the success of the operation.

  • Note while entering the name of the file loaded you will have to appropriate extension otherwise the file might not be openable.

  • You will also have to permission into the AndroidManifest to read the external storage.

  • uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

  • Below are the screenshots of the application.

 


Pr.Pg border                                              Next Pg