How to set onclick function to dynamic textview in android? - android

I set the onClick() function, but when I click the text it works two times that mean I have two dynamic text view. How to resolve it?
My code:
TextView tView[] = new TextView [Array.length];
for(int i =1; i<Array.length; i++)
{
tview[i] = new TextView(this);
tview[i].setId(i);
tview[i].setText(Array[i]);
tview[i].setOnTouchListener(new OnTouchListener()
{
Public boolean onTouch(View v ,MotionEvent event)
{
Toast.makeText(getApplicationcontext,"MapVal",Toast.LengthShort).show();
}
});
}

Problem is in using OnTouchListener. Event onTouch() is calling not one time on every tap action, but minimum two: on touch down and on touch up. Use OnClickListener and setOnClickListener() instead.

if u want to set an onclick listener then use
urtextvw_name.setOnClicklistener()

Related

Adding/removing edit text and a remove (X) on click

I am new to android development.
I want to add EditText and a remove button ( X ) when they click on a button (called “contact”). So that there will be multiple contacts. On click of the remove button the corresponding added EditText with remove button should be disappeared.
It will be something like this,
————————— X
————————— X
————————— X
————————— X
———————
|contact|
———————
If I use any Adapter, how to add the empty EditText and a remove button ( X ). If not any other better options?
Finally I need to get all the values entered in the EditText.
NOTE: On click of “contact” it should be adding EditText and a remove button ( X ).
Kindly give me a clear and simple way to do this.
Thanks!
Create a LinearLayout and button. On the button click call the following method. Also, create a List<EditText> in your onCreate Method to track all added edittexts & retrieve texts later.
private void createNewEditText() {
EditText editText = new EditText(this);
editText.setMaxLines(1);
editText.setSingleLine(true);
editText.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, VectorDrawableCompat.create(resources, R.drawable.ic_clear_black_24dp, null), null);
editText.setOnTouchListener(new View.OnTouchListener {
#Override
Boolean onTouch(View view,MotionEvent event) {
val DRAWABLE_BOTTOM = 3
val DRAWABLE_RIGHT = 2
val DRAWABLE_LEFT = 0
val DRAWABLE_TOP = 1
if (event.getRawX() >= (editText.right - editText.getCompoundDrawables()[DRAWABLE_RIGHT].bounds.width())) {
editTextList.remove(editText);
parentLinearLayout.removeView(editText);
return true;
}
return false;
}
})
editText.requestFocus();
editText.setText(voicetext);
editTextList.add(editText);
parentLinearLayout.addView(editText);
}
You can use a LinearLayoutwith orientation set to vertical. you can then create another layout with yourEditTextand Button and add them dynamically to the LinearLayout within the onClick method for contact button.
Do not forget to generate different IDs for your newly added views to be able to find them later
The X button will then just remove its parent from the layout

I have 200 textviews and i want to know which one was pressed then how to change the text

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}

Android: How to detect touch events on dynamically created ImageView

I have the following design :
<LinearLayout>
<ScrollLayout>
<LinearLayout> .... </LinearLayout> ----> TextView and ImageView dynamically created in this LinearLayout
</ScrollLayout>
</LinearLayout>
I need to detect touch events on the dynamically created ImageView(s). I don't know how many ImageView will be created dynamically. How can I detect which ImageView was touched by the user ?
Thanks for your help.
You'll want to create one [maybe more] View.OnClickListeners in your java code. Then when each ImageView is added, you can assign that OnClickListener to it using View.setOnClickListener. To determine which was clicked, you can set a "Tag" using View.setTag() and then when onClick is called, use View.getTag() to determine which was clicked. The tag should be uniquely identifiable.
Here's some rough code (untested):
private OnClickListener imageViewListener extends View.OnClickListener{
public void onClick(View v){
Intent i = new Intent(v.getContext(), NewActivity.class);
i.putExtra("ImageURI", v.getTag().toString());
startActivity(i);
}
}
Then as you add images, you just set the tag and listener:
for(String uri: someList){
ImageView iv = new ImageView(this);
iv.setTag(uri);
iv.setImageUri(uri);
iv.setOnClickListener(imageViewListener);
}
You should use View.OnTouchLisetener class. So, after initializing your ImageView, add this call to it:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
//Your code here
}
});

how to implement onclicklistener to a dynamically creating textview?

Currently i am having some problem with implementing onclicklistener to a dynamically creating textview. I will explain the problem more detailed. What i need to do is, i need to create textviews when i click a button in an activity and when i click on that textview it should get removed. but i am not able to set onclicklistener to each textview. Since, set onclicklistener of textviews are written inside the onclick function of the above said button(button used for creating the textview), its scope get over when it exits from onclick function of the button(i think this is the problem). So i tried using visible and invisible feature, which will create the textviews before hand and make them invisible and they are made visible only when the button(button used for creating the textview)is clicked. But here even though it is invisible the space will be allocated(ie, blank space will be availabe).
Here is my code
This button addphone will dynamically create textview by inserting the value present in the edittext phoneno
addphone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This doesn't work!!!!!
counter = counter+1;
}
}
});
But the setOnClickListener in the above line is not working
So can anyone pls help me with this problem. I hope you are clear with my question.
Thank You!
You can try this:
private OnClickListener phoneViewClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// your code
}
};
and use that listener in your TextViews:
contactbox[counter].setOnClickListener(phoneViewClickListener);
You will have to actually define a onClickListener instead of simply setting it as a boolean value.
contactbox[counter].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//this is where you would handle your click event
}
});
Good luck!
If your button was defined on the xml layout you can do that.
In your xml layout you can define which method will be called when a user click on your button:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="addTextView" /> // This is most imporant line
Your activity must have a method with the same name with a View parameter, like that:
/** Called when the user touches the button */
public void addTextView(View view) {
// Do something in response to button click
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This will work \o/
counter = counter+1;
}
}
}
On this method you should put your code to addViews.
As the behavior of all added textview must to be the same( i understood in that way), be removed when a user clicked on it, you can make your activity implements onClickListener and with that you just need to implement correctly the onClick method of your activity.

Android, event listener?

I have a loop that runs through an array of image views, adding an event listener to each, how can I find out which image view was pressed inside the listener?
imageViewArray[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
Doesn't the v parameter to the onClick method provide a reference to the ImageView?
EDIT
The thing is: You are not adding the same listener to all of the ImageViews in your code - every ImageView in your array gets its own listener.
In the listener's onClick method, the View that raised the event is passed in v, so when working with v you're working with the clicked ImageView.
To find the index of the ImageView in your array, you might as well set the ID as suggested by others and then use v.getId(), or you could loop over your array and check whether imageViewArray[i] == v, in which case i is the index of your ImageView within the array.
set id for each on the index value like eg: image_view.setId(i);
imageView.getId() will return you the ID you give this ImageView in findViewById()
method. Then you can check the ID's in a switch block. Hope this helps.

Categories

Resources