Example usage for android.widget Button Button

List of usage examples for android.widget Button Button

Introduction

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

Prototype

public Button(Context context) 

Source Link

Document

Simple constructor to use when creating a button from code.

Usage

From source file:com.acceleratedio.pac_n_zoom.SaveAnmActivity.java

private void dsply_tags() {

    TableLayout tbl_tag_lo = (TableLayout) findViewById(R.id.tl_tgs);
    int tag_mbr = 0;
    int tag_nmbr = fil_tags.length;
    String lst_str = "";
    int row_mbr;//from   w  w  w .j  a v a  2  s  . co  m

    tbl_tag_lo.setAlpha(185);

    // - Find the search string
    srch_str = srch_str.trim();
    boolean flg_srch_tags = !srch_str.equals("");

    if (flg_srch_tags) {

        String[] srch_ary = srch_str.split("\\s* \\s*");
        srch_str = srch_ary[srch_ary.length - 1].toLowerCase();
    }

    // - Remove any current rows
    int row_nmbr = tbl_tag_lo.getChildCount();

    if (row_nmbr > 0)
        tbl_tag_lo.removeAllViews();

    for (row_mbr = 0; tag_mbr < tag_nmbr; row_mbr++) {

        TableRow tableRow = new TableRow(this);
        tableRow.setGravity(Gravity.CENTER);

        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT, 1.0f));

        int clm_mbr;

        for (clm_mbr = 0; clm_mbr < 3; clm_mbr++) {

            Button btnTag = new Button(this);

            while (tag_mbr < tag_nmbr
                    && (fil_tags[tag_mbr].equals("") || fil_tags[tag_mbr].equalsIgnoreCase(lst_str)
                            || flg_srch_tags && !fil_tags[tag_mbr].toLowerCase().startsWith(srch_str))) {

                lst_str = fil_tags[tag_mbr];
                tag_mbr++;
            }

            if (tag_mbr >= tag_nmbr)
                break;

            lst_str = fil_tags[tag_mbr];
            btnTag.setText(fil_tags[tag_mbr++]);
            btnTag.setOnClickListener(this);

            btnTag.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.MATCH_PARENT));

            tableRow.addView(btnTag);
        }

        if (clm_mbr > 0)
            tbl_tag_lo.addView(tableRow);
    }
}

From source file:com.example.moneymeterexample.ViewExpenseActivity.java

private void fillListViewByDate() {
    DataBaseHelper db;/*from  w w  w  . j av a  2 s  .  com*/
    db = new DataBaseHelper(this);
    ArrayList<ExpenseEntry> exp_list = new ArrayList<ExpenseEntry>();

    String date = getIntent().getExtras().getString("date_val");
    exp_list = db.getExpensesByDate(date);
    float sum = 0;

    for (int i = 0; i < exp_list.size(); i++) {
        Button b = new Button(this);
        HashMap<String, String> temp = new HashMap<String, String>();
        temp.put("_id", Integer.toString(exp_list.get(i).getId()));
        temp.put("date", exp_list.get(i).getDate().toString());
        temp.put("category", exp_list.get(i).category.toString());
        temp.put("amount", format.format(exp_list.get(i).getAmount()));
        temp.put("notes", exp_list.get(i).getNotes().toString());
        sum += exp_list.get(i).getAmount();
        list.add(temp);
    }
    total.setText(String.valueOf(format.format(sum)));
    db.close();

}

From source file:com.esri.android.ecologicalmarineunitexplorer.bottomsheet.BottomSheetFragment.java

/**
 * Dynamically add a button for each EMU represented
 * in the water column.//from   w  w  w  . java2s  . c o  m
 * @param waterColumn - WaterColumn data object
 */
