Toast in Fragment, should use getActivity() or getAcitivity().getApplicationContext()? - android

Sorry for my newbie Question, i just cannot find the answer from google and stackoverflow.. i just start learning for android, wish can build a good base for android knowledge. I wondering which i should use in the following if i create toast.maketext in fragment. getActivity() or getAcitivity().getApplicationContext()?
i did try both, it works well..
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "hello",Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getApplicationContext(),"Hello",Toast.LENGTH_LONG).show();
}
});

For user interface related calls use the Activity context.
See this explanation by Reto Meier:
https://stackoverflow.com/a/987503/534471

Related

My apps won't display "KitKat" toast messages

So I just updated my phone to KitKat android 4.4.2 and the apps I made do not show the new themed KitKat toast. I import android.widget.Toast but still not showing the newly themed one. Is my ROM overriding it? However, Facebook does show the new toast. Not really important, just puzzled me.
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "This toast is not kit kat", Toast.LENGTH_LONG).show();
}
});
}
Better use YourActivity.this instead of getApplicationContext()
Welp, after much confusion. I finally figured it out! :D
I decided to go to the Google source code for the toast notifications. I then decompiled my ROMs framework-res.apk and found the same source. However, i found an aditional source with a "tw" in front of it. This stands for "TouchWiz".
So simply, Samsung was overriding certain toast messages. I simply went and made both sources match the default source and now all my toasts are the nice neat KitKat toast themed :)
Thanks everyone for chatting with me about it!

How to make OnDrawerOpenListener() work in android

Can someone explain how to get this to work please. Says its deprecated, just wondering if I can force it or if there is another way to change my title label when the SlidingDrawer is opened then change it back when it is closed. I still have a lot to learn so if possible please reply with something that is cut and paste friendly ;) Thanks.
Jesse
OnDrawerCloseListener onClick_DrawerClosed = new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
titletext.setText("Flow Charts");
}
};
OnDrawerOpenListener onClick_DrawerOpened = new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
titletext.setText("Options Menu");
}
};
SlidingDrawer in general is deprecated as of API 17 (the latest). This doesn't mean you can't use SlidingDrawer and it's methods, but that it's "frowned upon" and Eclipse will give you warnings. You can make those go away by right clicking on the warnings and selecting "Suppress..."
Look here for a more thorough discussion:
SlidingDrawer deprecated

Libgdx and Leadbolt

I'm thinking of integrating Leadbolt (or Tapjoy) to my Libgdx game. I want to make a store based on the clicked forms. More click - more stuff from the store for the user. I found Leadbolt and Tapjoy. These ad providers are providing direct helps for these stuffs. I have take a look on the Leadbolt integrating guide. I have a problem with it. It needs me to pass a context to the AdController. I don't know if it possible in any way to pass the context for the AdController or not, so I would like to ask you about it, that how can I do, if I can. (I haven't tried it in Libgdx yet, but with the superjumper example had problem too, when I wanted to pass a context for something, and I think because the Libgdx app don't extends an Activity, it will have problems too.)
Here is the sample code from Leadbolt:
AdController myControllerForm = new AdController(this, "MY_LB_ID", new AdListener() {
public void onAdProgress() {}
public void onAdLoaded() {
myControllerForm.hideAd();
}
public void onAdFailed() {
launchMain();
}
public void onAdCompleted() {
myControllerForm.hideAd();
launchMain();
}
public void onAdClosed() {
launchMain();
}
public void onAdClicked() {}
public void onAdAlreadyCompleted() {
launchMain();
}
public void onAdHidden() {
launchMain();
}
});
myControllerForm.setAsynchTask(true);
myControllerForm.loadAd();
}
public void launchMain()
{
finish();
startActivity(new Intent(Splash.this, MainApp.class));
}
}
The class extends Activity, and the methods are in the onCreate() method in this example.
If you have integrated the Leadbolt or Tapjoy to your Libgdx game, then could you please give me a code about how did you do it?
Thanks in advance!
LibGDX actually extends AndroidActivity (by AndroidApplication, which extends AndroidActivity).
If you setup your project correctly, you can access it from "Android starter" project. This is also the only place, where you can play with ads, because "Desktop starter" in no way extends AndroidActivity.
Here's also adMob tutorial, which you may find useful (creating overlaying views).
This is all info I can give you, as I don't know whether you want to display your ads always, reload them as time passes or just hide them after particular events. In such cases, you might want to implement your custom interfaces.
Good luck!
Check out the libGDX tutorial on AdMob: http://code.google.com/p/libgdx/wiki/AdMobInLibgdx
Skip the stuff at the top about setting up AdMob, I think you need the part in the "Control" section which talks about getting events from your generic libGDX code (which has to also run on the desktop) into your Android-specific code (for example to show an ad). The general way is to define your own interface (see the IActivityRequestHandler in the AdMob tutorial), and pass an object that implements that interface into your libGDX code. On the desktop this object would do nothing, and on the Android side you can use all the standard Android code to do the right thing.

How link 2 layout with a Button

I don't speak english very well, so please try to understand me.
iìm trying to develop a little app for android in eclipse. I've create 2 layout where, in first there are a button and in a second there is only the text "helloword". I want that the button to send me at the "helloworld" layout
I followed a tutorial but it's not correct.
Someone can help me posting th code to implement??
Excuse my English still
and thanks for any help
You need to create two different Activities, with separate layouts. You set their layouts using setContentView inside onCreate. Don't forget to include your activities in the manifest (eclipse generated one for you. follow how it was done in the first activity and you should be fine. no need to add the filters and such, just the .Activity)
then in your first activity's button, do this:
my_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, MySecondActivity.class);
startActivty(i);
}
});
If you want to use just 1 activity then use the following code,
my_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
FirstActivity.this.setContentView(R.layout.second_layout);
}
});
Its not so hard just do this:
#Override
public void onClick(View args0) {
setContentView(R.layout.aardelayout);
}
just a simple Search on google and you find your answer .
see this tutorials ( two are in frensh if you are prefering frensh tutorials ) :
tuto1
tuto2
tuto3

How design a button in Eclipse for Android App, so when you click, it takes you to a website

I already asked this question, but got answers that don't work and it counted as "Answered". Maybe someone knows how to design button in Eclipse for an Android app, so it takes you to a website ?Let's say this website.Thanks a lot.
Say u wanna go to google.com
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onOpenWebBrowser(View v)
{
Intent webPageIntent = new Intent(Intent.ACTION_VIEW);
webPageIntent.setData(Uri.parse("https://www.google.co.in/"));
try {
startActivity(webPageIntent);
} catch (ActivityNotFoundException ex) {
}
}
}
Make sure u add uses internet permission to the manifest!
you can refer the following to get an idea ,
see this vogella from there, you can get idea how to create android app which has a Button
http://www.vogella.de/articles/Android/article.html
and then create WebView in xml and map it in java, as
http://developer.android.com/resources/tutorials/views/hello-webview.html
and For mor info refer android people.com
http://www.androidpeople.com/
http://developer.android.com/resources/samples/index.html
and also read my blog to know about android basics
http://sankarganesh-info-exchange.blogspot.com/p/corridor-your-foremost-footstep-in.html
You would need to set up an OnClickListener and tie it to your button. In the Listener's onClick method, you would load the specified URL into the WebView, which, based on your comments, it sounds like you've got that part figured out.

Categories

Resources