LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(30, 20, 30, 0); Button okButton=new Button(this); okButton.setText("some text"); ll.addView(okButton, layoutParams);
Button bt = new Button(this); bt.setText("A Button"); bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); linerLayout.addView(bt);
ll.setOrientation(LinearLayout.VERTICAL); tv.setText("If you enjoy using " + APP_TITLE + ", please take a moment to rate it. Thanks for your support!"); tv.setWidth(240); tv.setPadding(4, 0, 4, 10); ll.addView(tv); b1.setText("Rate " + APP_TITLE); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME))); ll.addView(b1); b2.setText("Remind me later"); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); ll.addView(b2); b3.setText("No, thanks"); b3.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (editor != null) {
import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ThirdActivity extends RootActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.tvTitle); tv.setText("Third Activity"); Button bt = (Button) findViewById(R.id.buttonNext); bt.setText("previous"); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
for(int i = 0;i < playercountint; i++) { LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,(100/playercountint))); layout.setGravity(Gravity.CENTER_HORIZONTAL); final TextView tt = new TextView(this); tt.setText("0"); tt.setTextSize(14); tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tt.setGravity(Gravity.CENTER_HORIZONTAL); Button btadd = new Button(this); btadd.setText("Add"); btadd.setId(2); btadd.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { tt.setText(String.valueOf(Integer.parseInt(tt.getText())+1)); } }); }
public class Myfragment extends Fragment { LinearLayout linearLayout; View rootView; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment1, container, false); return rootView; } public void addPlaces() { Button button = new Button(getActivity()); // needs activity context button.setText("button name"); linearLayout.addView(button); } }
super.onActivityCreated(savedInstanceState); Button b = (Button) rootView.findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { button.setText("button name"); linearLayout.addView(button);
Button mButton_dial = null; mButton_dial = new Button(this); mButton_dial.setText("Dial!"); mLinearLayout_no_button = new LinearLayout(this); mLinearLayout_no_button.addView(mButton_dial); mButton_dial.setGravity(20); mButton_dial.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } });
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); Button button2 = new Button(this); button2.setText("I am button 2"); button2.setId(1000); m_relativeLayout.addView(button2, layoutParams); RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) m_listView .getLayoutParams(); rlp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
public class AlertDialogTestActivity extends Activity { AlertDialog alert; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button connect = new Button(this); connect.setText("Don't push me"); AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); alertBuilder.setTitle("Profile"); alertBuilder.setView(connect); connect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { alert.dismiss(); } }); alert = alertBuilder.create(); } }
class FragmentA extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button button = new Button(getActivity()); button.setText("Replace"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); FragmentB fragmentB = new FragmentB(); transaction.replace(R.id.container_id, fragmentB); transaction.commit(); } }); return button; } }
public class MainActivity extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TableLayout layout = new TableLayout (this); layout.setLayoutParams( new TableLayout.LayoutParams(4,5) ); layout.setPadding(1,1,1,1); for (int f=0; f<=13; f++) { TableRow tr = new TableRow(this); for (int c=0; c<=9; c++) { Button b = new Button (this); b.setText(""+f+c); b.setTextSize(10.0f); b.setTextColor(Color.rgb( 100, 200, 200)); b.setOnClickListener(this); tr.addView(b, 30,30); } // for layout.addView(tr); } // for super.setContentView(layout); } // () public void onClick(View view) { ((Button) view).setText("*"); ((Button) view).setEnabled(false); } } // class
Button button=new Button(getActivity()); LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.ALIGN_PARENT_TOP); button.setText("openDrawer"); root.addView(button); button.setOnClickListener(new OnClickListener() {
LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(10, 20, 30, 40); Button button = new Button(this); button.setText("some text"); layout.addView(button, params);
ll.setOrientation(LinearLayout.VERTICAL); tv.setText("If you enjoy using " + APP_TITLE + ", please take a moment to rate it. Thanks for your support!"); tv.setWidth(240); tv.setPadding(4, 0, 4, 10); ll.addView(tv); b1.setText("Rate " + APP_TITLE); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME))); ll.addView(b1); b2.setText("Remind me later"); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); ll.addView(b2); b3.setText("No, thanks"); b3.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (editor != null) {
// Inflate the layout and find the component views to configure final View item = inflater.inflate(R.layout.item, parentView, false); final TextView label = (TextView) item.findViewById(R.id.my_label); final Button button = (Button) item.findViewById(R.id.my_button); // Configure component views label.setText(labelText); button.setText(buttonText); button.setOnClickListener(buttonClickListener); // Add to parent parentView.addView(item);
for (int i = 1; i <= 20; i++) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); Button btn = new Button(this); btn.setId(i); final int id_ = btn.getId(); btn.setText("button " + id_); btn.setBackgroundColor(Color.rgb(70, 80, 90)); linear.addView(btn, params); btn1 = ((Button) findViewById(id_)); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Toast.makeText(view.getContext(), "Button clicked index = " + id_, Toast.LENGTH_SHORT) .show(); } }); }
public class MyActivity extends Activity { protected void onCreate(Bundle icicle) { super.onCreate(icicle); final Button button = new Button(this); button.setText("Press me!"); setContentView(button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); } }
/* Find Tablelayout defined in main.xml */ TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines); /* Create a new row to be added. */ TableRow tr = new TableRow(this); tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); /* Create a Button to be the row-content. */ Button b = new Button(this); b.setText("Dynamic Button"); b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); /* Add Button to row. */ tr.addView(b); /* Add row to TableLayout. */ //tr.setBackgroundResource(R.drawable.sf_gradient_03); tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));