Toast.. on android - java

Hello can you please help me.. I would like to add toast on my code.. But I dont know where to put it.. I just want toast to appear after pressing button..
public class Question1 extends Fragment{
RadioButton q1a2;
Button btn1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question1, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
btn1 = (Button)getView().findViewById(R.id.btnq1);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
if (q1a2.isChecked()){
editor.putInt("answer_value", 1);
} else {
editor.putInt("answer_value", 0);
}
editor.commit();
}
});
}
}

Inside
onClick(View v){
// some else
// showing toast
Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
}

On click display toast
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(getActivity(),"Button pressed",Toast.LENGHT_SHORT).show();
...//rest of the code
http://developer.android.com/guide/topics/ui/notifiers/toasts.html

To Display Toast
Do like this on your onClick of the button listener
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* here ->*/ Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
}

You can very easily add a Toast inside the OnClickListener. When the listener is invoked, the code inside onClick() is executed, so you could modify this to include your toast:
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
Toast.makeText(getActivity(), "onClick() executed", Toast.LENGTH_SHORT).show();
...
}
});
Bear in mind that Toast.makeText() simply creates a new Toast object. Invoking show() on that object will cause it to appear. The duration of the toast can be controlled using Toast.LENGTH_SHORT or Toast.LENGTH_LONG.
I'd suggest getting a little more acquainted with Android by understanding how events and listeners work.

Related

Android Studio cant resolve error setOnClickListener

Hi I can't seem to build this.. It states that it cannot resolve the OnClickListener. The onClick action is performed the back button that goes to the main activity.
Button bnCompute = (Button) this.findViewById(R.id.bnCompute);
bnCompute.setOnClickListener(new View.OnClickListener());
{
#Override
public void onClick (View view){
Toast.makeText(MainActivity.this, "You Compute All!", Toast.LENGTH_LONG).show();
EditText etBeauty = (EditText) MainActivity.this.findViewById(R.id.etBeauty);
EditText etBody = (EditText) MainActivity.this.findViewById(R.id.etBody);
EditText etIntelligence = (EditText) MainActivity.this.findViewById(R.id.etIntelligence);
int total = Integer.parseInt(String.valueOf(etBeauty.getText())) + Integer.parseInt(String.valueOf(etBody.getText()))
+ Integer.parseInt(String.valueOf(etIntelligence.getText()));
Intent actSummary = new Intent(MainActivity.this, Score.class);
actSummary.putExtra("total", Integer.toString(total));
MainActivity.this.startActivity(actSummary);
}
}
You have implemented onClickoutside the scope of listener.
It should be something like this below :
Button button = (Button) findViewById(R.id.button1);
//Your mistake is on this line.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});

Have to click a button twice for it to work in Android Studio

So I am currently creating an app and one of the small things that have been bothering me is the fact that I have to click a button twice for it to work.
This is my code and I can't see anything wrong with it:
public void signUpButtonClickAction(View v){
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
}
xml code for my button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUps"
android:id="#+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="signUpButtonClickAction"/>
It is probably a small fix but even I can't spot this bug
Solution
Remove the line android:onClick="signUpButtonClickAction" and add
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
to the onCreate method of your activity or the onCreateView method of your fragment.
Alternative Solution
Alternatively, change the code to this
public void signUpButtonClickAction(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
Explanation
The line android:onClick="signUpButtonClickAction" in the xml is causing an internal call to signUpButtonClick.setOnClickListener(), so you don't have to set up an onClickListener in the signUpButtonClickAction again.
Initializing multiple buttons
private void initializeButtons() {
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
Button anotherButton = (Button) findViewById(R.id.anotherButton);
anotherButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG", "Clicked on another button!");
}
});
}
Now simply call initializeButtons() from the onCreate method of your activity.
The problem is that you are setting two times a onClick action. In your xml code you have just asign an onClick() to your button, you don't need to setOnClickListener() inside the signUpButtonClickAction(View v). You have two options:
Leave the xml file like it is and inside signUpButtonClickAction(View v) do :
public void signUpButtonClickAction(View v){
startActivity(new Intent(MainActivity.this, Signup.class));
}
OR
Remove the onClick of your xml file:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUps"
android:id="#+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
And do this in your Activity:
Button yourButton = (Button) findViewById(R.id.signUpButton);
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
The cause of the problem is : onclick() and onClickListener are literally the same! And you are implementing both, the end result is you'll need to press the button twice to start the Activity!
See this question for more info
FIX:
The solution to your problem is :
1:
public void signUpButtonClickAction(View v)
{
startActivity(new Intent(MainActivity.this, Signup.class));
}
2:
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
as mcwise said
android:onClick="signUpButtonClickAction"
and
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
does the same thing. so you have to go with one of them. Having the two is causing the problem
For whom it may concern:
I had the same issue but none of the solutions above solved it.
For some reason I cannot understand, I had in my button this line of code:
android:textIsSelectable="true"
Deleting this attribute from the button makes it work.
This obviously made the first click to select the text, and the second click triggered the onClick button.

Why unable to start new activity when click on the rate design button

When I click on the rate design button it stops, I doubt is my manifest because I have check it already below are my codes. I have also enter the error log. Can someone help me instead of rating down for this question
Your forget to add
setContentView(R.layout.yourlayout);
before Button initialization in onCreate(....)
Correct:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
Button btnRating = (Button) findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NearablesDemoActivity.this, MainActivityRating.class);
startActivity(intent);
}
});
}

taking variable from another class

I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.

Make an onclicklistener in SherlockFragment

Hello I want to make a click listener in a sherlockfragment but every time I click it, it displays "view.class source not found" it doesn't matter what action I do I want to use it to start an intent to change an activity
here is my code:
public class SafanTab extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.safantab, container, false);
}
public void onOverClick(View view) {
Intent myIntent = new Intent(view.getContext(), OverSafan.class);
startActivityForResult(myIntent, 0);
}
public void nProductenClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
public void onTwitterClick(View view) {
Intent myIntent = new Intent(view.getContext(), Over_Safan.class);
startActivityForResult(myIntent, 0);
}
}
I define the listener to the button in the layout xml:
android:onClick="OnOverClick"
I tried it a long time and also searched the internet for "View.class source not found" but can't find anything to solve my problem. As you see in the question I'm using ActionBarSherlock.
You won't get the onOverClick callback in the fragment. Use something like the following in your onActivityCreated() method.
// Handle onclick
getView().findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something
}
});
See this question for more options: How to handle button clicks using the XML onClick within Fragments
EDIT:
For onCreateView you can do the following:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// do something
}
});
return view;
}
Not sure if this is the problem, but I see that the name of your method is defined as:
public void onOverClick(View view)
but in the XML you specify:
android:onClick="OnOverClick"
Letter "O" is capitalized in xml.

Categories