OnClick on TextView
Solved
Debutant en webmastering
Posted messages
444
Status
Member
-
Debutant en webmastering Posted messages 444 Status Member -
Debutant en webmastering Posted messages 444 Status Member -
Hello,
as part of a project, I am required to use an onClick on a textView (a constraint that allows me not to alter the existing setup)
while doing some research, I came across a solution, namely:
It seems from the java documentation that this feature is no longer available (or I haven't found it, which is entirely possible), I came across onKeyUp, which "could" solve the problem, but when I call my textView, I cannot access this kind of functionality via Intellisense.
Would you have a solution to resolve this issue?
Configuration: Linux / Firefox 47.0
Best regards, Beginner in web mastering
as part of a project, I am required to use an onClick on a textView (a constraint that allows me not to alter the existing setup)
while doing some research, I came across a solution, namely:
//In your Xml add this, android:clickable="true" //in your activity use like this, textView.setOnClickListener(new View.OnClickListener(){ public void onClick(){ } } It seems from the java documentation that this feature is no longer available (or I haven't found it, which is entirely possible), I came across onKeyUp, which "could" solve the problem, but when I call my textView, I cannot access this kind of functionality via Intellisense.
Would you have a solution to resolve this issue?
Configuration: Linux / Firefox 47.0
Best regards, Beginner in web mastering
6 answers
No, the context hasn't changed. It's just that this does not represent what you expect ;)
When you are in the
A few remarks:
- a good practice in variable naming is to keep the type of the object, especially for Activities. When we look at your code, we can't tell that Asking_If_Time is of type Activity. I suggest AskingIfTimeActivity
- another good practice is to avoid using underscores. It's better to prefer CamelCase notation
- On CCM, remember to use the "Reply to the topic" button in blue instead of adding a comment
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, provide for a delay...
When you are in the
onClick()method, you are part of an object of type
OnClickListener. So, at this level,
thisrefers to this object. You therefore need to use the context of the Activity:
Intent asking_if_time = new Intent(TonActivity.this, Asking_If_Time.class);
A few remarks:
- a good practice in variable naming is to keep the type of the object, especially for Activities. When we look at your code, we can't tell that Asking_If_Time is of type Activity. I suggest AskingIfTimeActivity
- another good practice is to avoid using underscores. It's better to prefer CamelCase notation
- On CCM, remember to use the "Reply to the topic" button in blue instead of adding a comment
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, provide for a delay...
Hello,
Why do you say that she is not available?
--
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, provide for a delay...
Why do you say that she is not available?
TextViewinheriting from
View, there is no problem
--
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, provide for a delay...
With the little knowledge I have in Java, yes it seems correct to me, however, by continuing to search, it could come from where I put this portion of code; apparently, it should be placed within the onCreate itself as shown in this code
but I admit I do not "visualize the functioning" of this way of doing (little practice with Android Studio)
public class mainactivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button SuSe = (Button) findViewById(R.id.SuSe); SuSe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Button Clicked } }); } } but I admit I do not "visualize the functioning" of this way of doing (little practice with Android Studio)
The assignment of an OnClickListener can be done anywhere, as long as you have access to the associated graphic component. The code you show is correct. Feel free to post your problematic code if needed.
And be careful! Android Studio is the IDE. When you talk about the language, you should say Android ;)
--
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, provide for a delay...
But I admit I do not "visualize the functioning" of this way of doing (little Android Studio practice)It’s not that complicated. You have an object (here a TextView) and you assign it a parameter (here an OnClickListener) using a method (here setOnClickListener).
And be careful! Android Studio is the IDE. When you talk about the language, you should say Android ;)
--
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, provide for a delay...
Thank you for the clarification regarding the IDE and the programming language. As for the code that wasn't working, that was my mistake; I forgot to retrieve my TextView within a variable using findViewById. However, placing this portion of code directly in onCreate shifts the context, resulting in the error Cannot resolve constructor Intent. Here is the code in question:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_temperature__high); String message = getIntent().getStringExtra("temperature_high_question"); TextView textView = (TextView) findViewById(R.id.Textview_temperature_high_Question); textView.setText(message); TextView textView1 = (TextView) findViewById(R.id.Textview_temperature_high_PC_Consol_Use); textView1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ //error on the this Intent asking_if_time = new Intent(this, Asking_If_Time.class); startActivity(asking_if_time); } }); }