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.example.moneymeterexample.ViewExpenseActivity.java

private void fillListViewByCat() {
    DataBaseHelper db;//from   w w  w . jav  a  2  s  .c o  m
    db = new DataBaseHelper(this);
    ArrayList<ExpenseEntry> exp_list = new ArrayList<ExpenseEntry>();
    String cat_value = getIntent().getExtras().getString("cat_value");
    exp_list = db.getExpenseByCategory(cat_value);
    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.matthewtamlin.sliding_intro_screen_manual_testing.TestButtonConfig.java

/**
 * @return a Button which enables/disables the left button
 *///  w w w. ja  v a 2s.  c  om
private Button createToggleLeftButtonButton() {
    final Button button = new Button(this);
    button.setText("Enable/disable left button");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final boolean initiallyDisabled = leftButtonIsEntirelyDisabled();
            disableLeftButton(!initiallyDisabled);
            assertThat("left button didn't disable/enable correctly",
                    initiallyDisabled != leftButtonIsEntirelyDisabled());
        }
    });

    return button;
}

From source file:edu.stanford.mobisocial.dungbeetle.ui.ViewContactActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feed_home);

    checked = new boolean[filterTypes.length];

    for (int x = 0; x < filterTypes.length; x++) {
        checked[x] = true;//from w  w  w .  j  a v a 2 s .co m
    }

    findViewById(R.id.btn_broadcast).setVisibility(View.GONE);
    mContactId = getIntent().getLongExtra("contact_id", -1);
    if (mContactId == -1) {
        Uri data = getIntent().getData();
        if (data != null) {
            try {
                mContactId = Long.parseLong(data.getLastPathSegment());
            } catch (NumberFormatException e) {
            }
        }
    }

    Bundle args = new Bundle();
    args.putLong("contact_id", mContactId);
    Fragment profileFragment = new ViewProfileFragment();
    profileFragment.setArguments(args);
    if (mContactId == Contact.MY_ID) {
        doTitleBar(this, "My Profile");
        mLabels.add("View");
        mLabels.add("Edit");
        mFragments.add(profileFragment);
        mFragments.add(new EditProfileFragment());

        // TODO: Legitimize this. Move objects to a randomly generated private feed
        // Ability to "move" private feeds.
        // Have a "feedPtrObj" that tracks your current private. Store in-app feed.
        Uri privateUri = Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/feeds/private");
        mLabels.add("Notes");
        Fragment feedView = new FeedViewFragment();
        args = new Bundle(args);
        args.putParcelable(FeedViewFragment.ARG_FEED_URI, privateUri);
        feedView.setArguments(args);
        mFragments.add(feedView);
    } else {
        String title = "Profile";
        Uri feedUri = null;
        try {
            Contact contact = Contact.forId(this, mContactId).get();
            title = contact.name;
            feedUri = contact.getFeedUri();
        } catch (NoValError e) {
        }
        args.putParcelable(FeedViewFragment.ARG_FEED_URI, feedUri);
        doTitleBar(this, title);
        mLabels.add("Feed");
        mLabels.add("Apps");
        mLabels.add("Profile");
        Fragment feedView = new FeedViewFragment();
        feedView.setArguments(args);
        Fragment appView = new AppsViewFragment();
        appView.setArguments(args);

        mFragments.add(feedView);
        mFragments.add(appView);
        mFragments.add(profileFragment);

        if (MusubiBaseActivity.getInstance().isDeveloperModeEnabled()) {
            FeedView sharingView = new PresenceView();
            sharingView.getFragment().setArguments(args);
            mLabels.add(sharingView.getName());
            mFragments.add(sharingView.getFragment());
        }
    }

    PagerAdapter adapter = new ViewFragmentAdapter(getSupportFragmentManager(), mFragments);
    mViewPager = (ViewPager) findViewById(R.id.feed_pager);
    mViewPager.setAdapter(adapter);
    mViewPager.setOnPageChangeListener(this);

    ViewGroup group = (ViewGroup) findViewById(R.id.tab_frame);
    int i = 0;
    for (String s : mLabels) {
        Button button = new Button(this);
        button.setText(s);
        button.setTextSize(18f);

        button.setLayoutParams(CommonLayouts.FULL_HEIGHT);
        button.setTag(i++);
        button.setOnClickListener(mViewSelected);

        group.addView(button);
        mButtons.add(button);
    }

    // Listen for future changes
    Uri feedUri;
    if (mContactId == Contact.MY_ID) {
        feedUri = Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/feeds/me");
    } else {
        feedUri = Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts");
    }
    mProfileContentObserver = new ProfileContentObserver(mHandler);
    getContentResolver().registerContentObserver(feedUri, true, mProfileContentObserver);

    onPageSelected(0);
}

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

