create button From Label - Android User Interface

Android examples for User Interface:Button

Description

create button From Label

Demo Code


//package com.java2s;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class Main {
    private static final String LOG_TAG = "TP-U";
    private static final int SCORE_COLOR = Color.WHITE;

    public static Button buttonFromLabel(CharSequence score, Context context) {
        Log.v(LOG_TAG, "Creating element: " + score);
        Button button = new Button(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 5, 0); // set right margin to 5 to separate elements
        button.setLayoutParams(params);//from  w ww . j a v  a  2  s  . c  o  m
        button.setBackgroundColor(SCORE_COLOR);
        button.setText(score);
        button.setGravity(Gravity.CENTER);
        return button;
    }
}

Related Tutorials