I have a method that creates layouts vertically each with 3 buttons aligned horizontally with onClick creating one at a time with each click to a max of 5 layouts. How do I remove those layouts with a button that was created.
public void addTroop(Editable name){
LinearLayout mainPage = (LinearLayout) findViewById(R.id.manageTroopsMain);
if (count <= 5)
{
//CREATE NEW LINEAR LAYOUT
LinearLayout addTroopLayout = new LinearLayout(this);
//CREATE LAYOUT PARAMS FOR LAYOUT
LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newLayout.bottomMargin = 10;
//STYLE NEW LINEAR LAYOUT
addTroopLayout.setTag("addTroopLayout" + count);
addTroopLayout.setLayoutParams(newLayout);
addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);
//CREATE NEW BUTTONS
Button newTroop = new Button(this);
Button remove = new Button(this);
Button change = new Button(this);
//CREATE LAYOUT PARAMS FOR BUTTONS
LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
//STYLE NEW BUTTONS
newTroop.setText(name);
newTroop.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
newTroop.setLayoutParams(newTroopParam);
remove.setTag("rmvBtn" + count);
remove.setText("-");
remove.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
remove.setLayoutParams(rmvBtnParam);
change.setText("...");
change.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
change.setLayoutParams(chngNameParam);
//ADD VIEWS TO NEW LAYOUT
addTroopLayout.addView(newTroop);
addTroopLayout.addView(remove);
addTroopLayout.addView(change);
//ADD NEW LAYOUT TO mainPage LAYOUT
mainPage.addView(addTroopLayout);
//Increment Counter
count++;
}
}
Each time a layout is created it set with the following so that each layout will be named addTroopLayout1, addTroopLayout2, etc etc.
addTroopLayout.setTag("addTroopLayout" + count);
I did the same thing with the remove button
remove.setTag("rmvBtn" + count);
So now how do I access/edit/remove these? Could I do something like
Button rmvBtn1 = (Button) findViewByTag(R.id.rmvBtn1);
rmvBtn1.setOnClickListener(new view.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//REMOVE CREATED LAYOUT
//DECREMENT ALL LAYOUT TAGS BY 1 IF NOT LAST LAYOUT
//DECREMENT count VAR BY ONE
//ERASE ALL VARIABLES ASSOCIATED WITH THIS VIEW
}
});
This code is obviously giving me errors and im sure this is something simple. Thanks.
////////////////////EDIT/////////////////////
Sorry if I wasent clear on what I wanted. So I have created/styled a button using my if statement that is called from a method using an onClick:
Button newTroop = new Button(this);
remove.setTag("rmvBtn" + count);
Count starts at 1 and is incremented in when a button is pressed. Now I want to findViewByTag for that button to use it with something like:enter code here
Button rmvBtn1 = (Button) findViewByTag(rmvBtn1);
rmvBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//REMOVE CREATED LAYOUT
//DECREMENT ALL LAYOUT TAGS BY 1 IF NOT LAST LAYOUT
//DECREMENT count VAR BY ONE
//ERASE ALL VARIABLES ASSOCIATED WITH THIS VIEW
}
});
So specifically how to I use a button that was created with my if statement? Do I still have to define it by
Button rmvBtn1 = (Button) findViewByTag(rmvBtn1);
or is there another way I should be doing this?
keep view in remove button, and keep view's parent int view's tag
addTroopLayout.setTag(mainPage);
remove.setTag(addTroopLayout);
and then use it like this
rmvBtn1.setOnClickListener(new view.OnClickListener() {
#Override
public void onClick(View v) {
View willRemove = (View)v.getTag();
LinearLayout mainPage = (LinearLayout)willRemove.getTag();
mainPage.removeView(willRemove);
}
});
Related
In my app there is a Spinner for colors and a Button for the user to add additional spinners in different colors. An existing spinner is coupled with a delete button for removal upon creation. It looks like this -
I'm trying to accomplish the following:
The user clicks "ADD PAINTYPE"
A Spinner is created, given it is not already present in the List
If the user deletes the item, it is available to insert again.
I´m pretty new to Android App development, and this logic is difficult for me to work through. Any suggestions on how to approach this would be appreciated.
Edit: Blow down is the function, which called after user click the button "ADD PAINTYPE". The arrayAdapter of the newly created spinner is as same as the fisrt one(which in the left side of the "ADD PAINTYPE" button). Right now all Spinners are sharing the sam adapter. What I want to achieve is, When I choose a Option in one spinner, then dynamiclly the option will not be listed in other spinners.(e.g. The "ANFALLSTARTIGER" would not be listed in the second and the third spinners). Is this possible?
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
protected void addItem(Context context,View view) {
if(clickedTimes < 16) {
clickedTimes++;
final LinearLayout linearLayout = new LinearLayout(context);
Spinner spinner = new Spinner(context);
spinner.setPopupBackgroundResource(R.color.white);
spinner.setBackgroundColor(getResources().getColor(R.color.white));
final Button button = new Button(context);
final Button button_delete = new Button(context);
button_delete.setText("DELETE");
button_delete.setTextColor(getResources().getColor(R.color.white));
button_delete.setBackground(context.getDrawable(R.drawable.mybutton));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setWeightSum(6);
LinearLayout.LayoutParams params_0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params_0);
LinearLayout.LayoutParams params_1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 4.0f);
spinner.setLayoutParams(params_1);
params_1.setMargins(2, 2, 2, 2);
LinearLayout.LayoutParams params_2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
params_2.setMargins(2, 2, 2, 2);
button.setLayoutParams(params_2);
button_delete.setLayoutParams(params_2);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.all_paintype);
layout.addView(linearLayout);
linearLayout.addView(spinner);
linearLayout.addView(button);
linearLayout.addView(button_delete);
button_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layoutAll = (LinearLayout) view.findViewById(R.id.all_paintype);
layoutAll.removeView(linearLayout);
spinners.remove(spinner);
clickedTimes--;
}
});
dropBoxButton(spinner, button, clickedTimes);
spinners.add(spinner);
}else {
Toast toast = Toast.makeText(getContext(),"Maximal Pain Type",Toast.LENGTH_SHORT);
toast.show();
}
}
protected void dropBoxButton(Spinner spinner, final Button button, int clickedTimes) {
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(clickedTimes);
spinner.setVisibility(View.VISIBLE);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string_1 = ((TextView) view).getText().toString();
setButtonColor(button, string_1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
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);
I want to make it so that every time someone types something in the editText and clicks the button, the text comes up. It does it in my code, but I also want to add a checkbox next to it. How can I add the checkbox every time the button is pressed?
This is how it looks in the emulator right now after I typed test in the editText:
https://gyazo.com/6b9a050976ecd2c4b509220263bbdce1
The second problem is: when I write a second todo, it overwrites the last one so right now I can only have 1 todo. Why?
Code:
final TextView textViewPrint;
Button btn_print;
final EditText editTextType;
textViewPrint = findViewById(R.id.print_text);
btn_print = findViewById(R.id.button_add);
editTextType = findViewById(R.id.test_textEdit);
btn_print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textViewPrint.setText(editTextType.getText().toString()) ;
editTextType.setText("");
}
});
You can add to your layout programmatically after it has been inflated. Add an id to your LinearLayout:
android:id ="#+id/layout"
Then in OnCreate (after your existing code), get a reference to it and add controls as you wish. And add these lines in OnCreate, For example:
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
CheckBox cb = new CheckBox(this);
int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
l.addView(cb, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(l);
First You need add to LinearLayout layout
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
btn_print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = new CheckBox(this);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cb.setLayoutParams(lparams);
l.addView(cb);
}
});
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);
}`
I am using following code to create buttons in a horizontal layout using array of button names:
LinearLayout tabView = (LinearLayout) findViewById(R.id.tabView);
tabView.setOrientation(LinearLayout.HORIZONTAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < tabButtonNames.length; i++) {
Button btnTag = new Button(this);
btnTag.setText(tabButtonNames[i]);
btnTag.setWidth(50);
btnTag.setHeight(14);
btnTag.setTextSize(8);
btnTag.setId(i);
btnTag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
---the code TODO
});
tabView.addView(btnTag);
}
They are created but I cannot change the height and the width of the buttons using setWidth, setHeight or LayoutParam. Then on pressing a button, I want to create a list of more buttons in my vertical layout using an array of button names. I used the same code as above in onClick method, but application crashes on pressing button. Also Button btn=new Button(this) cannot be used in onClick.
I have done this in i-Pad app easily,but here I am having trouble.
Use
Button btn = new Button(getApplicationContext());
OR
Button btn = new Button(ActivityName.this);
instead of
Button btn = new Button(this);
As Button requires context. And in OnClick, context of Activity is not accessible.
Button btn=new Button(this) is actually referring your clicklistiner, you have to refer your class, Button btn=new Button(classname.this) or create a simple function outside clickListener.
Just pass the context in new Button() and set layout params instead of height and width
for (int i = 0; i < tabButtonNames.length; i++) {
Button btnTag = new Button(<-Context->);//You need to pass context just write <ActivityName>.this
btnTag.setText(tabButtonNames[i]);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(<width>,<height>);(50,40)
//btnTag.setWidth(50);
//btnTag.setHeight(14);
btnTag.setTextSize(8);
btnTag.setId(i);
btnTag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
tabView.addView(btnTag);
btnTag.setLayoutParams(params)
}