org.howto.usesupportlibrary.v7.CardViewActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.howto.usesupportlibrary.v7.CardViewActivity.java

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.howto.usesupportlibrary.v7;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import org.howto.usesupportlibrary.R;

public class CardViewActivity extends ActionBarActivity {

    CardView mCardView;

    TextView mInfoText;

    SeekBar mCornerRadiusSeekBar;

    SeekBar mWidthSeekBar;

    SeekBar mHeightSeekBar;

    SeekBar mElevationSeekBar;

    SeekBar mMaxElevationSeekBar;

    SeekBar mAlphaSeekBar;

    boolean mResizeCardView = true;

    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangedListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            update();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    private void update() {
        mElevationSeekBar.setMax(mMaxElevationSeekBar.getProgress());
        if (mCornerRadiusSeekBar.getProgress() != mCardView.getRadius()) {
            mCardView.setRadius(mCornerRadiusSeekBar.getProgress());
        }
        if (mElevationSeekBar.getProgress() != mCardView.getCardElevation()) {
            mCardView.setCardElevation(mElevationSeekBar.getProgress());
        }
        if (mMaxElevationSeekBar.getProgress() != mCardView.getMaxCardElevation()) {
            mCardView.setMaxCardElevation(mMaxElevationSeekBar.getProgress());
        }
        ViewCompat.setAlpha(mCardView, mAlphaSeekBar.getProgress() / 255f);
        ViewGroup.LayoutParams lp;
        if (mResizeCardView) {
            lp = setViewBounds(mCardView);
        } else {
            lp = setViewBounds(mInfoText);
        }
        mInfoText.setText("radius: " + mCornerRadiusSeekBar.getProgress() + ", alpha: "
                + mAlphaSeekBar.getProgress() + "\n width: " + lp.width + "\nheight: " + lp.height + "\nelevation: "
                + mCardView.getCardElevation() + " of " + mCardView.getMaxCardElevation());
    }

    private ViewGroup.LayoutParams setViewBounds(View view) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        boolean changed = lp.width != mWidthSeekBar.getProgress() || lp.height != mHeightSeekBar.getProgress();
        if (!changed) {
            return lp;
        }
        lp.width = mWidthSeekBar.getProgress();
        lp.height = mHeightSeekBar.getProgress();
        view.setLayoutParams(lp);
        return lp;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_view_activity);
        mInfoText = (TextView) findViewById(R.id.info_text);
        mCardView = (CardView) findViewById(R.id.card_view);
        mCornerRadiusSeekBar = (SeekBar) findViewById(R.id.corner_radius_seek_bar);
        mCornerRadiusSeekBar.setProgress((int) mCardView.getRadius());
        mCornerRadiusSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);

        mWidthSeekBar = (SeekBar) findViewById(R.id.width_seek_bar);
        mWidthSeekBar.setProgress(mCardView.getLayoutParams().width);

        mWidthSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);

        mHeightSeekBar = (SeekBar) findViewById(R.id.height_seek_bar);
        mHeightSeekBar.setProgress(mCardView.getLayoutParams().height);
        mHeightSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);

        mElevationSeekBar = (SeekBar) findViewById(R.id.elevation_seek_bar);
        mElevationSeekBar.setProgress((int) mCardView.getCardElevation());
        mElevationSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);

        mMaxElevationSeekBar = (SeekBar) findViewById(R.id.max_elevation_seek_bar);
        mMaxElevationSeekBar.setProgress((int) mCardView.getMaxCardElevation());
        mMaxElevationSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);

        mAlphaSeekBar = (SeekBar) findViewById(R.id.alpha_seek_bar);
        mAlphaSeekBar.setProgress((int) ViewCompat.getAlpha(mCardView) * 255);
        mAlphaSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);

        RadioGroup rb = (RadioGroup) findViewById(R.id.select_target_radio);
        rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mResizeCardView = checkedId == R.id.resize_card_view;
                update();
            }
        });

        update();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                View content = findViewById(android.R.id.content);
                mWidthSeekBar.setMax(content.getWidth());
                mHeightSeekBar.setMax(content.getHeight());
            }
        }, 100);
    }

}