I am able to dynamically add edittext into layout on button click.
now i want user to also be able to remove the respective edittext.
I am able to delete edittext from arraylist, But i also want it to be gone from my layout/Screen.
how do i do it?
Is it possible?
any help would be appreciated.
Thanking you in advance.
Code Reference:
Adding Edittext to arraylist first:
ArrayList<EditText> list = new ArrayList<>();
ArrayList<Button> listbtn = new ArrayList<>();
Step 2:
spinnerAdd_Field.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener()
{
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, final Object item)
{
// Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
{
editText = new EditText(Add_Account.this);
btnremove = new Button(Add_Account.this);
RelativeLayout.LayoutParams lpb = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 120);
lpb.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lpb.setMargins(0, 0, 0, 32);
btnremove.setLayoutParams(lpb);
btnremove.setPadding(46, 0,0,0);
btnremove.setText( "Remove\n"+item.toString());
btnremove.setId(count);
btnremove.setTextColor(Color.parseColor("#000000"));
btnremove.setBackgroundResource(R.drawable.backgroundrounded);
btnremove.setOnClickListener(btnclick);
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 120);
lp.setMargins(0, 0, 0, 32);
editText.setLayoutParams(lp);
editText.setPadding(46, 0,0,0);
editText.setHintTextColor(Color.parseColor("#A17A7979"));
editText.setBackgroundResource(R.drawable.rounded_edittex);
editText.setTextColor(Color.parseColor("#7A7979"));
editText.setHint(item.toString());
}
if(btnremove.getParent() != null) {
((ViewGroup)btnremove.getParent()).removeView(btnremove); // <- fix
}
myLayout.addView(editText);
myLayoutbtn.addView(btnremove);
// for each EditText add it to the ArrayList
for(int i=0; i < count; i++)
{
list.add(editText);
}
// for each Button also add it to the ArrayList
for(int i=0; i < count; i++)
{
listbtn.add(btnremove);
}
}
}
});
Check this image for reference:
If you want to remove View from layout you should use removeView() method. In your case:
mLayout.removeView(editText);
Related
I got the following code
for (int i = 0; i < cantcuenta ; i++) {
textview = new ArrayList<>();
final Integer nro = i + 1;
Hmayor = new LinearLayout(view.getContext());
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = pesoLayout;
Hmayor.setOrientation(LinearLayout.VERTICAL);
Hmayor.setWeightSum(1);
Hmayor.setLayoutParams(param);
scrollview = new ScrollView(view.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
params.weight = 0.65f;
scrollview.setLayoutParams(params);
linearinterno = new LinearLayout(view.getContext());
linearinterno.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearinterno.setOrientation(LinearLayout.VERTICAL);
scrollview.addView(linearinterno);
for (int j = 0; j < 10; j++)
{
test = new TextView(view.getContext());
test.setId(j);
test.setText("Lorem Ipsum" + j);
test.setVisibility(View.GONE);
textview.add(test);
}
boleta = new Button(view.getContext());
RelativeLayout.LayoutParams parambo = new RelativeLayout.LayoutParams(100, 50);
boleta.setLayoutParams(parambo);
boleta.setText("BOLETA");
boleta.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
boleta.setTypeface(Typeface.DEFAULT_BOLD);
boleta.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
boleta.setTextColor(getResources().getColor(white, null));
boleta.setBackgroundColor(getResources().getColor(colorAccent, null));
boleta.setId(nro);
boleta.setPadding(0,0,0,0);
boleta.setVisibility(View.GONE);
boleta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
factura = new Button(view.getContext());
RelativeLayout.LayoutParams paramfa = new RelativeLayout.LayoutParams(100, 50);
paramfa.addRule(RelativeLayout.RIGHT_OF, boleta.getId());
factura.setLayoutParams(paramfa);
factura.setText("FACTURA");
factura.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
factura.setTypeface(Typeface.DEFAULT_BOLD);
factura.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
factura.setTextColor(getResources().getColor(white, null));
factura.setBackgroundColor(getResources().getColor(colorAccent, null));
factura.setPadding(0,0,0,0);
factura.setVisibility(View.GONE);
factura.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
ncuenta = new Button(view.getContext());
LinearLayout.LayoutParams paramb = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
paramb.weight = 0.15f;
ncuenta.setLayoutParams(paramb);
ncuenta.setPadding(0,0,0,0);
ncuenta.setText("CUENTA " + nro);
ncuenta.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
ncuenta.setTypeface(Typeface.DEFAULT_BOLD);
ncuenta.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
ncuenta.setTextColor(getResources().getColor(letras, null));
ncuenta.setBackgroundColor(getResources().getColor(transparent, null));
ncuenta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Boolean sw = false;
for (TextView este : textview)
{
if (este.getVisibility() == View.GONE)
{
este.setVisibility(View.VISIBLE);
linearinterno.addView(este);
factura.setVisibility(View.VISIBLE);
boleta.setVisibility(View.VISIBLE);
sw = true;
}
}
if (!sw)
{
for (TextView este : textview)
{
este.setVisibility(View.GONE);
factura.setVisibility(View.GONE);
boleta.setVisibility(View.GONE);
}
linearinterno.removeAllViews();
}
}
});
Hmayor.addView(ncuenta);
Hmayor.addView(scrollview);
linearinternodw = new LinearLayout(view.getContext());
LinearLayout.LayoutParams paramdw = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0);
paramdw.weight = 0.2f;
linearinternodw.setLayoutParams(paramdw);
Hmayor.addView(linearinternodw);
relainterno = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams paramr = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relainterno.setLayoutParams(paramr);
relainterno.addView(factura);
relainterno.addView(boleta);
linearinternodw.addView(relainterno);
theParent.addView(Hmayor);
}
it creates this distribution
when i press the button "CUENTA 1" with the name "ncuenta" in the code it should make all the text in the its own layout gone along with the buttons "BOLETA" and "FACTURA" also shown in the picture but instead, it makes it gone for the "CUENTA 2" views, I have done this kind of coding for recyclerviews and everything get instanced separatedly, would think that with a FOR everytime it does create a new instance of any view it would get attached to the corresponding cycle of the FOR.
if I divide the screen in 4 the first 3 buttons will show and hide the "CUENTA 4" wich tells me the the setOnclickListener its only taking the last set of items instanced..
Any ideas on how could solve this?
thanks in advance
Your onClickListener executes this code
for (TextView este : textview)
{
if (este.getVisibility() == View.GONE)
{
este.setVisibility(View.VISIBLE);
linearinterno.addView(este);
factura.setVisibility(View.VISIBLE);
boleta.setVisibility(View.VISIBLE);
sw = true;
}
}
When executed, the contents of the textview ArrayList will be the elements you add in the last iteration, that's why only the views in the last section are targeted.
Hope it helps.
I'm here today for my soundboard app ! son to make it simple i have a G_Son object which is the controller of the "Son" model. i get the list of sound from my database (everything is fine until here) but then when I dynamically try to create ImageButtons and add them on my activity (manageLayout() ), I have absolutely nothing appearing on my activity ! not even a single button. So if you have any Idea, or want to give me a hand, I'm aware of any suggestion
private G_Son gson;
private OurNiceSoundPlayer soundPlayer;
private List<Son> sons;
RelativeLayout gameBoard;
private Son selectedSound;
private View.OnClickListener mSoundOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlayer.setSound(sons.get(Integer.parseInt(v.getTag().toString())));
Log.i("Board", "Clicked on ImgButton ->" + v.getTag());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
gson = G_Son.getInstance();
sons = gson.getSons(getApplicationContext());
soundPlayer = new OurNiceSoundPlayer(getApplicationContext());
gameBoard = (RelativeLayout) findViewById(R.id.soundboard);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
manageLayout();
gameBoard.invalidate();
}
private void manageLayout() {
if (sons.size()>0)
{
int rawNbr = (int) Math.ceil((double) sons.size() / 3);
int currentSon = 0;
Son displayed = sons.get(currentSon);
for (int i = 0; i < rawNbr; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++)
{
ImageButton btnTag = new ImageButton(this);
if (displayed.getIsPerso()) {
File imgFile = new File(displayed.getPathImage());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
btnTag.setImageBitmap(myBitmap);
} else {
btnTag.setImageResource(R.drawable.defaultimage);
}
}
else
{
int id = getResources().getIdentifier("com.example.m.sbst:drawable/" + displayed.getPathImage(), null, null);
btnTag.setImageResource(id);
}
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setOnClickListener(mSoundOnClickListener);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setTag(currentSon);
btnTag.setId(i);
row.addView(btnTag);
currentSon++;
if (currentSon>=sons.size())
{
break;
}
else
{
displayed = sons.get(currentSon);
}
}
gameBoard.addView(row);
}
}
}
Forgot to mention a size, moreover, after months of learning android, I should advice people to use a grid view to do so, it allows with custom layout to do event better than creating your own layout with dynamic image buttons
This code programatically generates a layout which consists of views and TextViews in a way that is defined in an array. However, I am trying to make each TextView clickable which displays a toast of a bigger explanation of that TextView . Since the toast needs to be specific to the particular TextView , I am trying to set the TextView using another array of explanations at position i. However, when I try to do this I get the error "Cannot refer to a non final variable inside an inner class defined in a different method."
So I am wondering, how do I get around this? i can't be final because it changes on each loop iteration?
for ( int i = 0; i < formulae.length; i++){
if (formulae[i].equals("horizontalrule")){
View rule = new View(FormulaeActivity.this);
rule.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
rule.setBackgroundColor(0xFF000000);
View spacerbig = new View(FormulaeActivity.this);
spacerbig.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 20));
spacerbig.setBackgroundResource(android.R.color.transparent);
container.addView(rule);
container.addView(spacerbig);
}
else{
View spacersmall1 = new View(FormulaeActivity.this);
spacersmall1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
spacersmall1.setBackgroundResource(android.R.color.transparent);
tx[i] = new TextView(FormulaeActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
tx[i].setText(Html.fromHtml(formulae[i]));
tx[i].setTextSize(20);
tx[i].setPadding(10, 0, 0, 0);
tx[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), Html.fromHtml(explanations[i]), Toast.LENGTH_SHORT).show();
// ^this is the offending line
}
});
container.addView(tx[i]);
container.addView(spacersmall1);
}
Why not have a final variable which will hold the value of i on each iteration.
for ( int i = 0; i < formulae.length; i++){
final int pos = i;
And use it like this on the onClick()
Toast.makeText(getApplicationContext(), Html.fromHtml(explanations[pos])..
Try this,
for ( int i = 0; i < formulae.length; i++){
if (formulae[i].equals("horizontalrule")){
View rule = new View(FormulaeActivity.this);
rule.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));
rule.setBackgroundColor(0xFF000000);
View spacerbig = new View(FormulaeActivity.this);
spacerbig.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 20));
spacerbig.setBackgroundResource(android.R.color.transparent);
container.addView(rule);
container.addView(spacerbig);
}
else{
View spacersmall1 = new View(FormulaeActivity.this);
spacersmall1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
spacersmall1.setBackgroundResource(android.R.color.transparent);
tx[i] = new TextView(FormulaeActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
tx[i].setText(Html.fromHtml(formulae[i]));
tx[i].setTextSize(20);
tx[i].setPadding(10, 0, 0, 0);
container.addView(tx[i]);
container.addView(spacersmall1);
}
}
for(int i=0;i<tx.length;i++)
{
if(tx[i]!=null)
{
tx[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),Html.fromHtml(explanations[i]), Toast.LENGTH_SHORT).show();
// ^this is the offending line
}
});
}
}
I have an SQLite Database in my application with fieldname,fieldtype columns.
fieldname consists of EditText namimg ...ex: etsearch,etsave,etcancel etc..
fieldtype is EditText.
I add fieldname values dynamically. I need to declare these EditTexts in my java class, as i'am creating my layout using java code.
I created an arraylist and added the fieldname values into it. I am confused how to declare the EditTexts using for loop.
My code :
public class MainActivity extends Activity
{
LinearLayout ll;
LinearLayout llgrower;
Cursor cursor;
ArrayList<String> edittexts;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ll = new LinearLayout(this);
llgrower= new LinearLayout(this);
ll.addView(llgrower);
this.setContentView(ll);
cursor = DataBase.getdata("query to get fieldname,fieldtype");
try
{
if (cursor.moveToFirst())
{
do
{
edittexts.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
catch (Exception e) {}
for (int i=0;i<edittexts.size();i++)
{
}
}
}
Here's the basics of how to do it.
LinearLayout layout = (LinearLayout) findViewById(R.id.layout); // this is whatever view you want to add them to
EditText editText;
for(int i=0;i<nunberOfEditTexts;i++){
editText = new EditText(this);
...
// set the properties of the EditText
...
// add to the view
layout.addView(editText);
}
You will probably want to look at LayoutParams to understand how to size and position your EditTexts.
Good luck
private void addEditText(int count) {
int id = 999;
EditText et;
RelativeLayout.LayoutParams params;
for(int i=0; i<count; i++) {
et= new EditText(this);
et.setId(id);
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 0);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
if(i > 0) {
params.addRule(RelativeLayout.BELOW, id - i);
}
//ASSIGN VALUE FOR YOUR EDITTEXT FROM db HERE.
layoutSpinnerContainer.addView(spinner, params);
id++;
}
}
I am dynamically creating a table. I am inflating the rows using another layout file.
But the onclick event for these dynamic rows is triggered only after the second time.
Please assist.
private void populateRouteDetails(){
TableRow row;
View[] lArray = new View[24];
View lView = null;
LayoutParams lLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
for (int current = 0; current < 24; current++) {
row = new TableRow(this);
row.setPadding(0, 1, 0, 1);
LayoutInflater inflator = LayoutInflater.from(getBaseContext());
// inflate child
View item = inflator.inflate(R.layout.linear_row, null);
lArray[current] = item;
item.setPadding(0, 15, 0, 15);
row.addView(item);
row.setGravity(Gravity.CENTER_HORIZONTAL);
busRoute.addView(row, lLayoutParams);
lView = new View(this);
lView.setBackgroundColor(0xFF00FF00);
busRoute.addView(lView,
new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 1));
}
for(int lCount = 0;lCount<lArray.length;lCount++){
View lRow = lArray[lCount];
lRow.setClickable(true);
lRow.setFocusable(true);
lRow.setFocusableInTouchMode(true);
lRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View pView) {
Toast.makeText(BusDetails.this, "Hello",Toast.LENGTH_SHORT).show();
}
});
}
}
What do you mean when you say it work the second time? You could do away with the second loop. Add the listener to "item" in the first loop - just after
item.setPadding(0, 15, 0, 15);
Adding listener at before the view gets added to the row might give expected result.