private void showWaterColumnButtons(final WaterColumn waterColumn) {
    mButtonContainer.removeAllViews();

    // Each button will be added to layout with a layout_weight
    // relative to the ratio of the EUMObservation to
    // the depth of the water column
    final Set<EMUObservation> emuObservationSet = waterColumn.getEmuSet();
    final float depth = waterColumn.getDepth();
    final TextView tv = (TextView) mRoot.findViewById(R.id.txtBottom);
    tv.setText(getString(R.string.water_column_depth, waterColumn.getDepth()));

    int buttonId = 0;
    for (final EMUObservation observation : emuObservationSet) {
        final float relativeSize = observation.getThickness() / depth * 100;
        final Button button = new Button(getContext());
        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 0, relativeSize);
        button.setLayoutParams(layoutParams);
        // Enable the button background to be change color based on its state (pressed, selected, or enabled)
        button.setBackground(buildStateList(observation.getEmu().getName()));

        button.setId(buttonId);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mSelectedButton != null) {
                    mSelectedButton.setSelected(false);
                }
                button.setSelected(true);
                mSelectedButton = button;
                scrollToSummary(v.getId());
            }
        });
        mButtonContainer.addView(button);
        buttonId = buttonId + 1;
    }
}

From source file:com.hichinaschool.flashcards.anki.multimediacard.activity.EditFieldActivity.java

private void createSpareMenu(LinearLayout linearLayout) {

    LayoutParams pars = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);

    Button toTextButton = new Button(this);
    toTextButton.setText(gtxt(R.string.multimedia_editor_field_editing_text));
    toTextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            toTextField();//w w  w.  j av a2  s. c om
        }

    });
    linearLayout.addView(toTextButton, pars);

    Button toImageButton = new Button(this);
    toImageButton.setText(gtxt(R.string.multimedia_editor_field_editing_image));
    toImageButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            toImageField();
        }

    });
    linearLayout.addView(toImageButton, pars);

    Button toAudioButton = new Button(this);
    toAudioButton.setText(gtxt(R.string.multimedia_editor_field_editing_audio));
    toAudioButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            toAudioField();
        }

    });
    linearLayout.addView(toAudioButton, pars);

    Button doneButton = new Button(this);
    doneButton.setText(gtxt(R.string.multimedia_editor_field_editing_done));
    doneButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            done();
        }

    });
    linearLayout.addView(doneButton, pars);

}

From source file:nyc.c4q.jordansmith.finefree.ActivityMain.java

public void addCarstoNav() {
    final Menu menu = navDrawerView.getMenu();
    if (submenu == null) {
        submenu = menu.addSubMenu("Your Cars");
        for (int i = 0; i < cars.size(); i++) {
            submenu.add(cars.get(i).getName()).setTitle(cars.get(i).getName().toString())
                    .setIcon(R.drawable.ic_car_black_36dp);

            System.out.println(submenu.getItem(i).getItemId() + " " + submenu.getItem(i).getTitle());

            final MenuItem item = submenu.getItem(i);
            Button button = new Button(this);
            button.setText("Remove");
            button.setTextColor(ContextCompat.getColor(this, R.color.white));
            item.setActionView(button);/*from w  ww . ja va  2  s  .  c  om*/
            button.setBackground(getDrawable(R.drawable.button_shape));
            item.getActionView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(ActivityMain.this).setIcon(android.R.drawable.ic_dialog_alert)
                            .setTitle("Delete Car").setMessage("Are you sure you want to remove car?")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    System.out.println("CLICKED " + item.getTitle());
                                    cupboard().withDatabase(db).delete(Car.class, "name = ?",
                                            (String) item.getTitle());

                                    updateSubmenu();
                                }
                            }).setNegativeButton("Cancel", null).show();

                }
            });
        }
    } else {
        //            for (int i = 0; i < cars.size(); i++) {
        //                submenu.clear();
        //                submenu.add(cars.get(i).getName()).setTitle(cars.get(i).getName().toString())
        //                        .setIcon(R.drawable.ic_car_black_36dp);
        //
        //                final MenuItem item = submenu.getItem(i);
        //                Button button = new Button(this);
        //                button.setText("Remove");
        //                item.setActionView(button);
        //                item.getActionView().setOnClickListener(new View.OnClickListener() {
        //                    @Override
        //                    public void onClick(View view) {
        //
        //                        System.out.println("CLICKED " + item.getTitle());
        //                        cupboard().withDatabase(db).delete(Car.class, "name = ?", (String) item.getTitle());
        //
        //                        updateSubmenu();
        //                    }
        //                });
        //
        //            }
        setupCarMenu();

    }
}

