0

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.

Siva
  • 9,043
  • 12
  • 40
  • 63
  • u mean to ask for button color ? check for background attribute – Srinath Ganesh May 14 '14 at 11:46
  • @SrinathGanesh thanks for your response.. I have already applied background colour and my problem started after that... then I can't see any difference between textview and button.. please advice – Siva May 14 '14 at 11:50

3 Answers3

1

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

Community
  • 1
  • 1
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
  • Thanks for valuable information.. but for better user experience there should be difference between `button and `textview`.. Can you advice in that context.. – Siva May 14 '14 at 11:52
  • Thanks for your code... this I need to give it in `onCreate`? – Siva May 14 '14 at 12:06
  • @Siva: Yes.But remember to add it after your `Button` initialization otherwise you will get `NullPointerException`. – Spring Breaker May 14 '14 at 12:07
  • Thanks that has done the trick.... but a small correction before doing this in XML need to remove the `Background` attribute for button and apply this code... Thanks :) – Siva May 14 '14 at 12:16
  • @Siva: Edited your query in the answer. Thanks for pointing it out. – Spring Breaker May 14 '14 at 12:18
1

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

Sundeep1501
  • 1,510
  • 1
  • 18
  • 28
0
// 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>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Thanks @Haresh for your response but I need to apply colour to the button aswell and after applying colour I can't spot any difference between textview and button. – Siva May 14 '14 at 11:55