I need to call the setContentView(int) from my main Activity from another class which does not extends Activity.
In my custom class I've got the private Context context; var that is passed from the Activity in the Constructor but I can't figure out how to acces the Activity methods using the context variable.
If your context is an instance of Activity class, simple class cast should work:
Activity a = (Activity) context;
a.setContentView(R.layout.your_layout);
One solution (may not be the most elegant) is to pass the calling activity to the other class, not just the context.
You would have to pass in a reference to the Activity you're using.
Something like this
class ActivityA extends Activity{
#Override
public void onCreate(Bundle state){
super.onCreate(state);
ClassA myclass = new ClassA(this);
}
}
And then Class A would have:
class ClassA {
public ClassA(Activity yourActivity){
... Get your view here ....
yourActivity.setContentView(view);
... do more things...
}
}
Related
I want to know how can I send the context of an activity from ClassA.java to MainAddingItems.java.
I can pass the MainActivity.class as the argument but I am not able to pass the context.
Here is some of the code of ClassA calling the constructor of MainAddingItems
new MainAddingItems(MainActivity.class,"MainActivity Clicked",R.id.activity_main_linearLayout,"Profile Acitvity")
It will work ..make sure that you declerad it as public and static like below
Public static Context context ;
In your main activity
You can use any where that context in whole project
You can do this. If your constructor is:
MainAddingItems(Context context) {...}
Then from Activity1 you can just do:
Context context = Activity1.this; // Or getApplicationContext() or View.getContext() or whatever context you want
MainAddingItems(context);
Make your MainAddingItem class's constructor as this,
class MainAddingItem {
Context context;
MainAddingItem (Context context, rest of the parameters){
this.context = context;
}
}
Use the above constructor and pass the activity's context in the constructor's parameter.
new MainAddingItem (this, rest of the parameters);
Actually you are passing the context here . In android context is the current state of application . As an example if you are in a activity, then the context is the activity class itself , same for service and any other component of android
Let me give another example ,
Just try to show a toast message in onCreate method of any activity , considering your activity name is SimpleActivity you may write something like this
Toast.makeText(this "Understating context",Toast.LENGTH_LONG).show();
Or you may write the following
Toast.makeText(SimpleActivity.this "Understating context",Toast.LENGTH_LONG).show();
The first parameter of makeText method is context , and it works if you simply pass the class .
This is how you will pass activity as an argument
public SimpleClass{
AppCompatActivity mActivity;
public SimpleClass(AppCompatActivity mActivity){
this.mActivity = mActivity;
}
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleClass newSimpleClass = new SimpleClass(MainActivity.this);
}
}
Just make Context as static like in MainActivity.
Public static Context contextToUse;
You can use like below :
MainActivity.contextToUse
Please let me know ,if it is helpful to you
I have been struggling with this problem for two days,I am in situation where i need to use a method in ActivityB from ActivityA . The problems lays in getting the context of A i have tried many solutions like:
static ActivityA activityA;
In onCreate state:
activityA = this;
and add this method:
public static ActivityA getInstance(){
return activityA;
}
In ActivityB, call
ActivityA.getInstance().myFunction(); //call myFunction using activityA
it did not work out because this need the ActivityA to be instantiated in order to pass its context to A but this is not accomplishable in my case is there any way of getting an activity's context without switching activities .
my question might turn out to be simple or intuitive but im new to this concept , thanks in advance
As you want to have common functionality in both activities, you can create BaseActivity that extends Activity and define your method in that and extend ActivityA and ActivityB by BaseActivity then you can access methods.
You can do it like this,
public class BaseActivity extends Activity
{
public void myFunction()
{
...
}
}
And do this for other activities:
public class ActivityA extends BaseActivity
{
public void someMethod()
{
myFunction(); // you can call function here directly
}
}
You could extent class A using Class B simply
OR
public static ActivityA activityA;
In onCreate state:
{
activityA = this;
}
Outside Oncreate
public myFunction{
}
and in ActivityB call
activityA.myFunction();
Here I Created Two Classes Consider as Activities , And Then Created one Public methodA() in class Activity_A , then Created Class Activity_B and Created methodB() , And Created Object of Activity_A and Called methodA() by passing context of Activity Activity_B .
class Activity_A{
public void methodA(Context context){
Toast.makeText(context,"methodA",Toast.LENGTH_LONG).show();
}
}
class Activity_B{
public void methodB(){
Activity_A activity_a = new Activity_A();
activity_a.methodA(Activity_B.this);
}
}
There are two options:
1) Add the static keyword to your shared methods
OR
2) You can try reflection.
For reference follow the link:
What is reflection and why is it useful?
I have a class and inside the class I try to call the findViewById method but I am getting an error saying findViewById is undefined.
I think I have to pass the WebView to MyClass or something but I have no idea on how to do that.
Here is the code i am working on:
public class MyClass {
Context mContext;
MyClass(Context c) {
mContext = c;
}
...
WebView webview = (WebView) mContext.findViewById(R.id.myWebview);
webview.loadUrl("mysiteUrl");
...
}
public class MainActivity extends Activity {
final myClass mc = new myClass(this);
...
}
I know that this is happening because MyClass does not extend an Activity. However I can get the WebView without extending Activity...
Please help! Thanks.
findViewById is a method that is defined by Android. Therefore you need to actually extend an Android Activity like so for it to recognise it as a method:
public class MyClass extends Activity {
and then import Activity when prompted.
If you want to utilise findViewById in a non-Android Activity then you can specify an Activity for it to find the view in like so:
WebView webview = (WebView) OtherActivity.findViewById(R.id.myWebview);
i have a class called LauncherActivity that extends FragmentActivity as shown:
public class LauncherActivity extends FragmentActivity {
public Context mContext;
private Common mApp;
public Activity mActivity;
mContext = this;
mActivity = this;
mApp = (Common) mContext.getApplicationContext();
i have read about polymorphism and gotten to know the main use is to use parent references to access child objects.Since LaucherActivity is the child class and FragmentActivity is the parent class,and FragmentActivity is a child class of the Context class in reference to the android documentation, using this as shown below is allowed ? is it a form of polymorphism?:
mContext = this;
mActivity = this;
am presume this refers to the current object which is LauncherActivity ?
Usually , this keyword is uses to say that , refers to the context of the current activity.
I have a class that is currently extending Activity and I have methods like findViewById, ArrayAdapter etc.
I want to turn it into an independent class but all the above methods become undefined. What is the problem? Shouldn't importing the classes be enough? For eg, I import android.view.View for findViewById but it still makes no difference.
Please advise.
you should pass the instance of your Activity to your Second Class on the constructor like this :
In your Activity Instanciate your Class like this :
MyClass instance = new MyClass(this);
And in your second Class , the constructor will be like this :
public class MyClass {
public Activity activity;
//.... other attributes
public MyClass( Activity _activity){
this.activity = _activity;
//other initializations...
}
}
and then when you want to use the findViewById() method , you can do like this :
EditText txt = (EditText)this.activity.findViewById(R.id.txt);
if you want to call any function that belongs to Activity then only thing you need to have is context of the Activity.
eg.
class A extends Activity {
Context ctx;
void onCreate(Bundle b)
ctx = this;
B bob = new B(ctx);
}
}
Here is class B.
class B {
B (Activity a) {
a.anyFunctionOfActivity();
}
}
findViewById is non-static public method of the Activity Class ,so it only be available for a Activity object. Therefore, import android.view.View and android.app.Activity won't make it available. To make it available, you could pass around a Actvity object - usually a this point in your activity. However, notice that you should always update your View in the UI thread.
Please try the following:
public static View getLayoutByRes(#LayoutRes int layoutRes,#Nullable ViewGroup viewGroup)
{
final LayoutInflater factory = getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return factory.inflate(layoutRes, viewGroup);
}
TextView tv = (TextView) getLayoutByRes(R.layout.xxx ,Null).findViewById(R.id.editd);