saving toggle button and textView state - android

I'm new on android. I want to change the color of a text when a toggle button is checked and then save the state of both toggle button and text color even when the app is killed. can somebody give me some tips of how to do that. thanks.

here is simple example of toggle button and text view with Preferences(for save state).
create layout file below way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton" />
</LinearLayout>
in your activity
public class MainActivity extends AppCompatActivity {
TextView textItem;
SharedPreferences sPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
// init Preferences
sPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
// init view
textItem = (TextView) findViewById(R.id.textView);
ToggleButton syncSwitch = (ToggleButton) findViewById(R.id.toggleButton1);
// toggle button event
syncSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set text color method
setTextColor(isChecked);
// save Toggle button state in Preference
SharedPreferences.Editor ed = sPref.edit();
ed.putBoolean("ToggleButton_CHECK", isChecked);
ed.commit();
}
});
// set default color if TextView called when activity started only for first time
boolean saveState = sPref.getBoolean("ToggleButton_CHECK", false);
setTextColor(saveState);
syncSwitch.setChecked(saveState);
}
public void setTextColor(boolean isChecked) {
if (isChecked) {
// button is on
textItem.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.agro_purple));
} else {
// button is off
textItem.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
}
}
}
Hope it help!

Related

Button visible and invisible

I am new in android app development.
I am searching an example like bellow:
There is a checkbox and two buttons named btn1 and btn2.
Logic should be when user will checked on the checkbox then btn2 will visible and btn1 will invisible.
In my XML file:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/input_chk_provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:duplicateParentState="false"
android:text="#string/user_type_provider" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_sing_up"
android:background="#color/ic_launcher_background"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/btn_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_provider_dtl"
android:background="#color/ic_launcher_background"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
Without having the code...
You'll need a OnCheckedChangeListener for the Checkbox so you can adjust the button visibility when it's checked/unchecked.
Something like:
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.VISIBLE);
} else {
btn1.setVisibility(View.VISIBLE);
btn2.setVisibility(View.GONE);
}
});
In your XML use android:visibility="invisible" to set the visibility of the buttons.
In the code, get your views with findViewById()
CheckBox checkBox = (CheckBox) findViewById(R.id.check1);
Button yourButton = (Button) findViewById(R.id.btn1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if(isChecked){
yourButton.setVisibility(View.VISIBLE);
} else {
yourButton.setVisibility(View.INVISIBLE);
}
}
});
Use the method .setVisibility(View.VISIBLE) to change the visibility.
There are 3 states:
View.GONE
View.VISIBLE
View.INVISIBLE
Checkbox has OnCheckedChangeListener() method which will give you the state of the checkbox. Based on the state of the Checkbox you can use Button1.setVisibility(true/false); to show/hide the button.
try this..
<CheckBox
android:id="#+id/input_chk_provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:state_checked="true"
android:checked="false"
android:onClick="itemClicked"
android:duplicateParentState="false" />
public void itemClicked(View v) {
//code to check if this checkbox is checked!
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()==true){
bt1.setVisibility(View.VISIBLE);
bt2.setVisibility(View.INVISIBLE);
}else if(checkBox.isChecked()==false){
bt2.setVisibility(View.VISIBLE);
bt1.setVisibility(View.INVISIBLE);
}
}

Android: Change background colour of specific activity using SharedPreferences

