Saturday, September 8, 2012

Layout Shrink In Android

This example shows how you can perform shrink to fit action on a table layout.
Algorithm:
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="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
    <LinearLayout

       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
         <TableLayout
           android:id="@+id/menu"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content">
            <TableRow>
                <TextView
                   android:text="Click on the shrink button to see how the layout text will get shrink accordingly"
                   android:padding="3dip" />
            </TableRow>
        </TableLayout>
        <Button
           android:id="@+id/toggle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Shrink" />
    </LinearLayout>
</ScrollView>
3.) Run for output.
Steps:
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 LayoutShrinkActivity.java file and write following code there:
package com.example.LayoutShrink;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
public class LayoutShrinkActivity extends Activity {
    private boolean mShrink;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TableLayout table = (TableLayout) findViewById(R.id.menu);
        Button button = (Button) findViewById(R.id.toggle);
        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.
Output

No comments:

Post a Comment