java


How can I show this Toast message only if I click the second Tab?


I have a navigation bar with three tabs each tab is a Fragment.
I wrote in the onCreateView method of my second fragment this toast message:
public class SecondFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.second_layout,container,false);
Toast.makeText(getContext(), "Test", Toast.LENGTH_LONG).show();
return rootView;
}
What I want is that only if I click this tab the message displays.
But the problem is that if I start the application the message displays directly, but why?
Here is my MainActivity.java:
public class MainActivity extends AppCompatActivity{
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
}
}
And (I think this is Important for this question) my TabFragment:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
if(position == 0){
return new FirstFragment();
}
if(position == 1){
return new SecondFragment();
}
if(position == 2){
return new ThirdFragment();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "First";
case 1 :
return "Second";
case 2 :
return "Third";
}
return null;
}
}
}
So where do I have to say Toast.makeText(getContext(), "Test", Toast.LENGTH_LONG).show(); to display it only by pressing on the second tab?
You can use the code for terminating the toast once it is shown so that it will not be visible when you click on another fragment.
try this one
toast = new Toast(getContext());
toast.cancel(); //this will prevent toast from displaying multiple times
Hope it will work
Wait! if you have multiple tabs then each tabs must be containing individual fragment and for each fragment there is java class and a layout file for the same then the coding part will be inside onCreate method of java file and xml/layout part will be in .xml file thats it.

Related Links

Android: Can I write a Java Object to a file and retrieve it later?
How to create Table of Contents in pdf as shown in the image, using jasper/jrxml?
2 Dimensional Arrays (Java) - Printing a Triangle with Alphabets in Cyclic order
blocking threads in java using semaphore
Relation between android:onClick and View.OnClickListener s
Publicly accessible URL throwing IOException
Dagger 2 unchecked warnings
Unable to run test frameworks on mesos
JSP form tag action attribute URI Spring MVC
textarea not visible when adding scroll java
Recursion and if statement in java
I don't know what's wrong
Java Swing How to detect a list selection event
Error Building Player - Win32 Exception
JSSE SSL: Get algorithm that is used for encryption and Integrity
Why doesnt the colonySize variable change?

Categories

HOME
maven
client
testng
omnet++
google-play
react-virtualized
routes
razor
tizen-web-app
malloc
survey
django-imagekit
in-app-purchase
communication
izpack
google-cloud-ml
progressive-web-apps
pythonanywhere
karma-jasmine
textfield
cx-freeze
cloudhub
csrf-protection
sylius
bootstrap-material-design
phpfox
nhapi
windows-error-reporting
fifo
libssl
newline
oracle-fusion-middleware
caret
openoffice.org
http-referer
android-ble
temporary-files
user-controls
keydown
qcombobox
wptoolkit
logparser
fancybox-2
sybase-asa
qtwebview
mediaelement
background-service
abcpdf9
log4c
py2app
fuzzy-search
pagedlist
skype4py
pagerank
rhino-servicebus
cartesian-product
teamcity-8.0
google-places
icu4j
spidermonkey
map-projections
sonarqube5.1.2
pretty-print
c++03
facebook-graph-api-v2.4
gwidgets
typekit
flask-cors
responsive-slides
hippomocks
seaside
android-2.2-froyo
message-driven-bean
aqtime
tidy
coverflow
dataservice
eventlistener
http-unit
factory-method
isnullorempty
trusted
hosts-file
objective-c-2.0
pendrive
gnu-prolog
libc++
pydot
blackberry-playbook
hamachi
locationlistener
self-extracting
blitz++
nsviewanimation
dbisam
data-acquisition
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App