Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7704753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:45:06+00:00 2026-05-31T23:45:06+00:00

I am developing a PreferenceActivity with custom Preference views. My problem is that I

  • 0

I am developing a PreferenceActivity with custom Preference views. My problem is that I created a view with a ListView and it only shows the first element. I post my code and an image:
http://imageshack.us/photo/my-images/545/sc20120307161530.png/

http://img545.imageshack.us/img545/7207/sc20120307161530.png’ border=’0’/>

xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:key="player_settings"
        android:title="@string/settings_player_config" >
        <EditTextPreference
            android:defaultValue="@string/settings_player_default_name"
            android:dialogMessage="@string/settings_player_summary"
            android:dialogTitle="@string/settings_playersname"
            android:key="player_name"
            android:summary="@string/settings_player_summary"
            android:title="@string/settings_playersname" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="volume"
        android:title="@string/settings_volume" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="volume"
            android:title="@string/settings_volume" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="shine"
        android:title="@string/settings_shine" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="shine"
            android:title="@string/settings_shine" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="themeTitle"
        android:title="@string/settings_group_themes" >
        <com.battleship.preferences.ListPreferences android:key="theme" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="fontsTitle"
        android:title="@string/settings_group_font_size" >
        <com.battleship.preferences.ListPreferences android:key="font" />
    </PreferenceCategory>

</PreferenceScreen>

The Custom ListPreference:

package com.battleship.preferences;

import com.battleship.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class ListPreferences extends Preference implements
        OnCheckedChangeListener {

    public ListPreferences(Context context) {
        super(context);
    }

    public ListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListPreferences(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onClick() {

        super.onClick();
        Toast t = Toast.makeText(getContext(), "HOLA!", 3);
        t.show();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {



        String[] contentString = new String[3];
        if (getKey().equals("theme")) {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_theme_default)),
                    (getContext().getString(R.string.settings_theme_black)),
                    (getContext().getString(R.string.settings_theme_white)) };
        } else {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_font_big)),
                    (getContext().getString(R.string.settings_font_medium)),
                    (getContext().getString(R.string.settings_font_little)) };
        }

        ListView listView = new ListView(getContext());
        ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, contentString);
        listView.setAdapter(array);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


        return listView;
    }

    private void updatePreference(int intRadio) {
        SharedPreferences.Editor editor = getEditor();
        editor.putInt(getKey(), intRadio);
        editor.commit();
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:45:07+00:00Added an answer on May 31, 2026 at 11:45 pm

    Don’t you hate it when everyone tries to tell you that you shouldn’t do it that way instead of just answering your question? Being able to dynamically generate content is crucial to app flexibility, I totally know what you’re after.

    This will work assuming all the ListView entries will be roughly the same height (non-pertinent parts omitted). The order is important, try to keep it as I’ve indicated:

    import android.view.ViewGroup.OnHierarchyChangeListener;
    
    public class ListPreferences extends Preference implements
        OnCheckedChangeListener, OnHierarchyChangeListener {
    
    private ListView listView;
    private View thisView;
    private int listHeight = 0;
    
    @Override
    protected View onCreateView(ViewGroup parent) {
        this.setLayoutResource(R.layout.listview_preference_layout);
        thisView = super.onCreateView(parent);
        listView = (ListView) thisView.findViewById(android.R.id.list);
        listView.setOnHierarchyChangeListener(this);
        String[] contentString = new String[3];
        if (getKey().equals("theme")) {
            contentString = new String[] {
            (getContext().getString(R.string.settings_theme_default)),
            (getContext().getString(R.string.settings_theme_black)),
            (getContext().getString(R.string.settings_theme_white)) };
        } else {
            contentString = new String[] {
            (getContext().getString(R.string.settings_font_big)),
            (getContext().getString(R.string.settings_font_medium)),
            (getContext().getString(R.string.settings_font_little)) };
        }
    
        ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1, contentString);
        listView.setAdapter(array);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        return thisView;
    }
    
    public void onChildViewAdded(View parent, View child) {
        int childHeight = child.getMeasuredHeight();
        if(childHeight > 0)
        {
            listHeight = listView.getAdapter().getCount() * childHeight;
            thisView.setMinimumHeight(listHeight);
            Log.i(TAG,"onChildViewAdded, done: "+listHeight+" "+childHeight);
        }
    }
    
    public void onChildViewRemoved(View parent, View child) {
    }
    
    
    }
    

    You’ll also need res/layout/listview_preference_layout.xml with the following:

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="fill_vertical|fill_horizontal|fill"
        android:gravity="fill"
        android:orientation="vertical"
       >
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:clipChildren="true" 
            android:isScrollContainer="true"
            android:layout_gravity="fill_vertical|fill_horizontal|fill"
            android:layout_weight="1"
            android:choiceMode="singleChoice"/>
    </LinearLayout
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Developing with my first Mac and I noticed that my Rspec output isn't colorized
I´m developing an ASP.NET MVC app that uses EF 4.1 Code First. I have
Developing a little survey webapp, ran into problem that deals with ranges for rating
Iam developing one applciation.In that i added the one tableview to present view.I added
I´m developing a software in C# that uses static functions from a C++ .dll
I'm developing Android 2.2 application and I'm going to use Preference.OnPreferenceChangeListener interface. I've added
Developing rails has become lots of fun over the year that I've done it,
Developing for iPhone, I have a collection of points that I need to make
Developing a desktop application based on Java + Swing I faced the problem of
Developing with Google Maps v3. For some sort of reason, my custom marker icon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.