Author Archive

The following code snippet will show you how to save an output file in internal and external storage in android:

If you want to store a file in internal storage, you have to use path like following:

File f = this.getFilesDir();

String outputFile = f.getAbsolutePath()+”/myrecording.3gp”;

if you want to store a file in external storage, you have to use path like following:

String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + “/myfile.3gp”;

That’s it!

To integrate latest admob interstitial ads and banner ads, you have to follow following steps:

Login to your new admob home page and Click on Monitize tab.  Select one of your app that you want to add interstitial ads, then click on the “View set up instruction” link. You will see a box like following:

Set up AdMob ad units
Follow the Google Developers website for complete instructions on how to integrate the Google AdMob SDK.

click on the “Send an email with these instruction” button, a pop will come and enter your email address and click Send Email button.

You will receive an email from admob with following:

Ad unit name: xxxxxx
Ad unit ID: ca-app-pub-xxxxxx

Copy the ad unit id, we need it to paste it in the app.

/** Android Manifest settings **/

** Don’t forget to include Google play service as library in your app. For this you have to right-click on your app and select properties and select android. There you can add Google play service.

Open android Manifest XML file and add following:

Permissions:

<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

Inside the <activity> add <meta> tag:

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<meta-data android:name=”com.google.android.gms.version”
android:value=”@integer/google_play_services_version”/>

..

..

..

<!– Activity required to show ad overlays. –>
<activity android:name=”com.google.android.gms.ads.AdActivity”
android:configChanges=”keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize”/>

</application>

</manifest>

Open your app, and add following line:

/** Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = “<your ad unit id>”;

/** The interstitial ad. */
private InterstitialAd interstitialAd;

boolean interstitialStatus = false;

/* This is for banner ads */
private AdView adView;

Inside the onCreate() method, just below the setContentView(), add following:

// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

// Locate the layout to load banner ads

LinearLayout layout = (LinearLayout) findViewById(R.id.Adlayout);
layout.addView(adView);

AdRequest adRequest1 = new AdRequest.Builder().build();

// Start loading the ad in the background.
adView.loadAd(adRequest1);

/** Interstial ads **/

// add following just below the adView.setAdUnitId(AD_UNIT_ID);

interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {

interstitialStatus = true;
}

@Override
public void onAdFailedToLoad(int errorCode) {
interstitialStatus = false;
}

});

// add a function to handle back button press:

@Override
public void onBackPressed() {
if ( interstitialStatus ) {
if ( interstitialAd.isLoaded() )
interstitialAd.show();
}
super.onBackPressed();
}

That’s it!

To fix Lint error or app_name not translated error in Android, you have to add “translate” attribute to the <string> tag entries in the string.xml file.

It is under res > values folder.

Existing lines:

<string name=”app_name”>Test App</string>

 <string name=”hello_world”>Hello world!</string>
 <string name=”menu_settings”>Settings</string>

change it to following:

<string name=”app_name” translatable=”false”>Test App</string>
 <string name=”hello_world” translatable=”false”>Hello world!</string>
 <string name=”menu_settings” translatable=”false”>Settings</string>

Clean your project (Project > Clean) and build it again.

That’s it!

 

Sending SMS using implicit intent:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(“smsto:<your phone number>”));

intent.putExtra(“sms_body”, “your message”);

startActivity(intent);

Sending SMS using explicit intent:

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(“your phone”, null, “your message”, null, null);
Toast.makeText(getApplicationContext(), “SMS sent.”,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
“SMS faild, please try again.”,
Toast.LENGTH_LONG).show();
e.printStackTrace();
}

The following code snippet teach you on how to create a custom AlertDialog for accepting user input:

First of all, you have to create a layout file for the alert dialog:

//prompt.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/layout_root”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”
android:padding=”10dp” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Enter your name:”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<EditText
android:id=”@+id/editTextDialogUserInput”
android:layout_width=”match_parent”
android:layout_height=”wrap_content” >
</EditText>

