4

How could I implement a button background for ImageButton or Button which contains 2 textview each using different font sizes ? I cant use static images as background and I need to achieve this either using xml or programmatically.

This is a numeric keypad and it carries a numeric value and a set of alphabets each of which uses different font sizes and needs to be set as button text.

enter image description here

Here are some of the suggestions that pop-up on my brain but seems to have limitations since I want to reuse the UI component and avoid code repetition.

  1. Create a layout xml containing 2 textfields and set that as background on the button. But how can I get a reference of these textview fields to set the value on them in the MyActivity.java?

  2. Use a layeredlist ? still same problem reference of textviews

  3. Or create a custom view and inflate layout mentioned in step 1. This solution resolves my problem.

Does any other solution exist for this UI requirement?

Thanks.

Akh
  • 5,961
  • 14
  • 53
  • 82
  • 1
    You can use Html.fromHtml() method which takes html tags, lays their effect and converts n returns them back as Strings. So using setText() you can apply it to your button. Thers a link from Commonsware on what all tags are supportrd by this method but I dont have it right now. U gotta search 4 it. ;) – Atul O Holic Apr 11 '14 at 01:25
  • My suggestion is to use ImageButton with relative image :) – Lucifer Apr 11 '14 at 03:47
  • May be this one will help you..click [here][1] [1]: http://stackoverflow.com/questions/12048862/creating-a-custom-button-with-two-lines-of-text-each-with-a-different-font?rq=1 – user3468596 Apr 11 '14 at 05:37

2 Answers2

9

Hopefully this will help. This off course is not the only solution but it is a pretty simple one. Add the xml part in your xml instead of the button.

XML

<LinearLayout
    android:id="@+id/button_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" 
        android:text="5"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp" 
        android:text="ABC"/>

</LinearLayout>

CODE

    LinearLayout button= (LinearLayout)findViewById(R.id.button_layout);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO DO whatever you want to do here

        }
    });
Oya
  • 833
  • 1
  • 8
  • 21
1

For your button you can use code like this

don't forget put font in your Assets folder

 Button btnA=(Button) findViewById(R.id.button1);
 Typeface typeface = Typeface.createFromAsset(getAssets(), "yourFont.ttf");
 btnA.setText("my custom font");
 btnA.setTypeface(typeface);
 btnA.setTextSize(20);//as per your size

and for more reference see this

do as your second button if this helps let me know thanks...

MilapTank
  • 9,988
  • 7
  • 38
  • 53