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.everyplay.android.everyplayrecord.EveryplayRecordActivity.java

Button addButton(String text, String tag) {
    Button a = new Button(this);

    a.setWidth(380);/*from  w w w .  ja va2 s .  c  o m*/
    a.setText(text);
    a.setTag(tag);
    a.setId(buttonCnt++);
    a.setOnClickListener(this);
    buttons.addView(a);

    return a;
}

From source file:edu.csh.coursebrowser.SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    this.setTitle("Settings");
    final SharedPreferences sp = SchoolActivity.sp;
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.settings_layout);
    ll.setGravity(Gravity.CENTER);//from   w w  w  .j  a  va  2  s  . co  m
    final TextView tv = new TextView(this);
    if (!sp.contains("quarter")) {
        SharedPreferences.Editor e = sp.edit();
        e.putString("quarter", "20122");
        e.commit();
    }
    tv.setText("Current Quarter: " + sp.getString("quarter", "20122"));
    Button b = new Button(this);
    b.setText("Change Quarter");
    ll.addView(tv);
    ll.addView(b);
    ll.setPadding(10, 10, 10, 10);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            AlertDialog.Builder changeQuarter = new AlertDialog.Builder(SettingsActivity.this);
            changeQuarter.setTitle("Change Quarter");
            changeQuarter.setMessage("Select New Quarter");
            final RadioButton ld = new RadioButton(SettingsActivity.this);
            ld.setText("20121");
            ld.setId(1);
            final RadioButton s = new RadioButton(SettingsActivity.this);
            s.setText("20122 (Current)");
            s.setId(2);
            final RadioButton d = new RadioButton(SettingsActivity.this);
            d.setText("20123");
            d.setId(3);
            final RadioGroup rg = new RadioGroup(SettingsActivity.this);
            rg.addView(ld);
            rg.addView(s);
            rg.addView(d);
            changeQuarter.setView(rg);

            changeQuarter.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }

            });
            changeQuarter.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String q;
                    int n = rg.getCheckedRadioButtonId();
                    if (n == 1)
                        q = "20121";
                    else if (n == 2)
                        q = "20122";
                    else
                        q = "20123";
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("quarter", q);
                    edit.commit();
                    SettingsActivity.this.finish();
                }
            });

            changeQuarter.show();
        }

    });

}

From source file:com.RecEx.dtuexamtools.Groups.java

private void createButton(String string) {
    // TODO Auto-generated method stub
    Button cb = null;/* www  . ja va2 s.c o m*/
    cb = new Button(this);
    cb.setText(string);
    cb.setBackgroundResource(R.drawable.fancy_button_selector);
    cb.setOnClickListener(this);
    registerForContextMenu(cb);
    mButtons.add(cb);
}

From source file:com.sahana.geosmser.view.ReverseGeocoderView.java

public void initialUIComponent() {
    mTextView = new TextView(mContext);

    mEditText = new EditText(mContext);

    mButton = new Button(mContext);

    mTextView.setId(3);//from  w  w w .j a v a 2  s . c o  m
    mEditText.setId(1);
    mButton.setId(2);

    mTextView.setText("");
    mEditText.setHint("EX: ???");
    mButton.setText("");

    RelativeLayout.LayoutParams mTextLayoutParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams mEditLayoutParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams mButtonLayoutParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    mTextLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    mEditLayoutParams.addRule(BELOW, mTextView.getId());
    mButtonLayoutParams.addRule(RIGHT_OF, mEditText.getId());
    mButtonLayoutParams.addRule(ALIGN_BASELINE, mEditText.getId());

    mButton.setOnClickListener(getGeocoder);

    this.addView(mTextView, mTextLayoutParams);
    this.addView(mEditText, mEditLayoutParams);
    this.addView(mButton, mButtonLayoutParams);
}

