Android Open Source - MeasureVerify Main Activity






From Project

Back to project page MeasureVerify.

License

The source code is released under:

GNU General Public License

If you think the Android project MeasureVerify listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.measureverify;
//  w  w  w.ja va 2s.c  o m
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    public static final String TAG = "MainActivity";
    private View mLayout;
    private ListView mListView;
    private MenuListAdapter mListViewAdapter;
    private boolean setLayoutParamsProgrammatically = false;
    private boolean unsafetyInflate = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button triggerButton = (Button) findViewById(R.id.trigger);
        triggerButton.setOnClickListener(this);
        Button measureButton = (Button) findViewById(R.id.measure);
        measureButton.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.listview);
        mListViewAdapter = new MenuListAdapter(this);
        mListView.setAdapter(mListViewAdapter);
        addListenerOnChkSetLayoutParamsProgrammatically();
        addListenerOnChkUnSafetyInflate();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item != null && item.getItemId() == R.id.about) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("About " + getString(R.string.app_name));
            
            CheckBox checkBox = (CheckBox) findViewById(R.id.unsafety_inflate);
            checkBox.setChecked(false);
            unsafetyInflate = false;
            
            final TextView message = new TextView(this);
            // i.e.: R.string.dialog_message =>
            // "Test this dialog following the link to dtmilano.blogspot.com"
            final SpannableString s = new SpannableString("https://github.com/faywong");
            Linkify.addLinks(s, Linkify.ALL);
            message.setText(s);
            message.setMovementMethod(LinkMovementMethod.getInstance());

            AlertDialog d = builder.setCancelable(true)
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setPositiveButton(R.string.dialog_action_dismiss, null)
                    .setView(message)
                    .create();
            d.show();
            //((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
            return true;
        }
        return false;
    }

    public void addListenerOnChkSetLayoutParamsProgrammatically() {

        CheckBox checkBox = (CheckBox) findViewById(R.id.set_layoutparams_programmatically);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                MainActivity.this.setLayoutParamsProgrammatically = isChecked;
            }
        });

    }

    public void addListenerOnChkUnSafetyInflate() {

        CheckBox checkBox = (CheckBox) findViewById(R.id.unsafety_inflate);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                MainActivity.this.unsafetyInflate = isChecked;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class MenuListAdapter extends ArrayAdapter<ListItem> {
        private Activity context;

        public MenuListAdapter(Activity context) {
            super(context, 0);
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                if (unsafetyInflate) {
                    // risk inflate case
                    convertView = LayoutInflater.from(context).inflate(
                            R.layout.custom_infowindow, null);
                } else {
                    // safety inflate case(### Solution 1 ###)
                    // The second parameter "mListView" provides a set of
                    // LayoutParams values for root of the returned hierarchy
                    convertView = LayoutInflater.from(context).inflate(
                            R.layout.custom_infowindow, mListView, false);
                }

                // ### Solution 2 ###
                if (MainActivity.this.setLayoutParamsProgrammatically) {
                    // NOTE: the layout params set here should be of the
                    // {ParentView}.LayoutParams
                    convertView
                            .setLayoutParams(new ListView.LayoutParams(
                                    ListView.LayoutParams.WRAP_CONTENT,
                                    ListView.LayoutParams.WRAP_CONTENT));
                }
                Log.d(TAG, "case 1 parent:" + convertView.getParent() + " layoutParams:"
                        + convertView.getLayoutParams());
                final int width = context.getWindow().getDecorView().getWidth();
                final int height = context.getWindow().getDecorView().getHeight();
                convertView.measure(width, height);
                MainActivity.this.mLayout = convertView;
            }
            final ListItem item = (ListItem) getItem(position);
            TextView title = (TextView) convertView.findViewById(R.id.title);

            title.setText("title " + Math.random() + item.prop_1);
            TextView snippet = (TextView) convertView.findViewById(R.id.snippet);
            snippet.setText("snippet " + Math.random() + item.prop_2);
            return convertView;
        }

    }

    public static class ListItem {
        public String prop_1;
        public String prop_2;

        public ListItem(String prop1, String prop2) {
            this.prop_1 = prop1;
            this.prop_2 = prop2;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == null) {
            return;
        }
        final int id = v.getId();
        if (id == R.id.trigger) {
            Log.d("Measure Verify", "addView() called!");
            mListViewAdapter.add(new ListItem("faywong", "wonderful"));
        } else if (id == R.id.measure && mLayout != null) {

            final int width = getWindow().getDecorView().getWidth();
            final int height = getWindow().getDecorView().getHeight();
            mLayout.measure(width, height);
            Log.d("Measure Verify", "RelativeLayout measure() called!");

            Log.d(TAG,
                    "case 2 parent:" + mLayout.getParent() + " layoutParams:"
                            + mLayout.getLayoutParams());

            mListView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            Log.d("Measure Verify", "ListView measure() called!");
        }
    }

}




Java Source Code List

com.example.measureverify.MainActivity.java