Saturday, 21 December 2013

Android File Handling

Lets learn File handling in Android.

First, create a layout to enter and save the data and to display the saved data.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText 
        android:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Here"
        />
    
    <TextView
        android:id="@+id/tv"
        android:layout_below="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/save"
        android:layout_alignBottom="@+id/save"
        android:layout_toRightOf="@+id/save"
        android:text="READ" />

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv"
        android:layout_centerVertical="true"
        android:text="SAVE" />

</RelativeLayout>



And then make changes in the MainActivity.java.

package com.codingredefined.androidfilehandling;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
Button b1,b2;
EditText et;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.save);
b2=(Button)findViewById(R.id.read);
et=(EditText)findViewById(R.id.et);
tv=(TextView)findViewById(R.id.tv);
b1.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String str=et.getText().toString();
FileOutputStream fos;
try
{
fos=openFileOutput("abc",Context.MODE_PRIVATE);
fos.write(str.getBytes());
fos.close();
Toast.makeText(MainActivity.this,"Data Saved",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,"Data NOT Saved",Toast.LENGTH_SHORT).show();
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
FileInputStream fis;
try
{
fis=openFileInput("abc");
int i=0;
String str="";
while((i=fis.read())!=-1)
{
str=str+((char)+i);
}
tv.setText(str);
fis.close();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,"Data NOT Read",Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


The following output is obtained.




Friday, 20 December 2013

Android Notification

Learn to create Notification.

Open the project folder and create a new folder "raw" in the res folder. Put a .mp3 file which you want to use as notification sound.

Do the following coding in the MainActivity.java file.

package com.codingredefined.notification;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity 
{
private static final int HELLO_ID=1;
MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String ns=Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager=(NotificationManager)getSystemService(ns);
int icon=R.drawable.ic_launcher;
CharSequence tickerText="Hello!";
long when=System.currentTimeMillis();
Notification notification=new Notification(icon, tickerText, when);
//Vibrate on Notification
notification.defaults=Notification.DEFAULT_VIBRATE;
long[] vibrate={0,100,200,300};
notification.vibrate=vibrate;
//Glow LED
notification.defaults=Notification.DEFAULT_LIGHTS;
notification.ledARGB=0xff00ff00;
notification.ledOnMS=300;
notification.ledOffMS=1000;
notification.flags=Notification.FLAG_SHOW_LIGHTS;
//Activate notification
Context context=getApplicationContext();
CharSequence contextTitle="My Notification";
CharSequence contextText="Hello World!!!";
Intent notificationIntent=new Intent(this,MainActivity1.class);
PendingIntent contentIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contextTitle, contextText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
//Sound
mNotificationManager.notify(HELLO_ID, notification);
mMediaPlayer=new MediaPlayer();
mMediaPlayer=MediaPlayer.create(this,R.raw.abc);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
Thread t=new Thread()
{
@Override
public void run()
{
try
{
Thread.sleep(5000);
}
catch(Exception e){}
mMediaPlayer.stop();
}
};
t.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


The following output will be obtained.

Tuesday, 10 December 2013

Web View

WebView is a view which can display web pages in your activity.

Create a webview in layout file.

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="200dp" />

The button created in the below image is of no use.


Make the changes in the Activity file.

package com.codingredefined.webview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
WebView mywebView;
mywebView=(WebView)findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.loadUrl("http://www.google.com");
mywebView.setWebViewClient(new WebViewClient());
mywebView.setInitialScale(1);
mywebView.getSettings().setUseWideViewPort(true);
final Activity MyActivity=this;
mywebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view,int progress)
{
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress*100);
if(progress==100)
{
MyActivity.setTitle("WebView");
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



This will be the output.