android-context

Android: ProgressDialog doesn't show

谁说胖子不能爱 提交于 2019-11-26 15:39:33
问题 I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail. This is what I got: btnSubmit.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { (...) ProgressDialog pd = new ProgressDialog(MyApp.this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)

Using getResources() in non-activity class

折月煮酒 提交于 2019-11-26 15:07:40
I am trying to use getResources method in a non-activity class. How do I get the reference to the "resources" object so that I can access the xml file stored under resources folder? Example: XmlPullParser xpp = getResources().getXml(R.xml.samplexml); LuckyLuke You will have to pass a context object to it. Either this if you have a reference to the class in an activty, or getApplicationContext() public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { RegularClass regularClass = new RegularClass(this); } } Then you can use it in the constructor (or set it to

getString Outside of a Context or Activity

牧云@^-^@ 提交于 2019-11-26 15:01:09
问题 I've found the R.string pretty awesome for keeping hardcoded strings out of my code, and I'd like to keep using it in a utility class that works with models in my application to generate output. For instance, in this case I am generating an email from a model outside of the activity. Is it possible to use getString outside a Context or Activity ? I suppose I could pass in the current activity, but it seems unnecessary. Please correct me if I'm wrong! Edit: Can we access the resources without

How to access Activity UI from my class?

梦想与她 提交于 2019-11-26 14:15:47
问题 I have an activity which creates an object instance of my class: file MyActivity.java: public class MyActivity extends Activity { TextView myView = (TextView)findViewById(R.id.myView); ... Points myPoints new Points(); ... } -------------------------------------------------------------- file Points.java: private class Points { ... HOW TO USE myView HERE ??? ... } -------------------------------------------------------------- How do I use the UI objects in my class (which does not extend an

What's “tools:context” in Android layout files?

这一生的挚爱 提交于 2019-11-26 13:52:47
Starting with a recent new version of ADT, I've noticed this new attribute on the layout XML files, for example: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" /> What is "tools:context" used for? How does it even know the exact path to the activity that is written there? Does it look at the package of the app, inside the manifest? Is it limited to classes that extend Context or only

Android: why must use getBaseContext() instead of this

落花浮王杯 提交于 2019-11-26 12:36:27
问题 this often to reference to current context. But, at some case, why we must use getBaseContext() instead of this . (It means when use this will notice error). Here is my example: Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){ Toast.makeText(getBaseContext(),\"SELECTED\", Toast.LENGTH_SHORT).show(); /

Get context inside onClick(DialogInterface v, int buttonId)?

雨燕双飞 提交于 2019-11-26 12:25:28
问题 Getting the context inside onClick(View view) , the callback for a button\'s onClickListener() , is easy: view.getContext() But I can\'t figure out how to get the context inside onClick(DialogInterface v, int buttonId) , the callback for a dialog \'s onClickListener public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener Is this possible? 回答1: You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous

What is the difference between Activity and Context?

最后都变了- 提交于 2019-11-26 12:25:10
问题 Are Activity and Context the same, or are there differences? When should I have a method pass an Activity, and when a Context? 回答1: As far as I understand: Context is the Base Object. So every Activity same as Application derives from Context. This means that every Activity and every Application IS a Context ; From developer.android.com Activity java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity And Application

Use of Context to start another Activity

两盒软妹~` 提交于 2019-11-26 11:26:21
问题 To start an Activity you need an Intent, like: Intent i = new Intent(context, class) So to fill in the context parameter, a couple of options are available: Use MyActivity.this or just this Use getApplicationContext() Use getBaseContext() And I\'m sure there are one or two more options. These options all appear in some sort of tutorial, one uses the first, the next uses the third option. So which one should I use? Does it even matter? Is it different for different cases? 回答1: Yes its

Call to getLayoutInflater() in places not in activity

别说谁变了你拦得住时间么 提交于 2019-11-26 10:07:58
问题 What does need to be imported or how can I call the Layout inflater in places other than activity? public static void method(Context context){ //this doesn\'t work the getLayoutInflater method could not be found LayoutInflater inflater = getLayoutInflater(); // this also doesn\'t work LayoutInflater inflater = context.getLayoutInflater(); } I am able to call getLayoutInflater only in activity, is that an restriction? What if I want to create custom dialog and I want to inflate view for it, or