how to work with java making button? in Android app [duplicate] - java

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
how to work with this type button.
if someone pressed the button it will put the string value for next activity and the value will be button name.
LinearLayout loj = (LinearLayout) findViewById(R.id.line);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < eachname.length; i++) {
Button tv = new Button(this);
tv.setLayoutParams(lparams);
tv.setText(eachname[i]);
tv.setId(i);
loj.addView(tv);
} '

Try this.
LinearLayout loj = (LinearLayout) findViewById(R.id.line);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < eachname.length; i++) {
Button tv = new Button(this);
tv.setLayoutParams(lparams);
tv.setText(eachname[i]);
tv.setId(i);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("EXTRA_STRING_VALUE",tv.getText().toString());
startActivity(intent);
}
});
loj.addView(tv);
}

Related

Android - How to set TextInputEditText dynamically while incrementing the hint and adding a scroll view?

I'm trying to get my button to create a new TextInputEditText field every time the user presses the button. So far I've managed to dynamically add a field but I can't figure out how to increment the hint of the EditText.
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
TextInputEditText et = new TextInputEditText(MainActivity.this);
et.setHint("Enter Member + 1");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
}
});
For example, I want the hint to increase the number to "Enter Number + X" each time the button is clicked.
You can use the LinearLayout's getChildCount api, the demo code could be:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
TextInputEditText et = new TextInputEditText(this);
int childNum = layout.getChildCount();
et.setHint(String.format("Enter Member + %d", childNum+1));
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);

How do I add these buttons to my main activity? [duplicate]

This question already has answers here:
How to Programmatically Add Views to Views
(7 answers)
Closed 3 years ago.
I have made some buttons and stored them into an ArrayList. I need to add these buttons to my main activity layout.
I have tried to create a linearlayout and make that my main layout, however the buttons do not show.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// createButtons();setContentView(new CircleView(this));
}
public void createButtons() {
ArrayList<Button> buttons = new ArrayList<>();
int n = 0; // the number of buttons circumferencing the semicircle
for(int i = 0; i < 6; i ++) {
n = 7;
Button button = new Button(this);
double Xval = 500* Math.cos(i * Math.PI / n);
double Yval = 500* Math.sin(i * Math.PI / n);
button.setX((float)(Xval));
button.setY((float)(Yval));
buttons.add(button);
}
}
}
I expect to have my buttons appear in my main activity layout.
I Found some solutions for your case, but not the right solution, you must change it to your own implementation.
One way to do this, is create a layout and get the layout in code by id and in that you insert your buttons. This is one way to do this:
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Inserting mutiple buttons with an event, even if I do prefer to implement OnClickListener on the class:
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();
}
});
}
You can find many samples of android native in the link Android Samples
Source

android - dynamically adding a button to a custom dialogbox

I am writing an android app,
I have an activity, inside it i have a button, and it's on click listener opens a dialog box from a custom XML using the following code:
I would like to add that dialog box another button that is not set in it's XML file.
All the components are excising in the XML and working just fine, the dialog opens but I can't add the b button.
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LinearLayout layout = findViewById(R.id.dialog_layout_root);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
I tried to use this example but it does not work for me. What am i doing wrong here?
Thanks!
Just try below code
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
LinearLayout layout = d.findViewById(R.id.dialog_layout_root);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
layout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
You were using only findViewById(R.id.dialog_layout_root); instead of d.findViewById(R.id.dialog_layout_root);
Here ll_button is the id of layout which contains the two xml buttons you have.
LinearLayout ll_button = d.findViewById(R.id.ll_button);
LinearLayout.LayoutParams layoutParams = new inearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll_button.addView(b, layoutParams);
Remove your last line.
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

Access id of an ImageButton in onClickListener when there are dynamic no. of buttons in Android

