Skip to content Skip to sidebar Skip to footer

Creating A Customised Button With 2 Lines Of Text

I'm new to Android (Visual Studio for 20 years). I need to create a clickable control that features 2 lines of text (1 smaller font at the top of the button for a caption and a la

Solution 1:

You could try using Spannable in the code like:

publicclassTestextendsActivity {

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Buttonbut= (Button) findViewById(R.id.button1);
        String butText= "Line 1\nLine 2";
        but.setText(formatString(butText));
    }

    private Spannable formatString(String str) {

        intstartSpan= str.indexOf("\n");
        intendSpan= str.length();
        SpannablespanString=null;
        spanString = newSpannableString(str);
        spanString.setSpan(newTextAppearanceSpan(this,
                R.style.custompoint), startSpan, endSpan,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);        
        return spanString;
    }

}

Where you have a style 'custompoint'

<?xml version="1.0" encoding="utf-8"?><resources><stylename="custompoint"><itemname="android:textSize">24sp</item><itemname="android:textStyle">bold</item></style></resources>

Post a Comment for "Creating A Customised Button With 2 Lines Of Text"