Removing Views dynamically - android

I search a lot for this, but I can't solve it. I have LinearLayout with two textviews and a button (to add others LinearLayout) like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText android:id="#+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/ingredients"
android:inputType="text"
xmlns:android="http://schemas.android.com/apk/res/android" />
<EditText
android:id="#+id/quantityField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/quantity"
android:inputType="number"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/add_ingredient_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:layout_margin="#dimen/boxes_margin"
app:icon="#drawable/ic_add_ingr_btn"
/>
</LinearLayout>
The layout added is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="50dp">
<EditText android:id="#+id/ingredientsField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/ingredients"
android:inputType="text"
xmlns:android="http://schemas.android.com/apk/res/android" />
<EditText android:id="#+id/quantityField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:hint="#string/quantity"
android:inputType="number"
xmlns:android="http://schemas.android.com/apk/res/android" />
<ImageButton
android:id="#+id/remove_ingredient_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="#dimen/boxes_margin"
android:src="#drawable/ic_remove_ing_qnt"/>
</LinearLayout>
In the Activity, I created a method to add new Layouts (and it works), and inside of it a method to delete the corresponding Layout with the Delete Button. Delete Button works only one time and only if it's in the first layout added. Here's the code:
add_ingredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.ingredient_quantity_layout, null);
// Add the new row before the add field button.
parentIngredientLayout.addView(rowView);
ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);
removeChildIngredient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
parentIngredientLayout.removeView((View) v.getParent());
}
});
}
});

You have to set the OnClickListenerevent on the ImageButton from the last inserted layout.
To do that, just change this line
ImageButton removeChildIngredient = findViewById(R.id.remove_ingredient_btn);
to
ImageButton removeChildIngredient = rowView.findViewById(R.id.remove_ingredient_btn);
rowView.findViewById search the ImageButton on the last inserted layout and not the whole layout which contains other layouts.

In this line :
parentIngredientLayout.removeView((View)
v.getParent());
You are trying to remove the parent of the removeChildIngredient ImageButton, which is probably parentIngredientLayout, which i think is not what you want,
You could try this :
parentIngredientLayout.
removeView(rowView);
But in your implementation, you could encounter issues when you will add multiples ingredients, because you are setting a new onClickListener to each new ingredient, and The ImageButton will only delete the last one you have added(the last onClickListener set),
Instead, you could use a List/RecyclerView or search for another implementation, eg. put the ImageButton inside the Inflated Layout so you'll have one Remove Button in each Layout you add,
Then you should replace the findViewById of your ImageButton by rowView.findViewById
And this line should stay unchanged
parentIngredientLayout.removeView((View) v.getParent());

Related

Need to add layout dynamically

