Take action by clicking a button on another tab

0

I have two tabbed activity , I need a program to execute a code excerpt whenever a button is clicked on another tab , but this excerpt can not be static! How do I do it? Here is the code that has to be executed when the button is clicked on the other tab :

    caldroidFragment = new CaldroidFragment();

    cyan = new ColorDrawable(Caldroid_fragment.this.getResources().getColor(R.color.Cyan));
    white = new ColorDrawable(Caldroid_fragment.this.getResources().getColor(R.color.white));

    addToCalendar();

    Bundle args = new Bundle();
    args.putInt(CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY);
    caldroidFragment.setArguments(args);
    java.util.Calendar cal = Calendar.getInstance();

    args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
    args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));

    caldroidFragment.setArguments(args);
    caldroidFragment.setCaldroidListener(listener);

    FragmentTransaction t = getChildFragmentManager().beginTransaction();

    t.replace(R.id.caldroidCal, caldroidFragment);
    t.commit(); 

Parent Activity:

     public class Testes extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

public static int contador3 = 1;
private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
private Caldroid_fragment cf;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testes);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    cf = new Caldroid_fragment();
    if(myDB.getAllIds().size()!=0) {

        testes.clear();
        contador2 = 1;
        RestartArray();
    }



    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);




}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_testes, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()){
       case go_to_fazer_media:{
           if (item.isChecked())
               item.setChecked(false);
            else{
           Intent i = new Intent(Testes.this,Formas.class);
           startActivity(i);
               return true;
            }
       }
       case definicoes:{
           if (item.isChecked())
               item.setChecked(false);
           else{
              return true;
           }
       }
   }


    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }



    @Override
    public Fragment getItem(int position) {
        switch(position){

            case 0:
                Tab1tests tab1 =  new Tab1tests();
                return tab1;
            case 1:
                Tab2calendar tab2 =  new Tab2calendar();
                return tab2;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Adicionar Testes";
            case 1:
                return "Mapa de Testes";
        }
        return null;
    }
}

Body of tabs 1 and 2, now this method is executable in tab 2, at a click of a button on tab1:

    public class Tab1tests extends Fragment {


private ViewPager mViewPager;
private TimePickerDialog.OnTimeSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_button;
private TextView date_text;
private TextView hora_text;
private EditText sala_text;
private   Button add_test;
private String dt;
private Caldroid_fragment myCF;
public static String hora2;
java.util.Date dataInterpretada;
SimpleDateFormat sdf;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.tab1tests, container, false);

    add_test = (Button) rootView.findViewById(R.id.add_test);
    date_button = (Button) rootView.findViewById(R.id.date_button);
    hora_text = (TextView) rootView.findViewById(R.id.hora_text);
    hora_button = (Button) rootView.findViewById(R.id.hora_button);
    date_text = (TextView) rootView.findViewById(R.id.date_text);
    sala_text = (EditText) rootView.findViewById(R.id.sala_text);
    myCF = new Caldroid_fragment();


            date_button.setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            Calendar cal = Calendar.getInstance();
                            int year = cal.get(Calendar.YEAR);
                            int month = cal.get(Calendar.MONTH);
                            int day = cal.get(Calendar.DAY_OF_MONTH);
                            DatePickerDialog date_dialog = new DatePickerDialog(
                                    getActivity(),
                                    android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                                    dateSetListener,
                                    year, month, day);
                            date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            date_dialog.show();


                        }

                        ;
                    }

            );

    dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            date_button.setVisibility(View.GONE);
            date_text = (TextView) rootView.findViewById(R.id.date_text);
            date_text.setVisibility(View.VISIBLE);
            dt = day + "-" + month + "-" + year;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            try {
                dataInterpretada = sdf.parse(dt);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            date_text.setText(sdf.format(dataInterpretada));
            ;

        }
    };




    hora_button = (Button) rootView.findViewById(R.id.hora_button);

    hora_button.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {

                    Calendar cal = Calendar.getInstance();
                    int hora = cal.get(Calendar.HOUR);
                    int minutos = cal.get(Calendar.MINUTE);
                    TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(), TimePickerDialog.THEME_HOLO_LIGHT, hourSetListener, hora, minutos, true);
                    mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    mTimePicker.show();
                }
            }
    );

    hourSetListener = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker timePicker, int hora, int minutos) {
            hora_button.setVisibility(View.INVISIBLE);
            hora_text.setVisibility(View.VISIBLE);
            hora2 = (hora + ":" + minutos);
            hora_text.setText(hora2);
        }
    };

    hora_text.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Calendar cal = Calendar.getInstance();
            int hora = cal.get(Calendar.HOUR);
            int minutos = cal.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(),TimePickerDialog.THEME_HOLO_LIGHT,hourSetListener,hora,minutos,true);
            mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mTimePicker.show();
        }

    });

    date_text.setOnClickListener(new View.OnClickListener(){


        @Override
        public void onClick(View v){
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog date_dialog = new DatePickerDialog(
                    getActivity(),
                    android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                    dateSetListener,
                    year, month, day);
            date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            date_dialog.show();
        }
    });

    addData();
    return rootView;
}



public void addData(){
    add_test.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            if (dt == null){
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                    dataInterpretada = sdf.parse("00-00-0000");
                    Toast.makeText(getContext(),"Preencha todos os espaços",Toast.LENGTH_LONG).show();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
                else {
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                myDB.insertData("Portugues", sala_text.getText().toString(), sdf.format(dataInterpretada), hora2);
                Toast.makeText(getContext(), "Teste inserido ao calendario", Toast.LENGTH_LONG).show();
                date_button.setVisibility(View.VISIBLE);
                hora_button.setVisibility(View.VISIBLE);
                date_text.setVisibility(View.INVISIBLE);
                sala_text.setText("");
            }

            myCF = new Caldroid_fragment();


            contador3++;
            addToCalendar();
            if(myDB.getAllIds().size()!=0){

            testes.clear();
            contador2=1;
            RestartArray();}
        }

    });
    }

}

      public class Tab2calendar  extends Fragment{

private Button showAllButton;
private DataBaseHelper myDB;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab2calendar, container, false);

    showAllButton = (Button) rootView.findViewById(R.id.showAllButton);
    myDB = CustomApplication.getDatabaseHelper();


    showAllButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(myDB.getHowMany()==0){
                        Toast.makeText(getActivity(),"Ainda não indicou nenhum teste",Toast.LENGTH_LONG).show();
                    }else{
                        if(myDB.getAllIds().size()!=0){
                        testes.clear();
                        contador2=1;
                        RestartArray();}
                   Intent i = new Intent(getActivity(), TestList.class);
                    startActivity(i);}








                }
            }
    );



    return rootView;
}

The method is intended to be executable by clicking the add_test button.

    
asked by anonymous 20.07.2017 / 12:57

0 answers