/**
 * @return creates a Button which locks the ViewPager form touch events and commands
 *//*from   w w w .java  2  s  .  c  o m*/
private Button createLockAllButton() {
    final Button button = new Button(this);
    button.setText("Lock all");

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

    return button;
}

From source file:org.borderstone.tagtags.TTProtocolActivity.java

private void completeDrawer() {
    for (final String s : bonus) {
        Button l = new Button(this);

        l.setCompoundDrawablesWithIntrinsicBounds(R.drawable.document, 0, 0, 0);
        l.setOnClickListener(new View.OnClickListener() {
            @Override//from  w w w. j a  v a  2  s. c  o  m
            public void onClick(View v) {
                openFile(s);
            }
        });

        mDrawerList.addView(l);
    }

    mDrawerList.addView(lblGlobal);
    mDrawerList.addView(btnUpload);

    lblPercent.setTextSize(Constants.fontSize + 4);
    lblPercent.setTextColor(Color.WHITE);
    lblInfo.setTextColor(Color.WHITE);

    lblPercent.setGravity(Gravity.CENTER);
    lblInfo.setGravity(Gravity.CENTER);

    mDrawerList.addView(lblPercent);
    mDrawerList.addView(lblInfo);
}

From source file:org.cafemember.messenger.mytg.fragments.TransfareActivity.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    context = getContext();//from   ww w.  j a v  a2 s  .  c o  m
    //        View layout = inflater.inflate(R.layout.channels_layout, null);
    /*this.context = context;
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    actionBar.setTitle(LocaleController.getString("MenuTransfare", R.string.MenuTransfare));
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
    @Override
    public void onItemClick(int id) {
        if (id == -1) {
            finishFragment();
        } else if (id == done_button) {
            saveName();
        }else if (id == contact_button) {
            getContacts();
        }
    }
    });
            
    ActionBarMenu menu = actionBar.createMenu();
    contactsButton = menu.addItemWithWidth(contact_button, R.drawable.user_profile, AndroidUtilities.dp(56));
    doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56));
    */

    LinearLayout fragmentView = new LinearLayout(context);
    ((LinearLayout) fragmentView).setOrientation(LinearLayout.VERTICAL);
    fragmentView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
    TextView ruleTextView = new TextView(context);
    ruleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    ruleTextView.setTextColor(0xff212121);
    ruleTextView.setGravity(Gravity.RIGHT);
    //        ruleTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("ShareText", R.string.ShareText)));
    ruleTextView.setText(LocaleController.getString("transfare_rule", R.string.transfare_rule));
    ((LinearLayout) fragmentView).addView(ruleTextView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT,
            LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 24, 10, 24, 30));

    phoneField = new EditText(context);
    phoneField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    phoneField.setHintTextColor(0xff979797);
    phoneField.setTextColor(0xff212121);
    phoneField.setMaxLines(1);
    phoneField.setLines(1);
    phoneField.setGravity(Gravity.RIGHT);
    phoneField.setPadding(0, 0, 0, 0);
    phoneField.setSingleLine(true);
    phoneField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
    phoneField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_MULTI_LINE
            | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
    phoneField.setImeOptions(EditorInfo.IME_ACTION_DONE);
    phoneField.setHint(LocaleController.getString("TransfareUserHint", R.string.TransfareUserHint));
    AndroidUtilities.clearCursorDrawable(phoneField);

    amountField = new EditText(context);
    amountField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    amountField.setHintTextColor(0xff979797);
    amountField.setTextColor(0xff212121);
    amountField.setMaxLines(1);
    amountField.setGravity(Gravity.RIGHT);
    amountField.setLines(1);
    amountField.setPadding(0, 0, 0, 0);
    amountField.setSingleLine(true);
    amountField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
    amountField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_MULTI_LINE
            | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
    amountField.setImeOptions(EditorInfo.IME_ACTION_DONE);
    amountField.setHint(LocaleController.getString("TransfareAmountHint", R.string.TransfareAmountHint));
    AndroidUtilities.clearCursorDrawable(amountField);

    phoneField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_DONE && doneButton != null) {
                doneButton.performClick();
                return true;
            }
            return false;
        }
    });

    ((LinearLayout) fragmentView).addView(phoneField,
            LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 0));

    checkTextView = new TextView(context);
    checkTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    checkTextView.setGravity(Gravity.RIGHT);
    ((LinearLayout) fragmentView).addView(checkTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT,
            LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 12, 24, 0));

    Button button = new Button(context);

    button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    button.setTextColor(0xff6d6d72);
    button.setGravity(Gravity.CENTER);
    TextView helpTextView = new TextView(context);
    helpTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    helpTextView.setTextColor(0xff6d6d72);
    helpTextView.setGravity(Gravity.RIGHT);
    helpTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("RefHelp", R.string.RefHelp)));
    button.setText("");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveName();
        }
    });
    ((LinearLayout) fragmentView).addView(helpTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT,
            LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 10, 24, 0));
    ((LinearLayout) fragmentView).addView(amountField,
            LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 0));
    ((LinearLayout) fragmentView).addView(button,
            LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 24, 24, 24, 0));

    /*View v = LayoutInflater.from(context).inflate(R.layout.transfare_type, null);
    typeJoin = (RadioButton)v.findViewById(R.id.joinRadio);
    typeView = (RadioButton)v.findViewById(R.id.viewRadio);
    ((LinearLayout) fragmentView).addView(v, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 0));*/
    phoneField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            checkUserName(phoneField.getText().toString(), false);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    checkTextView.setVisibility(View.GONE);
    FontManager.instance().setTypefaceImmediate(fragmentView);

    return fragmentView;
}

