android - How to create a button of any form programmatically? -


i want create button properties.

  1. it can have form, color , placement.
  2. these parameters can changed in application
  3. the button can change colour on touch

i try use canvas, draw , ontouchevent. there exists more simple , fast aproach? help. thank you!

you use xml file 1 below, create states button.

you copy xml file drawables folder in project, name example custom_button.xml.

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:state_pressed="true" >     <shape android:shape="rectangle">         <solid             android:color="#00ff00" />         <stroke             android:width="5dp"             android:color="#ff0000"             android:dashwidth="3dp"             android:dashgap="2dp" />     </shape> </item>  <item android:state_focused="true" >     <shape>         <gradient             android:endcolor="#ffffff"             android:centercolor="#ffffff"             android:startcolor="#ffffff"             android:angle="270" />         <stroke             android:width="3dp"             android:color="#00ff00" />         <corners             android:radius="5dp" />         <padding             android:left="10dp"             android:top="10dp"             android:right="10dp"             android:bottom="10dp" />     </shape> </item>  <item>             <shape>         <gradient             android:endcolor="#ffffff"             android:centercolor="#ffffff"             android:startcolor="#ffffff"             android:angle="270" />         <stroke             android:width="5dp"             android:color="#00ff00" />         <corners             android:radius="5dp" />     </shape> </item> 

and reference in layout following code in oncreate method

linearlayout layout = (linearlayout) findviewbyid(r.id.linear_layout_tags);          //set properties button         button btntag = new button(this);         btntag.setlayoutparams(new actionbar.layoutparams(actionbar.layoutparams.wrap_content, actionbar.layoutparams.wrap_content));         btntag.settext("button");         btntag.setid(some_random_id);         btntag.setbackground(getresources().getdrawable(r.drawable.custom_button)); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -