Example usage for android.widget TableLayout setStretchAllColumns

List of usage examples for android.widget TableLayout setStretchAllColumns

Introduction

In this page you can find the example usage for android.widget TableLayout setStretchAllColumns.

Prototype

public void setStretchAllColumns(boolean stretchAllColumns) 

Source Link

Document

Convenience method to mark all columns as stretchable.

Usage

From source file:com.svpino.longhorn.artifacts.StockTileProcessor.java

public static void create(Fragment fragment, TableLayout tableLayout, List<Stock> stocks,
        SparseArray<View> tiles, boolean restart) {

    if (restart) {
        tableLayout.removeAllViews();// w  w w.  j a  v a2  s.  co m
        if (tiles != null) {
            tiles.clear();
        }
    }

    tableLayout.setStretchAllColumns(true);
    tableLayout.setShrinkAllColumns(true);

    int margin = Extensions.dpToPixels(fragment.getResources(), 3);
    int height = Extensions.dpToPixels(fragment.getResources(), 90);

    int index = createFixedHeaderRow(fragment, tableLayout, stocks, tiles, height, margin);

    int row = index == 3 ? 1 : 0;

    while (index < stocks.size()) {
        index = createStandardRow(fragment, tableLayout, stocks, tiles, height, margin, index, row);
        row++;
    }

    while (tableLayout.getChildCount() > row) {
        tableLayout.removeViewAt(tableLayout.getChildCount() - 1);
    }

    if (stocks.size() % 2 != 0) {
        TableRow tableRow = new TableRow(fragment.getActivity());

        View addNewStockTile = createTileForAddingNewStock(fragment);
        tableRow.addView(addNewStockTile, getSpannedLayoutParams(row, margin, height));

        tableLayout.addView(tableRow);
    } else {
        TableRow tableRow = (TableRow) tableLayout.getChildAt(tableLayout.getChildCount() - 1);
        LayoutParams layoutParams = (TableRow.LayoutParams) tableRow.getChildAt(0).getLayoutParams();
        layoutParams.bottomMargin = margin;
        layoutParams.height = height;
    }
}

From source file:info.semanticsoftware.semassist.android.activity.SemanticResultsActivity.java

/** Presents the results in a list format.
 * @param savedInstanceState saved instance state
 *///  w  w  w. j a  v a 2 s  .c om
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String resultsXML = getIntent().getStringExtra("xml");
    Vector<SemanticServiceResult> results = ClientUtils.getServiceResults(resultsXML);
    setContentView(R.layout.results);

    TableLayout tblResults = (TableLayout) findViewById(R.id.tblResultsLayout);
    tblResults.setStretchAllColumns(true);

    TableRow resultRow;
    TextView txtContent;
    TextView txtType;
    TextView txtStart;
    TextView txtEnd;
    TextView txtFeats;

    if (results == null) {
        // handle server errors
    } else {
        for (SemanticServiceResult current : results) {
            if (current.mResultType.equals(SemanticServiceResult.ANNOTATION)) {

                List<AnnotationInstance> annots = ServerResponseHandler.createAnnotation(current);
                for (int i = 0; i < annots.size(); i++) {
                    resultRow = new TableRow(getApplicationContext());

                    txtContent = new TextView(getApplicationContext());
                    txtContent.setText(annots.get(i).getContent());
                    txtContent.setTextAppearance(getApplicationContext(), R.style.normalText);
                    resultRow.addView(txtContent);

                    txtType = new TextView(getApplicationContext());
                    txtType.setText(annots.get(i).getType());
                    txtType.setTextAppearance(getApplicationContext(), R.style.normalText);
                    resultRow.addView(txtType);

                    txtStart = new TextView(getApplicationContext());
                    txtStart.setText(annots.get(i).getStart());
                    txtStart.setTextAppearance(getApplicationContext(), R.style.normalText);
                    resultRow.addView(txtStart);

                    txtEnd = new TextView(getApplicationContext());
                    txtEnd.setText(annots.get(i).getEnd());
                    txtEnd.setTextAppearance(getApplicationContext(), R.style.normalText);
                    resultRow.addView(txtEnd);

                    txtFeats = new TextView(getApplicationContext());
                    txtFeats.setText(annots.get(i).getFeatures());
                    txtFeats.setTextAppearance(getApplicationContext(), R.style.normalText);
                    resultRow.addView(txtFeats);

                    tblResults.addView(resultRow);
                }
            } else if (current.mResultType.equals(SemanticServiceResult.BOUNDLESS_ANNOTATION)) {
                //TODO find an actual pipeline to test this with
            } else if (current.mResultType.equals(SemanticServiceResult.FILE)) {
                fileName = current.mFileUrl;
                fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
                Log.d(Constants.TAG, fileName);
                getFileContentTask task = new getFileContentTask();
                task.execute(SemanticAssistantsActivity.serverURL);
            }
        }

        // reduce the number of allowed requests by one
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String strReqNum = settings.getString("reqNum", "0");
        try {
            int intReqNum = Integer.parseInt(strReqNum);
            Editor editor = settings.edit();
            intReqNum--;
            editor.putString("reqNum", Integer.toString(intReqNum));
            boolean result = editor.commit();
            if (result) {
                Log.d(Constants.TAG, "Successfully reduced the reqNum");
            } else {
                Log.d(Constants.TAG, "Cannot reduced the reqNum");
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    } //else

}

From source file:com.cssweb.android.quote.QuoteDetail.java

/**
 * /*w w w . ja v a  2  s .c  om*/
 * @param jArr
 * @param table
 * @param zrsp
 * @throws JSONException
 */
private void appendRow(JSONArray jArr, TableLayout table, double zrsp) throws JSONException {
    table.setStretchAllColumns(true);
    table.removeAllViews();

    if ("cf".equals(exchange.toLowerCase()) || "dc".equals(exchange.toLowerCase())
            || "sf".equals(exchange.toLowerCase()) || "cz".equals(exchange.toLowerCase())) {
        int len = jArr.length();
        int color = Utils.getTextColor(mContext, 0);
        for (int i = 0; i < len; i++) {
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.title23);
            linearLayout.setVisibility(View.VISIBLE);

            JSONArray jA = (JSONArray) jArr.get(i);
            TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.zr_quote_price_item, null);
            TextView t1 = (TextView) row.findViewById(R.id.zr_table_col1);
            TextView t2 = (TextView) row.findViewById(R.id.zr_table_col2);
            TextView t3 = (TextView) row.findViewById(R.id.zr_table_col3);
            TextView t4 = (TextView) row.findViewById(R.id.zr_table_col4);
            TextView t5 = (TextView) row.findViewById(R.id.zr_table_col5);
            t1.setText(jA.getString(4));
            t2.setText(Utils.dataFormation(jA.getDouble(0), Utils.getStockDigit(type)));
            t2.setTextColor(Utils.getTextColor(mContext, jA.getDouble(0), zrsp));
            t2.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            double cjsl = Arith.round(jA.getDouble(1), 0);
            t3.setText(Utils.dataFormation(cjsl, 0));
            t3.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            t4.setText(jA.getString(7));
            t5.setText(jA.getString(8));
            table.addView(row);
            if ("B".equals(jA.getString(6))) {
                color = Utils.getTextColor(mContext, 3);
                t3.setTextColor(color);
                t4.setTextColor(color);
                t5.setTextColor(color);
            } else if ("S".equals(jA.getString(6))) {
                color = Utils.getTextColor(mContext, 4);
                t3.setTextColor(color);
                t4.setTextColor(color);
                t5.setTextColor(color);
            } else {
                t3.setTextColor(color);
                t4.setTextColor(color);
                t5.setTextColor(color);
            }
        }
    } else {
        if (stocktype.charAt(0) == '0') {//
            //for (int i = jArr.length()-1; i >= 0; i--) {
            for (int i = 0; i <= jArr.length() - 1; i++) {
                JSONArray jA = (JSONArray) jArr.get(i);
                TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.zr_quote_price_item, null);
                TextView t1 = (TextView) row.findViewById(R.id.zr_table_col1);
                TextView t2 = (TextView) row.findViewById(R.id.zr_table_col2);
                TextView t3 = (TextView) row.findViewById(R.id.zr_table_col3);
                TextView t4 = (TextView) row.findViewById(R.id.zr_table_col4);
                t1.setText(jA.getString(4));
                t2.setText(Utils.dataFormation(jA.getDouble(0), Utils.getStockDigit(type)));
                t2.setTextColor(Utils.getTextColor(mContext, jA.getDouble(0), zrsp));
                t2.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                double cjsl = Arith.round(jA.getDouble(1), 0);
                t3.setText(Utils.dataFormation(cjsl, 0));
                if (cjsl > 500)
                    t3.setTextColor(Utils.getTextColor(mContext, 6));
                else
                    t3.setTextColor(Utils.getTextColor(mContext, 1));
                t3.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                t4.setText(jA.getString(6));
                if ("B".equals(jA.getString(6)))
                    t4.setTextColor(Utils.getTextColor(mContext, 3));
                else
                    t4.setTextColor(Utils.getTextColor(mContext, 4));
                table.addView(row);
            }
            //?
            int num = jArr.length();
            if (num < 16) {
                while (num < 16) {
                    TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.zr_quote_price_item,
                            null);
                    table.addView(row);
                    num++;
                }
            }
        } else if (stocktype.charAt(0) == '1' || stocktype.charAt(0) == '2') {//
            //for (int i = jArr.length()-1; i >= 0; i--) {
            for (int i = 0; i <= jArr.length() - 1; i++) {
                JSONArray jA = (JSONArray) jArr.get(i);
                TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.zr_quote_price_item, null);
                TextView t1 = (TextView) row.findViewById(R.id.zr_table_col1);
                TextView t2 = (TextView) row.findViewById(R.id.zr_table_col2);
                TextView t3 = (TextView) row.findViewById(R.id.zr_table_col4);
                t1.setText(jA.getString(4));
                t2.setText(Utils.dataFormation(jA.getDouble(0), Utils.getStockDigit(type)));
                t2.setTextColor(Utils.getTextColor(mContext, jA.getDouble(0), zrsp));
                t3.setText(Utils.getAmountFormat(jA.getDouble(5), true));
                t3.setTextColor(Utils.getTextColor(mContext, 1));
                table.addView(row);
            }
            //?
            int num = jArr.length();
            if (num < 16) {
                while (num < 16) {
                    TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.zr_quote_price_item,
                            null);
                    table.addView(row);
                    num++;
                }
            }
        }
    }

}

