Is it possible to somehow set the text on the ImageButton or animate the usual Button? In general, I need to solve the problem: there should be a button with a click animation (or something that clicks), and a text on top of it. How to do it better?

    2 answers 2

    Layouts always need to try to write optimally. The given example is valid, but it consists of 3 components. As far as I understood from the question, you can get away with a simpler layout

    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/appmenu_btn_style" android:clickable="true" android:drawableLeft="@drawable/ic_appmenu_photos" android:drawablePadding="15dp" android:onClick="dispatchAppMenuClick" android:paddingBottom="10dp" android:paddingLeft="18dp" android:paddingTop="10dp" android:tag="photos_btn" android:text="@string/appmenu_photos" android:textColor="#ffffff" android:textSize="9pt" /> 

    thanks to android: clickable = "true" the resulted TextView pretends to be a button. Next, you just need to register the state for our "button" and the matter is in the hat. Res / drawable / appmenu_btn_style.xml file

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/appmenu_btn_pressed" /> </selector> 

    Res / drawable / appmenu_btn_pressed.xml file

     <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#737373" /> </shape> 

    In simple layouts, thinking about optimizing the view-s may not make much sense, but when you need to make a list item - it is highly desirable to pay attention to "optimality"

      You can position TextView over ImageButton - you just need to use FrameLayout , like:

       <FrameLayout> <ImageButton android:background=" @null " android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/myButton"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Мой текст"/> </FrameLayout>