Sunday, August 9, 2015

How to change CheckBox size in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#94d088"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check your favorites."
        android:textSize="25dp"
        android:textStyle="italic"
        />
    <CheckBox
        android:id="@+id/cb_coldfusion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ColdFusion"
        android:layout_below="@id/tv"
        android:scaleX="1.5"
        android:scaleY="1.5"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:paddingRight="20dp"
        android:background="#ffc7d8"
        />
    <CheckBox
        android:id="@+id/cb_flex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flex"
        android:layout_below="@id/cb_coldfusion"
        android:background="#c5c3ff"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Increase Flex CheckBox Size"
        android:layout_below="@id/cb_flex"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);
        final CheckBox cb_flex = (CheckBox) findViewById(R.id.cb_flex);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Set Flex CheckBox scaling programmatically
                cb_flex.setScaleX(1.5f);
                cb_flex.setScaleY(1.5f);

                // Get the margins of Flex CheckBox
                ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) cb_flex.getLayoutParams();

                // Set left, top, right and bottom margins of Flex CheckBox
                mlp.setMargins(25,15,10,10);
                cb_flex.setLayoutParams(mlp);

                // Apply right padding of Flex CheckBox
                cb_flex.setPadding(0,0,50,0);
            }
        });
 }
}
More android examples