This is a problem I didn't forsee when designing the structure of my applicaton.
Basically i have 3 classes.
ProfileActivity //The main activity for the application.
ProfileView //Which extends Linear Layout so that I can tinker with my own view. Note that this is just part of the whole activity and not the main layout
ProfileData //This is just a class for storing data.
The activity contains multiple ProfileViews which each contain one profileData.
Heres where I'm stuck. My profile View has an on click method assigned which needs to call a populate method in the activity. Unfortunately ````
//I'm inside the activity
public void populateProfileDataForm(ProfileData pd)
{
//edit some text fields and other widgets from here
}
Is there any way to call the activity method from the profileView class?
If not, then the error is in my design and can anyone point me towards a better solution for binding data, view and activitys?
When you create a view, you always need a context, and always pass a activity to it.
So try use below code:
If(m_context instanceof ProfileActivity)
{
ProfileActivity activity = (ProfileActivity)m_context;
// Then call the method in the activity.
}
Or write an interface onProfileViewClickListener such as view.onclickListener. then the ProfileActivity implements it and set it to the ProfileView.
What about if you assign an OnClickListener or OnTouchListener in your ProfileActivity Class and in this Listener you cann call the populate Method. In the OnTouchListener you can get the Position of the TouchEvent so you can assign it to a specific Profile.
Related
I have an activity like this:
There are 2 buttons A and B on toolbar and a frame for fragment to take over. Say I have a string variable named button_type in fragment.
I want to have a system so that when I click button A in activity, the button_type in fragment sets to A and when I click button B in activity, the button_type in fragment sets to B.
How to do this?
Please note that I may click the buttons (A,B) when the fragment is already active (its not like after I click one button, the fragment comes.)
Thanks in advance.
Edit:
Currently I am trying this:
In MainActivity I get similar string button_type and set it as A or B according as button click and use the method:
public String getData(){return button_type;}
And in fragment I use: button_type= activity.getData(); in onViewCreated.
But it only seems to have the initial set value of A and B (which is A) and does not change when another button is clicked.
I think the best way if you use an interface for managing text in the fragment. the fragment will implement the interface. When the button click call the function in the fragment which is implemented by fragment.
Another way you can create an object of the fragment using findFragmentByTag() or findFragmentById() and then call the function in the fragment which handles the text.
Create an interface with methods onClickA(String buttonType) and onClickB(String buttonType).
Then create an object that implements this interface (or make fragment implement this interface by itself). I'll call this object listener.
call setButtonType(String buttonType) in listener methods implementation.
Then pass your listener to activity (you can get an instance of parent activity in fragment with getActivity() and cast it to your activity class) and in onClickListener of button A (in activity) call listener.onClickA(yourString) and do the same thing for button B.
I am more familiar with iOS development than Android and I am wondering if all code should be written in an Activity rather than having a "model" class.
I have a couple screens each with a few checkboxes and I want them all to behave the same on click, I am trying to figure out how I would do this without writing repeating code in each activity. Thanks!
No you should not. If you are familiar with java, think of an activity as a extension of main with OO added.
In your particular example you can create a class with a method like:
<MethodName>(View <checkboxClickedName>){ //your code here }. and then add this to the checkbox in the XML android:onClick="<MethodName>", you may need the full package path (e.g. com.example.app.)
Note: if some of the commands/objects you need are only available within an activity you should create this in an calss that extends Activity or preferably within the running activity.
You could have a base class that extends activity that contains the methods that you want executed on click (either implemented or abstract). Use this new base class instead of activity when making new activities. In the layout xml, you can set the onclick of each checkbox to be the method in the base activity you want executed.
The best practice would be to use a single activity and switch fragments as if they were your screens. Then, the activity could simply implement the listener interface that the fragments would re-use.
Since you have multiple activities this becomes a little bit harder. To really re-use a single listener, I can think of a single (not so beautiful) option. Create a static listener and lazy load it:
public class MainActivity extends Activity {
private static View.OnClickListener sCheckboxClickListener;
public static View.OnClickListener getCheckboxClickListener() {
if (sCheckboxClickListener == null) {
sCheckboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Work with v
}
};
}
return sCheckboxClickListener;
}
}
And in each of your activities call:
findViewById(R.id.checkbox1)
.setOnClickListener(MainActivity.getCheckboxClickListener());
I have a class called MainGamePanel that extends SurfaceView where I run a thread that handles my updating and drawing code. Then I have an activity which runs editor.xml file which contains a Relative layout which runs my MainGamePanel and a FrameLayout which I am using as a container to hold a fragment. So I have the MainGamePanel taking up 2/3 of the screen and the fragment is on the right taking the remaining 1/3 of the screen.
The fragment contains a button that I want to use to reset objects that are located within my MainGamePanel. How can I access a member of MainGamePanel from my fragment or vice versa?
Here is the code for my fragment where I check for the button click
Button button = (Button) view.findViewById(R.id.resetGrid_button);
// A simple OnClickListener for our button. You can see here how a Fragment can encapsulate
// logic and views to build out re-usable Activity components.
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Activity activity = getActivity();
if (activity != null)
Toast.makeText(activity, R.string.reset_button, Toast.LENGTH_LONG).show();
}
});
Instead of just doing the toast I would like to have it reset some objects located in my MainGamePanel surfaceView class. Thank you for your time and any information you can send my way.
Answered:
I figured out that I have to get a handle to my view inside of my activity and have the activity access the information. Then when the user clicks the button in my fragment, I let the activity know and update the information.
you need to create an interface to communicate between the Activity and Fragment,
follow this example -
http://developer.android.com/training/basics/fragments/communicating.html
once your Activity knows about the pressed button you should call a public method within your custom class that will handle the desired logic.
I have this class: public class HeaderView extends RelativeLayout implements OnClickListener, ProgressBarListener
In every Activity, this HeaderView is shown as I put it in the XML layout.
Now, the thing is that I want to be able to implement onResume in my HeaderView. When I launch an Activity, onCreate is called and the HeaderView draw the things I need at the top, but if I press back, my Activity will call onResume and my HeaderView is not going to be recreated and the old values will still be there. How can I implement onResume in my HeaderView or something similiar in order to recreate the HeaderView!?
Thanks in advance!
you cant call onResume in the HeaderView class as its is the callback method of the Activity class and the classes extending the Activity class.So in the onResume of your Activity class, call a method of the HeaderView class something like refreshView(), and then your view can be refreshed.Editted section: You can make a BaseScreen class extending Activity and implement this thing in its onResume.Now extend this class in all your activities instead of the Activity class.This will make to run refreshView() every time onResume is called an you no longer need to define it in every activity class of your code.
How to dynamically change the content of a TextView from another part of the screen?
I have a TabActivity class that draws a RelativeLayout that contains a TextView followed by a with several tabs. Within each tab is a separate Intent. From one of the tab intents, I would like to change the text (via .setText) of the TextView from the parent TabActvity.
Is this possible?
You should use Android Architecture Components :
You can create a ViewModel containing LiveData of your data object (LiveData<String> in case you want to change only text).
When you will change your live data object from one Activity or Fragment all other Activities and Fragments observing for this live data object will get notified.
Official API doc has complete example with description.
Make a public method in your TabActivity that sets the TextView's text, then call getParent() from the child activity, cast it to your TabActivity class, then call that public method.
You could try implementing a handler for the parent Tab which does the job. Pass the text in a message object from each of your respective tabs. To be safe, make changes within the handler inside a runOnUI block
In a case of changing text from asynctask file, you need to implement an interface with a listener. Example:
AsynctaskFile:
OnReadyListener onReadyListener;
public class ABCAsynctaskFile{
...
onReadyListener.onReady();
}
public interface OnReadyListener{
void onReady();
}
public void setOnReadyListener(OnReadyListener onReadyListener){
this.onReadyListener = onReadyListener;
}
ActivityFile:
public class ABC extends AppCompactActivity implements ABCAsynctaskFile.OnReadyListener{
..
ABCAsynctaskFile aBCAsynctaskFileObj = new ABCAsynctaskFile(context);
aBCAsynctaskFile.setOnReadyListener(ABC.this)
}
#Override
public void onReady(){
// Your wished changed in edit text.
}
This structure will help you to prevent null pointer exception.