This is my Layout , I have nested Linear layout inside another linear layout which is nested inside an Scroll view. Now i want to add an Linear layout dynamically (I may even add upto 10) inside android:id="#+id/formLayout" i.e beneth android:id="#+id/secondLayout"
Original Layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="420dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<!-- LinearLayout Inside ScrollView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/formLayout"
android:orientation="vertical">
<!-- Serial Layout -->
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<android.support.design.widget.TextInputLayout
android:id="#+id/serialno_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingStart="10dp">
<AutoCompleteTextView
android:id="#+id/fieldSerial"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/txt_sno_text"
android:singleLine="true"
android:textIsSelectable="false"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Dynamic Layout needs to be added :
<LinearLayout
android:id="#+id/sixthLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<android.support.design.widget.TextInputLayout
android:id="#+id/attr2_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingStart="10dp">
<EditText
android:id="#+id/fieldAttr2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="Attribute 2"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Can some one help me with this ?
Use the LayoutInflater to create a view based on your layout template, and then inject it into the view where you need it.
public static NavigableMap<Integer, String> navigableMap = new TreeMap<Integer, String>();
public static int count = 0; // to count no of views added
public static void add_new(final Activity activity)
{
final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.formLayout);
final LinearLayout newView = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.view_to_add, null);
newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final EditText edit_new = (EditText) newView.findViewById(R.id.fieldAttr2);
edit_new.setId(id++); // It will assign a different id to edittext each time while adding new view.
Log.i("actv id",edit_new.getId()+""); // You can check the id assigned to it here.
// use a Hashmap or navigable map (helpful if want to navigate through map)
// to store the values inserted in edittext along with its ids.
edit_new.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
navigableMap.put(edit_new.getId(), edit_new.getText().toString());
Log.i("navigablemap", navigableMap.toString());
}
});
// you can get values directly through map by referencing its ids.
// OR
EditText actv_uc = (EditText) linearLayoutForm.findViewById(navigableMap.lastKey());
// Provide the id of edittext you want to access
actv_uc.getText(); // to get value of EditText
actv_uc.setText("");
linearLayoutForm.addView(newView);
count++;
}
Call this function whenever you want to add new view. If you are calling this in Activity, then there is no need to pass Activity object as parameter. but if you are using this in fragment, then need to pass a Parent Activity object to function.
Very easy and helpful tutorial for adding views dynamically :-
http://android-er.blogspot.in/2013/05/add-and-remove-view-dynamically.html
Hope this will help you. Thank you
Try this:
LinearLayout myRoot = (LinearLayout) findViewById(R.id.formLayout);
LayoutInflater inf = LayoutInflater.from(yourContext);
View child;
for (int i = 0; i < 10; i++) {
child = inf.inflate(R.layout.your_added_layout, null);
child.setId("textView"+i);
// can set layoutparam if needed.
myRoot.addView(child);
}

Listener not working on linearlayout which contain Edittext

I have an EditText contained in a LinearLayout. For My project I have set an OnclickListener on the LinearLayout. When I launch application nothing happened on click to layout. Maybe It due to the EditText but need that OnCliclistener to be work on LenearLayout.
MyCode:
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/lnlabelnameuniteholder"
android:id="#+id/choix_decategorie"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
CODE
maCategorie = (LinearLayout) findViewById(R.id.choix_decategorie);
maCategorie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
MyDB db = new MyDB(MyActivityCategorie.this);
ArrayList<Category> categoryList = db.getCategory();
ChoiceCategoryDialog categoryDialog = new ChoiceCategoryDialog(MyActivityCategorie.this, R.string.add_category, mCategoryTextView.getText().toString(), categoryList, MyActivityCategorie.this, true);
categoryDialog.show();
}
});
Thanks
In your code, you are referring to R.id.choix_decategorie (which is not shown in your code, is this a LinearLayout you want to set onClickListener on?
For a LinearLayout that has child elements (like yours does) - may need to prevent the child elements from receiving focus - you can set android:descendantFocusability="blocksDescendants" like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
For a good measure you may also set android:clickable="false" on each of the child elements. So your layout would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:clickable="false"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
Give it a try and let us know if this works.
Try this example:
LinearLayout yourLayout = (LinearLayout) findViewById (R.id.yourID);
yourLayout.setOnClickListener(new View.OnClickListener() { // remember to use "View."
#Override
public void onClick(View v) {
Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
}
});
set LinearLayout attribute android:clickable="true"
if you have button or textview in layout set android:clickable="false" for all of them
You must have to set the LinearLayout clickable="true"
<LineartLayout....
android:clickable="true"...>
This will work
And you need to put some margins in EditText, because EditText comes upside on LinearLayout and space doesn't remain for LinearLayout to be clickable.
put
<EditText
android:layout_margin="5dp".../>
your click listener will work but your edit text hide the linear layout , if you remove the edit text it will work or you can set height and width not equal to linear layout.
As a workaround you could do something like this (kotlin code):
editText.inputType = InputType.TYPE_NULL
editText.setOnClickListener { linearLayout.performClick() }
In that case the editText won't pop up the keyboard and the linearLayout will receive the a click event.

Incorrect alignment creating button in Java vs XML

I have this XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_bar"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/md_green_400" />
<Button
android:id="#+id/action_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/md_green_400" />
</LinearLayout>
It´s just a buttonBar with two borderless buttons. It works. However, I don't want that, I need to inflate these buttons from a JSONArray. So I did this:
for (int b = 0; b < buttons.length(); b++) {
final JSONObject button = buttons.getJSONObject(b);
LinearLayout buttonBar = (LinearLayout) child.findViewById(R.id.button_bar);
View buttonChild = getLayoutInflater().inflate(R.layout.flat_button, null);
Button action = (Button) buttonChild.findViewById(R.id.action_button);
action.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {}
});
action.setText(button.getString("descricao"));
action.setTextColor(Color.parseColor(button.getString("text_color")));
buttonBar.addView(buttonChild);
}
It works too, but the buttons get a left alignment. I want they justified.
Why it works when I let them fixed but not when I inflate them?
OBS: The "button_bar" is A XML just with a LinearLayout and the "ActionButton" is just a XML with a Button.
This is the root of your problem:
View buttonChild = getLayoutInflater().inflate(R.layout.flat_button, null);
If you don't provide the parent view to the inflate() method, any LayoutParams attributes (e.g. layout_gravity) will be discarded since the parent is the one to interpret those attributes.
You can fix this by changing it to:
View buttonChild = getLayoutInflater().inflate(
R.layout.flat_button, buttonBar, false);
Which will give it the parent you're attaching it to, but not attach it to the hierarchy yet (you do that below with addView()).

