Search Tutorials

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.

2 comments:

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

    ReplyDelete
  2. You must know about server connection and then give permission handler in android manifest and make an Service Handler use post method to send value and then use server script to receive data, which is in localhost wamp/xamp. I think it is very difficult for beginers but you can try these steps and get code from any source like stackoverflow or androidhive.

    ReplyDelete

Back to Top