From source file:com.mifos.mifosxdroid.dialogfragments.DataTableRowDialogFragment.java

public void createForm() {

    Iterator<ColumnHeader> columnHeaderIterator = dataTable.getColumnHeaderData().iterator();
    while (columnHeaderIterator.hasNext()) {

        ColumnHeader columnHeader = columnHeaderIterator.next();
        if (!columnHeader.getIsColumnPrimaryKey()) {

            if (columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_STRING)
                    || columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_TEXT)) {

                FormEditText formEditText = new FormEditText(getActivity(), columnHeader.getColumnName());
                formWidgets.add(formEditText);
                linearLayout.addView(formEditText.getView());

            } else if (columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_INT)) {

                FormNumericEditText formNumericEditText = new FormNumericEditText(getActivity(),
                        columnHeader.getColumnName());
                formNumericEditText.setReturnType(FormWidget.SCHEMA_KEY_INT);
                formWidgets.add(formNumericEditText);
                linearLayout.addView(formNumericEditText.getView());

            } else if (columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_DECIMAL)) {

                FormNumericEditText formNumericEditText = new FormNumericEditText(getActivity(),
                        columnHeader.getColumnName());
                formNumericEditText.setReturnType(FormWidget.SCHEMA_KEY_DECIMAL);
                formWidgets.add(formNumericEditText);
                linearLayout.addView(formNumericEditText.getView());

            } else if (columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_CODELOOKUP)
                    || columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_CODEVALUE)) {

                List<String> columnValueStrings = new ArrayList<String>();
                List<Integer> columnValueIds = new ArrayList<Integer>();

                for (ColumnValue columnValue : columnHeader.getColumnValues()) {
                    columnValueStrings.add(columnValue.getValue());
                    columnValueIds.add(columnValue.getId());
                }/*from  ww w  .  j  a va2  s  .  c  om*/

                FormSpinner formSpinner = new FormSpinner(getActivity(), columnHeader.getColumnName(),
                        columnValueStrings, columnValueIds);
                formSpinner.setReturnType(FormWidget.SCHEMA_KEY_CODEVALUE);
                formWidgets.add(formSpinner);
                linearLayout.addView(formSpinner.getView());

            } else if (columnHeader.getColumnDisplayType().equals(FormWidget.SCHEMA_KEY_DATE)) {

                FormEditText formEditText = new FormEditText(getActivity(), columnHeader.getColumnName());
                formEditText.setIsDateField(true, getActivity().getSupportFragmentManager());
                formWidgets.add(formEditText);
                linearLayout.addView(formEditText.getView());
            }
        }

    }

    Button bt_processForm = new Button(getActivity());
    bt_processForm.setLayoutParams(FormWidget.defaultLayoutParams);
    bt_processForm.setText(getString(R.string.save));

    linearLayout.addView(bt_processForm);
    bt_processForm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                onSaveActionRequested();
            } catch (RequiredFieldException e) {
                Log.d(LOG_TAG, e.getMessage());
            }
        }
    });

}