Replace the content of Linear layout at onclick button android

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};

ListView with clickable/editable widget

Is it possible to use a OnItemClickListener on a ListView when the Items layout has a clickable/editable widget (RadioButton,EditText, or CheckBox)?
You might want to take a look at this issue. Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked. However, that does not mean you cannot have focusable/clickable items in a row, there are some workarounds like this one.
Also, you can take a look at the Call Logs screen. It has a ListView with clickable item(the call icon on the right).
See Source code here
Quoting comment #31 in the link mentioned by Samuh (which solved the problem for me):
In fact you can add it to the layout XML (if inflated by one): android:descendantFocusability="blocksDescendants".
Adding here JIC that webpage is down in the future.
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
Tried many complex solutions, but this was the simplest one that worked:
Just use android:focusable="false" as in:
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
Two best solution
Add android:descendantFocusability="beforeDescendants" to listView
in xml OR
Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
Then it will handle the listView row item child(Button,EditText etc) events instead of listView.setOnItemClick .
I fixed my problem different , in my item I have more than one LinearLayout
so if you give id to your linearayout and setOnclickListener in adapter class it will work, only original effect of touching will dissapear.
but this link Making a LinearLayout act like an Button is usefull to make linearlaout act like button on click
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/txt_item_followers_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center|start"
android:paddingLeft="15dp"
android:text="Ali"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/txt_item_followers_name"
android:layout_marginLeft="10dp"
android:src="#drawable/puan_icon" />
<TextView
android:id="#+id/txt_item_followers_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:background="#color/red_400"
android:paddingLeft="10dp"
android:text="25.5"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:id="#+id/linear_one"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_item_followers_name"
android:background="#color/red_400"
android:orientation="vertical">
<ImageView
android:id="#+id/btn_item_followers_2b_follow"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:src="#drawable/follow_buton" />
</LinearLayout>
</RelativeLayout>
inside getView method
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.deneme, null);
final Followers2 myObj = myList.get(position);
LinearLayout linear_one = (LinearLayout) view.findViewById(R.id.linear_one); // HERE WE DOMUNİCATE IT
linear_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(parentActivity, "One Two", Toast.LENGTH_SHORT).show();
}
});
TextView name = (TextView) view.findViewById(R.id.txt_item_followers_name);
TextView mark = (TextView) view.findViewById(R.id.txt_item_followers_mark);
final ImageView btn_follow = (ImageView) view.findViewById(R.id.btn_item_followers_2b_follow);
name.setText(myObj.getName());
mark.setText(myObj.getScore());
/* if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
btn_follow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Followers2 myObj = myList.get(position);
if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
}
});*/
return view;
}

Categories

Resources