Android: My programmatic buttons don't match my XML buttons

2020-02-15 android xml button themes

I am trying to dynamically create buttons on one of my activities and they don't look the way I want, specifically they don't match the buttons I created using XML (which look the way I want).

I have created a custom theme for the application but it doesn't seem to completely apply to buttons I create using code.

[style.xml]

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <!-- Cool dark purple theme theme. -->
  <style name="DarkPurpleTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/dpColorPrimary</item>
    <item name="colorPrimaryDark">@color/dpColorPrimaryDark</item>
    <item name="colorAccent">@color/dpColorAccent</item>
    <item name="android:background">@color/dpColorPrimary</item>
    <item name="android:windowBackground">@color/dpColorPrimary</item>
    <item name="android:colorBackground">@color/dpColorPrimary</item>
    <item name="android:textColor">@color/dpColorAccent</item>/>
    <item name="colorButtonNormal">@color/dpColorPrimaryDark</item>
    <item name="colorControlHighlight">@color/dpColorAccent</item>
  </style>

  <!-- Cool dark purple theme button. -->
  <!--
  <style name="DarkPurpleButtonTheme" parent="Base.Widget.AppCompat.Button">
    <item name="android:textColor">@color/dpColorAccent</item>/>
    <item name="colorButtonNormal">@color/dpColorPrimaryDark</item>
    <item name="colorControlHighlight">@color/dpColorAccent</item>
  </style>
  -->

  <style name="DarkPurpleButtonTheme" parent="Base.Widget.AppCompat.Button">
    <item name="colorPrimary">@color/dpColorPrimary</item>
    <item name="colorPrimaryDark">@color/dpColorPrimaryDark</item>
    <item name="colorAccent">@color/dpColorAccent</item>
    <item name="android:background">@color/dpColorPrimary</item>
    <item name="android:windowBackground">@color/dpColorPrimary</item>
    <item name="android:colorBackground">@color/dpColorPrimary</item>
    <item name="android:textColor">@color/dpColorAccent</item>/>
    <item name="colorButtonNormal">@color/dpColorPrimary</item>
    <item name="colorControlHighlight">@color/dpColorAccent</item>
  </style>

</resources>

I have 2 buttons in this XML that look correct (and will be removed when dynamic buttons look correct).

[activity_menu.xml]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:theme="@style/DarkPurpleTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <GridLayout
        android:id="@+id/menu_btn_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"        
        android:columnCount="2" > 

        <Button
            android:id="@+id/trn_purchase"
            android:drawableLeft="@drawable/ic_purchase64x64"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_gravity="fill"
            android:layout_columnWeight="1"
            android:text="@string/trn_purchase" />

        <Button
            android:id="@+id/trn_splitp"
            android:drawableLeft="@drawable/ic_splitp64x64"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            android:layout_gravity="fill"
            android:layout_columnWeight="1"
            android:text="@string/trn_splitp" />

    </GridLayout>

</LinearLayout>

[MenuActivity.java]

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme( R.style.DarkPurpleTheme );
        setContentView(R.layout.activity_menu);
        MyMenu = this.getIntent().getStringExtra( START_MENU );
        NewMenuItem( "Purchase", "ic_purchase64x64", 1 );
        NewMenuItem( "Split Purchase", "ic_splitp64x64", 2 );
        NewMenuItem( "Pre-Auth", "ic_preauth64x64", 3 );
        NewMenuItem( "Pre-Auth Complete", "ic_auth_complt64x64", 4 );
    }

    private void NewMenuItem( String label, String image, int key )
    {
        GridLayout grid = ( GridLayout )this.findViewById( R.id.menu_btn_grid );
        int count = grid.getChildCount();
        int row = count / 2;
        int column = count % 2;

        AppCompatButton button = new AppCompatButton( new ContextThemeWrapper( this.getApplicationContext(), R.style.DarkPurpleButtonTheme ), null, R.style.DarkPurpleButtonTheme );
        // Button button = new Button( new ContextThemeWrapper( this.getApplicationContext(), R.style.DarkPurpleButtonTheme ), null, R.style.DarkPurpleTheme );
        button.setText( label );
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tranButton_Click(v);
            }
        });
        button.setTag( key );
        // button.setBackgroundColor( 0x262626 );
        // button.setPadding( 25, 31, 25, 31 );

        GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
        lp.width = 0;
        lp.height = GridLayout.LayoutParams.WRAP_CONTENT;
        lp.rowSpec = GridLayout.spec( row );
        lp.columnSpec = GridLayout.spec( column, 1.0f );
        lp.setGravity( GridLayout.LayoutParams.FILL_PARENT );
        button.setLayoutParams( lp );

        if ( (image != null) && (!image.isEmpty()) )
        {
            int imgid = GetResourceId( image );
            Drawable img = this.getApplicationContext().getDrawable( imgid );
            img.setBounds( 0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight() );
            button.setCompoundDrawables( img, null, null, null );
        }

        grid.addView( button );
    }

Screenshot

Answers

Related