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
Related
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.
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
On an Android Project,
MainActivity with ViewPager and 3 Fragments:
1st page is fragment FindFrag.
Currently search query and display of results as list (in FindFrag) is coded in MainActivity:
accessed EditText in FindFrag from MainActivity, fetched query, formatted result, and displayed results in FindFrag from MainActivity ( the codes are written in MainActivity Class)
Is this the correct method?
1) Should I write it in FindFrag Class ?
2) How can I call methods in FindFrag from MainActivity (hook a method to an event in MainActivity) ?
3) Shall I use OnFragmentInteractionListener?
How ?
4) How to access views in fragments within the fragment class itself ?
My app works successfully. My question is whether this is correct style/method in Fragment - Activity Interactions
U need to code search query and display results in FindFrag , answer of How is you have to put layout code of searchview and listview or recyclerview in FindFrag.xml and copy your javacode from MainActivity.java to Your FindFrag.java
Best way to access rootview in fragments is to use within fragment class.
mainactivity is a separate java class, it should have a separate .xml file with a framelayout to show the fragments.
all other fragments should be in separate classes with separate .xml files.
use this tutorial its easy and clear : https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/
I have MainActivity with some buttons and I have different class with array of strings.
Can I send data (as I click on button) to a fragment so that it can choose from which class it takes the array strings?
If you instantiate your fragment in MainActivity:
MyFragment fragment = new MyFragment();
Then you can call methods from that fragment inside MainActivity like: fragment.myMethod(index_of_array);
If you do your fragments in XML, then you should find it by id.
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.