Saturday, May 25, 2013

Open & Read File from Assets

Hey Guys, Today I am going to share the important code of Assets. How to open the file and read the file from assets folder. In that Scenario, There is a class in Android API AssetManager.

Open file with AssetManager


package app.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
try {
AssetManager manager = getAssets();
InputStream mInput = manager.open("data.csv");
byte[] data = new byte[128];
mInput.read(data);
mInput.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

Read Asset Files


package app.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;

public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("/text.txt");
String text = loadTextFile(inputStream);
textView.setText(text);
} catch (IOException e) {
textView.setText("Couldn't load file");
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
textView.setText("Couldn't close file");
}
}
}

public String loadTextFile(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int len = 0;
while ((len = inputStream.read(bytes)) > 0)
byteStream.write(bytes, 0, len);
return new String(byteStream.toByteArray(), "UTF8");
}
}

Cheers! Guys

Alarm demo

Hello guys, Today I am going to share the knowledge about Alarm coding in java. There is class in Android API that manage the Alarm that is AlarmManager .And need to registered the broadcast Register, that one time it automatically trigger.

Main.xml



AlarmActivity.java

package app.test;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Toast;

public class AlarmActivity extends Activity implements View.OnClickListener {

private PendingIntent mAlarmIntent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.start).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
Intent launchIntent = new Intent(this, AlarmReceiver.class);
mAlarmIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0);
}

@Override
public void onClick(View v) {
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
long interval = 5*1000; //5 seconds

switch(v.getId()) {
case R.id.start:
Toast.makeText(this, "Scheduled", Toast.LENGTH_SHORT).show();
manager.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime()+interval,
interval,
mAlarmIntent);
break;
case R.id.stop:
Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
manager.cancel(mAlarmIntent);
break;
default:
break;
}
}

private long nextStartTime() {
long oneDay = 24*3600*1000; //24 hours
//Set the time to 09:00:00
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 9);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);

Calendar now = Calendar.getInstance();
if(now.before(startTime)) {
return startTime.getTimeInMillis();
} else {
startTime.add(Calendar.DATE, 1);
return startTime.getTimeInMillis();
}
}
}

AlarmReceiver.java

package app.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar now = Calendar.getInstance();
DateFormat formatter = SimpleDateFormat.getTimeInstance();
Toast.makeText(context, formatter.format(now.getTime()), Toast.LENGTH_SHORT).show();
}
}

Wednesday, May 22, 2013

Gallery View

Hey Guys, Today I am going to share code with you Gallery. This code to say how to move image from gallery and show on Image View. In that case we are using the adapter.

GalleryView.java

package com.android.gallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.antartica1,
R.drawable.antartica2,
R.drawable.antartica3,
R.drawable.antartica4,
R.drawable.antartica5,
R.drawable.antartica6,
R.drawable.antartica7,
R.drawable.antartica8,
R.drawable.antartica9,
R.drawable.antartica10
};
ImageView imageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));

imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (arg2+1) + " of Antartica",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[arg2]);

}

});

}


public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;

public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}

@Override
public int getCount() {

return pics.length;
}

@Override
public Object getItem(int arg0) {

return arg0;
}

@Override
public long getItemId(int arg0) {

return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}

}
}

main.xml










Screen Shot


Image Switcher View

Today i going to share the code the of image switcher. Image will move from one by one through Gallery.

Main.xml










ImageSwitcherView.java

package com.android.switcher;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherView extends Activity implements ViewFactory {

Integer pics[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e };

ImageSwitcher iSwitcher;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
iSwitcher.setFactory(this);
iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

Gallery gallery = (Gallery) findViewById(R.id.Gallery01);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
iSwitcher.setImageResource(pics[arg2]);
}
});
}

public class ImageAdapter extends BaseAdapter {

private Context ctx;

public ImageAdapter(Context c) {
ctx = c;
}

@Override
public int getCount() {

return pics.length;
}

@Override
public Object getItem(int arg0) {

return arg0;
}

@Override
public long getItemId(int arg0) {

return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

ImageView iView = new ImageView(ctx);
iView.setImageResource(pics[arg0]);
iView.setScaleType(ImageView.ScaleType.FIT_XY);
iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
return iView;
}

}

@Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
}



Screen Shot


 

 

Copyright @ 2013 Android Developers Tipss.