Dynamic buttons removing after app relaunches - java

I've created a button which adds buttons dynamically. It works and when
I click, it adds buttons to activity, but the problem is, when I relaunch the app or open it again, buttons are disappeared. I want those buttons to be permanently. Give the solution. My code is here:
public void onClick(View v)
{
LinearLayout constrain= findViewById(R.id.constrain);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams ( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(second.this);
btn.setOnClickListener ( new View.OnClickListener ( ) {
#Override
public void onClick(View v) {
}
} );
constrain.addView(btn,lp);
}

Related

Multiple denpendent Spinner in Android

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) {
}
});
}

How to add a new button in Main Activity after user clicks a button from a Popup?

I'm developing an Android app on my free time to learn about Android development. I'm trying to make a Grade/GPA Calculator App. I currently have a button called "+ New Semester" whose purpose is to open a popup where the user inputs the semester name. This can be seen in the following two images:
User clicks on "+ New Semester" and the popup appears prompting the user to add the name of that semester.
Now, what I want to do is that when the user clicks on the "Done" button a new button with the text that the user typed in into the text box is created below the "+ New Semester" button, but I can't figure out how to do it. I would appreciate any help.
This is the code I currently have:
package com.example.gradecalculator;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Private Fields
private Dialog d;
private ImageButton newSemesterButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = new Dialog(this);
}
// When user clicks on "+ New Semester" button open a popup where the user is prompted to
// type in the Semester Name and when "Done" is clicked the new semester appears in the view
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
// Open Main Activity
public void openMainActivity() {
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
}
}
You can achieve that by first getting the reference to the layout where you want to add your button. Lets say, the name of the layout where your button should be has the name btn_layout,
Bind the layout. (i.e. LinearLayout layout = findViewById (R.id.btn_layout); )
When the DONE button is clicked, create your new button and add it to the layout. For example:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText("Your text from edittext");
layout.addView(btn, params);
Even better if you can declare the Button and Layout as fields
You have to get the reference from your buttons parent layout. then create a button and add it to the view. Something like this:
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myParentLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
params = (LinearLayout.LayoutParams) new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
yourEditText = d.findViewById(R.id.YourEditText);
myNewButton = new Button(this);
String buttonText = yourEditText.text.toString();
myNewButton.setText(buttonText);
myParentLayout.addView(myNewButton, params);
d.dismiss();
}
});
d.show();
}
Just make that new button on main screen and set visibility to false = newButton.setVisibility(false);
and if you press button done on popup just set visibility to true hope that will help, not sure if that's what you mean

I want to create a checkbox when button is clicked

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);
}
});

How to edit/delete objects from another method that were created programatically

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);
}
});

How to create buttons on pressing a button?

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)
}

Categories