Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit:</p> <p>create a selector xml with your code and put in your drawable folder. I'll create a btn_custom.xml</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/btn_active" /&gt; &lt;item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_active" /&gt; &lt;item android:drawable="@drawable/btn_default" /&gt; &lt;/selector&gt; </code></pre> <p>Then, on your TextView:</p> <pre><code>&lt;TextView android:text="MyButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:clickable="true" android:background="@drawable/btn_custom" android:padding="10dp"/&gt; </code></pre> <p>On padding, you adjust your button padding.</p> <p>On background, put the name of your selector (in my case, @drawable/btn_custom)</p> <p>and your effect is already on.</p> <p>Than, just register an onClickListener</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView myTextButton = (TextView) findViewById(R.id.my_button_id); myTextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "ButtonClick", 200).show(); } }); } </code></pre> <p>Ps: You can use your onClick method instead :P</p> <p>Always remember: A Button is just a "styled" TextView.</p> <p>This is the Android source code for Button class:</p> <pre><code>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); } } </code></pre> <p>Yes, it's just that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload