I have a listview ,each item is a textView with property autoLink="web|email".Link will work properly,but I want to start another activity when text other than web|email is clicked,that was not happening.So I used setOnClickListner for textView,that also worked smoothly.My problem is when I click the email or web link both actions will occur -browser and other activity will open.How to prevent this?
I got the solutions.
I used getSelectionStart() and getSelectionEnd() functions of the Textview class,
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(textView.getSelectionStart()==-1&&textView.getSelectionEnd()==-1){
//This condition will satisfy only when it is not an autolinked text
//onClick action
}
}
});
TRY this ::
in layout :: android:autoLink="web"
OR
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
Related
im trying to create a listView, that has an Button item on it.
I want to make this button clickable, so I did something like this code in Adapter, getView:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("_myButton_Log", "ShowOnClick");
}
});
And now im trying to change the visibility parameter for my textView:
TextView myDesc = row.findViewById(R.id.my_desc);
myDesc.setVisibility(convertView.GONE);
I want to show this textView in only one row, after click this button.
Now I make that, the button is clickable for each rows but as you can see it's show only the Log. Im a newbie in the ListViews and buttons on it and im trying to get knowledge how to make it work, but for now I cannot find any help...
So im begging here for some help! :)
Anyway if you want me to use the OnItemClickListener it's not possible because im using it for another way.
Ok I found out an response for my question.
Everyone with this problem - just need to make a simple action for a button in list view in your adapter like:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView myPrice = row.findViewById(R.id.price);
Button myButton = row.findViewById(R.id.button);
myPrice.setVisibility(convertView.VISIBLE);
myButton.setVisibility(convertView.GONE);
}
});
I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});
I create a dialog window in an activity, then I put the buttons in this dialog window and listened to them. However, I want to listen to android device found in the back button. and dialog.setOnkeyListen method I used for it. I have put the program by executing dialog window EDITTEXT not enter data into the field. So when I add code to the setOnkeyListen I can not enter data into the EditText field. I hope you know what I mean
If you're asking how to set the OnClickListener for a textview here is an example hope this helps:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Code you want to execute on the click goes here
}
});
I know this has been asked before but I cannot make this work so here is what I have so far
class Click extends Activity {
int i=0;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
final TextView mTextView = (TextView) findViewById(R.id.Counter);
mTextView.setText(""+i);
final Button button = (Button) findViewById(R.id.AddOne);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
mTextView.setText(Integer.toString(i));
}
});
}
Every time I run the app in an emulator it crashes
java.lang.IllegalStateException: Could not find a method Click(View) in the activity class com.scouting.corbin.frc_201415_scouting.MainActivity for onClick handler on view class android.widget.Button with id 'AddOne'
I know this is probably something completely stupid but I am new to this and need help thank you in advance.
As per your logcat.
java.lang.IllegalStateException: Could not find a method Click(View)
in the activity class
com.scouting.corbin.frc_201415_scouting.MainActivity for onClick
handler on view class android.widget.Button with id 'AddOne'
I suggest you to add Click(View v) in your MainActivity
public void Click(View v)
{
}
You need to take the root element here. Depending on parent layout include this line in the activity after setContentView().
RelativeLayout layout=(RelativeLayout)findViewById(R.id.yourLayoutId);// If its some other layout change "RelativeLayout" to your opted layout.
and in onClick() method of button, add following.
layout.add(tv);
Yopu want To add one Linearlayout in xml file
and set id for your LinearLayout.
android:id="#+id/linearlayout"
And change your addTextView method to following
public void addTextView(String text){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView textView=new TextView(this);
textView.setText(text);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(textView);
}
and call this method from your Forloop
Perhaps consider using the android:onClick="example_method" attribute for the button in your xml file. Then create the appropriate method in the class. public void example_method(View v) {} Then place the code you have in your onClick function into the new one. It's easier than using an listener.
Ok so all of you helped I completely got rid of that code which was too comlicated for what I was trying to do. After taking bits of suggestions and some reasearch I came up with this
public void AddOne(View v) {
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
tv.setText(""+i);
}
As you can see much simpler than what I had before and this one works thank you all
I am developing an quiz based application. I wanted to know like, how can we change the questions and its options without changing the text view where the question will appear and the layout when the user clicks next button i want the same text view and layout only question and its options should change.I am new to the android so can anyone help me out ..
you can change the text of texview on click of next button like this
TextView textView=(TextView) findViewById(R.id.textview1);
Button next=(Button) findViewById(R.id.nextbutton);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Your Text");
}
});