How to add delete button on Touch event of dynamiclly generated view - android

I am working on an application, In this I have number of dynamically generated textviews and Image views. Now I want when I touch on any of the dynamically generated view it should come to front and should appear a cross button on it to delete. I am stucking on this for long time please help me. Thanks in advance.

this code will add view into LinearLayout
LinearLayout layout2=(LinearLayout )findViewById(R.id.relaGameZhaiGuoZi);
layout2.setOrientation(LinearLayout.VERTICAL);
Button btn1=new Button(this);
setContentView(layout2);
Button btn2=new Button(this);
btn1.setText("Button1");
btn2.setText("Button2");
layout2.addView(btn1);
layout2.addView(btn2);
hope it helps

LinearLayout layout2=(LinearLayout )findViewById(R.id.relaGameZhaiGuoZi);
layout2.setOrientation(LinearLayout.VERTICAL);
Button delbtn=new Button(this);
Button refreshbtn=new Button(this);
btn1.setText("Button1");
btn2.setText("Button2");
layout2.addView(delbtn);
layout2.addView(refreshbtn);
Then Add listener like this
delbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//del img
}
});
refreshbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//refresh img
}
});

private Button button;
onCreate(...) { //or any other method
button = new Button(this);
button.setText("touch me");
LayoutContainer cont = (LayoutContainer)findViewById(R.id.container); //your id here
cont.add(button);
button.addOnClickListener(new OnClickListener{
#Override
... onClick(){
cont.removeView(button);//and again, not sure of method name
}
}); // like this, i'm not sure
}

Related

Android Trouble with Edit Text Views

In my application,on press of a button "Add contact" phone book gets open and then user selects a contact that get displayed in Edittext View and when another button "Add More Contacts" is pressed, a another Edit-Text View gets displayed on the top for which i can again select the contact from phone book. But the problem that i am facing is that i want that user can only add up to 5 Edit Text .
I am using the following code, but its force crashing the application. Please help.
And also i want the Edit Text Views to be non editable, for which i tried editable=false but its working only for the first Edit Text View not on the other Views, that user adds afterwards.
int id = 1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(OnClick());
EditText editview = new EditText(this);
editview.setText("Add more");
editview.setEnabled(false);
editview.setFocusable(false);
}
// implementing OnClickListener OnClick() method for "btn_addmore_cntct"
// button
private OnClickListener OnClick() {
// TODO Auto-generated method stub
// changing return type "null" to "new OnClickListner"
return new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(layoutLinear.getChildCount()>5){
}else{
final EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setId(id);
id++;
layoutLinear.addView(tab,0);
tab.requestFocus();
}
Just check number of layoutLinear child
if(layoutLinear.getChildCount()>5){
//nothing to do
}else{
//create new EditText and add to layoutLinear
}
user this code to disable EditText
edittext.setEnabled(false);
edittext.setFocusable(false);
You can use following way.
int temp;
public void onCreate(Bundle savedinstance) {
super.onCreate(savedinstance);
setContentView(R.layout.yourlayout);
temp=1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(temp<=5){
EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setEnabled(false);
tab.setFocusable(false);
layoutLinear.addView(tab);
temp++;
}else{
//Print message to user Cant add more editText.
}
}
});
}
Let me know its working or not.

How to add onclicklistener to dynamically generated text views

I have dynamically created Textviews in my application. I want to give On click event to this textviews.. When I click on the textview, I need to get the id of the textview..
You try following code.
TextView text = new TextView(this);
text.setText("text here");
ll.addView(text);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "ID : "+arg0.getId(), Toast.LENGTH_SHORT).show();
}
});
in that "ll" is Layout which add textview and after adding put clickListener() for click event.
hope this is useful for you.
Have a look at setOnClickListener and getId.
TextView textView = new TextView(this);
// Set up your text view
textView.setId(1); // Any number is ok
textView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
int id = v.getId(); // This is the id you want
// Do whatever you want here
}
});
If you are creating TextViews dynamically than just implement OnClickListener:
import android.view.View;
import android.view.View.OnClickListener;
public class MyClickListener implements OnClickListener {
#Override
public void onClick(View v) {
int vId = v.getId();
}
}
and set it to dynamically created view like this:
private TextView createTextView(int vId) {
TextView textView = new TextView(this);
textView.setId(vId);
textView.setOnClickListener(myClickListener);
return textView;
}
P.S.: Don't forget to create & initilialize myClickListener in your Activity.

Add delete option with dynamically generated editText

