I'm attempting to develop and app using the new Android Studio, but I keep receiving major errors on my OnClickListeners. Mainly it is telling me that it cannot resolve symbol "setOnClickListener" and it also cannot resolve "View v"
package com.sigmachi.derbydays;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
That is the code in the class
Information:Compilation completed with 11 errors and 0 warnings in 4 sec
Information:11 errors
Information:0 warnings
/Users/angelo/AndroidStudioProjects/SigmaChiDerbyDaysProject/SigmaChiDerbyDays/src/main/java/com/sigmachi/derbydays/MainActivity.java
Error:Error:line (28)Gradle: <identifier> expected
Error:Error:line (28)Gradle: illegal start of type
Error:Error:line (28)Gradle: ')' expected
Error:Error:line (28)Gradle: ';' expected
Error:Error:line (28)Gradle: invalid method declaration; return type required
Error:Error:line (30)Gradle: illegal start of type
Error:Error:line (30)Gradle: ';' expected
Error:Error:line (30)Gradle: ')' expected
Error:Error:line (30)Gradle: not a statement
Error:Error:line (30)Gradle: ';' expected
Error:Error:line (33)Gradle: illegal start of type
Those are the errors I am receiving which makes absolutely no sense. Line 28 starts at when I do button.setOnClickListener
EDIT: Now I receive a force close when I press the button
This is the class it should open, a bare class with the only change being the layout to open
package com.sigmachi.derbydays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class StandingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.standings_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
This code is not in any method. If you want to use it, it must be within a method like OnCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
}
you will need to button initilzation inside method instead of trying to initlzing View's at class level do it as:
Button button; //<< declare here..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.standingsButton); //<< initialize here
// set OnClickListener for Button here
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
}
});
}
This worked for me:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newarea);
btnSave = (Button)findViewById(R.id.btnSave);
OnClickListener btnListener = new OnClickListener() {
#Override
public void onClick(android.view.View view) {
finish();
}
};
btnSave.setOnClickListener(btnListener);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
titolorecuperato = (TextView) findViewById(R.id.textView);
String stitolo = titolorecuperato.getText().toString();
Button btnHome = (Button) findViewById(R.id.button);
btnHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
same thing as Nic007 said before.
You do need to write code inside "onCreate" method. Sorry me too for the indent... (first comment here)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.standingsButton) {
startActivity(new Intent(MainActivity.this,StandingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
Related
I did not expect an error to come up but the 3-dot ActionBar menu is not displaying and I am getting an unexpected error. I am not sure where I went wrong in my code.
Please help,
thanks in advance!
MainActivity.java
package com.example.it5.foothillers;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button;
Button button2;
Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Display app icon in the ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
}
private void buttonClick() {
startActivity(new Intent("it5.foothillers.news"));
}
private void button2Click() {
startActivity(new Intent("it5.foothillers.sports"));
}
private void button3Click() {
startActivity(new Intent("it5.foothillers.events"));
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
buttonClick();
break;
case R.id.button2:
button2Click();
break;
case R.id.button3:
button3Click();
break;
}
}
#Override
public void onPause() {
super.onPause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(MenuItem item) {
int id = item.getItemId();
}
}
You have 3 onCreateOptionsMenu methods. Remove the last 2, you only need the first one with inflater. Also, Override the onOptionsItemSelected method to control the actions.
I'm trying to declare buttons and EditTexts in the OnCreate() function, calling the function findViewById(). But when I try to use the button in the emulator the application fails. When I use the findViewById() in the function of the treatment of the event (I used android:onClick because the treatment in java wasn't working either) the aplication worked normally. Why I can't only use the findViewById() one time, inside the onCreate?
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnClickListener{
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
status.setText("HA");
}
});
Button b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
Button b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
Button b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Inside oncreate() you have again define Button b1 like:
Button b1 = (Button) findViewById(R.id.b1);
Change it to:
b1 = (Button) findViewById(R.id.b1);
As you are creating the Button variable inside onCreate(),it will not work outside that method.So use the global reference variable of the Button that you have created.
try this hope it's worked:
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
String[] names;
EditText status;
EditText par1;
EditText par2;
EditText par3;
Button b1;
Button b2;
Button b3;
Button b4;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (EditText) findViewById(R.id.text);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status.setText("HA");
}
});
b2 = (Button) findViewById(R.id.b2);
b2.setActivated(false);
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Display any data about the persons
}
});
b3 = (Button) findViewById(R.id.b3);
b3.setActivated(false);
b3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Start the sensors lecture
}
});
b4 = (Button) findViewById(R.id.b4);
b4.setActivated(false);
b4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Execute the music
}
});
}
public void treatment_button(View v){
status = (EditText) findViewById(R.id.text);
status.setText("Connect");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I am basically only trying to get R.id.button1 to open up Google in a web browser, not sure whats wrong!
No errors, it just does nothing when pressing the button, I am using the emulator.
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
}
public void onClick(View v)
{
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
It seems to me your button is in your fragment layout (right?)
then you should handle your button's events in your fragment, not in the activity!
Your onCreate() method should be like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener( new OnClickListener{
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
});
}
The problem is here :
btn.setOnClickListener((OnClickListener) this);
replace it with :
btn.setOnClickListener(this);
And implement View.OnClickListner in order to get the onClick method
public class MainActivity extends ActionBarActivity implements View.OnClickListner {
/*
some code here
*/
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
}
You can also do it like this :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
});
Implement OnClickListner
public class MainActivity extends ActionBarActivity implements OnClickListner {
Button btn;
Define the listener to your button1.
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
use a switch in onClick() method to handle several views:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
break;
default:
//code..
break;
};
}
other way to set the listener to your button:
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener( new OnClickListener{
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
I'm starting learning android development but im facing a an error which says :
Multiple markers at this line
- view cannot be resolved to a type
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable
for the arguments (new
OnClickListener(){})
my program is :
package com.sc.uploader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add;
Button sub;
TextView disply;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add =(Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
disply= (TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new view.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
so please if you could help me resolving the error i will be grateful
thanks
Change this line
add.setOnClickListener(new view.OnClickListener() {
to
add.setOnClickListener(new View.OnClickListener() {
capital "V". It is looking for a variable view when it should be set on the View Class.
OnClickListener Docs
I am getting an error in 2 places and I don't know why as I am new to Android...
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
} //Error type Syntax error on token "}", delete this token
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
} // Error Syntax error, insert "}" to complete ClassBody
Write your TextView code in onCreate() method, you can't write this code outside any function the way you have written.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
Move this inside onCreate
TextView t1=(TextView)findViewById(R.id.text);
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this ,"Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
Paste the entire code and try this
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
//Error type Syntax error on token "}", delete this token
} //
}
This should definitely work and click the text for a long time .Sure it will give you the output.
Try this code...
class file...
package com.longclick;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
//#Override
//public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
//return true;
//}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
} //Error type Syntax error on token "}", delete this token
}
.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</RelativeLayout>
I think it is working for you...
demo here
This code is outside of any function:
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
Check your functions structure.
The last two paragraphs of your code need a function to wrap them, possibly onCreate.
You need to call setLongClickable on the view for the long click to register.
Try this,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.textView1);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
return false;
}
});
}
Click the text for a long time and don't leave it quickly.
It is working fine.