I'm new to Android development and Java and I'm having troubles with Toast:
I have a MainActivity with several buttons. Each one of this starts another Activity with typical setOnclickListener method like this:
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), secondaryActivity.class);
startActivity(intent);
}
});
Then, inside this secondaryActivity.class I have another button that make some stuff. Inside this Activity I wanna display a Toast on button's click but it doesn't work:
secondaryActivityBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"text",Toast.LENGTH_LONG).show();
}
});
As Toast's context I've tried: getApplicationContext(), getBasecontext(), view.getContext(), mySecondaryActivityClass.this... No one of this display the Toast, I don't know where's the mistake. Supposedly, view.getContext() might work but it doesn't display anything...
MainActivity extends AppCompactActivity and mySecondaryActivity extends Activity.
You can use secondaryActivity.this like:
Toast.makeText(secondaryActivity.this,"text",Toast.LENGTH_LONG).show();
It works only on secondaryActivity class
Try this:
Toast.makeText(getActivity().getApplicationContext(),"text",Toast.LENGTH_LONG).show();
Related
Hello at the moment I have a problem with calling a new activity.
I want to call a new Activity Inside my SettingsActivity.
I call the SettingsActivity by pressing an Button :
Button button = findViewById(R.id.button_navSecond);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SettingsActivityModern.class);
startActivity(intent);
}
});
But at the next Button click to get to another Acitivty inside this Activity I get back to the Mainactivity
premiumBuy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SettingsActivityModern.this, BuyPremiumActivity.class);
startActivity(intent);
}
});
It seems like it is not possible to Call startActivity(intent); inside an activty which was called with this command. So how to prevent that?
Try adding the Activity in the AndroidManifest.xml like this:
<activity
android:name="com.example.foodgent.YOUR_ACTIVITY" />
i am working on an app in which, button Onclick dynamically created a button from one activity and button is appeared in another activity.
So, for your button, see the code below. I'm just using SharedPreferences for testing if button has been clicked before or not.
// Inside your onCreate() method of your SecondActivity.java
((Button)findViewById(R.id.activity2_button)).setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).edit().putBoolean("ShowButton", true).commit(); // put Boolean inside SharedPreferences
Intent main = new Intent(this, FirstActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(main);
finish();
}
}
And now, the FirstActivity.java code :
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
if (getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).getBoolean("ShowButton", false))
((Button)findViewById(R.id.activity1_button)).setVisibility(View.VISIBLE);
else
((Button)findViewById(R.id.activity1_button)).setVisibility(View.GONE);
}
}
Test it, and tell me if this works great. Hope it will work for you, Darkball60 :)
I have an application where you can select an item from a AlertDialog spinner like, but I don't know how to make my app, to memorize the selected choice, and then behave my button to this. This is what I have now:
#Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mp = MediaPlayer.create(getApplicationContext(), R.raw.ais);
mp.start();
Intent myIntent = new Intent(MainActivity.this, Main2Activity.class);
MainActivity.this.startActivity(myIntent);
}
}, 9000);
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
});
}
And somehow to this code, I want to add my last selected item button, for example:
If ( last selected item from AlertDialog was = dance)
do the code, that I wrote above.
One way to do it is with an interface. Define an interface in your Alert Dialog fragment and instantiate it like this:
public interface MyDialogListener {
//put whatever data you want to pass as a paramenter, below I have two examples
public void onDataSelectedEvent( String action);
}
MyDialogListener myListener;
The listener will have to be instantiated in the dialog by typecasting your activity to a MyDialogListener.
You could do it like this:
myListener = (MyDialogListener) MainActivity.this;
Or even better would be to use the activity parameter in the dialog's onAttach(...) method.
We are able to do this because later, we will make your activity implement the MyDialogListener interface, which will effectively make the activity a MyDialogListener.
Put this line in the method or listener, maybe an onClick() in your alert dialog. This is for a spinner:
myListener.onDataSelectedEvent(mySpinner.getSelectedItem().toString())
Implement the interface in your Activity that receives the data like this:
public class MyActivity extends Activity
implements MyAlertDialog.MyDialogListener{
Then, in the activity receiving the data, have the interface method look like this:
#Override
public void onDataSelectedEvent(String action) {
//probably better to use a switch statement
if(action == "dance") {
// dance()
} else if(action == "stand") {
.......etc
}
}
There is a good example here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
actually I am bit confused on some thing which is working in one scenario very well but in other scenario that is not working fine so I am here to explain my problem to identify the wrong move made by me, if any on will identify the problem please mention it. thank you.
working fine:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.distance_demo_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
intent.putExtra(ListViewClass.EXTRAS_TARGET_ACTIVITY, Distance.class.getName());
startActivity(intent);
}
});
NOt working fine:
scan = (Button) view.findViewById(R.id.scan);
scan.setOnClickListener(startListener);
}
//Create an anonymous implementation of OnClickListener
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(view.getContext(),"Scanning", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
intent.putExtra(ListViewClass.EXTRAS_TARGET_ACTIVITY, Distance.class.getName());
startActivity(intent);
}
}
now the problem is in this line
Intent intent = new Intent(MainActivity.this, ListViewClass.class);
when I use this line in this scenario it shows me error on this line:
Error: 'The constructor Intent(MainActivity, Class<ListViewClass>) is undefined' please mention my problem if you are well aware of it.
use a global context variable glaboaly and use it.
Context myContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myContext = MyActivity.this;
}
and use myContext insted of MainActivity.this.
Im not sure whether it solve the problem, just try it out
When your calling Intent intent = new Intent(MainActivity.this, ListViewClass.class); inside MainActivity class. It will work fine. But when your not in Context of MainActivity class. i.e move to other class. This error will occur.
If you want to rectify, You can follow #jithu method. Otherwise store the context in Application class. And use it in anywhere.
In the first case you are overriding the method onClick().
If you override the method it's like you are declaring the function in your activity class.
This is the reason that you can call MainActivity.this
In the second case you are implementing the abstract method onClick(). You can't call MainActivity.this because your "MainActivity.this" is referencing to the current context and this isn't accessible from OnClickListener.class.
I am trying to build a char using the library from achartengine (http://www.achartengine.org/). So i try to run SalesGrowthChart.java on my own aplication so when someone clicks on a button it will show him the chart .
This is my code :
private IChart[] mCharts = new IChart[]{new generatedchart()};
And i try to generate it like this
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), generatedchart.class);
myIntent=mCharts[0].execute(this);
}
});
But this won't work. How can i make it to work.Hope you understand what i am saying.
This is the error :
The method execute(Context) in the type IChart is not applicable for the arguments (new View.OnClickListener(){})
Your problem is that the "this" mentioned inside that method refers to a view (which is what you are creating at that point.)
The method execute needs a Context, so you need to get the context in a different manner.
You should try getting the context like this:
YourActivityName.this
Which in your code would be like:
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), generatedchart.class);
myIntent=mCharts[0].execute(YourActivityName.this);
}
});
When you are calling this within execute(), it is referring to the OnClickListener class because of the dynamic class declaration. Try using getApplicationContext() instead of this.