From source file:es.uma.lcc.lockpic.SelectorActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button btnForward, btnBack, btnFaces;
    RelativeLayout layout = new RelativeLayout(this);
    layout.setBackgroundResource(R.drawable.background_gradient);

    mPath = getIntent().getStringExtra("path");

    SelectorActivityBundle bundle = (SelectorActivityBundle) getLastCustomNonConfigurationInstance();
    if (bundle != null) {
        mDrawView = new DrawView(this, mPath, false);
        mDrawView.setRectangles(bundle.getRectangles());
        mDrawView.setViewMode(bundle.getViewMode());
        mDrawView.setAspectRate(bundle.getAspectRate());
    } else {//from  w w w  .jav  a2  s  . co  m
        mDrawView = new DrawView(this, mPath, true);
    }
    mDrawView.setId(1);

    RelativeLayout.LayoutParams lpDrawView = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    lpDrawView.addRule(RelativeLayout.CENTER_VERTICAL);
    mDrawView.setLayoutParams(lpDrawView);
    layout.addView(mDrawView, lpDrawView);

    btnForward = new Button(this);
    btnForward.setText(R.string.selectorForwardButton);
    btnForward.setId(2);
    btnForward.setOnClickListener(new forwardButtonListener());
    RelativeLayout.LayoutParams lpButton = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    lpButton.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lpButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    layout.addView(btnForward, lpButton);

    btnBack = new Button(this);
    btnBack.setText(R.string.selectorBackButton);
    btnBack.setId(3);
    btnBack.setOnClickListener(new backButtonListener());
    lpButton = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lpButton.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lpButton.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    layout.addView(btnBack, lpButton);

    btnFaces = new Button(this);
    btnFaces.setText(R.string.facesButton);
    btnFaces.setId(4);
    btnFaces.setOnClickListener(new FacesButtonListener());
    if (!isViewSelectingRegions())
        btnFaces.setVisibility(View.INVISIBLE);
    lpButton = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lpButton.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lpButton.addRule(RelativeLayout.CENTER_HORIZONTAL);
    layout.addView(btnFaces, lpButton);

    setContentView(layout);
}

From source file:com.finlay.geomonsters.battle.ChooseCreatureDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Get the first six creatures of user
    ArrayList<Creature> creatures = ResourceManager.getUserCreatures(getResources(), 6);

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // View/* ww w  .ja  v  a2 s. c o m*/
    View contentView = inflater.inflate(R.layout.change_geomonster, null);
    ViewGroup view = (ViewGroup) contentView.findViewById(R.id.ChangeGeomonsterLayout);

    // Add a button for every creature (max of 6)
    Button button;
    for (Creature creature : creatures) {
        button = new Button(contentView.getContext());
        button.setText(creature.getNickName());

        // Can't reselect same creature
        if (creature.getNickName().equals(_currentCreature)) {
            //TODO: Make current more visible
            button.setTextColor(Color.WHITE);
        }

        //TODO: Use better button styles. Change bg colour depending on hp
        button.setBackgroundColor(Color.GREEN);
        button.setOnClickListener(new MyButtonClickListener());
        view.addView(button);
    }

    // Set layout for dialog
    builder.setView(contentView).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    return builder.create();
}

From source file:com.code.android.vibevault.FeaturedShowsScreen.java

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.featured_shows_screen);

    getMoreShowsButton = new Button(this);
    getMoreShowsButton.setText("More Featured Shows");
    getMoreShowsButton.setTextColor(Color.rgb(18, 125, 212));
    getMoreShowsButton.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
    getMoreShowsButton.setPadding(0, 6, 0, 6);

    getMoreShowsButton.setOnClickListener(new OnClickListener() {
        @Override//from  ww w  . ja  va2 s.c  om
        public void onClick(View v) {
            if (VibeVault.moreFeaturedShows.size() != 0) {
                fetchSelectedShows(VibeVault.moreFeaturedShows.get(0));
            } else {
                Toast.makeText(getBaseContext(), "More featured shows weekly...", Toast.LENGTH_SHORT).show();
            }
        }
    });

    this.featuredShowsList = (ListView) this.findViewById(R.id.SelectedShowsListView);
    featuredShowsList.addFooterView(getMoreShowsButton);
    if (VibeVault.featuredShows.size() == 0) {
        getMoreShowsButton.setVisibility(View.GONE);
    } else {
        getMoreShowsButton.setVisibility(View.VISIBLE);
    }
    featuredShowsList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            ArchiveShowObj show = (ArchiveShowObj) featuredShowsList.getItemAtPosition(position);
            Intent i = new Intent(FeaturedShowsScreen.this, ShowDetailsScreen.class);
            i.putExtra("Show", show);
            startActivity(i);
        }
    });
    featuredShowsList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.add(Menu.NONE, VibeVault.EMAIL_LINK, Menu.NONE, "Email Link to Show");
            menu.add(Menu.NONE, VibeVault.ADD_TO_FAVORITE_LIST, Menu.NONE, "Bookmark Show");
        }
    });

    Object retained = getLastNonConfigurationInstance();
    if (retained instanceof GetSelectedShowsListTask) {

        workerTask = (GetSelectedShowsListTask) retained;
        workerTask.setActivity(this);
    } else {
        workerTask = new GetSelectedShowsListTask(this);
        if (VibeVault.featuredShows.size() == 0) {
            this.fetchSelectedShows("http://andrewpearson.org/vibevault/shows/vvshows1");
        }
        this.refreshSelectedShowsList();
    }
}

