logo
down
shadow

Add new textview in fragment after click on button in another fragment


Add new textview in fragment after click on button in another fragment

By : Clarke West
Date : November 18 2020, 03:01 PM
seems to work fine I managed to make it work with TinyDB, which can store string arraylist as SharedPreferences.
This is what I have in fragment with button
code :
TinyDB tinydb=new TinyDB(getActivity().getApplicationContext());

    incometemp = edttxt.getText().toString();
    mylist= tinydb.getListString("income");
    mylist.add(incometemp );
    tinydb.putListString("income", mylist);
TinyDB tinydb;
    ArrayList<String> mylist = new ArrayList<>();

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        tinydb = new TinyDB(getActivity().getApplicationContext());
        mylist = tinydb.getListString("income");
        LinearLayout m_ll = (LinearLayout) view.findViewById(R.id.home);

        for (int i=0;i<mylist.size();i++) {
            m_ll.addView(createNewTextView(mylist.get(i)));
        }


        return m_ll;
    }

    private TextView createNewTextView(String text) {
        final LinearLayoutCompat.LayoutParams lparams = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(getActivity());
        textView.setLayoutParams(lparams);
        textView.setText("New text: " + text);
        return textView;
    }
}


Share : facebook icon twitter icon
update TextView in fragment A when clicking button in fragment B

update TextView in fragment A when clicking button in fragment B


By : Brent Behling
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can define an interface in Fragment B and implement it on the MainActivity. Then on the callback method (onClickOnB in this case) set the text on the TextView. You should obtain a reference of the TextView in the Activity's onCreate() after setContentView(). This works because Fragment A is static. Otherwise, you can create a public method inside Fragment A so you can set the text from inside the callback by getting a reference of Fragment A and calling such method.
Fragment B
code :
public class FragmentB extends Fragment implements onClickListener{
ClickOnB listener;
public void setOnFragmentBClickListener(ClickOnB listener){
this.listener = listener;

}

@Override

public void onClick(View v){
//stringMessage is a `String` you will pass to `Fragment` A to update its `TextView`
listener.onClickOnB(stringMessage);
}

interface ClickOnB{
public void onClickOnB(String message);
}

}
public class MainActivity extends Activity implements ClickOnB{
@Override   
protected onCreate(Bundle savedInstanceState){

//Get a reference of `Fragment` B somewhere in your code after you added it dynamically and set the listener.
((FragmentB)getFragmentManager().findFragmentByTag("FragmentB")).setOnFragmentBClickListener(this);

}

@Override
public void onClickOnB(String message){

//Set the text to the `TextView` here (I am assuming you get a reference of the `TextView` in onCreate() after inflating your layout.

mTextView.setText(message);

}
}
How to change a TextView Fragment by a button located on the ActionBar (In the main fragment)

How to change a TextView Fragment by a button located on the ActionBar (In the main fragment)


By : ntheorist
Date : March 29 2020, 07:55 AM
it fixes the issue You need to keep a reference of the InfoInstalacionFragment4 instance you are using in the pager within the activity , so you can access it when the user pressed on the menu item.
The way you keep the reference depends on where the ViewPager is located. If it is within the activity , make a field in the activity and assign the new InfoInstalacionFragment4 to it.
How can I update the TextView in Fragment when I click in a button?

How can I update the TextView in Fragment when I click in a button?


By : jbyauk
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can do it easily by using interface. Do something like this in your activity
code :
public class YourActivity extends AppCompatActivity{
    FragmentCommunicator fragmentCommunicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                fragmentCommunicator.changeText("Hello");

            }
        });

    }

    public void passVal(FragmentCommunicator fragmentCommunicator) {
        this.fragmentCommunicator = fragmentCommunicator;

    }

}

public interface FragmentCommunicator {
   public void changeText(String textVal);
}
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.activity_fragment1, container, false)
    ((YourActivity) getActivity()).passVal(new FragmentCommunicator() {
        @Override
        public void changeText(String textVal) {  
               TextView frv = (TextView) view.findViewById(R.id.msgStatusPorta);
               frv.setText(textVal);
           }
    });
    return view;
}
Calling fragment after recieved retrofit response (textview and button are null inside fragment)

Calling fragment after recieved retrofit response (textview and button are null inside fragment)


By : Daniel Hanley
Date : March 29 2020, 07:55 AM
Hope that helps I know my problem description does not point to the real problem that I found but still I will post it since maybe it will help someone. I my case it was problem in xml file I just removed android:fillViewport="true" or set it to false and it works.
transfer data from fragment A -> fragment B - > fragment C. When user click Back button, I want to return to fragm

transfer data from fragment A -> fragment B - > fragment C. When user click Back button, I want to return to fragm


