I have a problem to test my non activity-class which need the context of a activity class.
The class Card extends ImageView and have a few methods:
public class Card extends ImageView {
public Card(Context context, int cardID) {
super(context);
this.cardID = cardID;
this.turnback = 0;
backImage = getResources().getDrawable(R.drawable.backimage);
setBackgroundDrawable(backImage);
}
}
I would like to test this class as a standalone class, is there an opportunity to create an Dummy Context?
The simple answer is no. The reason being that you are extending a view. The only way to test a view is to view it. To do that you need an activity. This is android fundamentals.
Related
Actually, I want to pass a Variable type and Id into a class to initialize the variable.
Like, Button myButton=new MyClass(Button, myButtonId);
Here Button is variable type, and myButtonId is a id from xml.
And the class (method) will return something like that (Button)findViewById(R.id.myButtonId);
For this I wrote
Button myButton=new MyClass(Button, myButtonId);
And Android Studio Suggest me
private class MyClass extends Button {
public MyClass(Object p, Button myButtonId) {
super();
}
}
There is an error at super(); line.
I have no idea to do more.
Any suggestion?
You need to put at least one (context) parameter in the super function
Please check the public constructors for the Button class here:
Button public constructors
UPDATE
You need to pass an context to your class which is extending Button, something like this:
MyClass myButton = new MyClass(getApplicationContext());
And then in your MyClass:
public class MyClass extends Button {
public MyClass(Context context) {
super(context);
}
}
I am using a singleton class to store global variables for the entire project. Also, to host some common functions which several classes/Activities may use, such as launching an alertBuilder window. But in order to do that... I need my singleton to extend Activity like this:
public class dataBaseObject extends Activity {
I tried to extend application, but that won't allow me to do this:
View view = context.getLayoutInflater().inflate(layoutType, null);
therefore, can someone tell me if there are any hidden pitfalls of extending Activity for a singleton ?
It doesn't make sense for an Activity class to be a singleton, because instances of Activity are instantiated by the android system.
What you can do is make an abstract class that extends Activity, like this
public abstract class AbstractActivity extends Activity {
public static final int EXAMPLE_CONSTANT = 345;
public final void exampleMethod() {
...
}
// This may not be needed
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
....
}
}
Then you can make all of your activity classes extend AbstractActivity. You do not need to declare an abstract class like this in manifest.xml.
An alternative solution is to make all of your utility methods have a parameter that is an Activity or a Context and pass this to these methods.
I am looking for information regarding writing my own application class. In many tutorials on the net I have seen the following code:
class myapp extends Application
{
private static myapp mm;
private Context context;
public Context getContext()
{
return getApplicationContext();
}
public myapp getmyapp()
{
if(mm == null)
mm = new myapp();
return mm;
}
}
What is the difference in getting object of myapp and getApplicationContext and where to use object of myapp and where to use context object. I just want to clear the concept of usage of these objects.
that code is completely wrong:
public myapp getmyapp()
{
if(mm==null)
mm=new myapp();
return mm;
}
only the Android framework can instantiate the Application object. I mean, you can call new but the object won't be "connected" to the underlying framework
To have a static reference of the application object you should do as follows:
class MyApp extends Application{
// I fixed the names to follow some Java convention
private static MyApp instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static MyApp getMyApp(){
return instance;
}
Regarding the context, the code is not wrong, but simply doesn't make any sense. That's because the Application object already is the application context. So there's not need to ask for it.
Context is what gives Android apps access to resources, file system specific folders, permissions, etc (what I said about the Android framework creates it). The Application is one class that extends Context, other examples are Activity and Service classes.
I hope it's a bit clearer.
I am trying of access a view from a non activity class, but I am not sure how to do it
After looking at some other sources, I understand I must pass the Context like this
public Manager(Context c) {
context = c;
}
but when I try c.findViewById() it doesnt work. How do you do this?
Try this
public Manager(Activity a) {
Activity = a;
}
then do
a.findViewById
If you make your view a public static variable, you can access it from anywhere in your application.
for example:
In your Activity class, use
public static TextView mTextView = null;
In its onCreate(), initialize the TextView.
Now from any other class, you can always use:
myActivity.mTextView
to access the TextView
In my android project, I have many activities and some of them already extend other stuff like map activity or BroadcastReceiver.
How do I create a function that I can call from any activity, because I don't want to have to repeat any code in multiple activities.
thanks.
If I have useful functions that perform little helpful tasks that I want to invoke from several Activities, I create a class called Util and park them in there. I make them static so that I don't need to allocate any objects.
Here is an example of part of one such class I wrote:
public final class Util {
public final static int KIBI = 1024;
public final static int BYTE = 1;
public final static int KIBIBYTE = KIBI * BYTE;
/**
* Private constructor to prevent instantiation
*/
private Util() {}
public static String getTimeStampNow() {
Time time = new Time();
time.setToNow();
return time.format3339(false);
}
}
To use these constants and methods, I can access them from the class name, rather than any object:
int fileSize = 10 * Util.KIBIBYTE;
String timestamp = Util.getTimeStampNow();
There's more to the class than this, but you get the idea.
You can extend the Application class, then in your activities call the getApplication method and cast it to your application class in order to call the method.
You do this by creating a class that extends android.app.Application:
package your.package.name.here;
import android.app.Application;
public class MyApplication extends Application {
public void doSomething(){
//Do something here
}
}
In your manifest you must then find the tag and add the android:name="MyApplication" attribute.
In your activity class you can then call the function by doing:
((MyApplication)getApplication()).doSomething();
There are other ways of doing something similar, but this is one of the ways. The documentation even states that a static singleton is a better choice in most cases. The Application documentation is available at: http://developer.android.com/reference/android/app/Application.html
You could create a static method or an object that contains this method.
You can create a class extending Activity, and then make sure your real activities are subclasses of that activity, instead of the usual built-in one. Simply define your common code in this parent activity.
Shachar
Create a new Java class BaseActivity with abstract Modifiers and extends it with AppCompatActivity.
Move all your methods under Java class BaseActivity.
package com.example.madbox;
public abstract class BaseActivity extends AppCompatActivity {
protected void YourClass() {
}
}
Extends your Activities with BaseActivity but not AppCompatActivity.