I have a tableLayout in Android and each row in the table has an ImageButton, a TextView and another ImageButton. The no. of rows is variable and I am generating the table programmatically at runtime.
I am setting id of the buttons to be same as row number by using ImageButton.setId(i).
When the user clicks on any ImageButton, I want to be able to access its id in the onClickListener. Is there a way to do it? All answers on stackoverflow which I have seen regarding this involve a fixed no. of buttons and use a switch statement to find out the id. But since the no. of buttons is dynamic for me, this won't work for me.
Here's the code:
TableLayout tbl_layout = (TableLayout) findViewById(R.id.tbl_layout);
for (int i=0; i<num_rows; i++) {
TableRow tbl_row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbl_row.setLayoutParams(lp);
tbl_row.setPadding(0, 5, 0, 5);
//tbl_row.setBackground(R.drawable.border);
if(i == 3)
tbl_row.setBackgroundColor(Color.GREEN);
else
tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));
tbl_row.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("Some text goes here\New line.");
ImageButton infoBtn = new ImageButton(this);
infoBtn.setImageResource(R.drawable.ic_menu_info);
infoBtn.setAdjustViewBounds(true);
infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
infoBtn.setMaxHeight(120);
infoBtn.setMaxWidth(120);
infoBtn.setId(i);
//infoBtn.setBackgroundColor(Color.BLUE);
ImageButton cameraBtn = new ImageButton(this);
cameraBtn.setImageResource(R.drawable.ic_menu_camera);
cameraBtn.setAdjustViewBounds(true);
cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
cameraBtn.setMaxHeight(120);
cameraBtn.setMaxWidth(120);
cameraBtn.setId(i);
tbl_row.addView(infoBtn);
tbl_row.addView(tv);
tbl_row.addView(cameraBtn);
tbl_layout.addView(tbl_row, i);
}`
for (int i=0; i<num_rows; i++) {
TableRow tbl_row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbl_row.setLayoutParams(lp);
tbl_row.setPadding(0, 5, 0, 5);
//tbl_row.setBackground(R.drawable.border);
if(i == 3)
tbl_row.setBackgroundColor(Color.GREEN);
else
tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));
tbl_row.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("Some text goes here\New line.");
ImageButton infoBtn = new ImageButton(this);
infoBtn.setImageResource(R.drawable.ic_menu_info);
infoBtn.setAdjustViewBounds(true);
infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
infoBtn.setMaxHeight(120);
infoBtn.setMaxWidth(120);
//infoBtn.setId(i);
//infoBtn.setBackgroundColor(Color.BLUE);
infoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageButton cameraBtn = new ImageButton(this);
cameraBtn.setImageResource(R.drawable.ic_menu_camera);
cameraBtn.setAdjustViewBounds(true);
cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
cameraBtn.setMaxHeight(120);
cameraBtn.setMaxWidth(120);
//cameraBtn.setId(i);
cameraBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
tbl_row.addView(infoBtn);
tbl_row.addView(tv);
tbl_row.addView(cameraBtn);
tbl_layout.addView(tbl_row, i);
}`

How can I edit programmatically created buttons in Android?

I have this code which generates a GUI programmatically
TableLayout table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
table = new TableLayout(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
int count = 0;
for (int r = 0; r < 3; ++r) {
TableRow row = new TableRow(this);
for (int c = 0; c < 3; ++c) {
count++;
final Button btn = new Button(this);
btn.setText("");
btn.setId(count);
btn.setOnClickListener(this);
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
TableRow erow = new TableRow(this);
Button btn = new Button(this);
btn.setText("Reset");
btn.setId(10);
btn.setOnClickListener(this);
erow.addView(btn, cellLp);
table.addView(erow, rowLp);
setContentView(table);
}
In the listener, I can change the text of the button I click this way
#Override
public void onClick(View v) {
Button btn = (Button)v;
btn.setText("text");
}
But, how, can I interact with other buttons? I have tried with something like btn = (Button)findViewById(7); but it doesn't work.
I also tried with an array of buttons but still doesn't work. Any tips?
findViewById() is mainly for views created by inflating XML layout files.
Though you can call setId() for views, in this case you should keep simply references to the programmatically created views, i.e.
private Button button1;
and then just use those fields.
Try to use Tag instead id for your buttons:
for (...) {
count++;
final Button btn = new Button(this);
...
btn.setTag(count);
btn.setOnClickListener(this);
...
}
And in listener call (Integer)v.getTag()

Categories