Get toolbar button title inside fragment - android

On toolbar there is a button. When the button is clicked a dialog popup's. In the dialog there are 2 spinner and a Select button. I need to get the current selected items of spinner inside Fragment. How can i do it?
public class MainActivity extends AppCompatActivity {
//Toolbar
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
//Navigation Drawer
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
NavigationView navigationView;
Spinner spinner1, spinner2;
Dialog dialog;
Button toolbarButton;
public String sp11;
public String sp22;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbarButton = (Button) findViewById(R.id.toolBarButton);
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
});
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new TimeTableFragment(), "MONDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "TUESDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "WEDNESDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "THURSDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "FRIDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "SATURDAY");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
public void myDialogPopUp(View view)
{
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_design);
spinner1 = (Spinner) dialog.findViewById(R.id.spinner1);
MyCustAdapter adapter = new MyCustAdapter(MainActivity.this, getResources().getStringArray(R.array.sp1));
spinner1.setAdapter(adapter);
spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
adapter = new MyCustAdapter(MainActivity.this, getResources().getStringArray(R.array.sp2));
spinner2.setAdapter(adapter);
dialog.setCancelable(true);
dialog.show();
}
public void onButtonSelect(View view)
{
sp11 = spinner1.getSelectedItem().toString();
sp22 = spinner2.getSelectedItem().toString();
//Toast.makeText(MainActivity.this, sp11+"\n"+sp22, Toast.LENGTH_SHORT).show();
dialog.cancel();
toolbarButton.setText(sp11+"\t\t\t˅\n"+sp22);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
public class TimeTableFragment extends Fragment {
public TimeTableFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Toast.makeText(getActivity(), "\nonCreateView", Toast.LENGTH_SHORT).show();
return inflater.inflate(R.layout.fragment_time_table, container, false);
}
}

you can create your own dialog and extend Dialog.
then add a method in your dialog:
public void setIndex(int index) {
this.index = index;
}
and then in your activity:
dialog = new CustomDialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_design);
dialog.setCancelable(true);
dialog.setIndex(index);
dialog.show();

Create a method in your TimeTableFragment to get the two values.
public class TimeTableFragment extends Fragment {
public void spinnerSelection(String one,String two){
}
}
Now you need to get the current selected fragment from the view pager. For that use the following code.
TimeTableFragment ttFrag=(TimeTableFragment)viewPagerAdapter.instantiateItem(viewPager , currentPosition)
ttFrag.spinnerSelection("Spinner1","Spinner2");
Put this inside the Spinner item click event.
Current position can be obtained by using the following methods
viewPager.getCurrentItem()
or
viewPageListener = new PageListener();
viewPager.setOnPageChangeListener(viewPageListener);

Related

Where should I implement onClick method in a NavigationDrawer Activity?

I have a navigation drawer and a fragment that generates a list of products. The problem is that when I click on a product it tryes to find onClick method in base activity not in fragment's java class. Should I implement onClick method in the base activity or is there any way to implement it in fragment's class.
This is product's layout "item_order.xml":
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="orderClick"
android:id="#+id/tOrder"
android:padding="4dp">
....
</RelativeLayout>
Here is the base class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waiter);
//set fragment
OrdersFragment fragment=new OrdersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
OrdersFragment.wa=this;
}
Here is the fragment:
public class OrdersFragment extends Fragment {
private Database db;
private ArrayList<Order> orders=new ArrayList<Order>();
private OrderAdapter adapter;
private ListView listOrders;
public static WaiterActivity wa;
public OrdersFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orders, container, false);
// Inflate the layout for this fragment
db = new Database();
db.createNewOrderListener(this);
ListView listOrders = (ListView) view.findViewById(R.id.ordersList);
adapter = new OrderAdapter(this.getContext(), R.layout.item_order, orders);
listOrders.setAdapter(adapter);
return view;
}
I think you are talking about list view item selection.
Firstly remove onClick from you xml. Create a function orderClick in you base activity and Then in you fragment add onItemClick listener to your list view.
listOrders.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (getActivity() instanceof "your base activity"){
(("your base activity")getActivity()).orderClick();
}
}
});
You should call activity method for taking action by calling it in your fragment class and pass the itemIndex of item that has been called.
use setOnItemClickListener in your activity, and call the fragments from there only, as:-
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case 0:
fragment = new ProgramFragment();
break;
case 1:
fragment = new FeedbackFragment();
break;
case 2:
fragment = new ShareFragment();
break;
case 3:
fragment = new LogOutFragment();
break;
default:
fragment =null;
}
if (fragment != null) {
fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit();
}
mDrawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(mDrawerList);
}
});