From source file:org.mklab.mikity.android.editor.AbstractObjectEditor.java

/**
 * {@inheritDoc}//www.  j  a va2s. co m
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_object_editor, container, false);

    this.objectType = (TextView) view.findViewById(R.id.objectType);

    final TableLayout parameters = ((TableLayout) view.findViewById(R.id.layout_parameters));
    if (parameters != null) {
        parameters.removeAllViews();
        parameters.setStretchAllColumns(true);
    }

    createColorBoxes(parameters);
    createParameterBoxes(parameters);
    createTranslationBoxes(parameters);
    createRotationBoxes(parameters);

    // Next line should be located after setting the parameters in the boxes
    createButtonComposite(view);

    return view;
}

From source file:org.mklab.mikity.android.editor.GroupEditor.java

private void createCoordinateParameterBoxes(final View view) {
    final TableLayout parameters = ((TableLayout) view.findViewById(R.id.coordinate_parameters));
    parameters.removeAllViews();//from w ww . j  a  v a 2 s  .com
    parameters.setStretchAllColumns(true);

    final TableRow head = new TableRow(getContext());
    final TextView name = new TextView(getContext());
    name.setText(R.string.name);
    name.setTextColor(Color.BLACK);
    final TextView value = new TextView(getContext());
    value.setText(R.string.value);
    value.setTextColor(Color.BLACK);
    head.addView(name);
    head.addView(value);
    parameters.addView(head);

    createCoordinateTranslationBoxes(parameters);
    createCoordinateRotationBoxes(parameters);
}

From source file:org.mklab.mikity.android.editor.GroupEditor.java

private void createAnimationParameterBoxes(final View view) {
    final TableLayout parameters = ((TableLayout) view.findViewById(R.id.animation_parameters));
    parameters.removeAllViews();/* w w  w.ja v a2s .  c  o  m*/
    parameters.setStretchAllColumns(true);

    final TableRow head = new TableRow(getContext());
    parameters.addView(head);

    final TextView name = new TextView(getContext());
    name.setText(R.string.name);
    name.setTextColor(Color.BLACK);
    head.addView(name);

    final TextView id = new TextView(getContext());
    id.setText(R.string.id);
    id.setTextColor(Color.BLACK);
    head.addView(id);

    final TextView number = new TextView(getContext());
    number.setText(R.string.number);
    number.setTextColor(Color.BLACK);
    head.addView(number);

    createAnimationTranslationBoxes(parameters);
    createAnimationRotationBoxes(parameters);

    setAnimationInDialog();
}

