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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:27:30+00:00 2026-05-27T18:27:30+00:00

Please help me to fix the code below. I am making a mousepad view

  • 0

Please help me to fix the code below.
I am making a mousepad view for my Android Remote Application.

public class MousePadView extends RelativeLayout {

    private float scale;

    // MOUSE BUTTONS LEFT CLICK, MIDDLE CLICK, RIGHT CLICK

    private LinearLayout layoutMouseButtonBody;
    private RelativeLayout.LayoutParams paramsMouseButtonBody;

    private Button left;
    private Button middle;
    private Button right;
    private LinearLayout.LayoutParams paramsButtons;

    // BUTTONS FOR MOUSEWHEEL (UP AND DOWN)

    private LinearLayout layoutWheelsBody;
    private RelativeLayout.LayoutParams paramsWheelBody;
    private Button up;
    private Button down;
    private LinearLayout.LayoutParams paramsWheelButton;

    public MousePadView2(Context context, AttributeSet attrs, int defStyle) { // CONSTRUCTOR
        super(context, attrs, defStyle);
        scale = context.getResources().getDisplayMetrics().density; // GET SCALE FOR CONVERTING DPI TO PIXELS

        // MOUSE BUTTON LAYOUT

        paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
        left = new Button(context);
        left.setText("L");
        left.setLayoutParams(paramsButtons);
        middle = new Button(context);
        middle.setText("M");
        middle.setLayoutParams(paramsButtons);
        right = new Button(context);
        right.setText("R");
        right.setLayoutParams(paramsButtons);

        paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
        paramsMouseButtonBody.addRule(ALIGN_PARENT_LEFT); // RELATIVE LAYOUT RULES
        paramsMouseButtonBody.addRule(ALIGN_PARENT_RIGHT); // RELATIVE LAYOUT RULES
        layoutMouseButtonBody = new LinearLayout(context);
        layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
//      layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
        layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
        layoutMouseButtonBody.setLayoutParams(paramsMouseButtonBody);
        layoutMouseButtonBody.addView(left);
        layoutMouseButtonBody.addView(middle);
        layoutMouseButtonBody.addView(right);

        // WHEELS

        paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        up = new Button(context);
        up.setText("U");
        up.setLayoutParams(paramsWheelButton);
        down = new Button(context);
        down.setText("D");
        down.setLayoutParams(paramsWheelButton);

        paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
        paramsWheelBody.addRule(LEFT_OF, layoutMouseButtonBody.getId());
        paramsWheelBody.addRule(ALIGN_PARENT_BOTTOM);
        layoutWheelsBody = new LinearLayout(context);
        layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
        layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
        layoutWheelsBody.setLayoutParams(paramsWheelBody);
        layoutWheelsBody.addView(up);
        layoutWheelsBody.addView(down);

        // PARENT
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(layoutMouseButtonBody); // add mousebutton layout in parent (relativelayout)
        addView(layoutWheelsBody); // add mousewheel button layout in parent (relativelayout)
    }


    private int DpiToPixels(int dp) {
        return (int)(dp * scale + 0.5f); // converting DPI to Pixels
    }

}

The output generated by Android SDK
This is the output that I want

The image on the Left is the output generated by Android SDK and The right one is the output that I want.

Please Help me.

I don’t want to inflate layout from XML.

  • 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-27T18:27:31+00:00Added an answer on May 27, 2026 at 6:27 pm

    I think the only way to achieve what you’re looking for is by grouping both your wheel and button layout into another layout. After that, simply align the result to the bottom of the parent.

    Add something like this:

    LayoutParams paramsTotal = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    paramsTotal.addRule(ALIGN_PARENT_BOTTOM);
    LinearLayout layoutTotal = new LinearLayout(context);
    layoutTotal.setOrientation(LinearLayout.VERTICAL);
    layoutTotal.setLayoutParams(paramsTotal);
    layoutTotal.addView(layoutWheelsBody);
    layoutTotal.addView(layoutMouseButtonBody);
    
    // PARENT
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    addView(layoutTotal); // add the combined layout
    

    That should place the wheel buttons right above the mouse buttons (or vice versa: the mouse buttons directly below the wheel buttons) and the combination all the way at the bottom.

    //Edit: alternatively, you could make the root layout a LinearLayout and add a dummy view with a weight of ‘1’ at the top, which will push the other elements down to the bottom. I’d probably prefer the RelativeLayout option though.

    Just out of curiousity: why not just inflate the layout? Personally I find that way more managable.

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

Sidebar

Related Questions

Please help me to fix my code $fp = fsockopen(projecthoneypot.org/statistics.php, 80, $errno, $errstr, 5);
Please help me to fix the CSS error in profile on my site (IE).
Please help! Background info I have a WPF application which accesses a SQL Server
I need help with the javascript below please. I have 2 text boxes and
I have a DataGridView inside a ContextMenu control, please see the code snippet below:
Help me please with Boruvka algorithm for creating a minnimum-spanning tree. I wrote code
I'd like to use the vertical sliding/toggle menu, please see my code below, at
Please help! I'm really at my wits' end. My program is a little personal
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
Please help, I am stuck here --- irb> a = line of text\n line

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.