I have an activity, this activity name is Game, it's xml file is composed by two views control, first of them it's a TextView called Texto and the second one is a SurfaceView created by me called Juego.
The Juego view has an onTouchListener event, and i want to send a text to the control called Texto everytime the user clicks on the control Juego.
I have all the "structure" created but i can't "communicate" from Juego to Texto, every thing i try i get an error.
Thx for help in advance,
Create a listener such as
onViewClick(String text). Create instance of this on your SurfaceView and in constructor get this listener as parameter. Now call this listener wherever you want to update the textView.
In you activity class, implement onViewClick listener and in method do:
public void onViewClick(String text){
TextTo.setText(text);
}
Related
I have a class called LabelButton with an onClickListener. In my Activity class, when the user clicks a button it creates a LabelButton and a String. Then adds the LabelButton to the Layout.
Inside the onClickListener attached to the LabelButton, it calls setVisibility(View.GONE), which takes care of the LabelButton, but I also need to delete that String that was also created earlier.
Is this possible?
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.
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.
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.
public class ActivityForShow extends Activity
{
//have a ListView layout
ListView.setAdapter(SingleRowAdapter);
}
public class SingleRowAdapter extends ArrayAdapter
{
//every row has a CheckBox ,a TextView and a Button
}
Now I want to handle CheckBox and Button events,do some background work and show result in a dialog.
But how to getRefences of Checkbox and Button in ActivityForShow?
findViewById() will cause Exception.
I don't think you want to do that in the ActivityForShow class. Instead, register all event handlers inside of your SingleRowAdapter#getItem(int) method (you will have have to override this). In that method you create the view for the given row, so you know what the row is (position) and you can register event handlers for the CheckBox, Button, etc
It sounds like you want to handle the onCheckedChanged event or onClick event in ActivityForShow.
If so, have ActivityForShow create a OnCheckedChangeListener instance and OnClickListener instance (e.g. as local anonymous inner classes) and have ActivityForShow give them to SingleRowAdapter (e.g. as constructor params) to attach to the checkbox and button widgets as their callbacks.
Alternatively, define your own custom handler interface(s) to be called when the button is clicked or the checkbox is checked. Have ActivityForShow to instantiate an instance of the custom interface(s) with code to process the event. Have ActivityForShow give them to SingleRowAdapter. Finally, have SingleRowAdapter create event handlers to attach to the checkboxes and buttons - these event handlers simply call your own custom handlers.
Hope the following link may help you, event on an item of ListView custom row layout.
It's obvious it gives error with finviewbyid method because it is used for when reference by xml ele. You can use this link for study:
http://developer.android.com/reference/android/widget/ArrayAdapter.html