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 8478183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:40:33+00:00 2026-06-10T18:40:33+00:00

new to android. I created 12 (3 rows, 4 columns) buttons in a GridView

  • 0

new to android. I created 12 (3 rows, 4 columns) buttons in a GridView and I created a toast to display something after a button is pressed by following a tutorial. The buttons are displaying on the screen correctly, but the toast messages are not. When I press top left button, I get the bottom left toast. When I press middle left button, I get middle right toast. When I press bottom left button, I get top right toast.

Visually:

button location:
1 2 3 4
5 6 7 8
9 10 11 12

toast message
9 10 11 12
8 7 6 5
4 3 2 1

Here’s the adapter code:

public class KeypadAdapter extends BaseAdapter {
    private Context mContext;

    // Declare button click listener variable
    private OnClickListener mOnButtonClick;

    // Method to set button click listener variable
    public void setOnButtonClickListener(OnClickListener listener) {
        mOnButtonClick = listener;
    }

    public KeypadAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mButtons.length;
    }

    public Object getItem(int position) {
        return mButtons[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    private KeypadButtons[] mButtons = { KeypadButtons.ADD,
            KeypadButtons.SUBTRACT, KeypadButtons.MULTIPLY,
            KeypadButtons.DIVIDE, KeypadButtons.DET, KeypadButtons.INV,
            KeypadButtons.POW2, KeypadButtons.POWN, KeypadButtons.TRANSPOSE,
            KeypadButtons.NORM1, KeypadButtons.NORM2, KeypadButtons.NORMINF };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Button btn;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            btn = new Button(mContext);
            KeypadButtons keypadButton = mButtons[position];
            if (keypadButton != KeypadButtons.DUMMY) {
                btn.setOnClickListener(mOnButtonClick);
            }

            // Set CalculatorButton enumeration as tag of the button so that we
            // will use this information from our main view to identify what to
            // do
            btn.setTag(keypadButton);
        } else {
            btn = (Button) convertView;
        }

        btn.setText(mButtons[position].getText());
        return btn;
    }

}

and here’s the the activity code

public class MainActivity extends Activity {
    GridView mKeypadGrid;
    KeypadAdapter mKeypadAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get reference to the keypad button GridView
        mKeypadGrid = (GridView) findViewById(R.id.gridView);

        // Create Keypad Adapter
        mKeypadAdapter = new KeypadAdapter(this);

        // Set adapter of the keypad grid
        mKeypadGrid.setAdapter(mKeypadAdapter);

        // Set button click listener of the keypad adapter
        mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Button btn = (Button) v;
                // Get the KeypadButton value which is used to identify the
                // keypad button from the Button's tag
                KeypadButtons keypadButton = (KeypadButtons) btn.getTag();

                // Process keypad button
                ProcessKeypadInput(keypadButton);
            }
        });

        mKeypadGrid.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }
        });

    }

    protected void ProcessKeypadInput(KeypadButtons keypadButton) {
        // TODO Auto-generated method stub
        Toast.makeText(
                MainActivity.this,
                keypadButton.getText().toString() + " "
                        + keypadButton.toString(), Toast.LENGTH_SHORT).show();


    }
}

At first it seems the order of the toasts are reversed. So I tried doing

KeypadButtons keypadButton = mButtons[mButtons.length - 1 - position];

That fixed the bottom two rows but he top row is still reversed.

Thanks in advance.

  • 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-06-10T18:40:35+00:00Added an answer on June 10, 2026 at 6:40 pm

    Views are recycled (as you obviously know because you’re checking if the convertview is null). A simple fix here is to move the btn.setTag(keypadButton); to outside of theif(convertView==null)` block and execute it always before you return from getView();

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

Sidebar

Related Questions

i am new to android and i have created a login page after verifing
I am new to android development. I have created a popup window which is
i have one doubt,i have created new project in eclipse with android 2.3.1 and
New with Android. Sorry! I'm trying to create the following... Wait for Timer to
I'm pretty new to Android Development, i'm following some Youtube tutorials currently. I'd like
I just created a new android project and prepared the basic structure for it.
I am new to Android.I have created a dynamic textview which is displayed on
So I created a new Android junit test, which runs my ui-automation tests (Robotium,
I`m relatively new at Android development, although I have created a few basic applications.
I would know if there is the possibility to create a new Android Activity

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.