From source file:ru.orangesoftware.financisto.widget.QuickAmountInput.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    Activity activity = getActivity();/*from  ww  w. ja  v  a  2 s. c o  m*/
    LinearLayout layout = new LinearLayout(activity);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setBackgroundColor(ContextCompat.getColor(activity, R.color.calculator_background));

    LinearLayout.LayoutParams lpWrapWrap = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    lpWrapWrap.weight = 1;

    // picker
    Currency currency = CurrencyCache.getCurrencyOrEmpty(currencyId);
    picker = new AmountPicker(activity, currency.decimals);
    layout.addView(picker, lpWrapWrap);
    picker.setCurrent(new BigDecimal(amount));
    picker.setOnChangeListener((picker, oldVal, newVal) -> setTitle());

    // buttons
    LinearLayout buttonsLayout = new LinearLayout(new ContextThemeWrapper(activity, R.style.ButtonBar), null,
            R.style.ButtonBar);
    buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);

    Button bOK = new Button(activity);
    bOK.setText(R.string.ok);
    bOK.setOnClickListener(arg0 -> {
        listener.onAmountChanged(picker.getCurrent().toPlainString());
        dismiss();
    });
    buttonsLayout.addView(bOK, lpWrapWrap);

    Button bClear = new Button(activity);
    bClear.setText(R.string.reset);
    bClear.setOnClickListener(arg0 -> picker.setCurrent(BigDecimal.ZERO));
    buttonsLayout.addView(bClear, lpWrapWrap);

    Button bCancel = new Button(activity);
    bCancel.setText(R.string.cancel);
    bCancel.setOnClickListener(arg0 -> dismiss());

    buttonsLayout.addView(bCancel, lpWrapWrap);
    layout.addView(buttonsLayout,
            new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    return layout;
}

From source file:com.vrem.wifianalyzer.wifi.graph.channel.ChannelGraphNavigation.java

private Button makeNavigationItem(@NonNull Context context, @NonNull Configuration configuration,
        Pair<WiFiChannel, WiFiChannel> pair, boolean selected) {
    Button button = new Button(context);
    String text = pair.first.getChannel() + " - " + pair.second.getChannel();
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, TEXT_SIZE_ADJUSTMENT);
    if (configuration.isLargeScreenLayout()) {
        params.setMargins(10, -10, 10, -10);
    } else {/*from w w w  .j a v  a  2  s. com*/
        params.setMargins(5, -30, 5, -30);
    }
    button.setLayoutParams(params);
    button.setVisibility(View.GONE);
    button.setText(text);
    button.setOnClickListener(new ButtonOnClickListener(configuration, pair));
    setSelectedButton(button, selected);
    return button;
}

From source file:org.odk.collect.android.widgets.ImageWidget.java

public ImageWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    mInstanceFolder = Collect.getInstance().getFormController().getInstancePath().getParent();

    TableLayout.LayoutParams params = new TableLayout.LayoutParams();
    params.setMargins(7, 5, 7, 5);//w  ww.j ava2  s. c  o  m

    mErrorTextView = new TextView(context);
    mErrorTextView.setId(QuestionWidget.newUniqueId());
    mErrorTextView.setText("Selected file is not a valid image");

    // setup capture button
    mCaptureButton = new Button(getContext());
    mCaptureButton.setId(QuestionWidget.newUniqueId());
    mCaptureButton.setText(getContext().getString(R.string.capture_image));
    mCaptureButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
    mCaptureButton.setPadding(20, 20, 20, 20);
    mCaptureButton.setEnabled(!prompt.isReadOnly());
    mCaptureButton.setLayoutParams(params);

    // launch capture intent on click
    mCaptureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "captureButton", "click",
                    mPrompt.getIndex());
            mErrorTextView.setVisibility(View.GONE);
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // We give the camera an absolute filename/path where to put the
            // picture because of bug:
            // http://code.google.com/p/android/issues/detail?id=1480
            // The bug appears to be fixed in Android 2.0+, but as of feb 2,
            // 2010, G1 phones only run 1.6. Without specifying the path the
            // images returned by the camera in 1.6 (and earlier) are ~1/4
            // the size. boo.

            // if this gets modified, the onActivityResult in
            // FormEntyActivity will also need to be updated.
            i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getContext(),
                    BuildConfig.APPLICATION_ID + ".provider", new File(Collect.TMPFILE_PATH)));
            try {
                Collect.getInstance().getFormController().setIndexWaitingForData(mPrompt.getIndex());
                ((Activity) getContext()).startActivityForResult(i, FormEntryActivity.IMAGE_CAPTURE);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getContext(),
                        getContext().getString(R.string.activity_not_found, "image capture"),
                        Toast.LENGTH_SHORT).show();
                Collect.getInstance().getFormController().setIndexWaitingForData(null);
            }

        }
    });

    // setup chooser button
    mChooseButton = new Button(getContext());
    mChooseButton.setId(QuestionWidget.newUniqueId());
    mChooseButton.setText(getContext().getString(R.string.choose_image));
    mChooseButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
    mChooseButton.setPadding(20, 20, 20, 20);
    mChooseButton.setEnabled(!prompt.isReadOnly());
    mChooseButton.setLayoutParams(params);

    // launch capture intent on click
    mChooseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "chooseButton", "click",
                    mPrompt.getIndex());
            mErrorTextView.setVisibility(View.GONE);
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.setType("image/*");

            try {
                Collect.getInstance().getFormController().setIndexWaitingForData(mPrompt.getIndex());
                ((Activity) getContext()).startActivityForResult(i, FormEntryActivity.IMAGE_CHOOSER);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getContext(),
                        getContext().getString(R.string.activity_not_found, "choose image"), Toast.LENGTH_SHORT)
                        .show();
                Collect.getInstance().getFormController().setIndexWaitingForData(null);
            }

        }
    });

    // finish complex layout
    LinearLayout answerLayout = new LinearLayout(getContext());
    answerLayout.setOrientation(LinearLayout.VERTICAL);
    answerLayout.addView(mCaptureButton);
    answerLayout.addView(mChooseButton);
    answerLayout.addView(mErrorTextView);

    // and hide the capture and choose button if read-only
    if (prompt.isReadOnly()) {
        mCaptureButton.setVisibility(View.GONE);
        mChooseButton.setVisibility(View.GONE);
    }
    mErrorTextView.setVisibility(View.GONE);

    // retrieve answer from data model and update ui
    mBinaryName = prompt.getAnswerText();

    // Only add the imageView if the user has taken a picture
    if (mBinaryName != null) {
        mImageView = new ImageView(getContext());
        mImageView.setId(QuestionWidget.newUniqueId());
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        File f = new File(mInstanceFolder + File.separator + mBinaryName);

        if (f.exists()) {
            Bitmap bmp = FileUtils.getBitmapScaledToDisplay(f, screenHeight, screenWidth);
            if (bmp == null) {
                mErrorTextView.setVisibility(View.VISIBLE);
            }
            mImageView.setImageBitmap(bmp);
        } else {
            mImageView.setImageBitmap(null);
        }

        mImageView.setPadding(10, 10, 10, 10);
        mImageView.setAdjustViewBounds(true);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collect.getInstance().getActivityLogger().logInstanceAction(this, "viewButton", "click",
                        mPrompt.getIndex());
                Intent i = new Intent("android.intent.action.VIEW");
                Uri uri = MediaUtils
                        .getImageUriFromMediaProvider(mInstanceFolder + File.separator + mBinaryName);
                if (uri != null) {
                    Log.i(t, "setting view path to: " + uri);
                    i.setDataAndType(uri, "image/*");
                    try {
                        getContext().startActivity(i);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(getContext(),
                                getContext().getString(R.string.activity_not_found, "view image"),
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        answerLayout.addView(mImageView);
    }
    addAnswerView(answerLayout);
}