Android: How to load layout xml files dynamically during runtime

Here is a sample code to load XML layout files at runtime within your Android application. Basically, you have to have a Layout tag within your main XML file so that we get to have a placeholder to where we want to store our content.


<?xml version=”1.0″ encoding=”UTF-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  android:id=”@+id/content1″
  android:orientation=”vertical”
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent”
  android:background=”#ff99ccff”
>

</LinearLayout>

Then in your code, follow this:


LinearLayout ll = (LinearLayout) a.findViewById(R.id.content2);
ViewInflate vi = (ViewInflate) a.getSystemService(Context.INFLATE_SERVICE);
View vv = vi.inflate(R.layout.mynewxmllayout, null, null);
ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().height));

where a is the class that extends Activity. If you are going to place this code within your class that extends Activity, you don’t need to include the a. in the code. Of course, this does not restrict to LinearLayout alone. You can use all other layouts to add new Views as its content.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(3 votes, average: 4.67 out of 5)
 Loading ...

Android: Showing System.out messages to console

In Java, we use System.out or System.err.println() to show messages in the console. However, these don’t work in Android. These methods do not produce errors on compilation and does nothing during runtime. Chris Glover made this nifty class to actually be able to use System.out and System.err in your programming needs.


package ***;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.util.Log;

public class LogTool extends OutputStream {
  private ByteArrayOutputStream bos = new ByteArrayOutputStream();
  private String name;
  
  public LogTool(String name)   {
    this.name = name;
  }
  
  @Override
  public void write(int b) throws IOException {
    if (b == (int)’\n’) {
  String s = new String(this.bos.toByteArray());
  Log.v(this.name, s);
  this.bos = new ByteArrayOutputStream();
    } else {
    this.bos.write(b);
    }
  }
}

At the startup of your code, use these lines


System.setErr(new PrintStream(new LogTool(”System.err”)));
System.setOut(new PrintStream(new LogTool(”System.out”)));

Then you can use System.out and System.err.println(). If you want to convert Exception messages generated from the printStackTrace() method of Exception classes, use this Exception to String method converter that I have.


