Make a button invisible or gone in an Android Application

In this post I will show you how to make button visible and invisible or gone. First let’s create three¬† buttons in our application and make them visible.
This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:text="Button One"
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <Button
        android:text="Button Two"
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <Button
        android:text="Button Three"
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
</LinearLayout>

Setting buttons to be visible:

package com.lardev.myButton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button myButton1 = (Button) findViewById(R.id.Button01);
        myButton1.setVisibility(0);
        Button myButton2 = (Button) findViewById(R.id.Button02);
        myButton2.setVisibility(0);
        Button myButton3 = (Button) findViewById(R.id.Button03);
        myButton3.setVisibility(0);
    }
}

And the result is:

Now go to line 17 and change parameter from 0 to 4 and second button will be invisible when you save and start your application again, but it still takes up space in layout.

If we want to make button invisible and doesn’t take any space in the layout, then we must change parameter 8 for setVisibility method.

package com.lardev.myButton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button myButton1 = (Button) findViewById(R.id.Button01);
        myButton1.setVisibility(0);
        Button myButton2 = (Button) findViewById(R.id.Button02);
        myButton2.setVisibility(8);
        Button myButton3 = (Button) findViewById(R.id.Button03);
        myButton3.setVisibility(0);
    }
}

And now button two is invisible and gone.Here you can see the result:

And that’s all for now, good luck

Posted in Android at February 28th, 2011. 1 Comment.

Escape ampersand in XML item array

If you want to use an ampersand as a string in item for a string-array you must escape it. It is very easy, just replace ampersand sign ( ‘&‘ ) with &amp; and save the xml layout file. Good luck.

Posted in Android at February 28th, 2011. No Comments.

How to move Android tabs from top to bottom

Today I will show how to move tabs from top to bottom of the application. This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
        />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:layout_alignParentBottom="true"
        />
    </RelativeLayout>
</TabHost>

And this is the Tabs class:

package com.lardev.tabs;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class Tabs extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Search.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("search").setIndicator("Search",
                          res.getDrawable(R.drawable.ic_tab_search))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Points tabs
        intent = new Intent().setClass(this, Points.class);
        spec = tabHost.newTabSpec("points").setIndicator("Points",
                          res.getDrawable(R.drawable.ic_tab_search))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Social tabs
        intent = new Intent().setClass(this, Social.class);
        spec = tabHost.newTabSpec("social").setIndicator("Social",
                          res.getDrawable(R.drawable.ic_tab_search))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Contact tabs
        intent = new Intent().setClass(this, Contact.class);
        spec = tabHost.newTabSpec("contact").setIndicator("Contact",
                          res.getDrawable(R.drawable.ic_tab_search))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}

And the result is:

Android tabs from top to bottom

More info about Tab Layout you can read from here: Tab layout read

Posted in Android at February 2nd, 2011. 2 Comments.

How to make screenshot in Android Emulator using Eclipse

There is a very easy way to make screenshot on Android Emulator using Eclipse.

  1. Click on DDMS perspective
  2. Look for Devices View, if is not visible, on the menu bar click Window > Show View > Other. In the newly opened dialog box, under Android category, select Devices.
  3. Now on the Devices View, click the button /Screen Capture/ as shown on the image and your screenshot will be ready.

Screenshot of Android Emulator

Posted in Android, Eclipse at February 1st, 2011. No Comments.