Search Tutorials

Loading...

Sunday, 29 September 2013

Android Table Layout visible and invisible colomn Example

Here, I have given android example of table layout in which we can hide, show or stretch any column. Open your main xml file and paste below code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1,2">

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B1"
                android:onClick="gayab"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B2"
                />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B3" />

        </TableRow>
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <Button
                android:id="@+id/button5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B1"
                />

            <Button
                android:id="@+id/button6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B2"
                />

            <Button
                android:id="@+id/button7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B3" />

        </TableRow>


    </TableLayout>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gayab"
        android:onClick="gayab" />

</LinearLayout>

Now open your java file and paste below code

package selcom.testlay;//your package name

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;

public class MainActivity extends Activity {
      TableLayout ll;
      Button b4;
     

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll=(TableLayout)findViewById(R.id.tl);
        b4=(Button)findViewById(R.id.button4);
      
    }
    public void gayab(View v)
    {
//visible and invisible collumn
      ll.setColumnCollapsed(0, !ll.isColumnCollapsed(0));
      if(ll.isColumnCollapsed(0))
      {
            b4.setText("show");
      }
      else
      {
            b4.setText("Hide");
      }
     
    }   
}


Now run this android table layout example on your device and test.

1 comment:

  1. I want to share table layout throw sms so how to get table layout and share it ?
    thanks

    ReplyDelete

Back to Top