How to access activity resource from another activity? - android

I want to change textView(in mainActivity)'s property like textSize, or textColor.
Then I tried to use it at setting activity.
View view = getLayoutInflater().inflate(R.layout.activity_main, null);
readTextView = view.findViewById(R.id.textView);
And It doesn't work.
Also, I tried to How to update a TextView of an activity from another class this answer. But isn't it can only change the text? If I need to change much property, I have to make a method in my activity.

Android access main activity variables from another class
I referenced this answer.
Declare public static the resource which you want to change.
Use like [Your Activity].[The Resource] at your setting Activity.
I really sorry to question like this... Sorry.

You must not using a public variable as a mechanism to update your View inside an activity because of the following:
You can't ensure that the activity is always exist. There is a probability that the activity is killed by system because of error or you're finishing the activity.
You're coupling your activity with another class. So, each time you're changing the activity there is probability that your change propagate to another class. This probably will introduce bugs to multiple classes.
You better strictly access the View from another class by sending only the view as a parameter. For example, if you have the following activity:
public class YourActivity extends AppCompatActivity {
private TextView mTvName;
...
}
to change the properties of mTvName, you need to create something like this:
public class TextChanger {
public static void maximizeTextSize(TextView tv) {
tv.setTextSize(30);
}
}
then you can use it in your Activity:
TextChanger.maximizeTextSize(mTvName);
If you want to update the TextView from another Activity, you can use startActivityForResult for starting the another Activity. Then you need to receive the result to change your TextView by overriding onActivityResult in your activity.
In case you need to update the TextView without any coupling with another class or Activity, you can use Event Bus mechanism. You can use EventBus library.

Related

How create a global function to initiallize all UI widgets in android

I am new to android . My application contains several activities and each activity view contains several widgets. So at onCreate callback I am initiallizing all widgets like (TextView) findViewById(R.id.txt_id) and this is required in almost all activities and it is lengthy too. Since it is more like a common process in all activities , is it possible to create a function globally to perform this widget initiallization , and call this from activities with corresponding layout?
If all this widget have similar name, you can create BaseActivity class and replace all this thinks in onCreate of BaseActivity.
If all this widget different, you can try to use AndroidAnnotation
For example, if you want to inject some view in your activity:
#EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
#ViewById(R.id.txt_id)
TextView text; //Must be default, protected or public
#AfterViews
void initViews() {
//Start point, where you can use injected Views, NOT IN onCreate();
text.setText("Some text");
}}

Android: Using button from a non-acitvity class in activity class

I am wondering if it is possible to access a button from a non-activity class using an activity class? The button is in a different xml file than the one of the activity class.
Also, is it possible to create options menu in the non-activity class?
Whenever you need to access any UI element from a non-activity class, you need to pass the context to your class. A rough method is:
in your activity's onCreate() method:
myInstance.setContext(this);
and in your Class, you have:
Context mContext;
public void setContext(Context c){
mContext = c;
}
I am wondering if it is possible to access a button from a
non-activity class using an activity class?
Yes, it's possible if the non-activity class can access the methods of an activity class. This can be done by passing the reference of an activity to that non-activity class in the onCreate() method. But remember to release that reference and any other reference to the activity Views when the Activity is destroyed, that is when it's onDestroy() method being called.
The button is in a different xml file than the one of the activity
class.
A button that is not added to an Activity's View Tree can be accessed. You can inflate the layout that contains the button and add the inflated View root to your Activity's ViewGroup.
Also, is it possible to create options menu in the non-activity class?
Yes, it's possible if the non-activity class can access the methods of an activity class.
Anyone can correct me if I'm wrong. But my assumption answer to your question and how I understand what you trying to achieve. Android has made it easy to access controls in other xml files. You have the < include> which you can include other non-activity(fragments) in your activity. Actually it's pretty easy to do this that is the first way to do it. I know they is another way still playing around with your xml files. There is resource to bring non-activity controls to activity layouts with having to do much programmatically. Look at resources available to you on your layout builder.

Variable not updating value

This is strange, maybe someone can help with this. I have two classes and when I try to access variable from main class, value is always 0.0
MAIN CLASS
public class MainActivity extends Activity {
public float angleCurrent;
- do something whith angleCurrent
//System.out.println(angleCurrent); - I get right values
}
SECOND CLASS
public class SecondClass extends ImageView {
public float curAngle = new MainActivity().angleCurrent;
//System.out.println(curAngle); - I get 0.0 all the time
}
Code is just illustration.
There are a couple big issues at play here. First, why the behavior is occurring:
You are not accessing the variable from the first MainActivity instance inside of SecondClass, you have created a new second instance of MainActivity, so you are getting access to a different variable than the original.
Now, the second issue surrounds the correct way to do this in Android. You should NEVER directly instantiate Activity instances. It is also not good practice to pass references to an Activity around any more than the framework already does. If your Activity needs to pass some information to your custom ImageView, you should create a method on your ImageView that you can use for the Activity to pass the value forward (i.e. SecondClass.setCurrentAngle(angleCurrent))
If you are looking to a View to access variables in the activity, it's not going to work. You need to add a setMethod to your custom view and set the value from the activity. Chthlu is right about why you aren't getting a value: new creates a different instance than the one you have on screen. You should never call new on an Activity class. You shouldn't even HAVE a constructor for an Activity, you should setup everything in the onCreate method.

how to get id from android xml without using findviewbyId method in java class

I am using 2 java class in my android project, one class to get id from android xml & another class for performing the activity. I dont know how to access the second class to get id from first class.
you can't access.
when your first java class is foreground you can access Views which is showing on the screen
but when another activity is foreground why do you wanna access background activity Views that is not visible.???
But
you can add a method in first activity input the id and return the object
like this:
View getView(int id) {
return getViewById(id);
}
It sounds like you have a helper class for the activity and you want the helper class to have access to the view hierarchy. Just pass the activity instance to the helper class when you create an instance of it. It can then use findViewById to find the view(s) of interest.

Difference between making a button implementation in class or a final inside the onCreate()?

So usually in guides when making a holder for a View let's say Button, I usually see it on the onCreate method like this:
public class className extends Activiy{
public void onCreate(){
final Button button = (Button) findViewById(r.something.something);
}
}
Well I was wondering if there's any difference and if ever there is, which is a better in approach from that one to this one:
public class className extends Activiy{
Button button;
public void onCreate(){
button= (Button) findViewById();
}
}
The first creates a method local variable, the second creates a field in each instance of your Activity class.
It is always better to use the smalles (that is most local) scope possible. On Android the memory usage makes this even more important. Therefore please go with the first solution (assuming that you do not need to reference that button from multiple other places in your code).
If you declare it the first way, it will be available only inside onCreate() method.
If you declare it the second way, it will be available all around the activity.
Use the first way, if you do not need to use the button outside onCreate().
I do not see any other difference.

Categories

Resources