My Activity which have three fragments in it
package com.appointment.activities;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.appointment.adapters.TabsPagerAdapter;
import com.appointment.fragments.TabViewFragment;
import com.example.activity.R;
/**
* Created by Akshay on 8/17/2015.
*/
public class HomePageActivity extends ActionBarActivity {
public static ActionBar actionBar;
public static int fragment_count = 0;
public static FragmentTransaction fragmentTransaction;``
ImageView menu_icon;
Animation profile_in, profile_out;
LinearLayout right_slider_layout;
boolean menu_in = false;
TabsPagerAdapter tabsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page_layout);
actionBar = getSupportActionBar();
View custom_action_bar = LayoutInflater.from(actionBar.getThemedContext()).inflate(R.layout.custom_actionbar, null);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT);
profile_in = AnimationUtils.loadAnimation(this, R.anim.right_to_left_animation);
profile_out = AnimationUtils.loadAnimation(this, R.anim.left_to_right_animation);
right_slider_layout = (LinearLayout)findViewById(R.id.right_slider_layout);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setCustomView(custom_action_bar, layoutParams);
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#6FEAD1")));
menu_icon = (ImageView)actionBar.getCustomView().findViewById(R.id.menu_icon);
menu_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!menu_in)
{
right_slider_layout.startAnimation(profile_in);
right_slider_layout.setVisibility(View.VISIBLE);
menu_in = true;
}
else
{
right_slider_layout.startAnimation(profile_out);
right_slider_layout.setVisibility(View.GONE);
menu_in = false;
}
}
});
Toolbar parent = (Toolbar) custom_action_bar.getParent();
parent.setContentInsetsAbsolute(0, 0);
Log.e("ACTION BAR", actionBar + "");
TabViewFragment tabViewFragment = new TabViewFragment(actionBar);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, tabViewFragment).addToBackStack("TabViewFragment").commit();
Toast.makeText(getApplicationContext(), "home onCreate", Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed() {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
Toast.makeText(getApplicationContext(), fragmentManager.getBackStackEntryCount()+"", Toast.LENGTH_SHORT).show();
if(fragmentManager.getBackStackEntryCount() == 1) {
super.onBackPressed();
finish();
}
/*else if(fragmentManager.getBackStackEntryCount() == 2){
Toast.makeText(getApplicationContext(), "Hahahah", Toast.LENGTH_SHORT).show();
fragmentManager.popBackStack();
actionBar.removeAllTabs();
TabViewFragment tabViewFragment = new TabViewFragment(actionBar);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, tabViewFragment).commit();
}*/
else
{
fragmentManager.popBackStack();
}
}
}
*First Fragment with tabs and viewPager this is the fragment which is creating problem *
package com.appointment.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.appointment.activities.HomePageActivity;
import com.appointment.adapters.TabsPagerAdapter;
import com.example.activity.R;
/**
* Created by Akshay on 8/17/2015.
*/
public class TabViewFragment extends Fragment implements ActionBar.TabListener{
private ViewPager viewPager;
private TabsPagerAdapter tabsPagerAdapter;
ActionBar actionBar;
// Tab titles
private String[] tabs = { "Specialities", "Doctor" };
public TabViewFragment(ActionBar actionBar)
{
this.actionBar = actionBar;
}
@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.tab_view_layout, container, false);
HomePageActivity.fragment_count = 1;
// Initilization
viewPager = (ViewPager) view.findViewById(R.id.pager);
tabsPagerAdapter = new TabsPagerAdapter(getFragmentManager());
viewPager.setAdapter(tabsPagerAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.removeAllTabs();
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
return view;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if(tab.getText().equals("Specialities"))
{
viewPager.setCurrentItem(0);
}
else if(tab.getText().equals("Doctor"))
{
viewPager.setCurrentItem(1);
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
FragmentPagerAdapter to get view and show in viewPager
package com.appointment.adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import com.appointment.fragments.DoctorFragment;
import com.appointment.fragments.SpecialitiesFragment;
/**
* Created by Akshay on 8/17/2015.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index){
case 0:
Log.e("GET ITEM", "getItem()");
return new SpecialitiesFragment();
case 1:
return new DoctorFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
Second fragment
package com.appointment.fragments;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.appointment.activities.HomePageActivity;
import com.appointment.adapters.DoctorListViewAdapter;
import com.example.activity.R;
/**
* Created by Akshay on 8/20/2015.
*/
public class DoctorListFragment extends Fragment {
TextView doc_name;
ListView doctor_listview;
String doctor_name[] = {"Dr. Paul Wagner", "Dr. Robert", "Dr. Sam", "Dr. John", "Dr. Michel", "Dr. Paul", "Dr. Smith", "Dr. Tom", "Dr. Will", "Dr. Jerry"};
String speciality[] = {"Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist", "Cardiologist"};
String doctor_address[] = {"25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA", "25 Street Avenue, Newyork, USA"};
int doctor_image_id[] = {R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon, R.drawable.docicon};
Context context;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
context = getActivity();
HomePageActivity.actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
HomePageActivity.fragment_count = 2;
View view = inflater.inflate(R.layout.doctor_list, null);
doctor_listview = (ListView)view.findViewById(R.id.doctor_listview);
doctor_listview.setAdapter(new DoctorListViewAdapter(context, doctor_name, speciality, doctor_address, doctor_image_id));
doctor_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
doc_name = (TextView)view.findViewById(R.id.doctor_name);
DoctorDetailsFragment doctorDetailsFragment = new DoctorDetailsFragment(doc_name.getText()+"");
getFragmentManager().beginTransaction().replace(R.id.fragment_container, doctorDetailsFragment).addToBackStack("DoctorDetailsFragment").commit();
}
});
return view;
}
}
Third fragment
package com.appointment.fragments;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.appointment.activities.BookAppointmentActivity;
import com.appointment.activities.HomePageActivity;
import com.appointment.adapters.DoctorDetailsListViewAdapter;
import com.example.activity.R;
/**
* Created by Akshay on 8/20/2015.
*/
public class DoctorDetailsFragment extends Fragment{
ListView doctor_detail_listview;
TextView book_appointment, doc_name;
Context context;
int detail_icon[] = {R.drawable.address, R.drawable.speciality, R.drawable.education, R.drawable.achievement};
String detail_title[] = {"ADDRESS", "SPECIALITY", "EDUCATION", "ACHIEVEMENTS"};
String detail_description[] = {"FLAT #24, AVENUE APPT., AVENUE STREET, NY", "BROOKDALE UNIVERSITY HOSPITAL AND MEDICAL CENTER BROOKLYN HOSPITAL CENTER", "MBBS, MD, PHD, PHARMD", "DOCTOR OF SCIENCE, HONORARY DOCTORATE"};
String doctor_name;
DoctorDetailsFragment(String doctor_name)
{
this.doctor_name = doctor_name;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
context = getActivity();
HomePageActivity.fragment_count = 3;
View view = inflater.inflate(R.layout.doctor_info, null);
doc_name = (TextView)view.findViewById(R.id.doc_name);
book_appointment = (TextView)view.findViewById(R.id.book_appointment);
doc_name.setText(doctor_name);
doctor_detail_listview = (ListView)view.findViewById(R.id.doctor_detail_listview);
doctor_detail_listview.setAdapter(new DoctorDetailsListViewAdapter(context, detail_icon, detail_title, detail_description));
book_appointment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent book_appointment_intent = new Intent(getActivity(), BookAppointmentActivity.class);
book_appointment_intent.putExtra("DOCTOR_NAME", doctor_name);
startActivity(book_appointment_intent);
}
});
return view;
}
}
Speciality fragment
package com.appointment.fragments;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import com.appointment.activities.HomePageActivity;
import com.appointment.adapters.SpecialitiesGridAdapter;
import com.example.activity.R;
/**
* Created by Akshay on 8/17/2015.
*/
public class SpecialitiesFragment extends Fragment {
GridView gridView;
Context context;
String specialities[] = {"General Physician", "Gynacologist", "Orthopendician", "Pediatrician", "Dentist", "Ophthalmologist", "Homeopath", "ENT Specialist", "Cardiologist", "Psychiatrist", "Ayurveda", "MF Specialist"};
int iconID[] = {R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine, R.drawable.ten, R.drawable.eleven, R.drawable.tweleve};
//ActionBar actionBar;
HomePageActivity homePageActivity;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.specialities_layout, container, false);
context = getActivity();
gridView = (GridView)view.findViewById(R.id.grid_view);
gridView.setAdapter(new SpecialitiesGridAdapter(context, specialities, iconID));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView;
DoctorListFragment doctorListFragment = new DoctorListFragment();
textView = (TextView)view.findViewById(R.id.grid_text);
//Toast.makeText(getActivity(), textView.getText().toString(), Toast.LENGTH_SHORT).show();
if(textView.getText().toString().equals("Cardiologist"))
{
getFragmentManager().beginTransaction().replace(R.id.fragment_container, doctorListFragment).addToBackStack("DoctorListFragment").commit();
}
}
});
return view;
}
}
Doctor Fragment
package com.appointment.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.activity.R;
/**
* Created by Akshay on 8/17/2015.
*/
public class DoctorFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.doctor_layout, container, false);
return view;
}
}