i have the class MainActivity
public class MainActivity extends Activity {
int mVariable= ((Constant)getApplicationContext()).variable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button count= (Button)findViewById(R.id.button1);
final TextView tx = (TextView)findViewById(R.id.text);
count.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
mVariable ++;
tx.setText(" "+mVariable);
....
and the Constant class
public class Constant extends Application {
public int variable=0;
}
and this doesn't work ..i declare Constant in Manifest.
<application
...
android:name=".Constant"/>
I want to set value by onClick like this code and change the value of a global variable.. thanks
You can make this variable as Static and access it in every other classes in your project like Constant.variable :
public class Constant extends Application {
public static int variable = 0;
}
And
public class MainActivity extends Activity {
......
tx.setText(" " + Constant.variable);
.......
}
And now the variable is available throughout the whole project by Constant.variable and when you change the variable's value, it will be applied to all classes.
I don't now what actually happened inyour project but above is just a suggestion to your problem.
Related
I have two Java files
The first one extends AppCompactActivity
And the second one extends SQLiteDataBaseHelper
So this is the question:
Can I access an EditText variable from first one to second one?
of course, you can and Edit Text at the end is a place to store data so you can create an instant of that class you want to access and then you can use setters to set data or getters to get it, check this:
public class MainActivity extends AppCompatActivity {
private EditText editText ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public EditText getEditText(){
return editText ;
}
public void setEditText(EditText editText){
this.editText = editText;
}
here is the database class :
public class access extends SQLiteOpenHelper {
private MainActivity mainActivity= new MainActivity() ;
public void setData(EditText editText){
mainActivity.setEditText(editText);
}
So I have this "middle man" nonactivity class, where I want to get a string path from an activity class. Then, from my nonactivity class send that string path to a different activity?
Activity A
#Override
public void onCreate(Bundle savedInstanceState)
{
Intent imageToFactory = new Intent(this,NonActivity.class);
imageToFactory.putExtra("yourImage", user_image_path);//I already set user_image path
}
NonActivity
public class NonActivity
{
private Intent grabImagePath = new Intent();
private String grabImagePathString = getIntent.getStringExtra("yourImage");//this obviously gives an error but for the example
public String grabUserImage()
{
return grabImagePathString;
}
}
Activity B
#Override
public void onCreate(Bundle savedInstanceState)
{
NonActivity nonActivity;
String example = nonActivity.grabUserImage();
}
So this method doesn't work for some reason, I think I have to use contexts some how but im not sure exactly how to use them, if anyone can help with examples or modify the example code i did below that'd be awesome!
You can build a static variable that can serve as message bridge, first thing you need to create a class and name it anything you like, in my case I will name the example class as Conn, then add a static HashMap.
public class Conn {
public static HashMap<String,String> storage = new HashMap<>();
}
To utilize this this class in your example:
Activity A
#Override
public void onCreate(Bundle savedInstanceState){
Conn.storage.put("yourImage",user_image_path_in_string);
}
Activity B
#Override
public void onCreate(Bundle savedInstanceState)
{
String example = Conn.storage.get("yourImage");
}
if you want to use third class ( here NonActivity.class ) for some reasons, just do it like this:
create globals class like this :
package helper;
public class Globals {
private static final Globals instance = new Globals();
private GlobalVariables globalVariables = new GlobalVariables();
private Globals() {
}
public static Globals getInstance() {
return instance;
}
public GlobalVariables getValue() {
return globalVariables;
}
}
and global variables class like this :
public class GlobalVariables {
public String grabImagePathString;
}
now in activity A::
Globals.getInstance().getValue(). grabImagePathString = "something......";
and in activity B::
String gtabImagePathString = Globals.getInstance().getValue(). grabImagePathString;
Good Luck
is it possible to get all activities in the application? i have a global integer variable that should be in the ActionBar of every activity. i thought something like this:
for (Layout/Activity l in (all activites)) {
l.setTitle(variable);
}
i already tried it with R.layout but this didnt work for me.
How can i do this or is there a better way to display my variable in all activity labels? later i want to call this code from my set method for the global variable.
There is only one activity running at a time, so you can’t get this kind of references.
Said that, I think the way to go it’s create an int static variable in some class, and called it from your activities.
//SomeClass
public static int xValue = 0;
//ActivityOne || ActivityTwo || ActivityThree ...
String text = String.valueOf(SomeClass.xValue);
SomeClass.xValue = 1;
Because it’s a public static variable, you don’t need to instantiate any object to get/set its value, and it will be accesible from any class. Furthermore, this value will be reachable as long as its class is in the memory, and destroy just when class gets unloaded.
yes it's possible with singleton.
This is how to use singleton:
This is Singleton class:
public class Singleton {
private static Singleton mInstance = null;
private String mTitle;
public void setmTitle(String mtitle){
this.mTitle=mtitle
}
public String getmTitle(){
return mTitle;
}
public static FilterArrayList getInstance(){
if(mInstance == null)
{
mInstance = new FilterArrayList();
}
return mInstance;
}
}
This is the first activity:
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Singleton.mInstance.setmTitle("This is Singleton");
}
}
and in second activity:
public class SecondActivity extends Activity {
String Title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Title=Singleton.mInstance.getmTitle();
}
}
I am working in android. I have two acitivities in my project. I have declared a public static variable in one activity like this:-
public static String name="KUNTAL";
In my second activity i am trying to use this variable, then it is generating error that this name variable is not exist.
Is this possible to use a variable anywhere in my project if it is declared as public ?
Please suggest me what mistake i have done.?
Thank you in advance...
public class Activity1 extends Activity {
public static String name="KUNTAL"; //declare static variable.
#Override
public void onCreate(Bundle savedInstanceState) {
}
}
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Activity1.name; //way to access static variable using dot operator.
}
}
I think you must access them in a 'static way', i.e.:
String myVar= name; // wrong
String myVar= TheClassThatContainsName.name; // right
You can use the variable specified as public static in any Activity but you need to access that variable by using the Activity name where you Declared it.
For Accessing in Second Activity just use ;
Activity1.name ="Me";
means that name variable belongs to Activity1 and you are using in Acvity2
Declaring variables public static is not the recommended way of passing values between two activities. Use Intent object instead:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
yourButton.setOnClicklistener(listener);
}
}
//On click of some button start Activity2:
View.onClicklistener listener = new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent mIntent = new Intent(Activity1.this,Activity2.class);
mIntent.putExtra("yourKey","yourValue");
startActivity(mIntent);
}
};
//and then in your activity2 get the value:
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
String yourValue = getIntent().getExtras().getString("yourKey");
}
}
if you're using the same variable in more than one activity then make a class something like ActivityConsts{}, and declare and initialize this variable in there(in ActivityConsts). And access this variable from anywhere with the class name.
ex-
declare a class-
public class ActivityConsts {
//your String var
public static String name="KUNTAL";
}
now in your activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
String yourStringVar = ActivityConsts.name;
}
}
There is no datatype specified for your variable. Use
public static String name="KUNTAL";
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
Activity)? Should I pass some context to my Points class? How do I do, exactly?
see you post, i've edited it , to fix the problem
hope it helps :=)
here is the Edit :
file MyActivity.java:
public class MyActivity extends Activity {
TextView myView ;
protected void onCreate(android.os.Bundle savedInstanceState) {
myView = (TextView)findViewById(R.id.myView);
Points myPoints = new Points(this);
myPoints.displayMsg("Hello World !!!");
}
}
--------------------------------------------------------------
file Points.java:
private class Points {
protected MyActivity context;
//add a constructor with the Context of your activity
public Points(MyActivity _context){
context = _context;
}
public void displayMsg( final String msg){
context.runOnUiThread(new Runnable() {
#Override
public void run() {
context.myView.setText(msg);
}
});
}
}
Your Points can't be a private class without being an inner class. So your code doesn't even compile...
Pass the view as parameter to the constructor of your Points class:
public class MyActivity extends Activity {
TextView myView = (TextView)findViewById(R.id.myView);
Points myPoints new Points(myView);
private class Points {
public Points(TextView view) {
// todo
}
}
}
You should do everything and pass back the value to the activity to handle UI instead of doing any UI related stuff in the point stuff.
You can pass the main Activity's context (using Points(getApplicationContext());) to the class as a constructor parameter. You could also pass the specific UI elements you want to manipulate.
A better way to do it, however, may be to have Points not know about the Activity. Have your Activity call Points methods and take the necessary actions based on the method output.
You could just pass the view to your class.
Points myPoints = new Points(myView);
private class Points
{
private TextView mTextView;
Points(TextView textView)
{
this.mTextView = textView;
}
}
i was in same trouble..
i found the simple way..
make a static variable and function ...
call from other class..
TestActivity.java
public class TestActivity extends Activity {
static EditText edit_text1;
public void onCreate(Bundle savedInstanceState)
{
.....
edit_text1 = (EditText) findViewById(R.id.edit_text1);
.....
}
public static void setMSG(String str)
{
edit_text1.setText(str);
}
}
Test2.java
TestActivity.setMSG("this is text");
Could work using an interface
file MyActivity.java:
public class MyActivity extends Activity implements Points.MyListener {
TextView myView;
... onCreate(...){
myView = (TextView)findViewById(R.id.myView);
Points myPoints = new Points();
//pass in MyActivity's instance of the listener
myPoints.addListener(this);
}
#Override
public void updateTextView(String message){
myView.setMessage(message);
}
}
file Points.java:
public class Points {
public Points(){
}
public interface MyListener{
void updateTextView(String message);
}
MyListener myListener;
public void addListener(MyListener listener){
myListener = listener;
}
public void updatePoints(){
//do some operations in calculatePoints()
String points = calculatePoints();
//update views using MyActivity's implementation of updateTextView()
myListener.updateTextView(points);
}
}
Doing it this way, events can be fired / messages sent, for lack of better terms, from the external class to update the Activity UI. This might be overkill if all sb need is to call a method in the Points class that returns something