Delete a LinearLayout on long press event - android

I am creating a LinearLayout dynamically. I need to delete the LinearLayout on LongPress event.
My code :
public void addTileView(View v) {
_parentLayout = (LinearLayout) findViewById(R.id.gridCont);
View child = getLayoutInflater().inflate(R.layout.customtileview,null);
((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);
_parentLayout.addView(child);
_parentLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
return true;
}
});
}
How to do this ?

try this,
_parentLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
parentLayout.removeAllViews();
return true;
}
});

Its better to remove the view which you added in the parent .
Say Parent layout is the main layout and child layout is the one which you want to remove.
You should try
parent_layout.removeView(child_layout);
removeAllViews() - will remove all the views inside the view , but not the main view .
Please refer to ViewGroup
ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);
Alternatively , You can make the visibility of your view to Visible.GONE , and then make it visible when required.

This works for me.
public void addTileView(View v) {
_parentLayout = (LinearLayout) findViewById(R.id.gridCont);
child = getLayoutInflater().inflate(R.layout.customtileview,null);
((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);
_parentLayout.addView(child);
child.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
_parentLayout.removeView(v);
return true;
}
});
}
Thanks

Related

setOnItemLongClickListener not receiving events

I've got this problem that I cannot receive this event, something is blocking it from one of my items in the listView but I don't know what, I checked focus on the item set to false and it not worked. Also check setting the item to be onlongclickable but it didn't work also. What more?
I had a problem with that and set the parent of the layout android:descendantFocusability="blocksDescendants", this is the list view item layout
you can do something like this in your ListView Adapter ,getView() ,
// For onlongclicks
final View deleteView = convertView;
deleteView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
// TODO Auto-generated method stub
}
});
//For Just onClick
deleteView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

setOnLongClickListener doesn't work in viewpager

I have used the setOnLongClickListener() for the ViewPager:
viewPager.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "LongClick", Toast.LENGTH_SHORT).show();
return false;
}
});
But the above code didn’t work in the program.
In addition to that it does not return any warning or error …
I don’t know what is wrong with it!!!

How two build two line of text and one image in one item?

i am using java android .
i have a problem in my app and i need some code that can make my item has two lines of texts and one image .
please save me and help .
here is the code for what i tried please make sure that you can help and give your opinion and how i can solve it cause i stucked here.
the main activity
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
Pinput = (EditText)this.findViewById(R.id.editText1);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER){
addItemList();
}
return false ;
}
});
Pinput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(keyCode== KeyEvent.KEYCODE_NUM) {
addItemList();
}
return;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(Pinput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
} if (isInputValid(etInput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2 ) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
return false;
} else {
return true;
}
}
}
If you want a custom view in your ListView I recommend doing one xml layout by yourself and replace android.R.layout.simple_list_item_1 in your Adapter call with the new created one.
This would be the easiest way but as I think that you might need more freedom to create/modify the content of the row, I strongly suggest to create your own Adapter extending from BaseAdapter. In the getView() method of the adapter you can easily inflate and set up the layout like you want for that row.
Okie... you tag android-listview and list view item. So I think you need list view with image and and Text.
For that you have to use Custom List View.
See this
May be this will help you. and not then pls make your que more clear. :)

Button location in listview android

I have a button on the listview when click on that will show popup. I have implemented that successfully. But the issue is that i want to display popup on the clicked button position.
View layout = inflater.inflate(R.layout.popup_layout,null);
pwindo = new PopupWindow(layout, 300, 250, true);
pwindo.showAtLocation(layout, Gravity.NO_GRAVITY, 30, 40);
pwindo.setOutsideTouchable(true);
pwindo.setTouchable(true);
pwindo.setBackgroundDrawable(new BitmapDrawable());
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
pwindo.dismiss();
return false;
}
});
Button code
vi = inflater.inflate(R.layout.list_row, null);
vi.findViewById(R.id.statusImage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showPopup();//to display popup thats given above
}
});
You need to set buttons tag as its position, and onClick you need to get it:
button.setTag(position);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int position = (Integer) v.getTag();
// Do whatever you like with position
}
});
when the button is clicked you can get the position of the item in the list. Use
list.getFirstVisiblePosition();
to get the buttin postion. substract the FirstVisiblePosition from the position. you will get the click postion. and you can show the pop up in that postion.
You can get position for button click in listview by following code.
(vi.findViewById(R.id.statusImage)).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(Classname.this, "POSITION"+ position,Toast.LENGTH_SHORT).show();
showPopup();
}
});
Use QuickAction. Examples :
http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
http://www.edumobile.org/android/android-apps/quick-action-demo/
https://github.com/alhneiti/Android-QuickAction

View inside toast

I want to make a toast in which I have put EditText and a button ..but I can't type anything inside EditText neither I can click the button how to write inside EditText that viewed by the toast..
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button New=(Button)findViewById(R.id.button1);
Button save=(Button)findViewById(R.id.button3);
EditText ed1=(EditText)findViewById(R.id.editText1);
final Toast t=new Toast(getApplicationContext());
New.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ListView l=new ListView(getApplication());
l.setAdapter(new badp(getApplicationContext()));
t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
t.setView(l);
t.setDuration(Toast.LENGTH_LONG);
t.show();
}
});
}
public class badp extends BaseAdapter
{
Context context;
private badp(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
public int getCount() {
// TODO Auto-generated method stub
return 1;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout l=new LinearLayout(context);
Button b1=new Button(context);
b1.setText("Save");
EditText ed=new EditText(context);
ed.setGravity(Gravity.CENTER);
// LayoutParams lparams = new LayoutParams();
// ed.setLayoutParams(lparams);
ed.setWidth(5);
ed.setEms(10);
l.addView(ed);
l.addView(b1);
return l;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You should probably use a Dialog instead of a Toast.
Dialog is designed for more interactive pop-ups, where-as a toast is really designed to display a quick text message.
I really don't think you should use a toast for this kind of user interface. If you look at the documentation it says:
A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user's current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events.
Toast Notifications
What you want is probably a Dialog

Categories

Resources