Change TextView in other activity - android

I would like to change the TextView in an Fragment from my MainActivity. As the MainActivity contains a NavigationDrawer I have to change the TextView without such things like startActivityForResult.
I've also tried the LayoutInflater method, but it changes the whole layout, instead of just a small TextView.
Edit:
MainActivity:
new FRAGMENT_Main().setBalance(credits);
Fragment_Main:
public void setBalance(String credits)
{
AmountCredits.setText(credits);
}
Thank you very much!

If i get your question
If the MainActivity has the Instance of that fragment.
Just make a function in Fragment to update the textview.
and call it from MainActivity using the fragment's instance.

Related

How to change visibility of static Textview variable from another fragment?

I have two fragments A and B. In my 'A' fragment, there is Textview variable and which is public static. I want to access fragment 'A' Textview variable and change its visibility in fragment 'B'. Can anyone help me to solve this problem? Or anyone could tell me how to pass Textview from Fragment 'A' to Fragment 'B'.
make textview object as public static like below ..
first fragment like make
public static TextView textView;
onCreate() method..
textView.setVisibility(View.VISIBLE);
then second fragment in oncreate method ..
FragmentName. textView.setVisibility(View.GONE);
You should access from fragment A to parent activity and tell the activity to call the one of the fragment B methods like setTextViewAText(String str)
for more information about communicating between fragment and activity see this link :
https://developer.android.com/training/basics/fragments/communicating.html

Android Fragment TextView setText from main activity

I got a MainActivity and it has 3 fragments. One of those fragments got one TextView. How Can I manipulate that TextView from MainActivity?
On my MainActivity I have a TabLayout with that 3 fragments. I got data from database in MainActivity and I would like to change the textview that is on fragment 1 with that data I got.
Thank you.
I am suggest you to create some method in one of your Fragment
example :
In Fragment One create/ add this method :
public void Test(String jajal_disek) {
Toast.makeText(getActivity().getApplicationContext(), jajal_disek,Toast.LENGTH_SHORT).show();
ABC.setText(jajal_disek); //example your textview name ABC
}
and then in MainActivity call that's method inside FragmentOne like this :
FragmentOne fragment = (FragmentOne) getSupportFragmentManager().findFragmentById(R.id.container_body);
fragment.Test("StackOverFlow");
Hope this will help you, be great if you write source code to identified problems

How to call mypageradapter.notifyDataSetChanged() from another activity?

My app has the next structure, I have the Mainactivity where I have my viewPager. Inside this viewpager I add new fragment Activities. These Fragment Activities have a TextView with their names, I have created a OnlongClickListener in the textview so that when you OnlongClick in the textview the fragment Activity gets removed from the list in the viewpager I have in the main Activity.
I've tried to use in the fragment Activity the next code :
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
but the result is an empty space in the position where the deleted fragment was.
I think the solution would be calling mypageradapter.notifyDataSetChanged() in the fragment Activity but the thing is that the viewPager and the pagerAdapter are created in the mainActivity and I want to notify the changes in the fragment activity.
How can I call mypageradapter.notifyDataSetChanged() from the fragment activity?
In this moment I have a solution for this problem but I don't think this solution is the best one, when I delete the fragment I create an intent for the mainActivity and then I StartActivity(intent). In the mainActivity onStart method I call mypageradapter.notifyDataSetChanged() and then the empty spaces dissapear and the fragment list is sorted correctly. This solution is really slow and rough.
I would appreciate a better solution for my problem.
P.D: For help I have MyPagerAdapter class inside the mainActivity.

Calling activity method from extended view class in Android

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.

Changing text from another activity

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.

Categories

Resources