May 20, 2011

Hide and unhide a view

Hide and unhide a view

package com.AndroidHideView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidHideView extends Activity {

TextView targetText;
Spinner spVisibility;

String[] optVisibility = { "VISIBLE",
"INVISIBLE",
"GONE"};

int[] valueVisibility = { View.VISIBLE,
View.INVISIBLE,
View.GONE};

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

targetText = (TextView)findViewById(R.id.targetview);
spVisibility = (Spinner)findViewById(R.id.visibility);

ArrayAdapter<String> adapterVisibility = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, optVisibility);
adapterVisibility.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spVisibility.setAdapter(adapterVisibility);
spVisibility.setOnItemSelectedListener(spVisibilityOnItemSelectedListener);
}

private Spinner.OnItemSelectedListener spVisibilityOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

int visibility = valueVisibility[spVisibility.getSelectedItemPosition()];
targetText.setVisibility(visibility);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}};

}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/visibility"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="======================================="
/>
<TextView
android:id="@+id/targetview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This TextView can be hided and unhided."
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="======================================="
/>
</LinearLayout>


2 comments:

  1. Hi
    Do you also know how to code when a button get pressed, a spinner will be "gone" on another screen.

    grtz,
    Jack

    ReplyDelete

Infolinks In Text Ads