Meindert Stijfhals Meindert Stijfhals - 3 months ago 18
Android Question

Android: Toast in tabbed application & Next page onclick in tabbed application

I am trying to create a Toast from inside my fragment. I have been searching for hours on the internet and all I come across doesn't seem to work.

information.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getBaseContext(), "This information will not be published in the world wide web, but will be saved on your own device instead", Toast.LENGTH_SHORT);
toast.show();
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});


'getActivity' is red. On hover it says: "cannot resolve method 'getActivity()'".
If I remove the getActivity, and only use getBaseContext() or getApplicationContext(), it says that the non-static method cannot be referenced from a static context. Can anyone teach me how to create this specific Toast?

I used the standard gallery tabbed application from Android Studio.
Here is my code (you will find it at the very bottom):

public class ApplicationFragments extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

//get widgets voor alle functies
private static View rootView;

//variable
private static int section;
//fragment1
static ImageButton information;
static int addOneSex;
static int addOneWeight;
static int addOneAge;
static ImageView plusSex;
static ImageView plusWeight;
static ImageView plusAge;
static TextView sex;
static TextView weight;
static TextView age;

//fragment2
static int addOneHour;
static int addOneBeer;
static int addOneWine;
static int addOneShot;
static ImageView plusBeers;
static ImageView plusWines;
static ImageView plusShots;
static TextView plusHours;
static TextView amountOfHours;
static TextView amountOfBeers;
static TextView amountOfWines;
static TextView amountOfShots;

//fragment3

//all fragments
static Button btnNext;

@Override
protected void onCreate(Bundle savedInstanceState) {
//verander animatie
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.fade_out);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);

// 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);
}

public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";

public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

//bij het creƫren van een tab
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_information, container, false);

//set xml
section = getArguments().getInt(ARG_SECTION_NUMBER);
if (section == 1) {
rootView = inflater.inflate(R.layout.fragment_information, container, false);
GenerateFragment1();
}
if (section == 2) {
rootView = inflater.inflate(R.layout.fragment_drinks, container, false);
GenerateFragment2();
}
if (section == 3) {
rootView = inflater.inflate(R.layout.fragment_calculate, container, false);
GenerateFragment3();
}
return rootView;
}
}

//geef de juiste pagina terug
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
//hoeveelheid pagina's
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}

//fragment 1
public static void GenerateFragment1(){
information = (ImageButton) rootView.findViewById(R.id.imgBtnInfo);
sex = (TextView) rootView.findViewById(R.id.etSex);
weight = (TextView) rootView.findViewById(R.id.etWeight);
age = (TextView) rootView.findViewById(R.id.etAge);
plusSex = (ImageView) rootView.findViewById(R.id.btnSex);
plusWeight = (ImageView) rootView.findViewById(R.id.btnWeight);
plusAge = (ImageView) rootView.findViewById(R.id.btnAge);
btnNext = (Button) rootView.findViewById(R.id.btnNext);

plusSex.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOneSex = Integer.parseInt(sex.getText().toString());
addOneSex++;
sex.setText(Integer.toString(addOneSex));
}
});

plusWeight.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOneWeight = Integer.parseInt(weight.getText().toString());
addOneWeight++;
weight.setText(Integer.toString(addOneWeight));
}
});

plusAge.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addOneAge = Integer.parseInt(age.getText().toString());
addOneAge++;
age.setText(Integer.toString(addOneAge));
}
});


information.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getBaseContext(), "This information will not be published in the world wide web, but will be saved on your own device instead", Toast.LENGTH_SHORT);
toast.show();
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});

btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//to the next fragment
}
});
}


Also, I am wondering how I can make the app go to the next page on a buttonclick (btnNext).

Thanks for your time and help!

Answer

Do you have a list of fragments stored in the activity somewhere, or a fragment manager? If so, you can just get the next fragment from the list and switch it out with the fragment manager. Regarding the toast getActivity() problem: if I understand correctly, ApplicationFragments is your main activity? If so, why not call ApplicationFragments.this instead of getActivity()?

Comments