I am trying to change the background colour of a activity where the user can change the colour using 3 radio buttons in an android app. I am doing this using sharedPreferences.
So I have the activity page where the colour is chosen with the radio group and the code to use sharedPrefernces looks like this (i know the switch statement works aswell as I have a toast message apperring when the colour is supposed to change):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String colourSelected = "";
switch (checkedId) {
case R.id.radioButton1 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton2 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton3 :
colourSelected = "BLUE";
editor.putString("colour", colourSelected);
editor.commit();
break;
}
}
});
}
The XML looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.practical3_10327751_donnacha_holmes.Preferences" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radiogroup"
>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
Then there is the activity that changes the background colour, which looks like this when trying to change the colour:
public class Activity2 extends ActionBarActivity {
RelativeLayout rl;
String colour;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
colour = prefs.getString("Colour", "WHITE");
rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
if(colour=="GREEN"){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour=="YELLOW"){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour=="BLUE"){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
I know that the background colour is being changed because it is being set to red every time I go to this page.
Thanks for any help!
The == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.
Therefore to compare Strings use .equals(),i.e. rewrite your comparison as
if(colour.equals("GREEN")){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour.equals("YELLOW")){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour.equals("BLUE")){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
and you are saving value as colour and try to retrieving as Colour,so change
colour = prefs.getString("Colour", "WHITE");
to
colour = prefs.getString("colour", "WHITE");

Shared Preferences not updating Key values inside a Dialog

I have a class in which I have an option button that opens a dialog containing 6 checkboxes. By default, 4 checkboxes are set to true when the app is first loaded. The user can check and uncheck these checkboxes. When I press the backbutton, the dialog disappears and if again click the "option" button the checkboxes are not checked/unchecked as per the last state selected. I have used shared preferences to hold these values.
My Java class code is here. I have removed the unnecessary methods below and also posted the xml layout. Please help as to why the shared preferences is not working.
GlamDokuActivity
public class GlamDokuActivity extends Activity{
private Button easy,fair,hard,evil,insane;
private CheckBox screenOn,clashSquares,quickNotes,soundOn,showTimer,matchingNumbers;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
public static final String DefaultChoicesOn = "defaults";
public static final String Timer ="true";
public static final String QuickNotes ="true";
public static final String ScreenOn ="false";
public static final String ClashingSquares ="true";
public static final String SoundOn ="false";
public static final String MatchingNumbers ="true";
//keeping a tab on the status of the above default options
private boolean timerStatus=false,quickNotesStatus=false,screenOnOffStatus=false,clashingSquaresStatus=false,soundOnOffStatus=false,matchingNumberStatus=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glamdoku_main);
//call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
sharedpreferences=getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
//saving in the shared preferences
editor = sharedpreferences.edit();
//If the key is not present then only add the key-value pair
if(!sharedpreferences.contains(Timer))editor.putBoolean(GlamDokuActivity.Timer, true);
if(!sharedpreferences.contains(ClashingSquares))editor.putBoolean(GlamDokuActivity.ClashingSquares, true);
if(!sharedpreferences.contains(MatchingNumbers))editor.putBoolean(GlamDokuActivity.MatchingNumbers, true);
if(!sharedpreferences.contains(SoundOn))editor.putBoolean(GlamDokuActivity.SoundOn, false);
if(!sharedpreferences.contains(ScreenOn))editor.putBoolean(GlamDokuActivity.ScreenOn, false);
if(!sharedpreferences.contains(QuickNotes))editor.putBoolean(GlamDokuActivity.QuickNotes, true);
editor.commit();
//KEEP THE SCREEN ON
if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//System.out.println("I AM IN ONCREATE OF ");
}//onCreate() ends
public void optionsMethod(View v){//options button click method
//dialog that appears when the hints button is clicked
final Dialog dialog = new Dialog(GlamDokuActivity.this,R.style.dialogStyle);
dialog.setContentView(R.layout.layout_options);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
dialog.show();
//tagging the ids
screenOn = (CheckBox)dialog.findViewById(R.id.screenOn);
clashSquares = (CheckBox)dialog.findViewById(R.id.clash);
quickNotes = (CheckBox)dialog.findViewById(R.id.quickNotes);
soundOn = (CheckBox)dialog.findViewById(R.id.soundOnOff);
showTimer = (CheckBox)dialog.findViewById(R.id.timer);
matchingNumbers = (CheckBox)dialog.findViewById(R.id.matchNumbers);
//default values from shared preferences --- depending upon these the state of the respective checkboxes to change
timerStatus=sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
quickNotesStatus=sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
screenOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, false);
clashingSquaresStatus=sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
soundOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, false);
matchingNumberStatus=sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);
//check or uncheck depending upon the aforementioned boolean values
if(timerStatus==false)showTimer.setChecked(false); else showTimer.setChecked(true);
if(quickNotesStatus==false)quickNotes.setChecked(false);else quickNotes.setChecked(true);
if(screenOnOffStatus==false)screenOn.setChecked(false);else screenOn.setChecked(true);
if(clashingSquaresStatus==false)clashSquares.setChecked(false);else clashSquares.setChecked(true);
if(soundOnOffStatus==false)soundOn.setChecked(false);else soundOn.setChecked(true);
if(matchingNumberStatus==false)matchingNumbers.setChecked(false);else matchingNumbers.setChecked(true);
//-----------------------------------------------------------------------------------------------------------------
//click the screenOn chkbox
screenOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,1));
//click the clashing squares chkbox
clashSquares.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,2));
//click the quick notes chkbox
quickNotes.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,3));
//click the sound chkbox
soundOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,4));
//click the show timer chkbox
showTimer.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,5));
//click the matching numbers chkbox
matchingNumbers.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,6));
#Override
public void onResume(){
//KEEP THE SCREEN ON
if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onResume();
}
}
CommonCheckClickListener
public class CommonCheckClickListener implements OnClickListener {
private Dialog dialog;
private SherlockActivity activity;
private int thisOption;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
public CommonCheckClickListener(Dialog dialog, Activity activity, int thisOption){
this.activity=activity;
this.dialog=dialog;
this.thisOption=thisOption;
//call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
sharedpreferences=activity.getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
editor=sharedpreferences.edit();
}
#Override
public void onClick(View v) {
int flag=0;
if(((CheckBox)v).isChecked())flag=1; else flag=0;//flagged as checked
switch(thisOption){
case 1: //screenOn option
if(flag==1){
editor.putBoolean(GlamDokuActivity.ScreenOn, true);
Toast.makeText(activity, "Warning - Keeping the screen always on consumes battery.", Toast.LENGTH_LONG).show();
}
else{
editor.putBoolean(GlamDokuActivity.ScreenOn, false);
}
editor.commit();
boolean screenOn = sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, true);
break;
case 2: //clashing squares option
if(flag==1)editor.putBoolean(GlamDokuActivity.ClashingSquares, true);else editor.putBoolean(GlamDokuActivity.ClashingSquares, false);
editor.commit();
boolean cs = sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
break;
case 3: //quick notes option
if(flag==1)editor.putBoolean(GlamDokuActivity.QuickNotes, true); else editor.putBoolean(GlamDokuActivity.QuickNotes, false);
editor.commit();
boolean qn = sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
break;
case 4: //sound option
if(flag==1)editor.putBoolean(GlamDokuActivity.SoundOn, true); else editor.putBoolean(GlamDokuActivity.SoundOn, false);
editor.commit();
boolean sound = sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, true);
break;
case 5: //show Timer option
if(flag==1)editor.putBoolean(GlamDokuActivity.Timer, true); else editor.putBoolean(GlamDokuActivity.Timer, false);
editor.commit();
boolean timer = sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
break;
case 6: //matching numbers option
if(flag==1)editor.putBoolean(GlamDokuActivity.MatchingNumbers, true); else editor.putBoolean(GlamDokuActivity.MatchingNumbers, false);
editor.commit();
boolean matching = sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);
break;
}
}
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<Button
style="#style/button_main_screen"
android:id="#+id/playsudoku"
android:onClick="playMethod"
android:text="#string/playsudoku" />
<Button
style="#style/button_main_screen"
android:id="#+id/options"
android:onClick="optionsMethod"
android:text="#string/optionssudoku" />
</LinearLayout>
Dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical" >
<CheckBox
android:id="#+id/matchNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="21dp"
android:text="#string/match_numbers" />
<CheckBox
android:id="#+id/clash"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/clash_squares" />
<CheckBox
android:checked="true"
android:id="#+id/quickNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quick_notes" />
<CheckBox
android:id="#+id/timer"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/timer_always" />
<CheckBox
android:id="#+id/screenOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="#string/screen_always" />
<CheckBox
android:id="#+id/soundOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="25dp"
android:text="#string/sound_off" />
</LinearLayout>
The definition of the static key-variables is incorrect. They need to be other names rather than "true" and "false". The problem is solved once you change the initialization values. – Ganesh Hegde
You should use OnCheckedChangeListener instead of OnClickListener for your CheckBoxes. OnClickListener#onClick calls earlier then CheckBox's checked state changed.