Listview in android tabbed layout

I have implemented a TabLayout in android with two fragments. In one fragment I added a ListView. I am retrieving data from server to fill up the ListView. Everything was working fine until I tried to access the ListView from the MainActivity class.
I need access of the same listview(which is in the fragment) from the MainActivity where I created the TabLayout to load data. The fragment is created for a tab of the tablayout.
I added the code below.
Can anyone tell me how can I access that ListView from the MainActivity?
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_report);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_launcher));
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_launcher));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setVisibility(View.INVISIBLE);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
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) {
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
ReportListFragment.java
public class ReportListFragment extends Fragment{
private SimpleDateFormat mFormatter = new SimpleDateFormat("MMMM dd yyyy hh:mm aa");
Date newDate;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
finder_sharedprefs = getActivity().getSharedPreferences(FINDERPREFERENCES, Context.MODE_PRIVATE);
listView=(ListView) view.findViewById(R.id.list);
reportLayout = (RelativeLayout) view.findViewById(R.id.new_report);
} catch (ParseException e) {
e.printStackTrace();
}
}
return view;
}
}
Kindly check this answer if it helps you,
In your respective Fragment code create a public function
FragmentA.java
create a function like below.
public void getData(int r) {
Log.e("data got from Activity", "" + r); // this is sample
//Do your updations to list view here.
}
Now in your mainActivity.java call this function like below wherever you want
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new FragmentA().getData(34);
}
});
Now check the log cat. you will see the number 34 is printed.
Let me know if you have any queries.

ViewPager can not remove view in a fragment

