Question

I have one horizontal linear layout that has one textview and button but when I apply background colour button just resembles the text view and I can't find any differnece between both.

I want to format my button so that it should be identified differently from text view.

Image of layout where there is button and text view.

enter image description here

In above image Selected Option is textview and Update is button as you can see both looks alike.

Can any one help me on this, As I have no clue on how to do this.

Thanks for your time.

Was it helpful?

Solution

There is virtually no difference between a Button and a TextView. Button is extended from a TextView only. You can clear your doubt if you look at the source code of Button

public class Button extends TextView {
    public Button(Context context) {
        this(context, null);
    }

    public Button(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.buttonStyle);
    }

    public Button(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
        super.onInitializeAccessibilityEvent(event);
        event.setClassName(Button.class.getName());
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        info.setClassName(Button.class.getName());
    }
}

EDIT:

But if you just want to change the background color of the Button without changing the default style, you can do something like below,

btnName.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));

Remember to remove the android:background="" or android:src="" from your Button if any in xml file.

This code only changes the background color without affecting the default style. You can change the background color with the standard HEX color codes.

reference

Standard Android Button with a different color

OTHER TIPS

Check out this sample it has various styles you can apply to the button. Everything is done with only colors.

// Try this way,hope this will help you..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@color/button_default"> // set your background color here
        <TextView
            android:id="@+id/txtSelectedOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Selected Option"/>
    </LinearLayout>

    <Button
        android:id="@+id/btnUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update"/>

</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top