Lesson 73. Preferences. Manage settings activity (setEnabled)

Lesson 73. Preferences. Manage settings activity (setEnabled)


In this lesson:

– manage settings activity (setEnabled)

Sometimes you can see in the settings that some of them are inactive – you can interact with them. And it depends on the related settings. That is, one peg was turned off – another was inactive. Let’s see how this is done.

Let’s create a project:

Project name: P0731_PreferencesEnable
Build Target: Android 2.3.3
Application name: PreferencesEnable
Package name: en.startandroid.develop.p0731preferencesenable
Create Activity: MainActivity

let’s fill it strings.xml:



    Hello World, MainActivity!
    PreferencesEnable
    
        one
        two
        three
    
    
        1
        2
        3
    

These are two arrays for a ListPreference list.

Let’s create an xml settings file, res / xml / pref.xml:



    
    
    
    
    
    
    
        
        
        
            
            
        
        
            
            
            
            
        
    

We are interested in the dependency attribute. It can specify the key of any CheckBoxPreference – and on / off checkbox will be active / inactive setting. That is, for example, for ListPreference c key = list specified dependency = chb1. And now putting / removing chb1 activity will change list. This mechanism works with the default settings and with PreferenceScreen, but with PreferenceCategory – no. The categories will have the same code.

Note the summaryOn and summaryOff attributes of chb1. These are custom configuration texts, similar to a summary. They are displayed depending on whether the checkOn box is checked or off (summaryOff).

Create a settings window, PrefActivity.java:

package ru.startandroid.develop.p0731preferencesenable;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;

public class PrefActivity extends PreferenceActivity {
  
  CheckBoxPreference chb3;
  PreferenceCategory categ2;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref);

    chb3 = (CheckBoxPreference) findPreference("chb3");
    categ2  = (PreferenceCategory) findPreference("categ2");
    categ2.setEnabled(chb3.isChecked());
    
    chb3.setOnPreferenceClickListener(new OnPreferenceClickListener() {
      public boolean onPreferenceClick(Preference preference) {
        categ2.setEnabled(chb3.isChecked());
        return false;
      }
    });
  }
}

Using the findPreference method, we find a checkbox (chb3) And category (categ2). And with the setEnabled method, we set the category activity to be equal to the checkbox value (on / off). This is to make sure everything is correct when the screen starts.

Next, we register the handler for the checkbox, and after clicking it, we establish a connection – the activity of the category is equal to the value of the checkbox.

IN MainActivity.java just create a menu item to go to the settings:

package ru.startandroid.develop.p0731preferencesenable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
  
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuItem mi = menu.add(0, 1, 0, "Preferences");
      mi.setIntent(new Intent(this, PrefActivity.class));
      return super.onCreateOptionsMenu(menu);
    }
}

We all save and launch the application. We go to the settings.

CheckBox 1 off, respectively inactive and List. also CheckBox 2 off, inactive Screen. We spelled it out in attributes dependency in pref.xml.

we will include CheckBox 1:

List is now active. And note that the description has changed CheckBox 1. While the checkbox was off, text from the attribute was displayed summaryOffAnd now it’s on and we see the text from summaryOn.

Let’s be inclusive CheckBox2.

Screen is now active.

We press on it and we fall inside.

Let me remind you in the code we wrote that Category 2 depended on CheckBox 3. And indeed – the checkbox is off and the category is not active.

let’s turn on the checkbox

The category has become active. Everything is O.K.

In this tutorial, we looked a bit at how to use the settings screen through the code. I think it makes sense to consider this in more detail.

In the next lesson:

– create the settings screen programmatically




Discuss in the forum [10 replies]

Leave a Comment