Question

I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:

My TextView at the beginning:

enter image description here

When I click a button:

enter image description here

Was it helpful?

Solution

If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</FrameLayout>

When you want to change the color behind the TextView then change the ImageView background as below...

ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);

OTHER TIPS

If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.

Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).

You should use ImageView instead of TextView, the:

public void onClick(View v){
  imageView.setBackgroundColor(Color.GREEN);
}

ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this

android:drawableTop="@drawable/any_drawable"
android:text="@string/any_text"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top