Android: Nested XMLs not possible

In Android m5 rc15, nested XML is not possible. Google should have included this feature because it is very useful. You don’t have to clutter your layouts inside 1 XML file. As with other web programming languages where you can do includes, Android doesn’t have it. So far, there has been no workaround made. I came upon one person who gave a solution but somehow it didn’t work. I always have this “layout_width must be set” problem. You can however load XML layouts at runtime using the ViewInflate class. But you need to have layout tags in your XML files and retrieve them via your code. For a quick sample, I had a separate article regarding this.

Click here.

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


(No Ratings Yet)
 Loading ...

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 ...