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:Main.java

public static Button setActionButton(Context context, LinearLayout linearLayout, View.OnClickListener l) {
    Button b = new Button(context);
    b.setText("Click");
    linearLayout.addView(b);/*from w  ww  . j av a2 s.  c o m*/
    b.setOnClickListener(l);
    return b;
}

From source file:Main.java

public static View getTestFragmentView(Activity activity, OnClickListener clickListener) {
    LinearLayout v = new LinearLayout(activity);
    v.setBackgroundColor(Color.BLUE);
    v.setOrientation(LinearLayout.VERTICAL);
    TextView tv1 = new TextView(activity);
    TextView tv2 = new TextView(activity);
    Button b1 = new Button(activity);
    Button b2 = new Button(activity);
    Button b3 = new Button(activity);

    b1.setText("reload 1");
    b2.setText("reload 2");
    b3.setText("reload all");

    b1.setId(1);//from w w w.j a va 2s  .  c o m
    b2.setId(2);
    b3.setId(3);

    tv1.setId(android.R.id.text1);
    tv2.setId(android.R.id.text2);

    b1.setOnClickListener(clickListener);
    b2.setOnClickListener(clickListener);
    b3.setOnClickListener(clickListener);

    v.addView(tv1);
    v.addView(tv2);
    v.addView(b1);
    v.addView(b2);
    v.addView(b3);

    return v;
}

From source file:Main.java

/**
 * Add button to the layout as the left one on a row.
 *
 * @param act     The running activity./*w w w  .j  a  va  2s .  co  m*/
 * @param table   The table of buttons.
 * @param lastRow The previous row of buttons.
 * @param actName Name of the activity.
 * @return The Button object that is constructed.
 */
private static Button addButtonAsFirst(Activity act, TableLayout table, TableRow lastRow, String actName) {

    /**
     * Create new row for the table.
     */
    TableRow newRow = new TableRow(act);
    newRow.setLayoutParams(lastRow.getLayoutParams());

    /**
     * Create both buttons. Set the second invisible.
     */
    Button old = (Button) lastRow.getChildAt(0);

    Button left = new Button(act);
    left.setText(actName);
    left.setLayoutParams(old.getLayoutParams());

    Button right = new Button(act);
    right.setText("TempButton");
    right.setLayoutParams(old.getLayoutParams());

    right.setVisibility(View.INVISIBLE);

    /**
     * Add them on.
     */
    newRow.addView(left);
    newRow.addView(right);

    table.addView(newRow);

    return left;
}

From source file:com.github.preferencefragment.preference.PreferenceWithHeaders.java

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

    // Add a button to the header list.
    if (hasHeaders()) {
        Button button = new Button(this);
        button.setText("Some action");
        setListFooter(button);//from   w w  w .  j av a2  s  . c o m
    }
}

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

/**
 * @return a Button which is centred in its parent and provides visual feedback when clicked
 *///from w w w .  ja  v  a 2s.  co  m
private Button createButton() {
    final Button button = new Button(getContext());
    button.setText("Button inside fragment");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button.setText("Button click successful...");
        }
    });

    final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER;
    button.setLayoutParams(layoutParams);

    return button;
}

From source file:org.inversebit.proto01.main.DiagnoserFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final Button but = new Button(getActivity().getBaseContext());
    but.setText("Start");
    but.setOnClickListener(new OnClickListener() {
        @Override//from   www  .  j  a v  a2  s . c o  m
        public void onClick(View v) {
            but.setVisibility(View.GONE);
            pb = new ProgressBar(getActivity().getBaseContext(), null,
                    android.R.attr.progressBarStyleHorizontal);
            ((LinearLayout) getActivity().findViewById(R.id.progressLayout)).addView(pb, 300, 10);
            getDataAndDiagnose();
        }
    });
    ((LinearLayout) getActivity().findViewById(R.id.progressLayout)).addView(but);
}

From source file:com.android.sslload.SslLoad.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    Thread requestThread = new Thread(this);
    requestThread.setDaemon(true);// w  w  w.java2 s  .c o  m
    requestThread.start();

    button = new Button(this);
    button.setText("GO");
    button.setOnClickListener(this);

    setContentView(button);
}

From source file:com.loopj.android.http.sample.DirectorySample.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button deleteTargetFile = new Button(this);
    deleteTargetFile.setText(R.string.button_delete_target_file);
    deleteTargetFile.setOnClickListener(new View.OnClickListener() {
        @Override/*  ww  w . j av a 2  s  . co m*/
        public void onClick(View v) {
            clearOutputs();
            if (lastResponseHandler != null) {
                File toBeDeleted = lastResponseHandler.getTargetFile();
                debugResponse(LOG_TAG, String.format("File was deleted? %b", toBeDeleted.delete()));
                debugResponse(LOG_TAG, String.format("Delete file path: %s", toBeDeleted.getAbsolutePath()));
            } else {
                debugThrowable(LOG_TAG, new Error("You have to Run example first"));
            }
        }
    });
    cbAppend = new CheckBox(this);
    cbAppend.setText("Constructor \"append\" is true?");
    cbAppend.setChecked(false);
    cbRename = new CheckBox(this);
    cbRename.setText("Constructor \"renameTargetFileIfExists\" is true?");
    cbRename.setChecked(true);
    customFieldsLayout.addView(deleteTargetFile);
    customFieldsLayout.addView(cbAppend);
    customFieldsLayout.addView(cbRename);
}

From source file:com.example.android.supportv7.util.DiffUtilActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    RecyclerView rv = new RecyclerView(this);
    Button shuffle = new Button(this);
    shuffle.setText("Shuffle");
    ll.addView(shuffle);// w  ww . j  a va  2s . c o m
    ll.addView(rv);
    rv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    rv.setLayoutManager(new LinearLayoutManager(this));
    List<String> cheeseList = createRandomCheeseList(Collections.<String>emptyList(), 50);
    final SimpleStringAdapter adapter = new SimpleStringAdapter(this,
            cheeseList.toArray(new String[cheeseList.size()]));
    rv.setAdapter(adapter);
    final AtomicBoolean refreshingList = new AtomicBoolean(false);
    shuffle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (refreshingList.getAndSet(true)) {
                // already refreshing, do not allow modifications
                return;
            }
            //noinspection unchecked
            new AsyncTask<List<String>, Void, Pair<List<String>, DiffUtil.DiffResult>>() {

                @Override
                protected Pair<List<String>, DiffUtil.DiffResult> doInBackground(List<String>... lists) {
                    List<String> oldList = lists[0];
                    List<String> newList = createRandomCheeseList(oldList, 5);
                    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
                    //noinspection unchecked
                    return new Pair(newList, diffResult);
                }

                @Override
                protected void onPostExecute(Pair<List<String>, DiffUtil.DiffResult> resultPair) {
                    refreshingList.set(false);
                    adapter.setValues(resultPair.first);
                    resultPair.second.dispatchUpdatesTo(adapter);
                    Toast.makeText(DiffUtilActivity.this, "new list size " + resultPair.first.size(),
                            Toast.LENGTH_SHORT).show();
                }
            }.execute(adapter.getValues());

        }
    });
    setContentView(ll);
}

From source file:com.github.preferencefragment.preference.LaunchingPreferences.java

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

    /*/*from   w  w w . j a va  2 s.c  om*/
     * These preferences have defaults, so before using them go apply those
     * defaults. This will only execute once -- when the defaults are applied
     * a boolean preference is set so they will not be applied again.
     */
    PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

    // Simple layout
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    setContentView(layout);

    // Create a simple button that will launch the preferences
    Button launchPreferences = new Button(this);
    launchPreferences.setText(getString(R.string.launch_preference_activity));
    launchPreferences.setOnClickListener(this);
    layout.addView(launchPreferences, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    mCounterText = new TextView(this);
    layout.addView(mCounterText, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    updateCounterText();
}