KG -
KG -

Reputation: 7170

How does one insert some text like version number when using Android preference-headers

I thought my requirement was simple... now i'm tearing my hair bald.

I'm creating a settings page for my app and using the recommended preferences-headers approach.

All I need to do, is add the version number of my app at the bottom, such that, it is not clickable or is at least gray in color.

It doesn't necessarily have to even be a footer. It can be the last preference in my list, but i need a way to indicate that it is not a button (clickable) and/or is disabled (gray in color).

I've spent like 8 hours and this is not easy to do :(

Here's the code that I use to add my version number:

public class SettingsActivity extends PreferenceActivity {

  ... 

  @Override
  public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.settings_headers, target);
    addAppVersionNumberHeader(target);
  }

  ...

  private void addAppVersionNumberHeader(List<Header> target) {
    Header versionNumberHeader = new Header();
    StringBuilder versionInfo = new StringBuilder(getMyAppVersionNumberFromSomewhere());

    versionNumberHeader.title = versionInfo.toString();

    target.add(versionNumberHeader);
  }

  ... 

}

Since I'm programmatically adding it, my preference headers file is plain old simple xml and doesn't do anything related to my version number:

<!-- This file is res/xml/settings_headers.xml -->
<?xml version="1.0" encoding="utf-8"?>

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

  <header
    android:fragment="com.example.com.fragments.SettingsNotificationFragment"
    android:title="@string/settings_notifications">
  </header>

  <!-- bunch of other similar headers -->

</preference-headers>

What are my options?

FWIW: Here's what I've tried so far:

create an independent layout xml

Problem: I get the version number to show with this approach, but if i click the version number, I get an index out of bounds exception, which I'm guessing is because I'm adding a Preference, when actually I should be adding a Header.

Upvotes: 1

Views: 500

Answers (1)

ozbek
ozbek

Reputation: 21183

It doesn't necessarily have to even be a footer

But it can be, right? Try the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView tv = new TextView(this);
    tv.setText(getVersionNumber());
    setListFooter(tv);
}

private String getVersionNumber() {
    String versionNumber = "v3.12";
    // lots of code that gets the actual version number
    return "Version (latest and greatest): ".concat(versionNumber);
}

On your particular error of "index out of bounds exception", I guess, you need to override setListAdapter() method and add new headers there.

Upvotes: 3

Related Questions