From source file:com.wallerlab.compcellscope.Image_Gallery.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image__gallery);
    counter = 0;/*www  . jav a  2s .co  m*/
    final ImageView currPic = new ImageView(this);

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    final int screen_width = metrics.widthPixels;

    SharedPreferences settings = getSharedPreferences(Folder_Chooser.PREFS_NAME, 0);
    //SharedPreferences.Editor edit = settings.edit();
    String path = settings.getString(Folder_Chooser.location_name,
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());

    //Log.d(LOG, "  |  " + path + " |  ");
    TextView text1 = (TextView) findViewById(R.id.text1);
    final TextView text2 = (TextView) findViewById(R.id.text2);

    text1.setText(path);

    File directory = new File(path);

    // get all the files from a directory
    File[] dump_files = directory.listFiles();
    Log.d(LOG, dump_files.length + " ");

    final File[] fList = removeElements(dump_files, "info.json");

    Log.d(LOG, "Filtered Length: " + fList.length);

    Arrays.sort(fList, new Comparator<File>() {
        @Override
        public int compare(File file, File file2) {
            String one = file.toString();
            String two = file2.toString();
            //Log.d(LOG, "one: " + one);
            //Log.d(LOG, "two: " + two);
            int num_one = Integer.parseInt(one.substring(one.lastIndexOf("(") + 1, one.lastIndexOf(")")));
            int num_two = Integer.parseInt(two.substring(two.lastIndexOf("(") + 1, two.lastIndexOf(")")));
            return num_one - num_two;
        }
    });

    try {
        writeJsonFile(fList);
    } catch (JSONException e) {
        Log.d(LOG, "JSON WRITE FAILED");
    }
    //List names programattically
    LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.linear_table);
    final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, screen_width + 4);

    myLinearLayout.setOrientation(LinearLayout.VERTICAL);

    if (fList != null) {
        File cur_pic = fList[0];

        BitmapFactory.Options opts = new BitmapFactory.Options();
        Log.d(LOG, "\n File Location: " + cur_pic.getAbsolutePath() + " \n" + " Parent: "
                + cur_pic.getParent().toString() + "\n");

        Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);

        currPic.setLayoutParams(params);
        currPic.setImageBitmap(myImage);
        currPic.setId(View.generateViewId());
        text2.setText(cur_pic.getName());
    }

    myLinearLayout.addView(currPic);

    //Seekbar
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    seekBar.setEnabled(true);
    seekBar.setMax(fList.length - 1);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = 0;
        int length = fList.length;

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            setCounter(i);
            if (getCounter() <= fList.length) {
                File cur_pic = fList[getCounter()];
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);
                int image_width = opts.outWidth;
                int image_height = opts.outHeight;
                int sampleSize = image_width / screen_width;
                opts.inSampleSize = sampleSize;

                text2.setText(cur_pic.getName());

                opts.inJustDecodeBounds = false;
                myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);

                currPic.setImageBitmap(myImage);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    //Make Button Layout
    LinearLayout buttonLayout = new LinearLayout(this);
    buttonLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
    buttonLayout.setLayoutParams(LLParams);
    buttonLayout.setId(View.generateViewId());

    //Button Layout Params
    LinearLayout.LayoutParams param_button = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);

    //Prev Pic
    Button prevPic = new Button(this);
    prevPic.setText("Previous");
    prevPic.setId(View.generateViewId());
    prevPic.setLayoutParams(param_button);

    prevPic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (fList != null) {
                decrementCounter();
                if (getCounter() >= 0) {
                    File cur_pic = fList[getCounter()];
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds = true;
                    Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);
                    int image_width = opts.outWidth;
                    int image_height = opts.outHeight;
                    int sampleSize = image_width / screen_width;
                    opts.inSampleSize = sampleSize;

                    text2.setText(cur_pic.getName());

                    opts.inJustDecodeBounds = false;
                    myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);
                    //Log.d(LOG, getCounter() + "");
                    seekBar.setProgress(getCounter());
                    currPic.setImageBitmap(myImage);

                } else {
                    setCounter(0);
                }

            } else {
                Toast.makeText(Image_Gallery.this, "There are no pictures in this folder", Toast.LENGTH_SHORT);
                setCounter(getCounter() - 1);
            }
        }
    });

    buttonLayout.addView(prevPic);

    // Next Picture  Button
    Button nextPic = new Button(this);
    nextPic.setText("Next");
    nextPic.setId(View.generateViewId());
    nextPic.setLayoutParams(param_button);

    buttonLayout.addView(nextPic);
    nextPic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (fList != null) {
                incrementCounter();
                if (getCounter() < fList.length) {
                    File cur_pic = fList[getCounter()];
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds = true;
                    Bitmap myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);
                    int image_width = opts.outWidth;
                    int image_height = opts.outHeight;
                    int sampleSize = image_width / screen_width;
                    opts.inSampleSize = sampleSize;

                    text2.setText(cur_pic.getName());

                    opts.inJustDecodeBounds = false;
                    myImage = BitmapFactory.decodeFile(cur_pic.getAbsolutePath(), opts);
                    //Log.d(LOG, getCounter() + "");
                    seekBar.setProgress(getCounter());

                    currPic.setImageBitmap(myImage);
                } else {
                    setCounter(getCounter() - 1);
                }

            } else {
                Toast.makeText(Image_Gallery.this, "There are no pictures in this folder", Toast.LENGTH_SHORT);
                setCounter(getCounter() - 1);
            }
        }
    });

    myLinearLayout.addView(buttonLayout);
}

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

/**
 * @return creates a Button which unlocks the ViewPager
 *///from   w  w w  .  jav  a 2 s  .c  o  m
private Button createUnlockButton() {
    final Button button = new Button(this);
    button.setText("Unlock");

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

    return button;
}

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

/**
 * @return a Button which enables/disables the right button
 *//*from   w w w  .ja v  a  2 s.  c om*/
private Button createToggleRightButtonButton() {
    final Button button = new Button(this);
    button.setText("Enable/disable right button");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final boolean initiallyDisabled = rightButtonIsDisabled();
            disableRightButton(!initiallyDisabled);
            assertThat("right button didn't disable/enable correctly",
                    initiallyDisabled != rightButtonIsDisabled());
        }
    });

    return button;
}