public void onClick_addContact(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutLinear);
layout.addView(linearlayout(_intMyLineCount));
_intMyLineCount++;
}
private EditText editText(int _intID) {
EditText editText = new EditText(this);
editText.setId(_intID);
editText.setHint("My lines");
editText.setWidth(180);
editTextList.add(editText);
return editText;
}
private TextView textView(int _intID)
{
TextView txtviewAll=new TextView(this);
txtviewAll.setId(_intID);
txtviewAll.setText("My lines:");
textviewList.add(txtviewAll);
return txtviewAll;
}
private Button button(int _intID)
{
Button btn = new Button(this);
btn.setId(_intID);
btn.setTag("but1");
btn.setOnClickListener(newContact);
return btn;
}
OnClickListener newContact = new OnClickListener() {
//onClick view
public void onClick(View v) {
LinearLayout layout = (LinearLayout)v.getParent();
layout.removeViewInLayout(v);
// setContentView(layout);
_intMyLineCount--;
}
};
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain=new LinearLayout(this);
LLMain.setId(_intID);
LLMain.addView(textView(_intID));
LLMain.addView(editText(_intID));
LLMain.addView(button(_intID));
LLMain.setOrientation(LinearLayout.HORIZONTAL);
linearlayoutList.add(LLMain);
return LLMain;
}
In the newOnContact Listener, only the child button gets deleted, but not the entire linear layout that includes the textView and EditText.
How to do it?
You can use layout.setVisibility(View.INVISIBLE); or
layout.setVisibility(View.Gone); to remove the layout from UI.
View.INVISIBLE will hide the view, but it'll still take the space whereas View.GONE will remove the view as well as it won't take up any space in the UI.
Refer http://developer.android.com/reference/android/view/View.html#attr_android:visibility
Simply use GONE constant:
layout.setVisibility(LinearLayout.GONE)
then your linear layout will be invisible and it won't allocate space also.
Instead of setting the newContact onClickListener to the Button, try setting to LLMain LinearLayout. So the view that you receive in the onClick is the LinearLayout you want to remove, if I ve not misunderstood you.
Related
I have created dynamically a LinearLayout and inside a HorizontalScrollView and I have placed insite a TableLayout (my table is too big an I need to scroll). When first created my table has a row as a header and a row for the user to input data. Outside the HorizontalScrollView but inside the LinearLayout I want to add 2 buttons. One for adding a row and one for deleting rows. I setup onclickListener but it did not work. My code is :
LinearLayout linearLayout = new LinearLayout(findViewById(R.id.internal_nodes_layout).getContext());
HorizontalScrollView scrollView1 = new HorizontalScrollView(findViewById(R.id.opaque_scroll_view).getContext());
TableLayout in_opaque_table = new TableLayout(findViewById(R.id.opaque_table).getContext());
LinearLayout opaquebuttonsLayout = new LinearLayout(findViewById(R.id.opaque_buttons_layout).getContext());
TableRow in_opaque_table_HeaderRow = new TableRow(this);
in_opaque_table_HeaderRow.setLayoutParams(tlparams);
//.............. code for header...............
createRowInOpaqueTable(in_opaque_table, in_opaque_table_num_of_rows, tlparams);
scrollView1.addView(in_opaque_table);
opaquebuttonsLayout.addView(addInOpaque);
opaquebuttonsLayout.addView(removeInOpaque);
linearLayout.addView(scrollView1);
linearLayout.addView(opaquebuttonsLayout);
//setup buttons and onClickListeners
Button addInOpaque = new Button(this);
addInOpaque.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addInOpaqueRow(view);
}
});
addInOpaque.setText(R.string.add_line);
addInOpaque.setAllCaps(false);
Button removeInOpaque = new Button(this);
removeInOpaque.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
removeInOpaqueRow(view);
}
});
public void addInOpaqueRow(View view) {
createRowInOpaqueTable(in_opaque_table, in_opaque_table_num_of_rows, tlparams);
in_opaque_table_num_of_rows++;
}
public void removeInOpaqueRow(View view) {
if (in_opaque_table_num_of_rows > 0) {
in_opaque_table.removeViewAt(in_opaque_table_num_of_rows);
in_opaque_table_num_of_rows--;
} else {
Toast.makeText(this, "Δεν υπάρχουν γραμμές στον πίνακα", Toast.LENGTH_SHORT).show();
}
}
Although it appears the way I want to the add and remove buttons won't work.
I get this message:
E/libfingersense_wrapper: libfingersense.so was not loaded
but I see that rows count correct when I use add and remove buttons. Any ideas??
Is it possible to add a new EditText automatically in a ListView on a Button click?
If so please let me know.
EditText name = new EditText(youractivity);
convertView.addView(name);
Call notifydatasetchanged() but you have to save the field that you add becouse after scrolling into the list you will have in all list that editText and you have to remove where you don't want to appear.
What I recommand you is to make an editext into your cellView hidden and make it visible when you tap on your button.
You could make a custom adapter for your listview and give it a layout containing the edittext with the visibility set to gone. You can then set it to visible onbuttonclick.
I would like to suggest one code its not about listview but see if it could give you some idea,by adding views dynamically in linear layout which is inside scroll view.
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);
I currently have:
final TextView tv = new TextView(this);
final RelativeLayout rL = new RelativeLayout(this);
final EditText editText = (EditText)findViewById(R.id.editText);
final Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rL.addView(tv);
tv.setText(editText.getText());
editText.setText("");
}
});
In my onCreate method but my textView does not show up on the screen when text is inputted and my button is pressed? Is there code to set where it is set up on the screen of my phone?
Here is your problem
final RelativeLayout rL = new RelativeLayout(this);
This RelativeLayout that contains the TextView is not even displayed on the screen, all you are doing is creating a RelativeLayout.
What you should do instead is add a RelativeLayout to your XML layout file (same one containing the EditText and Button and do the following
final RelativeLayout rL = (RelativeLayout)findViewById(R.id.myRelativeLayout);
...
rL.addView(tv);
Now since you are referencing an actual RelativeLayout, your text will be visible.
Hope I made some sort of sense.
Do you have a base layout? You're adding the EditText to the RelativeLayout, but you need to add the RelativeLayout to some already existing layout.
First, inflate some base layout. Then do a findViewById on that layout. Use this to call addView(editText);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/base_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
RelativeLayout rl = (RelativeLayout)findViewById(R.layout.base_layout);
rl.addView(yourTextView);
}
}
I am new to Android.Can anyone give some ideas for my problem.
/* Parent Linear Layout */
final LinearLayout par_layout=new LinearLayout(this);
par_layout.setOrientation(LinearLayout.VERTICAL);
/* Child Linear Layout */
final LinearLayout chl_layout=new LinearLayout(this);
chl_layout.setOrientation(LinearLayout.VERTICAL);
TextView tv_name=new TextView(this);
tv_name.setText("Name ");
TextView tv_item=new TextView(this);
tv_item.setText("Items ");
Button btn_submit=new Button(this);
btn_submit.setText("Submit");
btn_submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
par_layout.removeAllViewsInLayout();
}
});
chl_layout.addView(tv_name);
chl_layout.addView(tv_item);
chl_layout.addView(btn_submit);
par_layout.addView(chl_layout);
setContentView(par_layout);
In the above code at the time of button click i wish to clear the chl_layout from the par_layout.But i can't . Can anyone give some ideas ??
Note :
The following code also not working
par_layout.removeView(chl_layout);
Use below code to remove child view from parent view.
par_layout.removeView(chl_layout);
to make whole layout empty
there is a void function ...
LinearLayout li=new LinearLayout(this);
li.removeAllViews();
Try using this:
fatherLayout.removeViewInLayout(childLayout);
I can't remove a child view (TableRow) from its parent (TableLayout) with removeView(View view).
But addView is working.
Strange...