A fragment named NewsCenterFragment would replace the FramLayout in DrawerActivity.The problem is program will crash when I swipe the viewPager to a next slide. The exception is given which says: java.lang.IllegalStateException: The specified child already has a parent.You must call removeView() on the child's parent first.
public class DrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,Serializable {
private FragmentManager fm;
private NavigationView navigationView;
private NewsMenu data;
private RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
radioGroup = (RadioGroup) findViewById(R.id.content_drawer_radioGroup);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
checkCacheForNewsCenterSlideBar();
changeNewsSlideBar();
Fragment homeFragment = new HomeFragment();
fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_drawer_content,homeFragment).commit();
getSupportActionBar().setTitle("Home page");
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.content_drawer_radioGroup_shouye:
Fragment homeFragment = new HomeFragment();
fm.beginTransaction().replace(R.id.content_drawer_content,homeFragment).commit();
getSupportActionBar().setTitle("Home page");
break;
case R.id.content_drawer_radioGroup_newscenter:
Fragment newsCenterFragment = new NewsCenterFragment(findNewsTabTitle(),DrawerActivity.this);
fm.beginTransaction().replace(R.id.content_drawer_content,newsCenterFragment).commit();
getSupportActionBar().setTitle("News center");
break;
case R.id.content_drawer_radioGroup_smartservice:
break;
case R.id.content_drawer_radioGroup_goveraffairs:
break;
case R.id.content_drawer_radioGroup_setting:
break;
}
}
});
}
public class NewsCenterFragment extends Fragment {
private ViewPager viewPager;
private Activity mActivity;
private ArrayList<String> title;
private ArrayList<View> views;
public NewsCenterFragment(ArrayList<String> title, Activity activity){
this.title = title;
this.mActivity = activity;
}
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_newscenter_layout,container,false);
viewPager = (ViewPager) view.findViewById(R.id.fragment_newscenter_viewPager);
views = new ArrayList<View>();
View tempLayout = inflater.inflate(R.layout.news_title_layout,container,false);
for(int i=0;i<title.size();i++){
views.add(tempLayout);
}
viewPager.setAdapter(new NewsMenuDetailAdapter());
return view;
}
class NewsMenuDetailAdapter extends PagerAdapter{
#Override
public int getCount() {
return views.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==(LinearLayout)object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
String tabTitle = title.get(position);
View view = views.get(position);
TextView textView = (TextView) view.findViewById(R.id.news_title_layout_title);
textView.setText(tabTitle);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
}
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I have been searching solution for a couple of hours.Please help me out.Thanks a lot.
It's looked like you have N references to one object in your views array from here:
View tempLayout = inflater.inflate(R.layout.news_title_layout,container,false);
for(int i=0;i<title.size();i++){
views.add(tempLayout);
}
So when your adapter started to return items it attached this view (tempLayout) for first time succesfully, but on the second it failed, because tempLayout (i.e. views[i]) already have a parent from previous call instantiateItem.
You should either provide another inflated view or call ((ViewGroup)child.getParent()).removeView(child) before call container.add()

Navigation drawer with tabs in fragment

I am making an android app . I am using navigation drawer and some of the fragments which i am inflating using navigation drawer contain tab layout and some are normal fragments with textview .
I am facing 2 issues that when
i am opening the navigation drawer item with tabs afterthat its automatically keeping those tabs in the view even for the fragments that are not with tabs.
When i am opening the fragment with tabs 2nd time or more . Its duplicating the tabs. I mean if the first time no of tabs are 3 then the next time when i open the fragment the no. of tabs get doubled.
Activity with navigation drawer
public class MainFeedActivity extends FragmentActivity {
public DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawertitles;
LinearLayout drawerll;
String username;
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainfeed);
username = getIntent().getExtras().getString("username").toString();
t = (TextView) findViewById(R.id.drawer_uname_tv);
t.setText(username);
mTitle = mDrawerTitle = getTitle();
mDrawertitles = getResources().getStringArray(R.array.array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
drawerll = (LinearLayout) findViewById(R.id.drawerll);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawertitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(2);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return true;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
if (position == 0) {
Fragment fragment = new Home();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(drawerll);
}
else if (position == 1) {
Fragment fragment = new Statistics();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(drawerll);
} else {
Fragment fragment = new Help();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerLayout.closeDrawer(drawerll);
}
}
/*
*
* protected boolean isOnline() { ConnectivityManager cm =
* (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
* NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null &&
* netInfo.isConnectedOrConnecting()) { return true; } else { return false;
* } }
*/
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
}
Fragments
public class Home extends Fragment {
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new Connected();
Fragment fragmentTab2 = new Disconnected();
Fragment fragmentTab3 = new AllDevices();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tabs, container, false);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(true);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
return view;
}
}
Fragments:
public class Help extends Fragment {
Button b;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.help, container, false);
setHasOptionsMenu(false);
return rootView;
}
}
I got the solution of my problems:
1st problem solved with the help of denvercoder9
when position!= 1, you need to change the actionBar Navigation Mode.... Android ActionBar: show/hide tabs dynamically? – denvercoder9
2nd problem's solution is
I need to use this
ActionBar actionBar = getActivity().getActionBar();
actionBar.removeAllTabs();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
in Home.java class
This resolved the problem of tabs duplication.
Thanks Stackoverflow for helping me :)
I was having the same issue so the solution is simple.
Ans 1: If you don't want Tabs in specific fragment then you can set the navigation bar as
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Ans 2: Either remove the tabs from the home fragment each time u select it or u can check if required tabs already present there then dont add any more tabs in it like this
int tabCount= actionBar.getTabCount();
if(tabCount!=3){
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
return view;
Hope it will solve your problem

Custom ActionBar and gettext from EditTetfrom ViewPager

I have Activity that contains custom ActionBar and ViewPager with three tabs.
I am not able to read text from EditText inside tab when click on button in ActionBar.
EDITED:
Contains TABS...
public class TranAdd extends FragmentActivity...{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
et = (EditText) mViewPager.findViewById(R.id.editText);
//...reference null...
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView textViewTitle = (TextView) mCustomView.findViewById(R.id.textViewTitle);
textViewTitle.setText("New Transaction");
ImageView imageViewOk = (ImageView) mCustomView.findViewById(R.id.imageViewOk);
imageViewOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String txt = et.getText().toString();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
FRAGMENT:
public class Fragment extends Fragment...{
...
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
}
Try calling setHasOptionsMenu(true) inside your onCreate() method of the Fragment (tab) which needs to interact with the ActionBar.

Categories

Resources