From source file:com.ichi2.anki.multimediacard.activity.TranslationActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        boolean b = savedInstanceState.getBoolean(BUNDLE_KEY_SHUT_OFF, false);
        if (b) {//from   w w w .  j  a v a2  s .c  o m
            finishCancel();
            return;
        }
    }

    setContentView(R.layout.activity_translation);

    try {
        mSource = getIntent().getExtras().getString(EXTRA_SOURCE).toString();
    } catch (Exception e) {
        mSource = "";
    }

    // If translation fails this is a default - source will be returned.
    mTranslation = mSource;

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.MainLayoutInTranslationActivity);

    TextView tv = new TextView(this);
    tv.setText(getText(R.string.multimedia_editor_trans_poweredglosbe));
    linearLayout.addView(tv);

    TextView tvFrom = new TextView(this);
    tvFrom.setText(getText(R.string.multimedia_editor_trans_from));
    linearLayout.addView(tvFrom);

    mLanguageLister = new LanguagesListerGlosbe(this);

    mSpinnerFrom = new Spinner(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            mLanguageLister.getLanguages());
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinnerFrom.setAdapter(adapter);
    linearLayout.addView(mSpinnerFrom);

    TextView tvTo = new TextView(this);
    tvTo.setText(getText(R.string.multimedia_editor_trans_to));
    linearLayout.addView(tvTo);

    mSpinnerTo = new Spinner(this);
    ArrayAdapter<String> adapterTo = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            mLanguageLister.getLanguages());
    adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinnerTo.setAdapter(adapterTo);
    linearLayout.addView(mSpinnerTo);

    final SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());

    // Try to set spinner value to last selected position
    String fromLang = preferences.getString("translatorLastLanguageFrom", "");
    String toLang = preferences.getString("translatorLastLanguageTo", "");
    mSpinnerFrom.setSelection(getSpinnerIndex(mSpinnerFrom, fromLang));
    mSpinnerTo.setSelection(getSpinnerIndex(mSpinnerTo, toLang));
    // Setup button
    Button btnDone = new Button(this);
    btnDone.setText(getText(R.string.multimedia_editor_trans_translate));
    btnDone.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Remember currently selected language
            String fromLang = mSpinnerFrom.getSelectedItem().toString();
            String toLang = mSpinnerTo.getSelectedItem().toString();
            preferences.edit().putString("translatorLastLanguageFrom", fromLang).commit();
            preferences.edit().putString("translatorLastLanguageTo", toLang).commit();
            // Get translation
            translate();
        }
    });

    linearLayout.addView(btnDone);

}

From source file:com.matthewtamlin.sliding_intro_screen_manual_testing.TestPageLock.java

/**
 * @return creates a Button which locks the ViewPager from touch events
 */// ww w .  ja v a 2 s .  com
private Button createLockTouchButton() {
    final Button button = new Button(this);
    button.setText("Lock to touch");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setPagingLockMode(LockableViewPager.LockMode.TOUCH_LOCKED);
        }
    });

    return button;
}

From source file:tinygsn.gui.android.ActivityViewData.java

