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

  • SEARCH
  • Home
  • 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 4045414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:25:28+00:00 2026-05-20T13:25:28+00:00

I’m trying to get an Android Alphabet positioning bar to work following an old

  • 0

I’m trying to get an Android Alphabet positioning bar to work following an old example provided by Josh Guilfoyle:

AlphabetBarListView <- original project and screen capture can be found here

I was able to debug the code to get the alphabet bar itself to display using

setContentView(new AlphabetBar(this));

but I can’t figure out how to display the alphabet bar over a listview under Android 2.1 as shown in the photo. I’ve created a really small project to try and get it to work, but I’m obviously doing something wrong. Thanks guys! I really appreciate the help.

HelloWorld.java

package com.demo.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */

    public AlphabetBar mAlphabetBar;
    public ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setContentView(new AlphabetBar(this));

        listView = (ListView)findViewById(R.id.mylist);

         mAlphabetBar = (AlphabetBar)findViewById(R.id.alphabet_bar);
    }
}

AlphabetBar.java

package com.demo.helloworld;

import java.util.Map;

import android.content.Context;
import android.text.Layout.Alignment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


public class AlphabetBar extends LinearLayout
{
    private static final String TAG = "AlphabetBar";

    public AlphabetBar(Context context)
    {
        super(context);
        init();
    }

    public AlphabetBar(Context context, AttributeSet attrs, Map inflateParams)
    {
        super(context, attrs);
        init();
    }

    public void init()
    {
        setLayoutParams(new LinearLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));

        /* Not strictly necessary since we override onLayout and onMeasure with
         * our custom logic, but it seems like good form to do this just to
         * show how we're arranging the children. */
        setOrientation(VERTICAL);

        char[] labels =
        { 
            '#',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        };

        for (int i = 0; i < labels.length; i++)
        {
            TextView label = new TextView(getContext());
            label.setText(String.valueOf(labels[i]));
//          label.setAlignment(Alignment.ALIGN_CENTER);
            label.setGravity(Gravity.CENTER_VERTICAL);

            label.setClickable(true);
            label.setFocusable(true);
            label.setOnClickListener(mLabelClicked);

            addView(label, new LinearLayout.LayoutParams(
              LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }

    private OnClickListener mLabelClicked = new OnClickListener()
    {
        public void onClick(View v)
        {
            if (mClickCallback != null)
                mClickCallback.onClick(v);
        }
    };

    private OnClickListener mClickCallback = null;

    /**
     * Set the click listener for alphabet labels.
     *  
     * @param listener
     *   Click listener, or null to unset.
     */
    public void setLabelClickListener(OnClickListener listener)
    {
        mClickCallback = listener;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
    }

    private void useDefaultBackground()
    {
        setBackgroundResource(R.drawable.alphabet_bar_bg);
    }

    @Override
    protected void onMeasure(int wSpec, int hSpec)
    {
        Log.d(TAG, "onMeasure(" + wSpec + ", " + hSpec + ")");

        if (getBackground() == null)
            useDefaultBackground();

        int count = getChildCount();

        int hMode = MeasureSpec.getMode(hSpec);
        int hSize = MeasureSpec.getSize(hSpec);

        assert hMode == MeasureSpec.EXACTLY;

        int maxWidth = 0;

        int hSizeAdj = hSize - getPaddingTop() - getPaddingBottom(); 
        float childHeight = hSizeAdj / count;

        /* Calculate how many extra 1-pixel spaces we'll need in order to make
         * childHeight align to integer heights. */
        int variance = hSizeAdj - ((int)childHeight * count);

        int paddingWidth = getPaddingLeft() + getPaddingRight();

        for (int i = 0; i < count; i++)
        {
            TextView label = (TextView)getChildAt(i);

            label.setTextSize(childHeight * 0.9F);

            int thisHeight = (int)childHeight;

            if (variance > 0)
            {
                thisHeight++;
                variance--;
            }

            label.measure
              (MeasureSpec.makeMeasureSpec(13, MeasureSpec.EXACTLY),
               MeasureSpec.makeMeasureSpec(thisHeight, MeasureSpec.EXACTLY));

            maxWidth = Math.max(maxWidth, label.getMeasuredWidth());
        }

        maxWidth += paddingWidth;

        setMeasuredDimension(resolveSize(maxWidth, wSpec), hSize); 
    }   
}

alphabet_bar_bg.xml (under drawable folder)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  type="rectangle">
    <solid android:color="#20777777" />
    <corners android:radius="8dip" />
</shape>

mail.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"
    >
    <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

    <com.demo.helloworld.AlphabetBar
        android:id="@+id/alphabet_bar"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="6dip"
        android:layout_marginTop="14dip"
        android:layout_marginBottom="14dip"
        android:paddingTop="2dip"
        android:paddingBottom="2dip"
        android:paddingLeft="4dip"
        android:paddingRight="4dip"
        />
</LinearLayout>
  • 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-20T13:25:29+00:00Added an answer on May 20, 2026 at 1:25 pm

    Okay, I figured this out.

    (1) Change the layout from LinearLayout to RelativeLayout.

    (2) Remove Map inflateParams from the AlphabetBar constructor.

    And Viola!! It works! LOL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.