Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Button with two TextView

Tags:

android

I'm trying to Customize button with two TextView with different typeface within a single button. For that I just extended Button and with have written the following code inside the constructor,

LayoutInflater layoutInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.custom_button,
        (ViewGroup) findViewById(R.id.custom_button_view));

TextView firstTextView = (TextView) layout
        .findViewById(R.id.firstTextView);
TextView secondTextView = (TextView) layout
        .findViewById(R.id.secondTextView);

in the layout custom_button I have placed two TextView with different typeface and text and custom_button_view is the ID of that LinearLayout, what I got is an empty button with no text.

Any Ideas, Thanks.

like image 591
Vignesh Avatar asked Sep 07 '11 04:09

Vignesh


1 Answers

You can use Layout as a button by setting ur custom button style to layout and can add two textViews to it, in this way:

<LinearLayout android:id="@+id/customButtonLayout"
    android:layout_height="wrap_content" style="@android:style/Widget.Button"
    android:layout_width="wrap_content">
    <TextView android:text="First" android:id="@+id/firstTextView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textColor="#000"></TextView>
    <TextView android:textColor="#000" android:text="Second"
        android:layout_height="wrap_content" android:id="@+id/secondTextView"
        android:layout_width="wrap_content" android:layout_marginLeft="10dp"></TextView>
</LinearLayout>

and in Activity you can have this to set different typeface:

    Typeface font=Typeface.createFromAsset(getAssets(),"ARIALN.TTF") ;   
    Typeface font2=Typeface.createFromAsset(getAssets(), "COMPCTAN.TTF");

    TextView firstTextView = (TextView)findViewById(R.id.firstTextView);
    TextView secondTextView = (TextView)findViewById(R.id.secondTextView);

    firstTextView.setTypeface(font);
    secondTextView.setTypeface(font2);

    LinearLayout btnLayout=(LinearLayout) findViewById(R.id.customButtonLayout);
    btnLayout.setOnClickListener(this);
like image 141
Permita Avatar answered Sep 29 '22 14:09

Permita