private void addTableViewModeLatest() {
    table_view_mode = (TableLayout) findViewById(R.id.table_view_mode);
    // table_view_mode.setLayoutParams(new
    // TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
    // LayoutParams.WRAP_CONTENT));
    table_view_mode.removeAllViews();/*from ww  w .  ja  v  a 2s  .c  om*/

    TableRow row = new TableRow(this);

    TextView txt = new TextView(this);
    txt.setText("         View ");
    txt.setTextColor(Color.parseColor("#000000"));
    row.addView(txt);

    final EditText editText_numLatest = new EditText(this);
    editText_numLatest.setText("10");
    editText_numLatest.setInputType(InputType.TYPE_CLASS_NUMBER);
    editText_numLatest.requestFocus();
    editText_numLatest.setTextColor(Color.parseColor("#000000"));
    row.addView(editText_numLatest);

    editText_numLatest.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            try {
                numLatest = Integer.parseInt(editText_numLatest.getText().toString());
                loadLatestData();
            } catch (NumberFormatException e) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                alertDialogBuilder.setTitle("Please input a number!");
            }
        }
    });

    txt = new TextView(this);
    txt.setText(" latest values");
    txt.setTextColor(Color.parseColor("#000000"));
    row.addView(txt);

    txt = new TextView(this);
    txt.setText("            ");
    row.addView(txt);
    table_view_mode.addView(row);

    row = new TableRow(this);
    Button detailBtn = new Button(this);
    // plotDataBtn.setTextSize(TEXT_SIZE);
    detailBtn.setText("Detail");
    detailBtn.setTextColor(Color.parseColor("#000000"));
    detailBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //            Toast.makeText(context, "detailBtn is clicked",
            //                  Toast.LENGTH_SHORT).show();

            showDialogDetail();
        }
    });

    Button plotDataBtn = new Button(this);
    // plotDataBtn.setTextSize(TEXT_SIZE);
    plotDataBtn.setText("Plot data");
    plotDataBtn.setTextColor(Color.parseColor("#000000"));
    plotDataBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            viewChart();
        }
    });

    TableRow.LayoutParams params = new TableRow.LayoutParams();
    // params.addRule(TableRow.LayoutParams.FILL_PARENT);
    params.span = 2;

    row.addView(detailBtn, params);
    row.addView(plotDataBtn, params);
    row.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    table_view_mode.addView(row);
}

From source file:website.openeng.anki.multimediacard.activity.TranslationActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        boolean b = savedInstanceState.getBoolean(BUNDLE_KEY_SHUT_OFF, false);
        if (b) {/*from  w ww  .j  a v a  2s .com*/
            finishCancel();
            return;
        }
    }

    setContentView(R.layout.activity_translation);

    try {
        mSource = getIntent().getExtras().getString(EXTRA_SOURCE).toString();
    } catch (Exception e) {
        mSource = "";
    }

    // If translation fails this is a default - source will be returned.
    mTranslation = mSource;

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.MainLayoutInTranslationActivity);

    TextView tv = new TextView(this);
    tv.setText(getText(R.string.multimedia_editor_trans_poweredglosbe));
    linearLayout.addView(tv);

    TextView tvFrom = new TextView(this);
    tvFrom.setText(getText(R.string.multimedia_editor_trans_from));
    linearLayout.addView(tvFrom);

    mLanguageLister = new LanguagesListerGlosbe(this);

    mSpinnerFrom = new Spinner(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            mLanguageLister.getLanguages());
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinnerFrom.setAdapter(adapter);
    linearLayout.addView(mSpinnerFrom);

    TextView tvTo = new TextView(this);
    tvTo.setText(getText(R.string.multimedia_editor_trans_to));
    linearLayout.addView(tvTo);

    mSpinnerTo = new Spinner(this);
    ArrayAdapter<String> adapterTo = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            mLanguageLister.getLanguages());
    adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinnerTo.setAdapter(adapterTo);
    linearLayout.addView(mSpinnerTo);

    final SharedPreferences preferences = KanjiDroidApp.getSharedPrefs(getBaseContext());

    // Try to set spinner value to last selected position
    String fromLang = preferences.getString("translatorLastLanguageFrom", "");
    String toLang = preferences.getString("translatorLastLanguageTo", "");
    mSpinnerFrom.setSelection(getSpinnerIndex(mSpinnerFrom, fromLang));
    mSpinnerTo.setSelection(getSpinnerIndex(mSpinnerTo, toLang));
    // Setup button
    Button btnDone = new Button(this);
    btnDone.setText(getText(R.string.multimedia_editor_trans_translate));
    btnDone.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Remember currently selected language
            String fromLang = mSpinnerFrom.getSelectedItem().toString();
            String toLang = mSpinnerTo.getSelectedItem().toString();
            preferences.edit().putString("translatorLastLanguageFrom", fromLang).commit();
            preferences.edit().putString("translatorLastLanguageTo", toLang).commit();
            // Get translation
            translate();
        }
    });

    linearLayout.addView(btnDone);

}