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) {
}
});
}
Related
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'm using a spinner for selecting amounts to re-calculate quantities of recipe-ingredients.
I have a horizontal LinearLayout which I add a label(TextView) to to describe the spinner and then the spinner.
When I first load the activity, the label is positioned a bit higher than the spinner, when I select an item, it corrects its height.
How do I fix that?
Code of my spinner-loading method:
// Horizontal LinearLayout for displaying label + spinner
LinearLayout spinnerLayout = new LinearLayout(getApplicationContext());
spinnerLayout.setOrientation(LinearLayout.HORIZONTAL);
spinnerLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
// Textview for description
TextView lblAmount = new TextView(getApplicationContext());
lblAmount.setText("Quantity:");
lblAmount.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
lblAmount.setTextColor(Color.parseColor("#161618"));
spinnerLayout.addView(lblAmount);
// add spinner
ArrayList<Integer> spinnerArray = new ArrayList<Integer>();
for(int i = 1; i<= 10; i++) {
spinnerArray.add(i);
}
final Spinner spnIngredientQuantity = new Spinner(this);
ArrayAdapter<Integer> spinnerArrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spnIngredientQuantity.setAdapter(spinnerArrayAdapter);
spnIngredientQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
spinnerLayout.addView(spnIngredientQuantity);
if(firstLoad) {
spnIngredientQuantity.setSelection(spinnerArrayAdapter.getPosition(recipe.getDefaultAmount()));
firstLoad = false;
}
spnIngredientQuantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
reloadIngredients((int) spnIngredientQuantity.getSelectedItem());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Use default Quantity
}
});
scrollLayout.addView(spinnerLayout);
I think it might be the fact, that I add the spinnerLayout with height set to WRAP_CONTENT, so it's smaller when the TextView comes in. later, the spinner takes more space in height, but the textView doesn't get updatet.
Could that be the problem?
edit: attached screenshot
I don't know what actually caused the problem,but I fixed it with centering the textView horizontally:
spinnerLayout.setGravity(Gravity.CENTER_VERTICAL);
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);
}
});
I have tabHost widget with dynamic created tabs.
In each of newly created tab, there is relative layout with some views (textView, listView etc).
My question is: how to access to these views and how to append new content there? I tried with setting ids or tags, but i didn't work.
public class Main extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("buttontab");
spec.setContent(R.id.form);
spec.setIndicator("Search-form");
tabs.addTab(spec);
Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(Main.this);
// Defining the RelativeLayout layout parameters.
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView t = new TextView(Main.this);
// t.setId(12); //This dosen't work
t.setText("Searching in progress. Please wait...");
ListView resultsList = new ListView(Main.this);
//Here: array adapter etc
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(t);
relativeLayout.addView(resultsList);
return relativeLayout;
}
});
spec.setIndicator("Tab with results");
tabs.addTab(spec);
new GetData(Main.this).execute();
}
});
}
public void appendResultsToViews(String result)
{
//Here i need to access TextView from tab with specific tag and append results to TextView, which was created dynamically earlier
}
Thank you for your help.
set tag of child view and use findViewWithTag(tag)
I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....