I'm having a tabbed View, and a Floating Action Button on my Main Activity. Whenever I click on the Floating Action Button, I want to call a method that is present in my fragment's code. I tried many things, but it is saying that
Attempt to invoke virtual method 'void com.shyamzawar.makeMyResume.fragments.EducationalDetailsFragment.showChangeLangDialog()' on a null object reference
Here's my code for UserDetailsActivity :
public class UserDetailsActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
private int currentPage;
private FloatingActionButton fab;
private int readFileRequestCode;
private int[] tabIcons = {
R.drawable.ic_account_circle_white, //0 Personal Details
...
R.drawable.ic_create_white_36dp, //7 Declaration
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_details);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Set Title according to the user clicked
String name = "Shyam Zawar";
this.setTitle(name+"'s Profile");
fab = (FloatingActionButton) findViewById(R.id.fab);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
viewPager.addOnPageChangeListener(new DetailOnPageChangeListener());
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
fab.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if (currentPage>0)
{
switch (currentPage)
{
case 1:
FragmentManager manager = getSupportFragmentManager();
EducationalDetailsFragment educationalDetailsFragment = (EducationalDetailsFragment) manager.findFragmentById(R.id.EduFragment);
educationalDetailsFragment.showChangeLangDialog();
break;
}
}
}
});
}
private void setupTabIcons()
{
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
...
tabLayout.getTabAt(7).setIcon(tabIcons[7]);
}
private void setupViewPager(ViewPager viewPager)
{
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new PersonalDetailsFragment(), " PERSONAL DETAILS"); //0
...
adapter.addFrag(new DeclarationFragment(), " DECLARATION"); //7
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Here is the code for EducationalDetailsFragment.java
public class EducationalDetailsFragment extends Fragment{
private FloatingActionButton fab;
private TextView TextViewEduDetails;
public EducationalDetailsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_educational_details, container, false);
TextViewEduDetails = (TextView) view.findViewById(R.id.TextViewEduDetails);
return view;
}
public void setData(String stringData)
{
TextViewEduDetails.setText(stringData);
}
public void showChangeLangDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.educational_details_dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setTitle("Educational Details");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
You can globally declare your educationFragment
private EducationalDetailsFragment mEducationalDetailsFragment;
//in your setupViewPager() method
mEducationalDetailsFragment = new EducationalDetailsFragment();
adapter.addFrag(mEducationalDetailsFragment , "Whatever the Name");
onYourFabClickListerner(){
call the method by using mEducationalDetailsFragment
}
It seems your EducationalDetailsFragment object is null. Use this to retrieve your fragment from your viewpager. Its a better practice:
Fragment EducationalDetailsFragment = viewPager.getAdapter().getItem(index);
If you dont know the index. You can set a tag to your fragment when you create them and find them by tag from your viewpager adapter ;)
change fragment:
public void changeFragment(Fragment fragment){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.content,
fragment).commit();
}
Related
I have an Android tabs with 3 fragments and a FAB. I want to replace to a new fragment when click the FAB but when i click the button it replace with a blank fragment. And when i click on my tabs, it doesn't show its fragment but an empty fragment instead (sometimes it show right but i don't know when)
Here is my MainActivity:
public class MainActivity extends AppCompatActivity {
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private TabLayout tabLayout;
private FloatingActionButton fabAdd;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.container);
PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());
pageAdapter.addFragment(new Wallet(), "Wallet");
pageAdapter.addFragment(new History(),"History");
pageAdapter.addFragment(new Setting(),"Setting");
mViewPager.setAdapter(pageAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
fabAdd = (FloatingActionButton) findViewById(R.id.fabAdd);
manager = getSupportFragmentManager();
fabAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddAccount addAccount = new AddAccount();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,addAccount);
transaction.commit();
}
});
}
}
And here is my adapter:
public class AccountAdapter extends BaseAdapter {
private Context context;
private int layout;
private List<Account> accountList;
public AccountAdapter(Context context, int layout, List<Account> accountList) {
this.context = context;
this.layout = layout;
this.accountList = accountList;
}
#Override
public int getCount() {
return accountList.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layout,null);
TextView accountName = (TextView) view.findViewById(R.id.tvAccount);
TextView accountAddress = (TextView) view.findViewById(R.id.tvAddress);
Account account = accountList.get(i);
accountName.setText(account.getAccountName());
accountAddress.setText(account.getAccountAddress());
return view;
}
}
Here is one of my Fragments (the other is same):
public class AddAccount extends Fragment {
public AddAccount() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.add_account,container,false);
}
}
I'm new to Android and not good English, so forgive me if i make grammar mistake.
Edit: Is there any way to show a new fragment to a viewpager?
Use this code inside onClick
Fragment fragment = new AddAccount();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment);
ft.commit();
and use this for your adapter
class AccountAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I have 2 Tabs as fragments in a ViewPager in an Activity
When I tap the second tab in the layout, the OnTabUnselected method works.
However, when I slide the screen from one tab to another, the method isn't called.
How do I achieve this calling of method by both means "Tapping as well as in a Sliding Gesture"
My Activity class is:
public class OrderActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private FloatingActionButton printAction;
ViewPagerAdapter adapter;
private Boolean exit = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_text_tabs);
TypefaceUtil.overrideFont(getApplicationContext(), "serif", "fonts/opensans.tff");
//TypefaceUtil.overrideFont(getApplicationContext(), "arial", "fonts/opensans.tff");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
//setupTabIcons();
printAction = (FloatingActionButton) findViewById(R.id.float_print);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(getApplicationContext(),"onTabSelected",Toast.LENGTH_LONG).show();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
String ac = null;
int pos = viewPager.getCurrentItem();
Fragment activeFragment = adapter.getItem(pos);
if(pos==0){
ac= ((PersonalDetailFragment)activeFragment).getXmlValue();
}
Toast.makeText(getApplicationContext(),ac,Toast.LENGTH_LONG).show();
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
Toast.makeText(getApplicationContext(),"onTabReselected",Toast.LENGTH_LONG).show();
}
});
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new PersonalDetailFragment(), "Personal");
adapter.addFrag(new OrderItemsFragment(), "Invoice");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Fragment A:
public class FragmentA extends Fragment {
private EditText custName;
public PersonalDetailFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.personal_detail_fragment, container, false);
custName = (EditText) view.findViewById(R.id.customerContactEditText);
return view;
}
public String getXmlValue(){
return custName.getText().toString();
}
}
Fragment B:
public class FragmentB extends Fragment {
private EditText contactNo;
public PersonalDetailFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.personal_detail_fragment, container, false);
contactNo = (EditText) view.findViewById(R.id.customerContactEditText);
return view;
}
}
You can use the ViewPager.addOnPageChangeListener() method to register a listener and get notified about changing the current tab with a pager swipe.
There is a really nice tutorial here.
In your fragment you can use a method named setVisibilityHint that is called every time the visibility of the fragment has been changed. There is a boolean parameter that can be true or false depending if the fragment is visible or not
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
I am trying to replace the fragment views in my tabbed swipe view with another fragment but it does not replace or add the fragment, instead it only adds the menu of the fragment in all the tabbed activity.
I want when in a particular tab activity in my swipeview, I can replace the content(fragmentA) with another fragment(FragmentBoy) which can override that particular menu of that particular tabbed activity. Also could also work when replacing content(fragmentB) with another fragment(FragmentGirl).
Please I stucked, I need a help on this-
public class Main2Activity extends AppCompatActivity {
private ViewPager mViewPager;
private int[] tabIcons = {
R.drawable.ic_grand,
R.drawable.ic_event,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
setupTabIcons();
}
private void setupTabIcons() {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager mViewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentA(), "ONE");
adapter.addFrag(new FragmentB(), "TWO");
mViewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}
public static class FragmentA extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.grand,container,false);
Button tb=(Button)view.findViewById(R.id.btn_c);
tb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction().add(R.id.swapfrag, new BOYFragment());
transaction.commit();
}
});
return view;
}
}
I have a MainActivity which has AppBar containing toolbar and TabLayout, and also ViewPager.
MainActivity holds 4 fragments home, cash, card and account.
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private AppBarLayout appBarLayout;
Window window;
#Override
#SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(this.getResources().getColor(R.color.color_primary_green_dark));
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
ab.setTitle("Example Wallet");
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Toast.makeText(getBaseContext(), "Tab " + position + " Onpage Selected " + viewPager.getCurrentItem(), Toast.LENGTH_SHORT).show();
if (position == 0) {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_green));
tabLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_green_dark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.color_primary_green_dark));
}
} else if (position == 1) {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
tabLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
} else if (position == 2) {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_yellow));
tabLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_yellow_dark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.color_primary_yellow_dark));
}
} else if (position == 3) {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_red));
tabLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_red_dark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.color_primary_red_dark));
}
} else {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_green));
tabLayout.setBackgroundColor(getResources().getColor(R.color.color_primary_green_dark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.color_primary_green_dark));
}
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
FloatingActionButton fab1 = (FloatingActionButton) findViewById(R.id.fab);
if (fab1 != null) {
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent registerIntent = new Intent(MainActivity.this, Detail.class);
startActivity(registerIntent);
}
});
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentMain(), "Home");
adapter.addFrag(new FragmentCash(), "Cash");
adapter.addFrag(new FragmentCard(), "Card");
adapter.addFrag(new FragmentAccount(), "Account");
adapter.addFrag(PartThreeFragment.createInstance(20), "Tab1");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return mFragmentTitleList.get(position);
}
}
}
home fragment code
public class FragmentMain extends Fragment {
private List<Movie> movieList1 = new ArrayList<>();
private List<Movie> movieList2 = new ArrayList<>();
private RecyclerView recyclerView,recyclerView1;
private MovieAdapter mAdapter1,mAdapter2;
private LinearLayout cash_layout,card_layout,account_layout;
private ViewGroup c;
public FragmentMain() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.content_main, container, false);
//final android.app.ActionBar actionBar = getActivity().getActionBar();
//c=container;
recyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
cash_layout = (LinearLayout) v.findViewById(R.id.linearLayout_cash_bal);
card_layout = (LinearLayout) v.findViewById(R.id.linearLayout_card_bal);
account_layout = (LinearLayout) v.findViewById(R.id.linearLayout_account_bal);
mAdapter1 = new MovieAdapter(movieList1);
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(v.getContext());
recyclerView.setLayoutManager(mLayoutManager1);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter1);
prepareMovieData1();
cash_layout.setOnClickListener(new View.OnClickListener() {
#Override
#SuppressWarnings("deprecation")
public void onClick(View v) {
getContext().getActionBar().setSelectedNavigationItem(2);
/*actionBar.selectTab(actionBar.getTabAt(1));
FragmentManager fm=getFragmentManager();
fm.beginTransaction().replace(R.layout.content_cash, (Fragment)new FragmentCash()).commit();
getActivity().getActionBar().setTitle("Home");
//ActionBar actionBar = getActivity().getActionBar();*/
/*FragmentCash fragment2 = new FragmentCash();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();*/
}
});
/*mAdapter2 = new MovieAdapter(movieList2);
RecyclerView.LayoutManager mLayoutManager2 = new LinearLayoutManager(v.getContext());
recyclerView1.setLayoutManager(mLayoutManager2);
recyclerView1.setItemAnimator(new DefaultItemAnimator());
recyclerView1.setAdapter(mAdapter2);
prepareMovieData2();*/
return v;
}
private void prepareMovieData1() {
movieList1.clear();
Movie movie = new Movie("info","List is empty", "To create an item, click on (+) button", "","");
movieList1.add(movie);
mAdapter1.notifyDataSetChanged();
}
/*public void onClick1(View v) {
FragmentCash fragment2 = new FragmentCash();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content,fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}*/
/*private void prepareMovieData2() {
Movie movie = new Movie("card","Card", "New Dress", "Rs.50.00","11/09/2016");
movieList2.add(movie);
mAdapter2.notifyDataSetChanged();
}*/
}
I am trying to call cash, card and account fragment from home fragment but this code
cash_layout.setOnClickListener(new View.OnClickListener() {
#Override
#SuppressWarnings("deprecation")
public void onClick(View v) {
FragmentCash fragment2 = new FragmentCash();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
adds other fragment with home fragment visible.
The solution I need is, in the below image when I click the cash balance it slide to the cash tab with cash fragment onscreen.
It looks like you are trying to add the cash fragment on top of the view pager, or replace it all together. Instead of replacing it in the current view, I'm assuming you would like to navigate to it within the viewPager.
To do that, replace all of your onClick code with pager.setCurrentItem(//Page number with cash fragment on it)
So, your onClick would look something like this:
cash_layout.setOnClickListener(new View.OnClickListener() {
#Override
#SuppressWarnings("deprecation")
public void onClick(View v) {
((MainActivity) getActivity()).getViewPager().setCurrentItem(//Your desired page number as int);
}
});
Or you could solve this easily with an interface.
Just create an interface to access your viewPager's operations like so.
public interface ViewPagerInterface {
ViewPager getViewPager();
}
Have your activity implement it:
public MainActivity extends AppCompatActvity implements ViewPagerInterface {
#Override
public ViewPager getViewPager() {
return this.viewPager;
}
}
Then pass that interface to your Fragments and call the getViewPager method.
viewPagerInterface.getViewPager().setCurrentItem(<my-int>);
I am new in Android Development and I don't know how to pass data from main activity with Listview to select particular product and go to new activity with fragement tablayout...I want to know how can I get data in fragment tablayout from main activity...Thanks
MainActivity.java
import packages...
public class HomeActivity extends AppCompatActivity {
String s_firstname, s_lastname, s_password, s_email, Err;
TextView fname,lname,email,password,err,sid;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private ListView lv;
private List<String> classList;
private FloatingActionButton fab;
Context ctx=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initNavigationDrawer();
lv = (ListView) findViewById(R.id.listView);
SharedPreferences ui = this.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
String s_id = ui.getString("s_id","s_id");
new SignupActivity(this).execute(s_id);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String c_name = (String) lv.getItemAtPosition(position);
Intent intent = new Intent(HomeActivity.this,ClassDetailsActivity.class);
intent.putExtra("c_name", c_name);
startActivity(intent);
}
});
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this,JoinclassActivity.class);
startActivity(intent);
}
});
}
}
DetailsProduct.java
public class ClassDetailsActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_details);
String name = getIntent().getExtras().getString("c_name");
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new EventFragment(), "Event");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
OneFragement.java
public class EventFragment extends Fragment {
TextView tv;
public EventFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_event, container, false);
}
}
Follow this link. There showed how to pass value to the viewpager fragment.
Send values from ViewPager Activity to a Fragment by bundle