Annie Annie - 12 days ago 7
Android Question

home fragment partially visible with mixed view of current fragment android studio

I have a navigation drawer in my android app its default loading fragment as home screen is homefragment. Home fragment has imageviews on it which listens to click and opens different fragment on each imageview, the problem is fragments show on full screen whereas i want them to be seen as under the toolbar of navigationview and when i press back button the app exits to main login screen. Someone please suggest me to deal with this issue.
This is the code of homefragment which is the default of navigation drawer:

public class HomeFragment extends Fragment {
ImageView im1,im2,im3,im4,im5,im6,im7,im8,im9,im10,im11,im12,im13,im14;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container != null) {
container.removeAllViews();
}
View view = inflater.inflate(R.layout.activity_home,
container, false);
im1 = (ImageView) view.findViewById(R.id.imageView37);
im2 = (ImageView) view.findViewById(R.id.imageView38);
im3 = (ImageView) view.findViewById(R.id.imageView39);
im4 = (ImageView) view.findViewById(R.id.imageView40);
im5 = (ImageView) view.findViewById(R.id.imageView41);
im6 = (ImageView) view.findViewById(R.id.imageView42);
im7 = (ImageView) view.findViewById(R.id.imageView43);
im8 = (ImageView) view.findViewById(R.id.imageView44);
im9 = (ImageView) view.findViewById(R.id.imageView45);
im10 = (ImageView) view.findViewById(R.id.imageView46);
im11 = (ImageView) view.findViewById(R.id.imageView48);
im12 = (ImageView) view.findViewById(R.id.imageView50);
im13 = (ImageView) view.findViewById(R.id.imageView51);
im14 = (ImageView) view.findViewById(R.id.imageView52);

im1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DashboardFragment fragment = new DashboardFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyLawyerFragment fragment = new MyLawyerFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im3.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AddHearingFragment fragment = new AddHearingFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyCasesFragment fragment = new MyCasesFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im5.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyPaymentsFragment fragment = new MyPaymentsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im6.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyClientsFragment fragment = new MyClientsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im7.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ArchivedCasesFragment fragment = new ArchivedCasesFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im8.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SubscriptionFragment fragment = new SubscriptionFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im9.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SettingsFragment fragment = new SettingsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im10.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WebsiteFragment fragment = new WebsiteFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im11.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
KeyJudgementsFragment fragment = new KeyJudgementsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im12.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DailyJudgementsFragment fragment = new DailyJudgementsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im13.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
JudgementLibraryFragment fragment = new JudgementLibraryFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
im14.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LogoutFragment fragment = new LogoutFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}}


And here is the code of one of the imageviews on homefragment:dashboard-->

public class DashboardFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
private String tabTitles[] = new String[] { "Schedule", "Payments", "Judgements" };
private int[] imageResId = {
R.drawable.schedule_active,
R.drawable.payment_summary_active,
R.drawable.judgements_active
};


@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);
tabLayout.setSelectedTabIndicatorHeight(0);

/**
*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);

}
});
// createTabIcons();

return x;

}


class MyAdapter extends FragmentPagerAdapter {

public MyAdapter(FragmentManager fm) {
super(fm);
}

/**
* Return fragment with respect to Position .
*/

@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Schedule();
case 1 : return new Payment();
case 2 : return new Judgement();
}
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) {

Drawable image = getView().getResources().getDrawable(imageResId[position]);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
SpannableString sb = new SpannableString(" " + tabTitles[position]);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}}}


And here is another one fragment which is also called on click of one of the image views:

public class MyLawyerFragment extends Fragment {

ImageButton im1;
LinearLayout l1;
ChangeFragListener listener;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = (ChangeFragListener)getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container != null) {
container.removeAllViews();
}
View v1 = inflater.inflate(R.layout.activity_mylawyer,
container, false);
im1 = (ImageButton) v1.findViewById(R.id.imageButton);
l1 = (LinearLayout) v1.findViewById(R.id.linearLayout4);
im1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
change("lawyer");

AddLawyer fragment = new AddLawyer();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.drawer_layout, fragment);
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
l1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LawyerDetailsFragment fragment = new LawyerDetailsFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.drawer_layout, fragment);
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});

return v1;
}

private void change(String lawyer) {
}}


I added this piece of code and now two screens don't merge however the toolbar is still missing.

screenshot of lawyer fragment where toolbar of navigationview is missing like othe fragments

Answer

As a help to other who might be going through the same problem as i was, i found out a way out of the issue as i came to know that i was using wrong id for the fragmentTransaction.replace(); , as it should be same as i used in navigation drawer and that is R.id.frame