Saturday, September 8, 2012

Layout Shrink In Android

This example shows how you can perform shrink to fit action on a table layout.
1.) Create a new project by File-> New -> Android Project name it LayoutShrinkExample.
2.) Write following into main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android=""

                   android:text="Click on the shrink button to see how the layout text will get shrink accordingly"
                   android:padding="3dip" />
           android:text="Shrink" />
3.) Run for output.
1.) Create a project named LayoutShrinkExample and set the information as stated in the image.
Build Target: Android 2.1
Application Name: LayoutShrinkExample
Package Name: com.example.LayoutShrink
Activity Name: LayoutShrinkActivity
Min SDK Version: 7
2.) Open file and write following code there:
package com.example.LayoutShrink;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
public class LayoutShrinkActivity extends Activity {
    private boolean mShrink;
    protected void onCreate(Bundle savedInstanceState) {
        final TableLayout table = (TableLayout) findViewById(;
        Button button = (Button) findViewById(;
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mShrink = !mShrink;
                table.setColumnShrinkable(0, mShrink);
        mShrink = table.isColumnShrinkable(0);
3.) Compile and build the project.

No comments:

Post a Comment