Populating Android ListView from Java

<?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" >     <ListView android:id="@+id/statesList" android:layout_width="match_parent" android:layout_height="fill_parent" />   </LinearLayout> package com.w3mentor.android;   import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;   public class MyListViewActivity extends Activity { /** Called when the activity is first created. */ @Override public […]
Continue reading…

Enjoyed this post? Share it!

 
 

Android TableLayout with ImageView

<?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" >   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="States of The USA" />   <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow>   <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/california" />   <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/nevada" />   <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/washington" />   <ImageView android:id="@+id/imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ohio" />     </TableRow> <TableRow> […]
Continue reading…

Enjoyed this post? Share it!

 
 

Android RelativeLayout example

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/wrapperLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="3dp">   <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="User Name:" />   <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" > </EditText>   <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:text="Passoword" />   <EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/textView2" />   <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" […]
Continue reading…

Enjoyed this post? Share it!

 
 

Android LinearLayout example

<?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" android:gravity="center_vertical">   <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="White" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/White" android:textColor="#000000" android:gravity="center_horizontal" android:layout_weight=".5"/>   <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Green" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/Green" android:gravity="center_horizontal" android:layout_weight=".16"/>   <TextView android:id="@+id/textView3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Yellow" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/Yellow" android:textColor="#000000" android:gravity="center_horizontal" android:layout_weight=".16"/>   <TextView android:id="@+id/textView4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Red" android:textAppearance="?android:attr/textAppearanceLarge" android:background="@color/Red" android:gravity="center_horizontal" android:layout_weight=".16"/>   </LinearLayout>
Continue reading…

Enjoyed this post? Share it!

 
 

Audio recorder in Android

// initialize the MediaRecorder MediaRecorder mediarecorder = new MediaRecorder(); mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediarecorder.setOutputFile("/sdcard/myaudiorecord.3gpp"); mediarecorder.prepare(); mediarecorder.start(); //To stop recording use below code.. //mediarecorder.stop(); //mediarecorder.reset(); //After MediaRecorder is no longer needed, release it: //mediarecorder.release();
Continue reading…

Enjoyed this post? Share it!

 
 

Playing music/audio file in Android

string mediaFilePath = "PATH TO YOUR MUSIC/AUDIO FILE"; File mediafile = new File(mediaFilePath); short[] audio = new short[(int) (mediafile.length()/2)]; AudioTrack audiotrack = new AudioTrack( AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, audio.length, AudioTrack.MODE_STREAM );   audiotrack.play(); audiotrack.write(audio, 0, audio.length); // To pause use code below //audiotrack.pause(); // To stop use code below //audiotrack.stop(); // release all resources audiotrack.release();
Continue reading…

Enjoyed this post? Share it!

 
 

Check network connection availability on android using Java

public boolean checkTheNetworkAvailability() { Context mycontext = getApplicationContext(); ConnectivityManager myconnectivity = (ConnectivityManager) mycontext.getSystemService(Context.CONNECTIVITY_SERVICE); if (myconnectivity == null) { return false; // failed null!.. } else { NetworkInfo[] myinfo = myconnectivity.getAllNetworkInfo(); if (myinfo != null) { for (int i = 0; i < myinfo.length; i++) { if (myinfo[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example of Simple Retry Technique to Deal with Timeouts in Android

<div><a href="http://orderessayonlineon.com/">academic writing help online</a> – <a href="http://spyphoneapp-software.com/">software spy phone download</a> – <a href="http://iphonespyapponline.com/">iphone spy</a> – <a href="http://buyessay-onlinein.com/">coursework writing service</a> – <a href="http://cellphonespyappon.com/">spy cell phone voicemail</a> – <a href="http://orderessayonlineon.com/">do my essay for money</a> – <a href="http://cellphonespyappon.com/">cell phone spy iphone free</a></div> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example of multipart POST using Android

import java.io.ByteArrayInputStream; import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.InputStreamBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; public class TestMultipartPost extends Activity { public void executeMultipartPost()throws Exception { try { InputStream is = this.getAssets().open("data.xml"); HttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://w3mentor.com/Upload.aspx"); byte[] data = IOUtils.toByteArray(is); InputStreamBody isb […]
Continue reading…

Enjoyed this post? Share it!

 
 

Perform a HTTP POST Request with the HttpClient in Android

HTTP POST calls are made with the HttpClient by calling the execute() method of the HttpClient with an instance of HttpPost. We should pass URL-encoded name/value form parameters as part of the HTTP request. import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; public class TestHttpPost […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example of HTTP GET Request using HttpClient in Android

import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class TestHttpGet { public void executeHttpGet() throws Exception { BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI("http://w3mentor.com/")); HttpResponse response = client.execute(request); in = new BufferedReader (new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = […]
Continue reading…

Enjoyed this post? Share it!