I have a Layout with a TextView. The TextView has
android:autoLink="all"
How can I achieve the following:
if user clicks a link, an action associated with that link is executed (i.e., click on phone number invokes dialer, etc.)
if user clicks anywhere else within Layout boundaries, Layout's onClick is called.
Thanks.
Attach an oclicklistener to the TextBox layout, and handle it from there.
TextView txt = (TextView) findViewById(R.id.yourTextBoxId));
txt.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// USer Clicked the textBox
}
});
The same apply for the Layout, find it and ...
layout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// USer Clicked the layout
}
});
I hope it helps.
Related
I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html
How can I make a TextView selectable? By this I do not mean that I want to make the text within the TextView to be selected. What I want is that when the user taps on the TextView, it enters the selected state, and when the user taps another TextView in the layout, it exits the selected state and enters the default state.
You can assign an onClickListener to any view, and do whatever you want when it is clicked.
So bottom line:
findViewById(R.id.yourViewName).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do whatever you need to do here
}
});
You can create an instance of TextView which will be used to know the last clicked TextView:
private TextView lastClicked;
Then in the onClickListener method of your TextViews, you have to change your lastClicked TextView to the current one.
myTextView.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
lastClicked = (TextView) v;
}
});
This way, you can retrieve the selected TextView from the lastClicked variable.
That already exists, it's not called selected, it's called focused. You can check if a TextView is focused by doing:
myTextView.isFocused()
And in an activity you can get the current focused view by doing
getCurrentFocus()
I have a button that is set to VISIBLE under certain circumstances, then once its clicked its suppose to make the button INVISIBLE again but for some reason its not working. Here is my code,
if(variable == 2){
testButton.setVisibility(View.VISIBLE);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
testButton.setVisibility(View.INVISIBLE);
test2Button.setVisibility(View.VISIBLE);
}
});
}
Have you tried displaying a toast when the button is clicked, just to see if that block of code is even executing? I don't see it, but I'm assuming you've actually declared a View associated with that button via 'findViewById'
EDIT:1
Do this
public void onClick(View view) {
view.setVisibility(View.INVISIBLE);
findViewById(R.id.<your test2Buttons ID>).setVisibility(View.VISIBLE);
}
Note: If you do View.GONE it will leave all area acquired by it and the other control will capture this area
where is with View.INVISIBLE it will maintain its acquired area
I have a TextView with the android:onClick attribute. When clicked, the TextView disappears. I don't want the TextView to disappear when clicked. Any ideas?
Edit:
<TextView android:id="#+id/textView1"android:text="Click Me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="processClick"
android:clickable="true"/>
http://i1179.photobucket.com/albums/x386/jenningsr2006/unclicked.png
http://i1179.photobucket.com/albums/x386/jenningsr2006/clicked.png
Edit
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
TextView t = (TextView)findViewById(R.id.textView1111);
t.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do some job here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
Clicking it does the operation correctly, that's not the problem. When I "mousedown" on the TextView, it disappears, then reappears on "mouseup".
I thought I had the same problem but it turned out the textview was not dissapearing, rather the color was changing so that it was the same as the background color. Thus it appeared hidden but it really was there. You can set the clicked color of the text view by setting it's color state list resource
http://developer.android.com/guide/topics/resources/color-list-resource.html
Have you registered a method processClick? There is no need to do it this way. Remove the clickable property and also onClick property. More simple approach is to set onClick listener from the code, for example in onCreate method:
TextView text = (TextView) findViewById(textView1);
text.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do some job here
}
});
The view becomes clickable automatically when you set an on click listener. Good luck
when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?
button only fire when pressed
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(DEBUG_TAG, "button clicked");
}
});
stroke object fires when you press anywhere on screen, even outside of stroke's bounding box
Stroke stroke = new Stroke(this);
mainLayout.addView(stroke);
stroke.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// fires on every screen click :>(
Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
}
});
I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.