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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:01:06+00:00 2026-06-17T22:01:06+00:00

I have found some code by Scott Lund on Catch the CowsCode Bits and

  • 0

I have found some code by Scott Lund on Catch the CowsCode Bits and Bits of Thoughts on Coding for Android. see: http://catchthecows.com/?p=72 this code creates a button and enlarges text to fill the full button size.

I am trying without luck to detect a button press on the new button class BigTextButton

package com.sample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.ImageButton;

public class BigTextButton extends ImageButton {
    String mText = "";
    Paint mTextPaint;

    int mViewWidth;
    int mViewHeight;
    int mTextBaseline;

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

    public BigTextButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        parseAttrs(attrs);
        init();
    }

    public BigTextButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        parseAttrs(attrs);
        init();
    }

    /**
     * Dig out Attributes to find text setting
     * 
     * This could be expanded to pull out settings for textColor, etc if desired
     * 
     * @param attrs
     */

    private void parseAttrs(AttributeSet attrs) {
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String s = attrs.getAttributeName(i);
            if (s.equalsIgnoreCase("text")) {
                mText = attrs.getAttributeValue(i);
            }
        }
    }

    public void setText(CharSequence text) {
        mText = text.toString();
        onSizeChanged(getWidth(), getHeight(), getWidth(), getHeight());
    }

    /**
     * initialize Paint for text, it will be modified when the view size is set
     */
    private void init() {
        mTextPaint = new TextPaint();
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setAntiAlias(true);
    }

    /**
     * set the scale of the text Paint objects so that the text will draw and
     * take up the full screen width
     */
    void adjustTextScale() {
        // do calculation with scale of 1.0 (no scale)
        mTextPaint.setTextScaleX(1.0f);
        Rect bounds = new Rect();
        // ask the paint for the bounding rect if it were to draw this
        // text.
        mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);

        // determine the width
        int w = bounds.right - bounds.left;

        // calculate the baseline to use so that the
        // entire text is visible including the descenders
        int text_h = bounds.bottom - bounds.top;
        mTextBaseline = bounds.bottom + ((mViewHeight - text_h) / 2);

        // determine how much to scale the width to fit the view
        float xscale = ((float) (mViewWidth - getPaddingLeft() - getPaddingRight()))
                / w;

        // set the scale for the text paint
        mTextPaint.setTextScaleX(xscale);
    }

    /**
     * determine the proper text size to use to fill the full height
     */
    void adjustTextSize() {
        if (mText.isEmpty()) {
            return;
        }
        mTextPaint.setTextSize(100);
        mTextPaint.setTextScaleX(1.0f);
        Rect bounds = new Rect();
        // ask the paint for the bounding rect if it were to draw this
        // text
        mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);

        // get the height that would have been produced
        int h = bounds.bottom - bounds.top;

        // make the text text up 70% of the height
        float target = (float) mViewHeight * .7f;

        // figure out what textSize setting would create that height
        // of text
        float size = ((target / h) * 100f);

        // and set it into the paint
        mTextPaint.setTextSize(size);
    }

    /**
     * When the view size is changed, recalculate the paint settings to have the
     * text on the fill the view area
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // save view size
        mViewWidth = w;
        mViewHeight = h;

        // first determine font point size
        adjustTextSize();
        // then determine width scaling
        // this is done in two steps in case the
        // point size change affects the width boundary
        adjustTextScale();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // let the ImageButton paint background as normal
        super.onDraw(canvas);

        // draw the text
        // position is centered on width
        // and the baseline is calculated to be positioned from the
        // view bottom
        canvas.drawText(mText, mViewWidth / 2, mViewHeight - mTextBaseline,
                mTextPaint);

    }
}
  • 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-17T22:01:07+00:00Added an answer on June 17, 2026 at 10:01 pm

    I worked it out myself, the following code detects a press and opens the editing screen2.xml

     // open the editor screen2.xml if you click the bigTextButton1 eg line 1 of screen
        BigTextButton progress = (BigTextButton) findViewById(R.id.bigTextButton1);        
        progress.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {                
    
                // Do something when clicked
                // Starting a new Intent to switch screens
                Intent Config = new Intent(getApplicationContext(), Configuration.class);
    
                // starting new activity
                startActivity(Config);
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
Hi all I have found some really good code over on http://apptools.com thats helped
I have found some code which recognize circles in particular image and I was
I have found some code snippet in the internet. But it is missing some
I am now trying to understand some code and I have found a pattern,
I have adapted some code from a great article I found about circle drawing
I have found some example code that creates a gradient fill in a WPF
I have found some nice dark themes for editing code in Eclipse (screenshot below),
I have looked around and found some code which so called chooses an image
I'm maintaining some code and have found the following pattern a lot: var isMale

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.