2010-03-12

Piece of code to get the text content in a web file to an Android application.

// Make the connection
URL url = new URL(type_url_here_as_a_string);
URLConnection con = url.openConnection();
// Get the response
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader rd = new BufferedReader(isr);
if(rd.ready()){ //Check the buffer reader ready for read.
    //Perform the read here by using rd.read() or rd.readLine() according to your requirement.
}
rd.close(); //Close the Buffer Reader.

Piece of code to create a file in Android sdcard

byte[] byte_array = new byte[100];

File file = new File("sdcard/file_name.extension");

FileOutputStream fos = new FileOutputStream(file);

fos.write(byte_array);

            fos.flush();

            fos.close();

This code demonstrate how to make a file from a byte array, in the Android sdcard. You can create any kind of file using a regarding byte array.

2010-03-04

How to load a PDF file in a URL from an android app

First of all you have to install a 3rd party application to your Android running device to view PDF files as Android operating system does not have a native app to view PDF files. There are many free apps for view PDF, in the Android market. You can download and install a one application from there. Then call the following lines of code where you want to download the PDF file and display it.

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(
your_pdf_web_address_here));

startActivity(i);

Then a new activity will start with a web browser pointing to that address. There it will prompt you to download the PDF file. Then you can download it and view it from the PDF viewer you have downloaded before. It’s ridiculous that android and blackberry doesn’t support a in-built PDF viewer. PDF is a world accepted format for documents. iPhones and Windows mobile running phones have a great support for PDF viewing. But shamefully android and blackberry doesn’t support it. :(

2010-03-02

How to call the native email client in an Android phone from an application

Write down the following piece of code where you want to call the email client.

 

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient_email_address });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);

startActivity(Intent.createChooser(emailIntent, messageYouwantToDisplay));

How to display a progress indicator in an Android app

dialog = ProgressDialog.show(Context, "Heading", "Message", false);

call the above code where you want to display the progress dialog and call the following code to dismiss the progress dialog.

dialog.dismiss();

Note: Never ever call a network operation in UI thread. If you call those both operations in the same thread, then the progress dialog may not shown.

How to set a background image for a controller in Android

Drawable d = getResources().getDrawable(R.drawable.pho_cellbackground);
tr.setBackgroundDrawable(d);

“pho_cellbackground” is the image name. You must include that image in ‘res’ folder in order to refer it. Image file name must be in lowercase and it is preferred that it’s in PNG format.

How to send a XML web request and get the response in an Android application

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
your_url_here
);
HttpResponse response = null;
InputStream content = null;

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("data",
your_xml_request_here));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    response = httpclient.execute(httppost);
    content = response.getEntity().getContent();

    BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    rd.close();
    strResponse = sb.toString();

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

How to pass a value to an activity from another activity in Android

In starting activity(first activity) write down the following piece of code.

Intent photoDetails = new Intent(this, PhotoDetails.class);
photoDetails.putExtra("id_number", v.getId());

photodetails is the second activity. Add the following piece of code in the photodetails activity to get the value for “id_number”.

Bundle extras = getIntent().getExtras();
int photo_id = extras.getInt("id_number");

How to set a listener to a button in Android.

In XML file

<Button android:id="@+id/PhotosBtn" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:gravity="center"
    android:text="photos" />

In Java file

View photosButton = findViewById(R.id.PhotosBtn);
photosButton.setOnClickListener(this);

Then implement the onClick(View v) method as you desire.

How to set a theme for android app using java code

Simply use the following piece of code where appropriate.

setTheme(android.R.style.Theme_Light);

You can use your desire theme rather than “Theme_Light”. Just replace that name by your desire theme name.