I'm new in android and i have created editText dynamically with the following code while clicking add new button.Is it possible to add a delete button near editText so that each while clicking the delete respective editText will be removed?
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(textb);
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
}
});
Yes, create your remove button at the same time as your EditText and use the removeView() method just like the addView() method, maybe like this:
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
...
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner();
Button btnRemoveOld = new Button(this);
btnRemoveOld.setId(32); // arbitrary number
btnRemoveOld.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
rAlign.removeView(findViewById(textb));
}
});
// You will need to set parameters to define how the button looks
// and where it is in relation to the EditText here
rAlign.addView(btnRemoveOld);
}
});

multiple setOnClickListeners for a single process

In my App I have a line showing an (ImageButton)icon and a (textView)title of an audio file to play and I use the setOnClickListener for an ImageButton to initiate a process that plays the file. I would also like to use the (textView)title as a clickable item to begin the same process.
I could simply duplicate all of the functionality in both setOnClickListeners but that does not seem to be the most efficient way to do it.
Now I am new so is there such a thing as
ImageButton.setOnClickListener() || textView.setOnClickListener() {
.
.
.
}
Basically if the ImageButton is clicked or the textView is clicked do this procedure.
I know the above syntax is not correct but it gives you an idea of what I want to do
Have the class that has both these elements ImageButton and textView implement the OnClickListener. OnClickListener is an interface that has the method onClick(View v) which will have the click implementation for both these elements. Then you can use imageButton.setOnClickListener(this) and textView.setOnClickListener(this).
Example Code:
public MyClass extends Activity implements OnClickListener {
ImageButton imageButton;
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageButton = (ImageButton) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.txt);
imageButton.setOnClickListener(this);
textView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int viewId = v.getId() ;
if(viewId == R.id.btn || viewId == R.id.txt){
//common implementation of click event
}
}
}
Hope this explanation helps.
ImageButton iv=(ImageButton)findViewById(R.id.imagebutton1);
TextView tv=(TextView)findViewById(R.id.textview1);
iv.setOnClickListener(OnClick);
tv.setOnClickListener(OnClick);
add this in OnCreate() method and after that add this method as shown below
private OnClickListener OnClick=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imagebutton1:{
// do here code what u want on imagebutton click
break;}
case R.id.textview1:{
// do here code what u want on textview click
break;}
}
}
}
};
Put both the imagebutton and textview in a layout and give it an ID. Then implement the onClickListener for the layout, by this way you need to write the code only once.

how to set onclicklistener to items in current page?

I have an application where i want to add onclicklistener to my individual items in pager.
Below is my screen shot . please some one tell me how to add onclicklistener to individual image in this current page .
Thanks
below is my code for pager view.
here when 1st time run the app initialize the every view page, when i try to click on image it return only last image info. But i want the current image info.
scroller = ((Pager)findViewById(R.id.scrollView));
indicator = ((PageIndicator)findViewById(R.id.indicator));
indicator.setPager(scroller);
// Pager pager = new Pager(this, attrs)
//indicator.getActivePage();
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pageView = null;
for (int i = 0; i < NUM_PAGES; i++) {
pageView = layoutInflater.inflate(R.layout.page, null);
// ((TextView) pageView.findViewById(R.id.pageText)).setText("Page " + (i+1));
c=4*i;
imageView1 = (ImageView) pageView.findViewById(R.id.imageView1);
imageView1.setImageResource(icons[c]);
imageView2 = (ImageView) pageView.findViewById(R.id.imageView2);
imageView2.setImageResource(icons[c+1]);
imageView3 = (ImageView) pageView.findViewById(R.id.imageView3);
imageView3.setImageResource(icons[c+2]);
imageView4 = (ImageView) pageView.findViewById(R.id.imageView4);
imageView4.setImageResource(icons[c+3]);
// pageView.setBackgroundColor(COLORS[i % COLORS.length]);
imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
scroller.addPage(pageView);
}
It seem to be you didn't read information available at android developers.
Use below code to add onClickListener for images. I don't know whether you are using ImageView or ImageButton. I took ImageView.
ImageView iv = new ImageView(/**Context you have to pass**/);
iv.setOnClickListener(new View.OnClickListener) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do whatever you want
}
});
I hope it may help you.
set this on your images..or button
in the xml such that
<Button [...]
android:onClick: "onClick" [...]
/>
"onClick": name this whatever you want your method name that handles clicks to be
This can be done by assigning onClickListeners to Image Buttons. You can use Image buttons to display these images and set onClickListeners on them. Perhaps something like this can help?
ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Do your stuff here
}
});
If you use ImageView then set the property OnClick to true.

Categories

Resources