Kamilski81
Kamilski81

Reputation: 15107

How can I "preview layout" of an xml file on Android?

I am creating a bunch of layout files and would like to preview roughly what they will look like on the Android. Is there a way to do this? and if so, what would be the best way?

Right now, I have to boot up the device and navigate to the Activity every time in order to see the layout. Also, it looks like the preview of Eclipse is not ideal as it doesn't display things correctly (is this the best it gets?)

Upvotes: 3

Views: 3134

Answers (5)

wendigo
wendigo

Reputation: 1993

You can create an Activity like this:

//add the imports

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Ask for a full screen window
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onStart()
     */
    @Override
    protected void onStart() {

        // TODO Auto-generated method stub
        super.onStart();
        // Get the info about the screen

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Change the activity_main layout and you can test any layout clicking in Graphical Layout of your xml.

If you don't trust in Eclipse-preview and you want to avoid the slow Android emulator, try to enable the Virtualization (Intel processors). You must install the packages Intel x86 Emulator Accelerator (HAXM) and Intel x86 System-Image. Next, you can create an AVD using that System-Image and the emulator will speed up.

Otherwise, if you haven't got a capable Virtualization processor, I'm sorry for you, but you must trust in Eclipse preview ;)

Bye!

Upvotes: 1

Ewoks
Ewoks

Reputation: 12435

Add as parameter of ListView in xml file following line:

tools:listitem="@layout/my_custom_list_item"

where my_custom_list_item is layout you want to use as list item.

Upvotes: 0

rizalp1
rizalp1

Reputation: 6524

You can use the utility at http://www.droiddraw.org/.

Simply copy and paste your layout xml at the Output window, and click "Load". This works both ways.

Upvotes: 4

jbowes
jbowes

Reputation: 4150

Besides running on a real device or in the emulator, Eclipse's preview is the best it gets. Make sure you set the right options for screen size and API version in the preview screen; that sometimes helps.

Upvotes: 1

noni
noni

Reputation: 2947

You can create a Dummy Activity, and set the intent MAIN to it.

When you want to test how that activity looks like, just setLayout on onCreate to the xml that you want to test and you will see it.

I also don't trust in Eclipse preview, and AVDs are really slow, so I attach an android device and use a dummy activity to test layouts

Upvotes: 4

Related Questions