</LinearLayout>

 

Open your activity file and add following:

final AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
button4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//Inflating custom layout to alert dialog
LayoutInflater li = LayoutInflater.from(getApplicationContext());
View promptsView = li.inflate(R.layout.prompt, null);
builder4.setView(promptsView);

final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);

builder4.setPositiveButton(“OK”, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println(“Your name is ” + userInput.getText().toString());
}
});

builder4.setNegativeButton(“Cancel”, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println(“You pressed Cancel button”);
}
});

AlertDialog alert = builder4.create();
alert.show();
}
});

The following code will show you how to download image from remote url with AsyncTask in Android

In the onCreate you have to add following code:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView1);
new DownloadImageTask(imageView).execute( “http://remote_url_to_image” );
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String… urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e(“Error”, e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}

Incoming search terms:

  • download image from url android

The following code snippet will show you how to read Files from internal storage in Android:

byte[] buffer = null;
File file = new File(context.getFilesDir(), “myfile”);
FileInputStream fis;
String strData = null;
if( file.exists() ) {
int size;
try {
fis = new FileInputStream(file);
size = fis.available();
buffer = new byte[size];
fis.read(buffer);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
strData = new String(buffer);

}

The following code snippet will show you how to write a file in the internal storage in Android:

String fname = “myfile”;
String string = data;
FileOutputStream outputStream;

try {
outputStream = openFileOutput(fname, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

The following program will display an AlertDialog with a ListView of your String array or ArrayList:

Step1:

Create a string array inside your Activity class:

String [] fruits= { “Apple”, “Banana”, “Mango”,”Grapes”, “Orange”};

Step2:

Inside your onCreate() method add following:


//Don't forget to create a button to trigger the Alert Dialog

final AlertDialog.Builder builder = new AlertDialog.Builder(this);

myButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

builder.setTitle("Fruits");
builder.setItems(fruits, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {

System.out.println("Selected Item:" + fruits[item]);

}
});
AlertDialog alert = builder.create();
alert.show();
}
});

Thats it!

Advanced Training in Android. Online Training also available at http://schogini.com/mobile-app-development-training .

Android Application Development Course Details are given below:

1. Eclipse: To Configure Eclipse to create, edit, build, and debug your Android applications and to manage project resources.

2. Android SDK: Installation, configure and use Android SDK Manager and Android Virtual Device Manager.

3. Activity Lifecycle: How activities are supposed to behave, onCreate(), onDestroy(), onPause(), onResume() and onSaveInstanceState()

4. GUI: Using XML for UI layout, Basic widgets, introductions to LinearLayout, RelativeLayout, and TableLayout.

5. Emulator: How to configure and use Android Virtual Device.

6. Resources: General theory of resources, Drawables, Values, XML and Offering multiple resource Sets

7. UI Widgets: Basic and Advanced Widget for UI Design.

8. Menu: Adding option menus, adding popup menus, showing dialogs

9. Media: Supported audio and video formats, simple audio and video playback, streaming audio and video.

10. Intents: Explicit Intents & Implicit intents.

11. Preferences and Files: Using shared preferences and simple reading and writing of files.

12. Notifications: Role of notifications, raising a notification and reacting to selected notifications.

13. Databases: Introducing SQLite, SQLiteOpenHelper and creating a database, Opening and closing a database, Working with cursors, Inserts, updates, and deletes

14. SMS, Email and Alarm: Sending an SMS and Email from your application, using AlarmManager for periodic actions and WakeLocks for work done while phone is asleep

15. Using Services: Broadcast Receivers, Services -Notifications and Toasts.

16. Maps: Adding a Google map, custom overlays with push-pins, Zoom controls and setting different map views.

17. Remote Server Communication: Use Http GET/POST for Remote Server communication via internet using AsyncTask.

 

Page 1 of 5712345...102030...Last »
We Can Build Your App for You. Lets Get Started !