I want to start activity with three fragments
Intent intent = new Intent(NewMealActivity.this, MainActivity.class);
intent.putExtra("id", 1);
startActivity(intent);
MainActivity includs three fragments (Frag1, Frag2, Frag3).
I want to open Frag3, but opens Frag1
This is my MainActivity with three tabs
public class MainActivity extends FragmentActivity {
private String txtFragmentMyData;
private String txtFragmentProductList;
private String txtFragmentCalorieCalculator;
private TabLayout tabLayout;
private long tabId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtFragmentMyData = getString(R.string.txt_fragment_my_data);
txtFragmentProductList = getString(R.string.txt_fragment_product_list);
txtFragmentCalorieCalculator = getString(R.string.txt_fragment_calorie_calculator);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentMyData));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentProductList));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentCalorieCalculator));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tabId = extras.getLong("id");
}
if (tabId > 0){
viewPager.setCurrentItem(2);
} else {
viewPager.setCurrentItem(0);
}
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
I want, that at the first start opens first tab, but if I click on buttom in another activity opens third tab
Related
I am using two Activities. One is MainActivity and the second one is for NFC. MainActivity has nav_graph.xml.
After reading the NFC, I'd like to start MainActivity and move to a certain fragment of MainActivity.
How can I implement this?
You can try this:
(in MainActivity.java)
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
Frag1 frag1 = new Frag1();//this is one activity
return frag1;
case 1:
Frag2 frag2 = new Frag2();
return frag2;
default:
return null;
}
}
#Override
public int getCount() {
return 2; //'the number 2 is 'cause you've got a two activities
}
}
and if you want open another activity with de floating action button, you can try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mViewPager.getCurrentItem()==0){
Fragment1();
Toast.makeText(MainActivity.this, "Example 1", Toast.LENGTH_SHORT).show();
}else if(mViewPager.getCurrentItem()==1){
Fragmen2();
Toast.makeText(MainActivity.this, "Example 2", Toast.LENGTH_SHORT).show();
}
}
});
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
public void Fragment1(){//Call to the activity Example1
Intent fragment0 = new Intent(getApplicationContext(), example1.class);
startActivity(fragment0);
onResume();
}
public void Fragmen2(){ //Call to the activity Example2
Intent fragment1 = new Intent(getApplicationContext(), example2.class);
startActivity(fragment1);
onResume();
}
I have used Tab Control and RecyclerView together in Android. The way it functions is I have SearchBar in the toolbar. When the user enters the search query in the search bar. Upon hitting the search button, tab's Fragments are supposed to be loaded with data. Issues are as follows.
1) Tabs icon disappear
2) When debugging through the code found (Since I am using ViewPager with Tab Layout to implement Tabs in android) that getItem method of PagerAdapter class is called twice every time getItem is triggered.
I am stuck and don't know what to do. Not much help online as well.
1) Tab initialization Code:-
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout)findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.craigslist_new1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.kijiji_new1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.letgo_new1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.varagesale_new1));
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.facebook_new1));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
drawerLayout = findViewById(R.id.drawer_layout);
}
#Override
public boolean onQueryTextSubmit(String query) {
//loadRecyclerViewData(query);
Bundle bundle = new Bundle();
bundle.putString("searchText", query);
final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), bundle);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return true;
}
2) PagerAdapter:-
public class PageAdapter extends FragmentStatePagerAdapter {
int mNoofTabs;
Bundle bundle;
public PageAdapter(FragmentManager fragmentManager, int NumberOfTabs, Bundle bundle)
{
super(fragmentManager);
this.mNoofTabs = NumberOfTabs;
this.bundle = bundle;
}
#Override
public Fragment getItem(int i) {
switch(i)
{
case 0 :
CraigslistFragment tab1 = new CraigslistFragment();
tab1.setArguments(bundle);
return tab1;
case 1:
KijijiFragment tab2 = new KijijiFragment();
tab2.setArguments(bundle);
return tab2;
case 2:
letgoFragment tab3 = new letgoFragment();
tab3.setArguments(bundle);
return tab3;
case 3:
VaragesaleFragment tab4 = new VaragesaleFragment();
tab4.setArguments(bundle);
return tab4;
case 4:
FacebookFragment tab5 = new FacebookFragment();
tab5.setArguments(bundle);
return tab5;
default:
return null;
}
}
#Override
public int getCount() {
return mNoofTabs;
}
}
I am having 4 tabs in tablayout and I am using fragments with view pager for them.Now for better customisation,I am providing user a choice to remove any tab or add any tab when they require.
But the problem is now I am not able to remove a tab and add a tab.THe code I have used for making tablayout is mentioned below-
Here objects and reference is passed from Pageradapter class.
public class PagerAdapter extends FragmentStatePagerAdapter {
int NoofTabs;
public PagerAdapter(FragmentManager fm,int NoofTabs) {
super(fm);
this.NoofTabs=NoofTabs;
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
BlankFragment blankFragment=new BlankFragment();
return blankFragment;
case 1:
BlankFragmentia blankFragmentia=new BlankFragmentia();
return blankFragmentia;
case 2:
BlankFragmentyou blankFragmentyou=new BlankFragmentyou();
return blankFragmentyou;
case 3:
Blankt t=new Blankt();
return t;
default:
return null;
}
}
#Override
public int getCount() {
return NoofTabs;
}
}`
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener,BlankFragmentyou.OnFragmentInteractionListener,BlankFragmentina.OnFragmentInteractionListener,Blankt.OnFragmentInteractionListener{
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout=(TabLayout)findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setText("a"));
tabLayout.addTab(tabLayout.newTab().setText("b"));
tabLayout.addTab(tabLayout.newTab().setText("c"));
tabLayout.addTab(tabLayout.newTab().setText("d"));
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
viewPager=(ViewPager)findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(3);
PagerAdapter adapter=new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition()==0)
{
tabLayout.setBackgroundColor(Color.parseColor("#3b5998"));
}
else if(tab.getPosition()==1)
{
tabLayout.setBackgroundColor(Color.parseColor("#E91E63"));
}
else if(tab.getPosition()==2)
{
tabLayout.setBackgroundColor(Color.parseColor("#FF0000"));
}
else if(tab.getPosition()==3)
{
tabLayout.setBackgroundColor(Color.parseColor("#00aced"));
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Please help me with adding and removing tabs.Only 4 mentioned above tabs can be used.
i create a viewPager and is work , my problem is for select a page i just can drag page but I want to click on the tabs to select the pages . now i add some new code but i cant find this error
my code
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
Pager adapter = new Pager(getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
You cant use like viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); Because addOnAdapterChangeListener() method expect OnAdapterChangeListener as paramenter. You should use void addOnPageChangeListener (ViewPager.OnPageChangeListener listener) on viewPager instead.It has several callback method, So you can detect the event when page of viewPager is changed.
Reference
when i open my application,default selected Home_Tab is opened,working fine....but in this Home_Tab,Bottombar also there ,i dont want default selection tab of About Us??
if i want click AboutUs_Tab that time it will open???
but in this above image when i open Home_Tab ,automatically selected AboutUs_Tab but not opened it ,will try to click that aboutUs_Tab not working???
When i click ContactUS_Tab and return to AboutUs_Tab ,that time AboutUS_Tab opened??how to avoid this issue??
this is my code
public class MainActivity extends FragmentActivity {
private Context context;
//This is our tablayout
private TabLayout tabLayout;
BottomBar bottombar;
//This is our viewPager
private ViewPager viewPager;
public ViewPagerAdapter FragmentStatePagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setHasOptionsMenu(true);
bottombar = BottomBar.attach(MainActivity.this, savedInstanceState);
bottombar.noNavBarGoodness();
bottombar.setItemsFromMenu(R.menu.bottom_bar_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
switch (itemId) {
case R.id.about_us:
About_Us about_us = new About_Us();
About_Fragment about_fragment = new About_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, about_fragment).commit();
break;
case R.id.contact_us:
ContactUs_Activity contactUs_activity = new ContactUs_Activity();
Contact_us_Fragment contact_us_fragment = new Contact_us_Fragment();
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction ft2 = fm2.beginTransaction();
ft2.replace(R.id.Home_BottomBar_Fragment, contact_us_fragment).commit();
//squareFragment.updateColor(Color.parseColor(colors[1]));
//Toast.makeText(MainActivity.this,"selected contact_us",Toast.LENGTH_SHORT).show();
break;
case R.id.donate_us:
Donate_us_Fragment donate_us_fragment = new Donate_us_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, donate_us_fragment).commit();
//Toast.makeText(MainActivity.this,"selected donate_us",Toast.LENGTH_SHORT).show();
break;
default:
}
}
});
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Home").setIcon(R.drawable.home_selector));
tabLayout.addTab(tabLayout.newTab().setText("News").setIcon(R.drawable.news_selector));
tabLayout.addTab(tabLayout.newTab().setText("Videos").setIcon(R.drawable.video_selector));
tabLayout.addTab(tabLayout.newTab().setText("PodCasts").setIcon(R.drawable.podcast_selector));
tabLayout.addTab(tabLayout.newTab().setText("More").setIcon(R.drawable.more_selector));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//tabLayout.setupWithViewPager(viewPager);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 0) {
bottombar.show();
//bottombar.getCurrentTabPosition();
} else if (tab.getPosition() == 1) {
bottombar.hide();
} else if (tab.getPosition() == 2) {
bottombar.hide();
} else if (tab.getPosition() == 3) {
bottombar.hide();
}else if (tab.getPosition() == 4){
bottombar.hide();
}
super.onTabSelected(tab);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
//tab.getIcon().setAlpha(127);
}
});
}
#Override
protected void onSaveInstanceState(Bundle outstate) {
super.onSaveInstanceState(outstate);
// Necessary to restore the BottomBar's state, otherwise we would
// lose the current tab on orientation change.
bottombar.onSaveInstanceState(outstate);
}
}
You can manually set the default selected tab in bottom bar with:
bottomBar.setDefaultTabPosition(1);
Use this after initializing the bottom bar.
You need to add one dummy invisible item for make your thinks work. For that create one more item :
<item android:id="#+id/none"
android:title=""/>
where you create your items. And then in your code change to this :
bottombar.setItemsFromMenu(R.menu.bottom_bar_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
switch (itemId) {
case R.id.none:
break;
case R.id.about_us:
About_Us about_us = new About_Us();
About_Fragment about_fragment = new About_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, about_fragment).commit();
break;
case R.id.contact_us:
ContactUs_Activity contactUs_activity = new ContactUs_Activity();
Contact_us_Fragment contact_us_fragment = new Contact_us_Fragment();
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction ft2 = fm2.beginTransaction();
ft2.replace(R.id.Home_BottomBar_Fragment, contact_us_fragment).commit();
//squareFragment.updateColor(Color.parseColor(colors[1]));
//Toast.makeText(MainActivity.this,"selected contact_us",Toast.LENGTH_SHORT).show();
break;
case R.id.donate_us:
Donate_us_Fragment donate_us_fragment = new Donate_us_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.Home_BottomBar_Fragment, donate_us_fragment).commit();
//Toast.makeText(MainActivity.this,"selected donate_us",Toast.LENGTH_SHORT).show();
break;
default:
}
}
});