From source file:com.example.drugsformarinemammals.Dose_Information.java

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

    Bundle parameters = this.getIntent().getExtras();
    if (parameters != null) {
        TextView textViewDrug = (TextView) findViewById(R.id.textView_drug_name);
        textViewDrug.setTypeface(Typeface.SANS_SERIF);
        textViewDrug.setText(parameters.getString("drugName"));
        TextView textViewGroupName = (TextView) findViewById(R.id.textView_group_name);
        textViewGroupName.setTypeface(Typeface.SANS_SERIF);
        textViewGroupName.setText("(" + parameters.getString("groupName") + ")");
        layoutDose = (LinearLayout) findViewById(R.id.layout_dose);

        helper = new Handler_Sqlite(this);
        SQLiteDatabase db = helper.open();
        ArrayList<String> notes_index = new ArrayList<String>();
        ArrayList<String> references = new ArrayList<String>();
        ArrayList<Article_Reference> references_index = new ArrayList<Article_Reference>();
        reference_index = 'a';
        ArrayList<String> families = new ArrayList<String>();
        if (db != null)
            families = helper.read_animals_family(parameters.getString("drugName"),
                    parameters.getString("groupName"));

        for (int l = 0; l < families.size(); l++) {
            //if exists animals family

            TextView textView_family = new TextView(this);
            textView_family.setText(families.get(l));
            textView_family.setTextSize(20);
            textView_family.setTextColor(getResources().getColor(R.color.darkGray));
            textView_family.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            params.leftMargin = 30;//w w  w  . j  a va2  s .c  o m
            params.topMargin = 20;
            layoutDose.addView(textView_family, layoutDose.getChildCount(), params);

            //dose information

            LinearLayout layout_dose_information = new LinearLayout(this);
            layout_dose_information.setOrientation(LinearLayout.VERTICAL);
            layout_dose_information
                    .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            layout_dose_information.setBackgroundResource(R.drawable.layout_border);

            ArrayList<Dose_Data> dose = new ArrayList<Dose_Data>();
            if (db != null) {
                dose = helper.read_dose_information(parameters.getString("drugName"),
                        parameters.getString("groupName"), families.get(l), "", "");
            }
            TableLayout doseTable = new TableLayout(this);
            doseTable.setStretchAllColumns(true);

            screenWidth = Integer.parseInt(getString(R.string.display));

            TableRow header = new TableRow(this);

            //Amount

            TextView textView_dose_amount = new TextView(this);
            textView_dose_amount.setText("Dose");
            textView_dose_amount.setSingleLine(true);
            textView_dose_amount.setTextColor(getResources().getColor(R.color.darkGray));
            textView_dose_amount.setTextSize(17);
            textView_dose_amount.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
            TableRow.LayoutParams paramsAmount = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            paramsAmount.gravity = Gravity.CENTER;
            header.addView(textView_dose_amount, paramsAmount);

            //Posology
            TextView textView_posology = new TextView(this);
            if (screenWidth >= 720)
                textView_posology.setText("Posology");
            else
                textView_posology.setText("Pos");
            textView_posology.setSingleLine(true);
            textView_posology.setTextColor(getResources().getColor(R.color.darkGray));
            textView_posology.setTextSize(17);
            textView_posology.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
            TableRow.LayoutParams paramsPosology = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            paramsPosology.gravity = Gravity.CENTER;
            if ((screenWidth < 600 && !isCollapsed(parameters.getString("drugName"),
                    parameters.getString("groupName"), families.get(l), "Posology")) || screenWidth >= 600)
                header.addView(textView_posology, paramsPosology);

            //Route

            TextView textView_route = new TextView(this);
            textView_route.setText("Route");
            textView_route.setSingleLine(true);
            textView_route.setTextColor(getResources().getColor(R.color.darkGray));
            textView_route.setTextSize(17);
            textView_route.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
            TableRow.LayoutParams paramsRoute = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            paramsRoute.gravity = Gravity.CENTER;
            header.addView(textView_route, paramsRoute);

            //Reference

            TextView textView_reference = new TextView(this);
            textView_reference.setText("Ref");
            textView_reference.setSingleLine(true);
            textView_reference.setTextColor(getResources().getColor(R.color.darkGray));
            textView_reference.setTextSize(17);
            textView_reference.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
            TableRow.LayoutParams paramsReference = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
            paramsReference.gravity = Gravity.CENTER;
            header.addView(textView_reference, paramsReference);

            //Specific Note

            TextView textView_specific_note = new TextView(this);
            textView_specific_note.setText("Note");
            textView_specific_note.setSingleLine(true);
            textView_specific_note.setTextColor(getResources().getColor(R.color.darkGray));
            textView_specific_note.setTextSize(17);
            textView_specific_note.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
            TableRow.LayoutParams paramsSpecificNote = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
            paramsSpecificNote.gravity = Gravity.CENTER;
            if ((screenWidth < 600 && !isCollapsed(parameters.getString("drugName"),
                    parameters.getString("groupName"), families.get(l), "Note")) || screenWidth >= 600)
                header.addView(textView_specific_note, paramsSpecificNote);

            TableRow doseData = new TableRow(this);
            doseTable.addView(header);

            //General Dose

            if (dose.size() > 0) {
                show_dose(dose, doseTable, doseData, parameters.getString("drugName"),
                        parameters.getString("groupName"), families.get(l), "", "", notes_index, references,
                        references_index);
            }

            HashMap<String, ArrayList<String>> animal_information = new HashMap<String, ArrayList<String>>();
            if (db != null)
                animal_information = helper.read_animal_information(parameters.getString("drugName"),
                        parameters.getString("groupName"), families.get(l));

            String animalName;
            Object[] animalsName = animal_information.keySet().toArray();
            for (int i = 0; i < animalsName.length; i++) {
                doseData = new TableRow(this);

                //if exists animal name
                animalName = (String) animalsName[i];
                TextView textView_animal_name = new TextView(this);
                if (!animalName.equals("")) {

                    //Animal name

                    textView_animal_name.setText(animalName);
                    textView_animal_name.setSingleLine(false);
                    textView_animal_name.setTextColor(getResources().getColor(R.color.darkGray));
                    textView_animal_name.setTextSize(15);
                    textView_animal_name.setTypeface(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
                }

                //if exists category
                ArrayList<String> categories = animal_information.get(animalName);
                String animalCategory;
                for (int j = 0; j < categories.size(); j++) {

                    animalCategory = categories.get(j);

                    if (!animalCategory.equals("")) {

                        //Animal category

                        TextView textView_animal_category = new TextView(this);
                        textView_animal_category.setText(animalCategory);
                        textView_animal_category.setSingleLine(false);
                        textView_animal_category.setTextColor(Color.BLACK);
                        textView_animal_category.setTextSize(15);
                        textView_animal_category.setTypeface(Typeface.SANS_SERIF);
                        if (!animalName.equals("")) {
                            if (j == 0) {
                                TableRow.LayoutParams paramsAnimalName = new TableRow.LayoutParams(
                                        TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
                                if (screenWidth < 600
                                        && isCollapsed(parameters.getString("drugName"),
                                                parameters.getString("groupName"), families.get(l), "Posology")
                                        && isCollapsed(parameters.getString("drugName"),
                                                parameters.getString("groupName"), families.get(l), "Note"))
                                    paramsAnimalName.span = 3;
                                else if (screenWidth < 600 && (isCollapsed(parameters.getString("drugName"),
                                        parameters.getString("groupName"), families.get(l), "Posology")
                                        || isCollapsed(parameters.getString("drugName"),
                                                parameters.getString("groupName"), families.get(l), "Note")))
                                    paramsAnimalName.span = 4;
                                else
                                    paramsAnimalName.span = 5;

                                doseData.addView(textView_animal_name, paramsAnimalName);
                                doseTable.addView(doseData);
                            }

                            if (db != null)
                                dose = helper.read_dose_information(parameters.getString("drugName"),
                                        parameters.getString("groupName"), families.get(l), animalName,
                                        animalCategory);

                            doseData = new TableRow(this);
                            textView_animal_category.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);
                            TableRow.LayoutParams paramsCategoryName = new TableRow.LayoutParams(
                                    TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
                            if (screenWidth < 600
                                    && isCollapsed(parameters.getString("drugName"),
                                            parameters.getString("groupName"), families.get(l), "Posology")
                                    && isCollapsed(parameters.getString("drugName"),
                                            parameters.getString("groupName"), families.get(l), "Note"))
                                paramsCategoryName.span = 3;
                            else if (screenWidth < 600 && (isCollapsed(parameters.getString("drugName"),
                                    parameters.getString("groupName"), families.get(l), "Posology")
                                    || isCollapsed(parameters.getString("drugName"),
                                            parameters.getString("groupName"), families.get(l), "Note")))
                                paramsCategoryName.span = 4;
                            else
                                paramsCategoryName.span = 5;
                            if (screenWidth < 600)
                                paramsCategoryName.leftMargin = 15;
                            else if (screenWidth >= 600 && screenWidth < 720)
                                paramsCategoryName.leftMargin = 20;
                            else
                                paramsCategoryName.leftMargin = 30;
                            doseData.addView(textView_animal_category, paramsCategoryName);
                            doseTable.addView(doseData);
                            doseData = new TableRow(this);
                            show_dose(dose, doseTable, doseData, parameters.getString("drugName"),
                                    parameters.getString("groupName"), families.get(l), animalName,
                                    animalCategory, notes_index, references, references_index);

                            doseData = new TableRow(this);
                        } else {
                            if (db != null)
                                dose = helper.read_dose_information(parameters.getString("drugName"),
                                        parameters.getString("groupName"), families.get(l), animalName,
                                        animalCategory);

                            textView_animal_category.setTypeface(Typeface.SANS_SERIF,
                                    Typeface.DEFAULT_BOLD.getStyle());
                            textView_animal_category.setTextColor(getResources().getColor(R.color.darkGray));
                            TableRow.LayoutParams paramsCategoryName = new TableRow.LayoutParams(
                                    TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
                            if (screenWidth < 600
                                    && isCollapsed(parameters.getString("drugName"),
                                            parameters.getString("groupName"), families.get(l), "Posology")
                                    && isCollapsed(parameters.getString("drugName"),
                                            parameters.getString("groupName"), families.get(l), "Note"))
                                paramsCategoryName.span = 3;
                            else if (screenWidth < 600 && (isCollapsed(parameters.getString("drugName"),
                                    parameters.getString("groupName"), families.get(l), "Posology")
                                    || isCollapsed(parameters.getString("drugName"),
                                            parameters.getString("groupName"), families.get(l), "Note")))
                                paramsCategoryName.span = 4;
                            else
                                paramsCategoryName.span = 5;
                            doseData.addView(textView_animal_category, paramsCategoryName);
                            doseTable.addView(doseData);
                            doseData = new TableRow(this);
                            show_dose(dose, doseTable, doseData, parameters.getString("drugName"),
                                    parameters.getString("groupName"), families.get(l), animalName,
                                    animalCategory, notes_index, references, references_index);

                            doseData = new TableRow(this);
                        }

                    }

                    if (!animalName.equals("") && animalCategory.equals("")) {
                        if (db != null)
                            dose = helper.read_dose_information(parameters.getString("drugName"),
                                    parameters.getString("groupName"), families.get(l), animalName,
                                    animalCategory);

                        TableRow.LayoutParams paramsAnimalName = new TableRow.LayoutParams(
                                TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
                        if (screenWidth < 600
                                && isCollapsed(parameters.getString("drugName"),
                                        parameters.getString("groupName"), families.get(l), "Posology")
                                && isCollapsed(parameters.getString("drugName"),
                                        parameters.getString("groupName"), families.get(l), "Note"))
                            paramsAnimalName.span = 3;
                        else if (screenWidth < 600 && (isCollapsed(parameters.getString("drugName"),
                                parameters.getString("groupName"), families.get(l), "Posology")
                                || isCollapsed(parameters.getString("drugName"),
                                        parameters.getString("groupName"), families.get(l), "Note")))
                            paramsAnimalName.span = 4;
                        else
                            paramsAnimalName.span = 5;
                        doseData.addView(textView_animal_name, paramsAnimalName);
                        doseTable.addView(doseData);
                        doseData = new TableRow(this);
                        show_dose(dose, doseTable, doseData, parameters.getString("drugName"),
                                parameters.getString("groupName"), families.get(l), animalName, animalCategory,
                                notes_index, references, references_index);

                        doseData = new TableRow(this);
                    }
                }

            }

            LinearLayout.LayoutParams paramsDoseTable = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if (screenWidth >= 600) {
                paramsDoseTable.topMargin = 5;
                paramsDoseTable.leftMargin = 50;
                paramsDoseTable.rightMargin = 50;
            } else {
                paramsDoseTable.topMargin = 5;
                paramsDoseTable.leftMargin = 60;
                paramsDoseTable.rightMargin = 30;
            }
            layout_dose_information.addView(doseTable, paramsDoseTable);

            layoutDose.addView(layout_dose_information, layoutDose.getChildCount());
        }
        helper.close();

        //Notes
        additionalInformationInterface("GENERAL NOTES", parameters.getString("drugName"),
                parameters.getString("groupName"), notes_index, references_index);
        additionalInformationInterface("SPECIFIC NOTES", parameters.getString("drugName"),
                parameters.getString("groupName"), notes_index, references_index);
        //References
        additionalInformationInterface("REFERENCES", parameters.getString("drugName"),
                parameters.getString("groupName"), notes_index, references_index);

    }

}

From source file:edu.ksu.cs.a4vm.bse.MorphologyCount.java

@Override
public void onResume() {
    super.onResume();
    test = "Resume called...";
    final MediaPlayer btnChangeSound = MediaPlayer.create(getApplicationContext(), R.raw.button_changed);
    final MediaPlayer limitRchdSound = MediaPlayer.create(getApplicationContext(), R.raw.limit_reached);
    initVals = (HashSet<String>) SharedPrefUtil.getValue(getApplicationContext(),
            Constant.PREFS_BULL_MORPHOLOGY_INFO, morphKey);
    /*morphologyLabels = (HashSet<String>) SharedPrefUtil.getValue(getApplicationContext(),
        Constant.PREFS_FILE_MORPH_INFO,Constant.KEY_MORPHOLOGY);*/
    morphologyLabels = (HashSet<String>) SharedPrefUtil.getValue(getApplicationContext(),
            Constant.PREFS_GRP_MORPH_CONFIG, grpKey);
    TableLayout table = new TableLayout(this);
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    table.setLayoutParams(lp);/*from   w  ww.j ava 2  s  . c o  m*/
    table.setShrinkAllColumns(true);
    table.setStretchAllColumns(true);

    TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 0.5f);
    TableRow.LayoutParams cellLp = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);

    TableRow.LayoutParams cell1Lp = new TableRow.LayoutParams(60, 120, 1.0f);

    if (initVals != null) {
        for (String Val : initVals) {
            String[] values = Val.split("=");
            if (values != null && values.length == 2 && values[0].equalsIgnoreCase("Normal")) {
                NormalCount = Integer.valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                NormalProp = Double.valueOf(values[1].trim().substring(values[1].trim().indexOf("(") + 1,
                        values[1].trim().indexOf("%")));
            } else if (morphologyLabels != null) {
                for (String label : morphologyLabels) {
                    String[] lbls = label.split("=");
                    if (lbls != null && lbls.length == 2 && lbls[1].equalsIgnoreCase(values[0])) {
                        if (lbls[0].equalsIgnoreCase("Morphology Field 2")) {
                            Lb2Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb2Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        } else if (lbls[0].equalsIgnoreCase("Morphology Field 3")) {
                            Lb3Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb3Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        } else if (lbls[0].equalsIgnoreCase("Morphology Field 4")) {
                            Lb4Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb4Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        } else if (lbls[0].equalsIgnoreCase("Morphology Field 5")) {
                            Lb5Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb5Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        } else if (lbls[0].equalsIgnoreCase("Morphology Field 6")) {
                            Lb6Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb6Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        } else if (lbls[0].equalsIgnoreCase("Morphology Field 7")) {
                            Lb7Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb7Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        } else if (lbls[0].equalsIgnoreCase("Morphology Field 8")) {
                            Lb8Count = Integer
                                    .valueOf(values[1].trim().substring(0, values[1].trim().indexOf("(")));
                            Lb8Prop = Double.valueOf(values[1].trim().substring(
                                    values[1].trim().indexOf("(") + 1, values[1].trim().indexOf("%")));
                        }
                    }
                }
            }
        }
    }

    Constant.sum = NormalCount + Lb2Count + Lb3Count + Lb4Count + Lb5Count + Lb6Count + Lb7Count + Lb8Count;

    row = new TableRow(this);
    btn1 = new Button(this);
    btn1.setId(R.id.button1);
    btn1.setText("Normal:" + NormalCount + "(" + String.format("%.2f", NormalProp) + "%)");
    btn1.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button));
    btn1.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
    row.addView(btn1, cellLp);
    table.addView(row, rowLp);
    setContentView(table);
    if (morphologyLabels != null) {
        Iterator<String> it;

        TableRow row2 = new TableRow(this);
        TableRow row3 = new TableRow(this);
        TableRow row4 = new TableRow(this);
        TableRow row5 = new TableRow(this);

        it = morphologyLabels.iterator();
        while (it.hasNext()) {
            String label = it.next();
            String text[] = label.split("=");
            if (label.contains("Morphology Field 2")) {
                btn2 = new Button(this);
                btn2.setId(R.id.button2);
                if (text != null && text.length == 2) {
                    btn2.setText(text[1] + ":" + Lb2Count + "(" + String.format("%.2f", Lb2Prop) + "%)");
                }
                btn2.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button2));
                btn2.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                btn2.setHeight(300);
                row2.addView(btn2);
            } else if (label.contains("Morphology Field 4")) {
                btn4 = new Button(this);
                btn4.setId(R.id.button4);
                if (text != null && text.length == 2) {
                    btn4.setText(text[1] + ":" + Lb4Count + "(" + String.format("%.2f", Lb4Prop) + "%)");
                }
                btn4.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button4));
                btn4.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                btn4.setHeight(300);
                row3.addView(btn4);
            } else if (label.contains("Morphology Field 6")) {
                btn6 = new Button(this);
                btn6.setId(R.id.button6);
                if (text != null && text.length == 2) {
                    btn6.setText(text[1] + ":" + Lb6Count + "(" + String.format("%.2f", Lb6Prop) + "%)");
                }
                btn6.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button6));
                btn6.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                btn6.setHeight(300);
                row4.addView(btn6);
            } else if (label.contains("Morphology Field 8")) {
                btn8 = new Button(this);
                btn8.setId(R.id.button8);
                if (text != null && text.length == 2) {
                    btn8.setText(text[1] + ":" + Lb8Count + "(" + String.format("%.2f", Lb8Prop) + "%)");
                }
                btn8.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button8));
                btn8.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                //btn = new Button(this);
                btn8.setHeight(300);
                row5.addView(btn8);
                //row5.addView(btn);
            }
        }

        it = morphologyLabels.iterator();
        while (it.hasNext()) {
            String label = it.next();
            String text[] = label.split("=");
            if (label.contains("Limit")) {
                if (text != null && text.length == 2) {
                    limit = Integer.valueOf(text[1]);
                }
            } else if (label.contains("Morphology Field 3")) {
                btn3 = new Button(this);
                btn3.setId(R.id.button3);
                if (text != null && text.length == 2) {
                    btn3.setText(text[1] + ":" + Lb3Count + "(" + String.format("%.2f", Lb3Prop) + "%)");
                }
                btn3.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button3));
                btn3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                btn3.setHeight(300);
                row2.addView(btn3);
            } else if (label.contains("Morphology Field 5")) {
                btn5 = new Button(this);
                btn5.setId(R.id.button5);
                if (text != null && text.length == 2) {
                    btn5.setText(text[1] + ":" + Lb5Count + "(" + String.format("%.2f", Lb5Prop) + "%)");
                }
                btn5.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button5));
                btn5.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                btn5.setHeight(300);
                row3.addView(btn5);
            } else if (label.contains("Morphology Field 7")) {
                btn7 = new Button(this);
                btn7.setId(R.id.button7);
                if (text != null && text.length == 2) {
                    btn7.setText(text[1] + ":" + Lb7Count + "(" + String.format("%.2f", Lb7Prop) + "%)");
                }
                btn7.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.button7));
                btn7.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
                btn7.setHeight(300);
                row4.addView(btn7);
            }

        }

        table.addView(row2, rowLp);
        table.addView(row3, rowLp);
        table.addView(row4, rowLp);
        table.addView(row5, rowLp);

    }

    row = new TableRow(this);

    btn = new Button(this);
    btn.setId(R.id.button);
    btn.setText("Edit Morphology Counts");
    btn.setGravity(Gravity.CENTER);
    row.addView(btn, cell1Lp);

    tv = new TextView(this);
    tv.setId(R.id.totals);
    tv.setText("Total Count:" + Constant.sum);
    tv.setGravity(Gravity.CENTER);
    row.addView(tv, cell1Lp);
    //table.addView(row, rowLp);
    //setContentView(table);

    //row = new TableRow(this);

    table.addView(row, rowLp);
    setContentView(table);

    //initializing morphology counts
    if (initVals == null) {
        morphologyCounts = new HashSet<String>();
        if (btn1 != null) {
            String initEntry = btn1.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn2 != null) {
            String initEntry = btn2.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn3 != null) {
            String initEntry = btn3.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn4 != null) {
            String initEntry = btn4.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn5 != null) {
            String initEntry = btn5.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn6 != null) {
            String initEntry = btn6.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn7 != null) {
            String initEntry = btn7.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
        if (btn8 != null) {
            String initEntry = btn8.getText().toString().trim().replace(":", "=");
            morphologyCounts.add(initEntry);
        }
    } else {
        morphologyCounts = new HashSet<String>();
        for (String initVal : initVals) {
            morphologyCounts.add(initVal);
        }
    }

    if (btn != null) {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //SharedPrefUtil.saveGroup(getApplicationContext(),Constant.PREFS_BULL_MORPHOLOGY_INFO,morphKey,morphologyCounts);
                Intent gotoEditCount = new Intent(getApplicationContext(), EditMorphologyCounts.class);
                gotoEditCount.putExtra("morphKey", morphKey);
                startActivity(gotoEditCount);
            }
        });
    }
    if (btn1 != null) {
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //final MediaPlayer btnChangeSound = MediaPlayer.create(getApplicationContext(),R.raw.button_changed);
                if (currentButton == null)
                    currentButton = "btn1";
                else {
                    if (!"btn1".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn1";
                    }
                }
                try {
                    String[] btnStrings = btn1.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    NormalCount = Integer.valueOf(btnCount);
                    NormalProp = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + NormalCount + "(" + String.format("%.2f", NormalProp)
                                + "%" + ")";
                        morphologyCounts.remove(oldTxt);
                        NormalCount = NormalCount + 1;
                        Constant.sum = Constant.sum + 1;
                        NormalProp = (NormalCount * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + NormalCount + "(" + String.format("%.2f", NormalProp)
                                + "%" + ")";
                        btn1.setText(newTxt);
                        newTxt = btnLbl + "=" + NormalCount + "(" + String.format("%.2f", NormalProp) + "%"
                                + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    if (btn2 != null) {
        tv.setText("Total Count:" + Constant.sum);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //final MediaPlayer btnChangeSound = MediaPlayer.create(getApplicationContext(),R.raw.button_changed);
                if (currentButton == null)
                    currentButton = "btn2";
                else {
                    if (!"btn2".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn2";
                    }
                }

                try {
                    String[] btnStrings = btn2.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb2Count = Integer.valueOf(btnCount);
                    Lb2Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb2Count + "(" + String.format("%.2f", Lb2Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb2Count = Lb2Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb2Prop = (Lb2Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb2Count + "(" + String.format("%.2f", Lb2Prop) + "%"
                                + ")";
                        btn2.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb2Count + "(" + String.format("%.2f", Lb2Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if (btn3 != null) {
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentButton == null)
                    currentButton = "btn3";
                else {
                    if (!"btn3".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn3";
                    }
                }

                try {
                    String[] btnStrings = btn3.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb3Count = Integer.valueOf(btnCount);
                    Lb3Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb3Count + "(" + String.format("%.2f", Lb3Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb3Count = Lb3Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb3Prop = (Lb3Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb3Count + "(" + String.format("%.2f", Lb3Prop) + "%"
                                + ")";
                        btn3.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb3Count + "(" + String.format("%.2f", Lb3Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if (btn4 != null) {
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentButton == null)
                    currentButton = "btn4";
                else {
                    if (!"btn4".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn4";
                    }
                }

                try {
                    String[] btnStrings = btn4.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb4Count = Integer.valueOf(btnCount);
                    Lb4Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb4Count + "(" + String.format("%.2f", Lb4Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb4Count = Lb4Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb4Prop = (Lb4Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb4Count + "(" + String.format("%.2f", Lb4Prop) + "%"
                                + ")";
                        btn4.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb4Count + "(" + String.format("%.2f", Lb4Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if (btn5 != null) {
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentButton == null)
                    currentButton = "btn5";
                else {
                    if (!"btn5".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn5";
                    }
                }

                try {
                    String[] btnStrings = btn5.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb5Count = Integer.valueOf(btnCount);
                    Lb5Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb5Count + "(" + String.format("%.2f", Lb5Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb5Count = Lb5Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb5Prop = (Lb5Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb5Count + "(" + String.format("%.2f", Lb5Prop) + "%"
                                + ")";
                        btn5.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb5Count + "(" + String.format("%.2f", Lb5Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if (btn6 != null) {
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentButton == null)
                    currentButton = "btn6";
                else {
                    if (!"btn6".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn6";
                    }
                }

                try {
                    String[] btnStrings = btn6.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb6Count = Integer.valueOf(btnCount);
                    Lb6Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb6Count + "(" + String.format("%.2f", Lb6Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb6Count = Lb6Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb6Prop = (Lb6Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb6Count + "(" + String.format("%.2f", Lb6Prop) + "%"
                                + ")";
                        btn6.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb6Count + "(" + String.format("%.2f", Lb6Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if (btn7 != null) {
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentButton == null)
                    currentButton = "btn7";
                else {
                    if (!"btn7".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn7";
                    }
                }

                try {
                    String[] btnStrings = btn7.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb7Count = Integer.valueOf(btnCount);
                    Lb7Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb7Count + "(" + String.format("%.2f", Lb7Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb7Count = Lb7Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb7Prop = (Lb7Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb7Count + "(" + String.format("%.2f", Lb7Prop) + "%"
                                + ")";
                        btn7.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb7Count + "(" + String.format("%.2f", Lb7Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if (btn8 != null) {
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentButton == null)
                    currentButton = "btn8";
                else {
                    if (!"btn8".equalsIgnoreCase(currentButton)) {
                        //make sound
                        btnChangeSound.start();
                        currentButton = "btn8";
                    }
                }

                try {
                    String[] btnStrings = btn8.getText().toString().trim().split(":");
                    String btnLbl = btnStrings[0].trim();
                    String btnCount = btnStrings[1].trim().substring(0, btnStrings[1].trim().indexOf("("));
                    String btnprop = btnStrings[1].trim().substring(btnStrings[1].trim().indexOf("(") + 1,
                            btnStrings[1].trim().indexOf("%"));

                    Lb8Count = Integer.valueOf(btnCount);
                    Lb8Prop = Double.valueOf(btnprop);
                    if (Constant.sum < limit) {
                        String oldTxt = btnLbl + "=" + Lb8Count + "(" + String.format("%.2f", Lb8Prop) + "%"
                                + ")";
                        morphologyCounts.remove(oldTxt);
                        Lb8Count = Lb8Count + 1;
                        Constant.sum = Constant.sum + 1;
                        Lb8Prop = (Lb8Count * 100.0) / Constant.sum;
                        String newTxt = btnLbl + ":" + Lb8Count + "(" + String.format("%.2f", Lb8Prop) + "%"
                                + ")";
                        btn8.setText(newTxt);
                        newTxt = btnLbl + "=" + Lb8Count + "(" + String.format("%.2f", Lb8Prop) + "%" + ")";
                        morphologyCounts.add(newTxt);
                    } else if (Constant.sum == limit) {
                        limitRchdSound.start();
                        Constant.sum++;
                    }

                    if (Constant.sum < limit)
                        tv.setText("Total Count:" + Constant.sum);
                    else
                        tv.setText("Total Count:" + limit);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error occured. Restart and try again",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}

From source file:com.murrayc.galaxyzoo.app.QuestionFragment.java

private void update() {
    final FragmentActivity activity = getActivity();
    if (activity == null)
        return;// ww  w  .j a  v a 2 s  . com

    if (mRootView == null) {
        //This can happen when update() is called by the parent fragment
        //after this fragment has been instantiated after an orientation change,
        //but before onCreateView() has been called. It's not a problem
        //because onCreateView() will call this method again after setting mRootView.
        //Log.error("QuestionFragment.update(): mRootView is null.");
        return;
    }

    //Wipe the question details,
    //to ensure that we don't have old question details if somethng goes wrong when we
    //try to get and show the correct question details.
    final TextView textViewTitle = (TextView) mRootView.findViewById(R.id.textViewTitle);
    if (textViewTitle == null) {
        Log.error("update(): textViewTitle is null.");
        return;
    }
    textViewTitle.setText("");

    //Show the text:
    final TextView textViewText = (TextView) mRootView.findViewById(R.id.textViewText);
    if (textViewText == null) {
        Log.error("update(): textViewText is null.");
        return;
    }
    textViewText.setText("");

    final TableLayout layoutAnswers = (TableLayout) mRootView.findViewById(R.id.layoutAnswers);
    if (layoutAnswers == null) {
        Log.error("update(): layoutAnswers is null.");
        return;
    }
    layoutAnswers.removeAllViews();

    if (getSingleton() == null) {
        //The parent fragment's onSingletonInitialized has been called
        //but this fragment's onSingletonInitialized hasn't been called yet.
        //That's OK. update() will be called, indirectly, later by this fragment's onSingletonInitialized().
        return;
    }

    final DecisionTree.Question question = getQuestion();
    if (question == null) {
        Log.error("update(): question is null.");
        return;
    }

    //Show the title:
    textViewTitle.setText(question.getTitle());

    //Show the text:
    textViewText.setText(question.getText());

    layoutAnswers.setShrinkAllColumns(true);
    layoutAnswers.setStretchAllColumns(true);

    //Checkboxes:
    mCheckboxButtons.clear();
    final int COL_COUNT = 4;
    int col = 1;
    int rows = 0;
    TableRow row = null;
    final LayoutInflater inflater = LayoutInflater.from(activity);
    for (final DecisionTree.Checkbox checkbox : question.getCheckboxes()) {
        //Start a new row if necessary:
        if (row == null) {
            row = addRowToTable(activity, layoutAnswers);
            rows++;
        }

        final ToggleButton button = (ToggleButton) inflater.inflate(R.layout.question_answer_checkbox, null);

        //Use just the highlighting (line, color, etc) to show that it's selected,
        //instead of On/Off, so we don't need a separate label.
        //TODO: Use the icon. See http://stackoverflow.com/questions/18598255/android-create-a-toggle-button-with-image-and-no-text
        //TODO: Avoid the highlight bar thing at the bottom being drawn over the text.
        final String text = checkbox.getText();
        button.setText(text);
        button.setTextOn(text);
        button.setTextOff(text);

        insertButtonInRow(activity, row, button);

        final BitmapDrawable icon = getIcon(activity, checkbox);
        button.setCompoundDrawables(null, icon, null, null);

        mCheckboxButtons.put(checkbox.getId(), button);

        if (col < COL_COUNT) {
            col++;
        } else {
            col = 1;
            row = null;
        }
    }

    //Answers:
    for (final DecisionTree.Answer answer : question.getAnswers()) {
        //Start a new row if necessary:
        if (row == null) {
            row = addRowToTable(activity, layoutAnswers);
            rows++;
        }

        final Button button = createAnswerButton(activity, answer);
        insertButtonInRow(activity, row, button);

        final String questionId = question.getId();
        final String answerId = answer.getId();
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                // Perform action on click
                onAnswerButtonClicked(questionId, answerId);
            }
        });

        if (col < COL_COUNT) {
            col++;
        } else {
            col = 1;
            row = null;
        }
    }

    //Add empty remaining cells, to avoid the other cells from expanding to fill the space,
    //because we want them to line up with the same cells above and below.
    if ((row != null) && (rows > 1)) {
        final int remaining_in_row = COL_COUNT - col + 1;
        for (int i = 0; i < remaining_in_row; i++) {
            //TODO: We could use Space instead of FrameLayout when using API>14.
            final FrameLayout placeholder = new FrameLayout(activity);
            insertButtonInRow(activity, row, placeholder);
        }
    }

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

        /* This wastes even more space to be even more consistent:
        //Make sure there are always at least 2 rows,
        //so we request roughly the same amount of space each time:
        if (rows < 2) {
        row = addRowToTable(activity, layoutAnswers);
                
        final DecisionTree.Answer answer = new DecisionTree.Answer("bogus ID", "bogus title", getArbitraryIconId(), null, 0);
        final Button button = createAnswerButton(activity, answer);
        button.setVisibility(View.INVISIBLE); //It won't be seen, but it's size will be used.
        insertButtonInRow(activity, row, button);
        }
        */

        //This will be used in a later onLayout(),
        //so we will know the correct height at least during the second classification,
        mRootView.setRowsCountForMaxHeightExperienced(rows);

        //Try to keep the height consistent, to avoid the user seeing everything moving about.
        final int min = mRootView.getMaximumHeightExperienced(rows);
        if (min > 0) {
            mRootView.setMinimumHeight(min);
        }
    } else {
        //Ignore any previously-set minimum height,
        //to stop the portrait-mode's layout from affecting the layout-mode's layout:
        mRootView.setMinimumHeight(0);
    }
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

public void setupBaseBarButtons(Menu menu, boolean flush) {
    if (flush)//from w w w.  jav  a2  s .  co  m
        mLastMenuClass = "";
    TableLayout mBaseBar = (TableLayout) findViewById(R.id.base_bar);
    mToolbarButtons = (ViewGroup) findViewById(R.id.base_row);
    mStaticButtons = (ViewGroup) findViewById(R.id.title_static_buttons);
    OpenFragment f = getSelectedFragment();
    boolean topButtons = false;
    if (!getResources().getBoolean(R.bool.allow_split_actionbar)
            || !(getSetting(null, "pref_basebar", true) || mBaseBar == null || mToolbarButtons == null)
                    && findViewById(R.id.title_buttons) != null) {
        mToolbarButtons = (ViewGroup) findViewById(R.id.title_buttons);
        if (mToolbarButtons == null && !BEFORE_HONEYCOMB)
            mToolbarButtons = (ViewGroup) getActionBar().getCustomView().findViewById(R.id.title_buttons);
        if (mBaseBar != null)
            mBaseBar.setVisibility(View.GONE);
        topButtons = true;
    }
    if (!shouldFlushMenu(menu))
        return;
    USE_SPLIT_ACTION_BAR = !topButtons;
    if (mToolbarButtons != null) {
        mToolbarButtons.removeAllViews();
        //if(!topButtons) mToolbarButtons.measure(LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.actionbar_compat_height));

        int i = -1;
        int btnWidth = getResources().getDimensionPixelSize(R.dimen.actionbar_compat_button_width)
                + (getResources().getDimensionPixelSize(R.dimen.vpi_padding_horizontal) * 2); // (int)(16 * getResources().getDimension(R.dimen.one_dp));
        int tblWidth = mToolbarButtons.getWidth();
        if (tblWidth <= 0 && !topButtons)
            tblWidth = getWindowWidth();
        if (topButtons || tblWidth <= 0 || tblWidth > getWindowWidth()
                || !getResources().getBoolean(R.bool.ignore_max_base_buttons))
            tblWidth = btnWidth * getResources().getInteger(R.integer.max_base_buttons);
        ArrayList<View> buttons = new ArrayList<View>();
        buttons.addAll(ViewUtils.findChildByClass(mToolbarButtons, ImageButton.class));
        boolean maxedOut = false;
        while (++i < menu.size()) {
            if (buttons.size() * btnWidth >= tblWidth) {
                maxedOut = true;
                Logger.LogDebug("Base bar full after #" + i + " ~ " + buttons.size() + " ("
                        + (buttons.size() * btnWidth) + ">" + tblWidth + ")!");
                break;
            } else if (menu.getItem(i) instanceof MenuItemImpl) {
                final MenuItemImpl item = (MenuItemImpl) menu.getItem(i);
                //if(item.getItemId() == R.id.title_menu) break;
                if (!item.isCheckable() && item.isVisible()) {
                    View btn = makeMenuButton(item, mToolbarButtons);
                    if (item.hasSubMenu())
                        btn.setTag(item.getSubMenu());
                    else if (!BEFORE_HONEYCOMB && item.getActionView() != null) {
                        if (DEBUG)
                            Logger.LogDebug("ACTION VIEW!!!");
                        btn = item.getActionView();
                        //ActionBarHelper h = ActionBarHelper.createInstance(this);
                    }
                    buttons.add(btn);
                    if (i > 0)
                        btn.setNextFocusLeftId(menu.getItem(i - 1).getItemId());
                    if (i < menu.size() - 1)
                        btn.setNextFocusRightId(menu.getItem(i + 1).getItemId());
                    if (!USE_PRETTY_MENUS || topButtons)
                        btn.setOnCreateContextMenuListener(this);
                    menu.getItem(i).setVisible(false);
                    btn.setOnClickListener(this);
                    btn.setOnFocusChangeListener(this);
                    btn.setOnKeyListener(this);
                    if (mToolbarButtons.findViewById(menu.getItem(i).getItemId()) == null)
                        mToolbarButtons.addView(btn);
                    //menu.removeItem(item.getItemId());
                    if (DEBUG)
                        Logger.LogDebug("Added " + item.getTitle() + " to base bar.");
                }
                //else Logger.LogWarning(item.getTitle() + " should not show. " + item.getShowAsAction() + " :: " + item.getFlags());
            }
        }
        mToolbarButtons.setVisibility(View.VISIBLE);
        mLastMenuClass = f.getClassName();
        if (MenuUtils.countVisibleMenus(mMainMenu) > 0) {
            if (maxedOut && buttons.size() > 0) {
                View old = buttons.remove(buttons.size() - 1);
                MenuUtils.setMenuVisible(mMainMenu, true, old.getId());
                mToolbarButtons.removeView(old);
            }
            final ImageButton btn = (ImageButton) getLayoutInflater().inflate(R.layout.toolbar_button,
                    mToolbarButtons);
            btn.setImageResource(R.drawable.ic_menu_more);
            //btn.measure(getResources().getDimensionPixelSize(R.dimen.actionbar_compat_button_home_width), getResources().getDimensionPixelSize(R.dimen.actionbar_compat_height));
            btn.setId(R.id.menu_more);
            if (buttons.size() > 0) {
                buttons.get(buttons.size() - 1).setNextFocusRightId(R.id.menu_more);
                btn.setNextFocusLeftId(buttons.get(buttons.size() - 1).getId());
            }
            btn.setOnKeyListener(this);
            btn.setOnClickListener(this);
            btn.setOnLongClickListener(this);
            btn.setFocusable(true);
            btn.setOnFocusChangeListener(this);
            buttons.add(btn);
            mToolbarButtons.addView(btn);
        }
        if (buttons.size() > 0) {
            View last = buttons.get(buttons.size() - 1);
            last.setNextFocusRightId(android.R.id.home);
            if (findViewById(android.R.id.home) != null)
                findViewById(android.R.id.home).setNextFocusLeftId(last.getId());
        }

        Logger.LogDebug("Added " + buttons.size() + " children to Base Bar.");
        if (mBaseBar != null) {
            if (buttons.size() < 1)
                mBaseBar.setVisibility(View.GONE);
            else
                mBaseBar.setStretchAllColumns(true);
        }
    } else if (BEFORE_HONEYCOMB)
        Logger.LogWarning("No Base Row!?");
}