0

How can I add big AND small letters in a TextView like in the image below:

Sorry: "nuber" = "number"

enter image description here

Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
  • Could you maybe highlight which letters a bit more? – poss Apr 21 '15 at 14:29
  • Check this out - http://stackoverflow.com/questions/23001374/how-to-use-two-different-font-sizes-for-button-text-in-android – TechnoBlahble Apr 21 '15 at 14:38
  • @SuperThomasLab does it have to be a `button` view? Why not use custom `View` with background and 2 `textview` fields? – poss Apr 21 '15 at 14:39
  • @poss I want to make a custom dialer so i put buttons in a gridlayout. but i want the buttons to be like the image attached. – Thomas Vos Apr 21 '15 at 14:40
  • You can put views in `gridlayout` and call `onClick` on them as well – poss Apr 21 '15 at 14:41
  • @poss so i need to make a custom view for each button? – Thomas Vos Apr 21 '15 at 14:45
  • 1
    @SuperThomasLab don't use Button at all. Create own `View` with 2 `TextView` inside it and place those on a grid. Similar approach like in @TechnoBlahble link – poss Apr 21 '15 at 14:50

2 Answers2

2

It looks like the button you see in the UI isn't a standard Android Button, but rather a composite View made up of two TextViews.

You should make sure that your composite View is focusable and clickable, (not the two TextViews that its made of) and provide default, focus and pressed states for this View.

public class MyCompositeView extends LinearLayout {

    private TextView primaryTextView;
    private TextView secondaryTextView;

    public MyCompositeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setOrientation(HORIZONTAL);
    }

    @Override
    public final void setOrientation(int orientation) {
        if (orientation != HORIZONTAL) {
            throw new IllegalArgumentException("MyCompositeView is a button-like widget, not a ViewGroup! Don't change my orientation");
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View.inflate(getContext(), R.layout.merge_my_composite_view, this);
        primaryTextView = (TextView) findViewById(R.id.my_composite_text_primary);
        secondaryTextView = (TextView) findViewById(R.id.my_composite_text_secondary);
    }

    public void setPrimaryText(String text) {
        if (valid(text)) {
            primaryTextView.setVisibility(VISIBLE);
        } else {
            primaryTextView.setVisibility(GONE);
        }
        primaryTextView.setText(text);
    }

    private static boolean valid(String text) {
        return text != null && !text.trim().isEmpty();
    }

    public void setSecondaryText(String text) {
        if (valid(text)) {
            secondaryTextView.setVisibility(VISIBLE);
        } else {
            secondaryTextView.setVisibility(GONE);
        }
        secondaryTextView.setText(text);
    }

}

The merge layout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:id="@+id/my_composite_text_primary"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center" />

  <TextView
    android:id="@+id/my_composite_text_secondary"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center" />

</merge>

And you would use it as:

<?xml version="1.0" encoding="utf-8"?>
<com.example.MyCompositeView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
ataulm
  • 15,195
  • 7
  • 50
  • 92
0

You can try to use Html.fromHtml as:

button.setText(Html.fromHtml("html_code_with"));

instead of html_code_with set your html code for big text and small text.

Simon Schnell
  • 1,160
  • 14
  • 24
Toukea Tatsi
  • 189
  • 1
  • 5
  • Could you give me a small sample of what i want – Thomas Vos Apr 21 '15 at 14:53
  • button.setText(Html.fromHtml("

    BIG

    small"));
    – Toukea Tatsi Apr 21 '15 at 14:57
  • ok, the Html.fromHtml(StringHtmlValue) allow to transform a simple String to a android Html compatible String. so, if you set your button text as button.setText(Html.fromHtml(myStringButtonText)); and that, myStringButtonText as "

    "+bigValu+"

    "+smallValue; on your Button, android will display the myStringButtonText like a web navigator. in Html language, Tag

    allow render a big text please try this on your button and look result.

    – Toukea Tatsi Apr 22 '15 at 14:06