Java Code Examples for android.widget.ToggleButton#setLayoutParams()

The following examples show how to use android.widget.ToggleButton#setLayoutParams() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommentActivity.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
private void addTag(String tagName, int id, ViewGroup column, int color) {
    ToggleButton tagButton = new ToggleButton(this);
    if(color != -1) {
        LinearLayout colorBox = new LinearLayout(this);
        // Convert the dps to pixels, based on density scale
        final int tagPaddingPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                1, getResources().getDisplayMetrics());
        final int tagPaddingPxBottom = (int) TypedValue.applyDimension(TypedValue
                .COMPLEX_UNIT_DIP,
                3, getResources().getDisplayMetrics());
        //use a GradientDrawable with only one color set, to make it a solid color
        colorBox.setBackgroundResource(R.drawable.tag_round_corner);
        GradientDrawable gradientDrawable = (GradientDrawable) colorBox.getBackground();
        gradientDrawable.setColor(color);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(tagPaddingPx,tagPaddingPx,tagPaddingPx,tagPaddingPxBottom);
        colorBox.setLayoutParams(params);
        colorBox.addView(tagButton);
        column.addView(colorBox);
    } else {
        column.addView(tagButton);
    }
    tagButton.setTextOff(tagName);
    tagButton.setTextOn(tagName);
    boolean isChecked = checkedTags.contains(id);
    tagButton.setChecked(isChecked);
    if(isChecked) {
        tagButton.setTextColor(selectedColor);
    }
    tagButton.setOnCheckedChangeListener(new TagStateListener(id, checkedTags));
    tagButton.setMinHeight(0);
    tagButton.setMinimumHeight(0);
    tagButton.setTextSize(Dimension.SP, 12);
    tagButton.setEnabled(record == null || record.getUploadId().isEmpty());
    tagButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    tagButton.invalidate();
}