public static String getExceptionStackTraceAsString(Exception exception) {
  StringWriter sw = new StringWriter();
  exception.printStackTrace(new PrintWriter(sw));
  return sw.toString();
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

Having a dedicated server with GoDaddy.com

GoDaddy is a hosting provider. You can either apply for a shared hosting or a dedicated server. Shared hosting means your hosting account is shared inside a server with all other customers. At least this is how I see it. A dedicated server meanwhile is like having your own server where you have total control over it. You can only access it remotely as the server itself does not have a physicality. Whatever you do with your server, just connect to it remotely. They also have this policy that since you are your own administrator, they won’t install things for you unless you apply for their maintenance fee. I encountered a problem with them before, took a lot of email exchanges before they finally gave me a good answer. If you need to add windows components like network services and others, you would need an OS installer for the components to be installed. They actually have an i386 folder in drive C in case you want to install or uninstall components. I was thinking of connecting my remote desktop application to my dvd-rom drive and install it from there but it was a good thing that GoDaddy placed that i386 folder in case we need something. Took probably 5 customer service agents exchanging emails till the last one gave me that tip, which solved my problem

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

Android m5 rc15 and Web services

Currently Google’s Android is under version m5 rc15. Instead of dabbling on converting all my java applet games to possibly android’s format, I focused on checking if Android is capable of calling web services or not. The answer is no. Android’s API doesn’t contain classes that support this capability. However, a 3rd party library called KSoap2 is available for use. From their sourceforge website, “kSOAP is a SOAP web service client library for constrained Java environments such as Applets or J2ME applications (CLDC / CDC / MIDP)”. And how lucky that Android uses Java as its developmental language. I am not sure if it is possible to build an Android application and call a remote web service by compiling it using a builder app like Ant. Most posts in the mailing list do cover making it work using the Eclipse IDE, as does most of Android’s tutorial focuses on. I kept swarming the net looking for possible ways and solutions with using just Ant and not an IDE as i abhor using it. In the end, I had no choice but to use Eclipse to be able to at least verify that the sample code and solution provided by someone in the mailing list does indeed work.

Download the KSoap2 library. Extract the jar file library and add it in your Eclipse project. Create a lib folder and import the jar file there. Next, right click the project name and select properties. Under the Java Build Path, add the KSoap2 jar so that it will be included in the classpath during compilation and deployment. Click OK.

The following 2 files are needed for web service communication


AndroidServiceConnection.java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.ksoap2.transport.ServiceConnection;

/**
* Connection using apache HttpComponent
*/
public class AndroidServiceConnection implements ServiceConnection {
    private static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    private HttpConnection connection;
    private PostMethod postMethod;
    private java.io.ByteArrayOutputStream bufferStream = null;

    /**
    * Constructor taking the url to the endpoint for this soap communication
    * @param url the url to open the connection to.
    */
    public AndroidServiceConnection(String url) throws IOException {
        HttpURL httpURL = new HttpURL(url);
        HostConfiguration host = new HostConfiguration();
        host.setHost(httpURL.getHost(), httpURL.getPort());
        connection = connectionManager.getConnection(host);
        postMethod = new PostMethod(url);
    }

    public void connect() throws IOException {
        if (!connection.isOpen()) { connection.open(); }
    }

    public void disconnect() { connection.releaseConnection(); }

    public void setRequestProperty(String name, String value) { postMethod.setRequestHeader(name, value); }

    public void setRequestMethod(String requestMethod) throws IOException {
        if (!requestMethod.toLowerCase().equals(”post”)) { throw(new IOException(”Only POST method is supported”)); }
    }

    public OutputStream openOutputStream() throws IOException {
        bufferStream = new java.io.ByteArrayOutputStream();
        return bufferStream;
    }

    public InputStream openInputStream() throws IOException {
        RequestEntity re = new ByteArrayRequestEntity(bufferStream.toByteArray());
        postMethod.setRequestEntity(re);
        postMethod.execute(new HttpState(), connection);
        return postMethod.getResponseBodyAsStream();
    }

    public InputStream getErrorStream() { return null; }
}

AndroidHttpTransport.java
import java.io.*;

import org.ksoap2.transport.*;
import org.ksoap2.*;
import org.xmlpull.v1.*;

/**
* Apache HttpComponent based HttpTransport layer.
*/
public class AndroidHttpTransport extends Transport {
 String response;

 /**
 * Creates instance of HttpTransport with set url
 *
 * @param url
 * the destination to POST SOAP data
 */
 public AndroidHttpTransport(String url) {
 super(url);
 }

 /**
 * set the desired soapAction header field
 *
 * @param soapAction
 * the desired soapAction
 * @param envelope
 * the envelope containing the information for the soap call.
 */
 public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException {
  if (soapAction == null) soapAction = “\”\”";
  byte[] requestData = createRequestData(envelope);
  requestDump = debug ? new String(requestData) : null;
  responseDump = null;
  ServiceConnection connection = getServiceConnection();
  connection.connect();

  try {
   connection.setRequestProperty(”User-Agent”, “kSOAP/2.0″);
   connection.setRequestProperty(”SOAPAction”, soapAction);
   connection.setRequestProperty(”Content-Type”, “text/xml”);
   connection.setRequestProperty(”Connection”, “close”);
   connection.setRequestProperty(”Content-Length”, “” + requestData.length);
   connection.setRequestMethod(”POST”);  
   OutputStream os = connection.openOutputStream();
   os.write(requestData, 0, requestData.length);
   os.flush();
   os.close();
    requestData = null;
   
   InputStream is;
   try {
    is = connection.openInputStream();
   } catch (IOException e) {
    is = connection.getErrorStream();
    if (is == null) {
    connection.disconnect();
    throw (e);
    }
   }
   //if (debug) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[256];
    while (true) {
    int rd = is.read(buf, 0, 256);
    if (rd == -1)
     break;
    bos.write(buf, 0, rd);
    }
    bos.flush();
    buf = bos.toByteArray();
    responseDump = new String(buf);
   
    is.close();
    is = new ByteArrayInputStream(buf);
    if (debug) {
     System.out.println(”DBG:request:” + requestDump);
     System.out.println(”DBG:response:” + responseDump);
    }
   //}  
   parseResponse(envelope, is);
   System.out.println(”—————-><"+envelope+"><<<");
   System.out.println(”|—————->["+envelope.bodyIn +"]<<<");
   System.out.println(”DBG:request:” + requestDump);
   System.out.println(”DBG:response:” + responseDump);
  
 } finally {
   connection.disconnect();
  }
 }

 protected ServiceConnection getServiceConnection() throws IOException {
  return new AndroidServiceConnection(url);
 }
}

Those are the two main classes that are vital for web service communication. This last code here will be your client Android app that will connect to the web service.


private static final String SOAP_ACTION = “method”;
private static final String METHOD_NAME = “method
private static final String NAMESPACE = “namespace”;
private static final String URL = “http://url/pathto/service/namespace”;

public static String mymethod() {
 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
 request.addProperty(”whatever_name”, “whatever_value”);

 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
 envelope.setOutputSoapObject(request);
 AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

 try {
  androidHttpTransport.call(SOAP_ACTION, envelope);
 } catch (Exception e) {
  e.printStackTrace();
 }

}

The Android app code is pretty self explanatory. Just fill in the your values to the pre-defined variables. The request.addProperty() method is where you place your parameters in case the webservice method you want to call needs parameters supplied to it. Requests will still be passed as XML. I hope this can help those who would want their Android apps to call webservices. I pretty had was stuck with this for probably a week browsing through the web, mailing lists or anything that I could come upon that would give me a hint as to how to make this work. These codes were from someone in a mailing list who posted a solution. But I modified parts of it because they didn’t work in my case. Hit the run in the RUN menu of Eclipse to deploy your Android app. If you wish to see the System.out.println() messages in the console, run logcat. You won’t see those messages in the Eclipse console.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(1 votes, average: 5.00 out of 5)
 Loading ...