How to change input type Number to Text of EditText in android

I have a requirement in my project,that by default the input type of the Edittext is Number.How we can give an option to the User to change the input type Number to Text/Alphabets.
Thanks
Tiru
You could use a toggle button to switch between number to text and vice versa.
in your layout xml:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="InputType"
android:textOff="Text"
android:textOn="Number" />
Activity class:
EditText ed;
ToggleButton edtb;
//flag : used in the added code
static boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText) findViewById(R.id.editText1);
edtb = (ToggleButton) findViewById(R.id.toggleButton1);
edtb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
ed.setInputType(InputType.TYPE_CLASS_TEXT);
}
else
{
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
}
}
});
/* **Edited** Adding a way to achieve the same using EditText only. */
ed.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(flag)
{
ed.setInputType(InputType.TYPE_CLASS_TEXT);
flag=false;
}
else
{
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
flag=true;
}
}
});
}
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number|text"
/>
ToggleButton can help you. Create listener and asociate number type with one state and text with other, when user click the button, the input text will change.
First put in xml :
android:inputType="number"
change the input type Number to Text/Alphabets dynamically like.
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
if you want only number in EditText
you can use - android:inputType="number"
android:digits="0,1,2,3,4,5,6,7,8,9"
also use android:numeric.
You can use android:inputType=text and write your own validation code to accept number/single decimal after number etc.

make a frame with components visible only when needed in android

Well what i wanted to do was create an initial layout which will have a toggle button and on clicking the toggle but it should make a frame visible which will have a few buttons or textviews.
Does anybody know how to do this in Android 2.2??
You can use the visibility attribute on a view to control whether it is visible or not. Here's a small example that should do what you're looking for.
The main Activity:
public class DynamicLayoutTestActivity extends Activity {
private ToggleButton toggleButton;
private View possiblyHiddenView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
possiblyHiddenView = (View) findViewById(R.id.possiblyHiddenView);
toggleButton.setOnCheckedChangeListener(toggleButtonOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener toggleButtonOnCheckedChangeListener
= new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
possiblyHiddenView.setVisibility(View.VISIBLE);
} else {
possiblyHiddenView.setVisibility(View.INVISIBLE);
}
}
};
}
The layout file, main.xml:
<?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"
>
<ToggleButton
android:id="#+id/toggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Show"
android:textOn="Hide" />
<LinearLayout
android:id="#+id/possiblyHiddenView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
>
<TextView
android:text="Stuff that could be hidden."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
If you don't want the hidden view to take up any space, use visibility gone instead of invisible.
Hope this helps!
If you're developing for Android 3.0+, look into fragments.

Categories

Resources