Sunday, December 14, 2014
Thursday, December 11, 2014
Wednesday, November 12, 2014
android headset or headphone button controller
download: http://www.mediafire.com/download/0667cfia4yo0yig/HeadsetControll.zip
package com.headset;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
public class AudioEqualizerActivity extends Activity {
// Using LinearLayout instead of R.layout.main (main.xml)
BroadcastReceiver broadcastsHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);// "android.intent.action.MEDIA_BUTTON"
MediaButtonReceiver r = new MediaButtonReceiver();
filter.setPriority(100);
registerReceiver(r, filter);
// unregisterReceiver(broadcastsHandler);
Log.e("----onCreate--", " ");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
broadcastsHandler = new MediaButtonReceiver();
registerReceiver(broadcastsHandler, new IntentFilter(
Intent.ACTION_HEADSET_PLUG));
registerMediaButton(AudioEqualizerActivity.this);
}
public static void registerMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(receiver);
}
/**
* Unregister the media buttons from AudioManager.
*
* @param context
* A context to use.
*/
public static void unregisterMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.unregisterMediaButtonEventReceiver(receiver);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
}
package com.headset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("----onReceive--", " ");
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
Log.e("----jack out--", " ");
if (intent.getExtras().getInt("state") == 1)// if plugged
Toast.makeText(context, "earphones plugged", Toast.LENGTH_LONG)
.show();
else
Toast.makeText(context, "earphones un-plugged",
Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "button pressed", Toast.LENGTH_LONG).show();
// key=intent.getExtras().getString("EXTRA_KEY_EVENT");
}
abortBroadcast();
}
}
mainifest
------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.headset"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- This is needed for isWiredHeadsetOn() to work in some cases. (bug?) -->
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AudioEqualizerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.headset.MediaButtonReceiver" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
<intent-filter android:priority="20" >
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.headset;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
public class AudioEqualizerActivity extends Activity {
// Using LinearLayout instead of R.layout.main (main.xml)
BroadcastReceiver broadcastsHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);// "android.intent.action.MEDIA_BUTTON"
MediaButtonReceiver r = new MediaButtonReceiver();
filter.setPriority(100);
registerReceiver(r, filter);
// unregisterReceiver(broadcastsHandler);
Log.e("----onCreate--", " ");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
broadcastsHandler = new MediaButtonReceiver();
registerReceiver(broadcastsHandler, new IntentFilter(
Intent.ACTION_HEADSET_PLUG));
registerMediaButton(AudioEqualizerActivity.this);
}
public static void registerMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(receiver);
}
/**
* Unregister the media buttons from AudioManager.
*
* @param context
* A context to use.
*/
public static void unregisterMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.unregisterMediaButtonEventReceiver(receiver);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
}
package com.headset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("----onReceive--", " ");
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
Log.e("----jack out--", " ");
if (intent.getExtras().getInt("state") == 1)// if plugged
Toast.makeText(context, "earphones plugged", Toast.LENGTH_LONG)
.show();
else
Toast.makeText(context, "earphones un-plugged",
Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "button pressed", Toast.LENGTH_LONG).show();
// key=intent.getExtras().getString("EXTRA_KEY_EVENT");
}
abortBroadcast();
}
}
mainifest
------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.headset"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- This is needed for isWiredHeadsetOn() to work in some cases. (bug?) -->
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AudioEqualizerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.headset.MediaButtonReceiver" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
<intent-filter android:priority="20" >
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
Monday, November 10, 2014
Monday, August 11, 2014
custom notification
download: http://www.mediafire.com/download/1nis9ose1xs13u2/NotificationCustom.zip
package com.mirtech.mirtube;
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.os.Bundle;
import android.widget.RemoteViews;
public class SplashActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = SplashActivity.this;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.splash);
remoteViews.setTextViewText(R.id.textView1, "ssssssss");
// When we click the widget, we want to open our main activity.
Intent defineIntent2 = new Intent(context, MyBroadcastReceiver.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,
0 /* no requestCode */, defineIntent2, 0 /* no flags */);
remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent2);
Notification.Builder mBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setContent(remoteViews);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(100, mBuilder.build());
}
}
package com.mirtech.mirtube;
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.os.Bundle;
import android.widget.RemoteViews;
public class SplashActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = SplashActivity.this;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.splash);
remoteViews.setTextViewText(R.id.textView1, "ssssssss");
// When we click the widget, we want to open our main activity.
Intent defineIntent2 = new Intent(context, MyBroadcastReceiver.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,
0 /* no requestCode */, defineIntent2, 0 /* no flags */);
remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent2);
Notification.Builder mBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setContent(remoteViews);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(100, mBuilder.build());
}
}
package com.mirtech.mirtube;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context _context, Intent _intent) {
Log.e("-----onReceive", " ");
// if (_intent.getAction().equals("hi")) {
// // TODO Broadcast a notification
//
// }
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mirtech.mirtube"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mirtech.mirtube.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver" >
<intent-filter>
<action android:name="hi" />
</intent-filter>
</receiver>
</application>
</manifest>
Wednesday, February 19, 2014
view flipper
download: http://www.mediafire.com/download/7j0ycdg5j2mdpvy/ViewFlipperDemos.zip
package com.ui.yogeshblogspot;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ViewFlipper;
public class Main extends Activity {
ViewFlipper flipper;
Button btBackPage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Getting View Flipper from main.xml and assigning to flipper reference
// variable
flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
btBackPage = (Button) findViewById(R.id.btBackPage);
btBackPage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if (flipper.isFlipping())// Checking flipper is flipping or not.
{
flipper.stopFlipping(); // stops the flipping .
}
flipper.showPrevious();// shows the next view element of ViewFlipper
}
});
}
public void flipByClick(View v) {
if (flipper.isFlipping())// Checking flipper is flipping or not.
{
flipper.stopFlipping(); // stops the flipping .
}
flipper.showNext();// shows the next view element of ViewFlipper
}
public void flipByInterval(View v) {
flipper.setFlipInterval(500);// setting the interval 500 milliseconds
flipper.startFlipping(); // views flipping starts.
}
}
--------------------------------
main.xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Calling flipByInterval() method of Activity class -->
<Button
android:id="@+id/btBackPage"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="Back Flip"
android:background="@android:color/holo_green_dark"/>
<Button
android:id="@+id/flipbyclick"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:background="@android:color/holo_green_dark"
android:onClick="flipByClick"
android:layout_marginLeft="10dp"
android:text="Flip By Click" />
<!-- calling flipByClick() method of Activity class -->
</LinearLayout>
<!-- ViewFlipper can contains any view elements in this case it contains 5 ImageView view Component with different Images -->
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/dialog_text" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img1" />
<!-- Setting img1 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img2" />
<!-- Setting img2 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img3" />
<!-- Setting img3 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img4" />
<!-- Setting img4 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img5" />
<!-- Setting img5 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img6" />
<!-- Setting img6 from drawable folder in ImageView -->
</ViewFlipper>
<Button
android:id="@+id/flipbyinterval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="flipByInterval"
android:text="Flip By Interval" />
</LinearLayout>
--------------------------------
dialog_text.xml
--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".50" >
<Button
android:id="@+id/imageViewSearchBanner"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="new btn" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".50" >
<Button
android:id="@+id/ivForText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="other btn" />
</LinearLayout>
</LinearLayout>
package com.ui.yogeshblogspot;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ViewFlipper;
public class Main extends Activity {
ViewFlipper flipper;
Button btBackPage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Getting View Flipper from main.xml and assigning to flipper reference
// variable
flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
btBackPage = (Button) findViewById(R.id.btBackPage);
btBackPage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if (flipper.isFlipping())// Checking flipper is flipping or not.
{
flipper.stopFlipping(); // stops the flipping .
}
flipper.showPrevious();// shows the next view element of ViewFlipper
}
});
}
public void flipByClick(View v) {
if (flipper.isFlipping())// Checking flipper is flipping or not.
{
flipper.stopFlipping(); // stops the flipping .
}
flipper.showNext();// shows the next view element of ViewFlipper
}
public void flipByInterval(View v) {
flipper.setFlipInterval(500);// setting the interval 500 milliseconds
flipper.startFlipping(); // views flipping starts.
}
}
--------------------------------
main.xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Calling flipByInterval() method of Activity class -->
<Button
android:id="@+id/btBackPage"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="Back Flip"
android:background="@android:color/holo_green_dark"/>
<Button
android:id="@+id/flipbyclick"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:background="@android:color/holo_green_dark"
android:onClick="flipByClick"
android:layout_marginLeft="10dp"
android:text="Flip By Click" />
<!-- calling flipByClick() method of Activity class -->
</LinearLayout>
<!-- ViewFlipper can contains any view elements in this case it contains 5 ImageView view Component with different Images -->
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/dialog_text" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img1" />
<!-- Setting img1 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img2" />
<!-- Setting img2 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img3" />
<!-- Setting img3 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img4" />
<!-- Setting img4 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img5" />
<!-- Setting img5 from drawable folder in ImageView -->
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img6" />
<!-- Setting img6 from drawable folder in ImageView -->
</ViewFlipper>
<Button
android:id="@+id/flipbyinterval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:onClick="flipByInterval"
android:text="Flip By Interval" />
</LinearLayout>
--------------------------------
dialog_text.xml
--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".50" >
<Button
android:id="@+id/imageViewSearchBanner"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="new btn" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".50" >
<Button
android:id="@+id/ivForText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="other btn" />
</LinearLayout>
</LinearLayout>
Tuesday, February 11, 2014
file download with asynctask & progress bar
download: http://www.mediafire.com/download/zlvy8f0gomntnhj/DownloadFileByProgressBar.zip
package com.example.androidhive;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidDownloadFileByProgressBarActivity extends Activity {
// button to show progress dialog
Button btnShowProgress;
// Progress Dialog
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
// File url to download
private static String file_url = "http://api.androidhive.info/progressdialog/hive.jpg";
String download_path = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
download_path = Environment.getExternalStorageDirectory().toString() + "/Download/";
// show progress bar button
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
// Image view to show image after downloading
my_image = (ImageView) findViewById(R.id.my_image);
/**
* Show Progress bar click event
* */
btnShowProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
}
});
}
/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/Download/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = download_path +"downloadedfile.jpg";
// setting downloaded into image view
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
}
Monday, February 10, 2014
scrollView with linearLayout
<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" >
<!-- non-scrolling top pane -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This text will not scroll" />
</LinearLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".80" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/black" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/fish_pressed"
android:text="Column 2" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/fish_pressed"
android:text="Column 3" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/gentleman_2"
android:text="Column 2" />
<Button
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/gentleman_2"
android:text="Column 3" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/button5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/fish_pressed"
android:text="Column 2" />
<Button
android:id="@+id/button6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/fish_pressed"
android:text="Column 3" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/button7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/gentleman_2"
android:text="Column 2" />
<Button
android:id="@+id/button8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/gentleman_2"
android:text="Column 3" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/button9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/fish_pressed"
android:text="Column 2" />
<Button
android:id="@+id/button10"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/fish_pressed"
android:text="Column 3" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/button11"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/gentleman_2"
android:text="Column 2" />
<Button
android:id="@+id/button12"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:background="@drawable/gentleman_2"
android:text="Column 3" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This text will not scroll" />
</LinearLayout>
</LinearLayout>
Tuesday, February 4, 2014
facebook wall post with image
download: http://www.mediafire.com/download/dd0qqra99yrnbs1/facebookLoginWallPostWithSdk3.0.zip
public class MainActivity extends Activity {
private static final String APP_ID = "529772120384562";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
private Facebook facebook;
private String messageToPost;
Button btFacebookShareButton;
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean deleteCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, "");
editor.putLong(EXPIRES, 0);
Log.e("------deleteCredentials() ", " ");
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btFacebookShareButton = (Button) findViewById(R.id.btFacebookShareButton);
btFacebookShareButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
// loginAndPostToWall();
}
}
});
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null) {
facebookMessage = "https://play.google.com/store/apps/details?id=com.polarbit.rthunder2lite"
+ " minute = "
+ new Date().getMinutes()
+ " "
+ new Date().getSeconds();
}
messageToPost = facebookMessage;
}
@Override
public void onBackPressed() {
Log.e("------onBackPressed() ", " ");
// TODO Auto-generated method stub
super.onBackPressed();
deleteCredentials(facebook);
}
@Override
protected void onDestroy() {
Log.e("------onDestroy", " ");
// TODO Auto-generated method stub
super.onDestroy();
deleteCredentials(facebook);
}
public void doNotShare(View button) {
finish();
}
/*
* public void share(View button) { if (!facebook.isSessionValid()) {
* loginAndPostToWall(); } else { postToWall(messageToPost); //
* loginAndPostToWall(); } }
*/
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
parameters.putString("picture", "http://upload.wikimedia.org/wikipedia/commons/a/a6/Bottlenose_Dolphin_KSC04pd0178.jpg");
parameters.putString("name", "BanglaGaan");
parameters.putString("caption", "G-Series Online Music Store");
parameters.putString("description", "Listening to ......");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
// finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}
}
private static final String APP_ID = "529772120384562";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
private Facebook facebook;
private String messageToPost;
Button btFacebookShareButton;
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean deleteCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, "");
editor.putLong(EXPIRES, 0);
Log.e("------deleteCredentials() ", " ");
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btFacebookShareButton = (Button) findViewById(R.id.btFacebookShareButton);
btFacebookShareButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
// loginAndPostToWall();
}
}
});
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null) {
facebookMessage = "https://play.google.com/store/apps/details?id=com.polarbit.rthunder2lite"
+ " minute = "
+ new Date().getMinutes()
+ " "
+ new Date().getSeconds();
}
messageToPost = facebookMessage;
}
@Override
public void onBackPressed() {
Log.e("------onBackPressed() ", " ");
// TODO Auto-generated method stub
super.onBackPressed();
deleteCredentials(facebook);
}
@Override
protected void onDestroy() {
Log.e("------onDestroy", " ");
// TODO Auto-generated method stub
super.onDestroy();
deleteCredentials(facebook);
}
public void doNotShare(View button) {
finish();
}
/*
* public void share(View button) { if (!facebook.isSessionValid()) {
* loginAndPostToWall(); } else { postToWall(messageToPost); //
* loginAndPostToWall(); } }
*/
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
parameters.putString("picture", "http://upload.wikimedia.org/wikipedia/commons/a/a6/Bottlenose_Dolphin_KSC04pd0178.jpg");
parameters.putString("name", "BanglaGaan");
parameters.putString("caption", "G-Series Online Music Store");
parameters.putString("description", "Listening to ......");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
// finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}
}
Subscribe to:
Posts (Atom)