By : fabio g
Date : March 29 2020, 07:55 AM
With these it helps I'm a newbie in android developer. I have a question about transfer with 3 fragments. , Here is a sample idea how to achieve communication.
code :
 // activity classs
 public class SampleActivity extends Activity implements    OnFragmentChangeListener {
 OnBackPressListener  dataFragment;
 public void onCreate(bundle){

      android.app.FragmentManager fragmentManager=getFragmentManager();

    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    dataFragment = new DataFragment();
    fragmentTransaction.add(R.id.audio_permission_button,dataFragment);
    fragmentTransaction.commit()
 }
  @override
  public void OnFragmentChange(Bundle bundle){
  //here you go.
  // write code to load new fragment with same idea. now you have bundle do what you want.

}
@Override
public void onBackPressed() {
 // you can call this method from any click event, This just an sample idea.
 dataFragment.OnActivityBackPress();
}

}
 // interface to communicate with fragment
 public interface OnFragmentChangeListener {
     public void OnFragmentChange()
 }
 // fragment class
public class DataFragment extends Fragment implements OnBackPressListener {

 OnFragmentChangeListener onFragmentChangeListener;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 onFragmentChangeListener=(OnFragmentChangeListener) getActivity();


}

    @Override
public void OnActivityBackPress() {
 // pass you data to activity for loading new fragment or to refresh data.
 Bundle bundle= new Bundle();

 onFragmentChangeListener.OnFragmentChange(bundle);



}
}

// interface behave like mediator 
public interface OnBackPressListener {

public void OnActivtiyBackPress();
}
Related Posts Related Posts :
  • Interrupt CompletableFuture with default value
  • Eclipse - Choosing servers in "Run on server" vs setting "Target Runtime"
  • Spring JPA doesn't validate bean on update
  • How is takeWhile different from filter?
  • Spring Scheduled Cron jobs firing at wrong time
  • JFrame window resizing on every compile. Different results every time
  • Kotlin unresolved reference :add after converting from Java code
  • How to use Neo4j APOC method " apoc.export.csv.query(query,file,config)" in Java program
  • Regex to include all spanish characters and number
  • Parsing and Mapping a JSON Object with Dynamic Properties
  • Which approach to building domain objects in Spring MVC is more correct?
  • Java Long Min and Max not exact as mentioned in docs
  • Selenium opens new tab after SendKeys
  • How to sort a large array of ints?
  • FitNessse Test can not find Fixture
  • Including Signed Libraries in Executable JAR with Maven
  • If and Else statement run on second iteration of while loop
  • Change class of disabled tab on change of dropdown value
  • Junit5 @ParameterizedTest How to pass array as one of parameter
  • Text message not being sent to a list of numbers but is sent when just one number is specified
  • Spring mapped servlet not called in jetty-maven-plugin
  • How to pace the consumption of a sqs queue using spring integration
  • Autowiring Not getting disabled using xml configuration
  • Maze solve and shortest path with Java BFS
  • Spinner updating only on user actions
  • Why are Java rules for protected access such as they are?
  • Modify parameter of method by value in annotation aspectJ
  • How to send sensitive data to a Liferay portlet from outside the Liferay portal
  • Does a server connection stay open if created inside a method?
  • JAR manifest Class-Path, use folder instead of jar
  • How to do a bulk update in Firestore
  • ItemArray.checklist.add() not appending string to ArrayList
  • How to move something on screen without other picture staying there
  • SpringBoot : No matching bean found exception
  • Implementing the Clonable interface, but don't have to override the clone() method
  • how can i get this code to choose a random word once from each array to print a sentence?
  • Static class to get connections from connection pool
  • Unable to start Chrome browser with user profile in Selenium
  • How to pass a object to be created as a parameter for a method in Java?
  • How to retry with hystrix
  • Loading key to KeyStore fails on Android Oreo
  • Spring+Velocity unsuccessful attempts to save object
  • Private constructor in Kotlin
  • Jetty:run fails with NoSuchMethodError with Spring 5
  • Cannot upload my Web Applicartion in Jboss7 EAP7 EAP
  • How to load a certificate from "Credential storage"?
  • Call Genexus procedure stub in Java environment
  • JavaFX clipping produces a 'lottery scratch ticket'-Effect
  • Using DateTimeFormatter on january first cause an invalid year value
  • Get all the output from Watson conversation using java
  • Java unable to open main class and jar file
  • How to override @override method from activity into another class
  • Adding Runtime VM parameters to intellij for Java 9
  • Java IBM MQ Client connected but not getting messages from queue
  • Questions of Tomcat SSL configuration
  • Stale JNLP files for <extension> (since Java 9?)
  • Properly set (system) properties in JUnit 5
  • Spring MVC Model within POST Method is empty
  • VSTS buildagent: Java 9
  • java.lang.NumberFormatException: For input string: "id" for Hibernate
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org