I have 2 classes BlockIdActivity.java and ScanWifi.java. I have 2 buttons in BlockIdActivity.java file and i am able to see my toast defined there. However i cant see the toast for the button i defined in ScanWifi.Java class.
Following is the code for BlockIdActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class BlockIdActivity extends ActionBarActivity {
private ImageButton mUpButton;
private ImageButton mDownButton;
private TextView mBlock_Id_Field;
int counter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_block_id);
mUpButton = (ImageButton)findViewById(R.id.arrow_up);
mDownButton = (ImageButton)findViewById(R.id.arrow_down);
mBlock_Id_Field = (TextView)findViewById(R.id.BlockIdField);
mUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mBlock_Id_Field.getText().toString().trim().equals(""))
{
counter = 1;
mBlock_Id_Field.setText(String.valueOf(counter));
}
else {
counter = Integer.valueOf(mBlock_Id_Field.getText().toString().trim());
counter++;
mBlock_Id_Field.setText(String.valueOf(counter));
}
}
});
mDownButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int counter = Integer.valueOf(mBlock_Id_Field.getText().toString().trim());
// boolean emptyfield = mBlock_Id_Field.getText().toString().equals("");
if(counter <=1 ){
Toast.makeText(BlockIdActivity.this,
R.string.negative_blockid_toast,
Toast.LENGTH_SHORT).show();
counter = 1;
mBlock_Id_Field.setText(String.valueOf(counter));
}else {
counter--;
mBlock_Id_Field.setText(String.valueOf(counter));
}
}
});
}
}
and code for ScanWifi.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ScanWifi extends ActionBarActivity {
private Button mScanWifiButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_block_id);
mScanWifiButton = (Button)findViewById(R.id.ScanWifiButton);
mScanWifiButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
R.string.ScanWifi_toast,
Toast.LENGTH_SHORT).show();
}
});
}
}
This is the strings.xml file :
<resources>
<string name="app_name">SnifferTrain</string>
<string name="BlockId">BlockId</string>
<string name="ScanWifi">ScanWifi</string>
<string name="ScanWifi_toast">ScanWifi Button Pressed</string>
<string name="negative_blockid_toast">Block Id Field Cannot Be Less Than 1 or Empty.Setting BlockID To 1</string>
<string name="action_settings">Settings</string>
</resources>
Can someone please explain why ScanWifi_toast does not show when i press ScanWifi Button. I have tried changing the context of the toast to ScanWifi.this or getApplicationContext() but it doesn't seem to work. I don't get any compilation errors. Please help
I think the problem with your string file. Do like other way:
Toast.makeText(ScanWifi.this,getResource().getString(R.string.string.ScanWifi_toast),Toast.LENGTH_SHORT).show();
Your ScanWifi activity code is not run. Hence the click listener is not registered to a button on screen and therefore the toast is not seen.
Since both your activities are using the same content view layout, you should probably move the findViewById() and setOnClickListener() to the main activity.
To launch other activities, use an Intent, e.g.
startActivity(new Intent(context, ActivityName.class));
It should be :
Toast.makeText(getBaseContext(),getResource().getString(R.string.ScanWifi_toast), Toast.LENGTH_SHORT).show();
Because R.string.ScanWifi_toast dont automatically return the String you put in your XML, it only return the id.
you should use getString(R.string.ScanWifi_toast) in place of directly using R.string.ScanWifi_toast
Use this for Toast message
Toast.makeText(getBaseContext(),
getResource().getString(R.string.ScanWifi_toast),
Toast.LENGTH_SHORT).show();
Try this
Toast.makeText(context, context.getString(R.string.ScanWifi_toast), Toast.LENGTH_LONG).show();
You can use this
Toast.makeText(getBaseContext(),"xyz",Toast.LENGTH_SHORT).show();
or
Toast.makeText(Activity.this,string,Toast.LENGTH_SHORT).show();
Related
It's just a simple text with two buttons that creates toasts and change the text. I'm very new to this AS so I really don't know how to even attempt to fix this..
Any help is very appreciated...
MainActivity.java:
package com.example.david.davidisawesome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Started.");
final TextView firstText = (TextView) findViewById(R.id.firstText);
Button firstButton = (Button) findViewById(R.id.firstBtn);
Button secondButton = (Button) findViewById(R.id.secondBtn);
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: First Button Clicked.");
toastMessage("You Clicked the first button");
firstText.setText("Nice Job.");
}
});
secondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Second Button Clicked.");
toastMessage("You Clicked the second button");
firstText.setText("Good Effort.");
}
});
}
private void toastMessage(String message){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
In picture:
activity_main.xml:
Error:
It is not finding your xml elements. The error shows that you have a null exception on setting the onclick interface to the button. So start by confirming that you are inflating the proper xml and finding the proper id.
More specifically your secondBtn is not found.
I m trying to create a new java file for button (sin) as this is about calculator. But it is showing Error in second override annotations. (Method does not override method from its SuperClass). There is one more Error associating with brackets in the bottom part of code. Being newbie this is the first time I'm having my hands on this part of java. I have searched many of the sites but didn't found any solution. Any help will be appreciated.
SinglesinActivity.java
package com.marsh.calculator.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class SinglesinActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.btnsin);
b.setOnClickListener(this);
}
#Override
public void onclick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin:
break;
}
};
}
It's onClick not onclick.
Make the following changes:
#Override public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin: break;
}
};
Its a typo. change onclick to onClick.
I have a imageview that randomly generate 1 out of 2 possibles images clicking on one button.
I want that when one image is showed (R.drawable.aa) and I press other button, a toast is shown.
My problem is that once a random image is shown and click on the other button, nothing happens.
package com.example.isaiasalarcon.menu;
import java.util.Random;
import java.util.jar.Attributes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import static com.example.isaiasalarcon.menu.R.drawable.aa;
public class buho extends Activity {
// UI components
private Button drawButton;
private Button boton2;
private ImageView cardImage;
// Random object
private final static Random random = new Random();
// The card deck
private final static int[] cardDeck = new int[] {
R.drawable.aa,
R.drawable.a2,
};
private Integer q;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buho);
drawButton = (Button)findViewById(R.id.drawButton);
boton2 = (Button)findViewById(R.id.button2);
cardImage = (ImageView)findViewById(R.id.cardImage);
drawButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
Integer q = cardDeck[random.nextInt(cardDeck.length)];
cardImage.setImageResource(q);
}
});
boton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (q.equals(R.drawable.aa)) {
Toast toast = Toast.makeText(buho.this, "si", Toast.LENGTH_LONG);
toast.show();
} else {
Toast toast = Toast.makeText(buho.this, "no", Toast.LENGTH_LONG);
toast.show();
}
}
});
}
}
two things:
1) Integer q I don't see a reason why this needs to be an Integer. You should be able to int
2) your q inside the onClick is creating a NEW variable called q. You need to update your code to the following: (note how it doesn't have the Type declaration before it, so it means to use the previously declared variable)
drawButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
q = cardDeck[random.nextInt(cardDeck.length)];
cardImage.setImageResource(q);
}
});
I am trying to access MainActivity function to my another java class. But i am not able to use these function. Please tell me what else need to be added to get it access.
My code:
Where i am trying to access my MainActivity
package com.example.musicplayer;
**import com.example.musicplayer.MainActivity;**
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class current_song extends Activity implements OnClickListener{
MainActivity ma = new MainActivity();
protected void onCreate(Bundle icicle) {
Bundle extra = getIntent().getExtras();
super.onCreate(icicle);
setContentView(R.layout.songplay_page);
if(extra != null){
String song_name = extra.getString("song_name");
TextView textchange = (TextView)findViewById(R.id.current_song_name);
textchange.setText(song_name);
textchange.setSelected(true);
}
Button btn_pause = (Button)findViewById(R.id.pause_btn);
btn_pause.setOnClickListener(this);
Button btn_next = (Button)findViewById(R.id.next_btn);
btn_next.setOnClickListener(this);
Button btn_prv = (Button)findViewById(R.id.prv_btn);
btn_prv.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "In Onclick ()", Toast.LENGTH_SHORT).show();
switch(v.getId())
{
case R.id.pause_btn:
Toast.makeText(getApplicationContext(), "pause", Toast.LENGTH_SHORT).show();
ma.pause();
break;
case R.id.next_btn:
ma.next();
break;
case R.id.prv_btn:
ma.prv();
break;
}
}
}
Make sure that MainActivity has a zero argument constructor and the access specifier for pause , next and prv function is public.
In response to "i have some methods defined by me stop(), next(), pri() i am trying to access these methods when i click on each button. If you think that "creating a separate common class for sharing all methods" can you please show me 1 example bec i don't know how to access a method from 1 activity to another. "
public class myController{
private MyActivity m;
public myController(MyActivity m){
this.m = m;
}
public void stop(){
m.stop;
}
}
In other classes you initialize in the main activity and pass it the controller object so it can call the stop method
I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. Here is my activity:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class test extends Activity {
private Button btnChangeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salesticketoilui);
mainProgram();
}
public void mainProgram() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
} // end onClick
}); // end setOnClickListener
Button buttonExit = (Button)findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitActivity();
} // end onClick
}); // end OnClickListener
// setup button listener for saving data and exit to main
Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
buttonSaveExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveExit();
} // end onClick
}); // end OnClickListener
} // end MainProgram ()
public void saveExit() {
// does stuff
}
public void exitActivity () {
// does stuff
}
} // end class
any thoughts?
Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. Either call mainProgram from onCreate or just put that code directly into onCreate.
I believe the onClickListeners need to go inside of your onCreate method.
Listen to Scott
and looks like your missing the #Override
new View.OnClickListener() {
#Override
public void onClick(View view) {
exitActivity();
} // end onClick
}
Make sure your java settings are to 1.6 to avoid code completion missing this.
I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste.
To make it work, I had to create new buttons in my layout and declare the events manually. Weird!