0

I am trying to pass toolbar as parameter from MainActivity to DailVerseActivity on Navigation drawer project. Since toolbar is in MainActivity and if I want to add toolbar in DailyVerseActivity. It will be duplicate. so I have to pass toolbar from MainActivity to DailVerseActivity. I am having this issue more than one week. Any help would be appreciated. Thank you in Advance. Here is the code.

MainActivity:

 @Override
    public void onFragmentSelected(int position, Bundle bundle) {
        String value = bundle.getString("value");
        Fragment curFragment = null;
        if(position == 0){
            curFragment = new CardFragment();
            toolbar.setTitle(value);

        }else if(position==1){
            curFragment = new DailyVerseFragment(toolbar);
       

        }else if(position>=2){
            curFragment = new Fragment1(value);
            toolbar.setTitle(value);
        }

        // toolbar.setTitle(value);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, curFragment).commit();
    }

DailyVerseFragment:

public class DailyVerseFragment extends Fragment {

    private ImageView imageView;
    private TextView bibleType;
    private TextView verse;
    private TextView content;
    public Bible bible;
    private SharedPreferences checkDialog;
    private SharedPreferences saveDialog;
    private SharedPreferences shareDialog;
    private ImageView like;
    private ImageView check;
    private ImageView save;
    private TextView likecount, savecount, link;
    public static final int REQUEST_CODE = 101;
    private String key;
    private Toolbar toolbar;
    private boolean flag;
    private AdView mAdView;

    private String categoryName;
    private DrawableImage DrawbleImage;
    private AlertReceiver AlertReceivr;

    public DailyVerseFragment(){}
    public DailyVerseFragment(Toolbar value) {
        this.toolbar = value;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Activity a = getActivity();
        if (a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        final ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_daily_verse, container, false);

       // toolbar = rootView.findViewById(R.id.toolbar);
       // ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);


        verse = rootView.findViewById(R.id.verse);
        content = rootView.findViewById(R.id.content);
        imageView = rootView.findViewById(R.id.imageView);
        save = rootView.findViewById(R.id.save);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("bibleNum", Context.MODE_PRIVATE);
        final int bibleNum = sharedPreferences.getInt("bibleNum", 1);
        Log.d("DailyVerseNum", String.valueOf(bibleNum));

        MyDatabaseHelper db = new MyDatabaseHelper(getActivity());
        Cursor cursor = db.readDailyCardData(bibleNum);
        cursor.moveToFirst();
        bible = new Bible();
        Log.d("cursorid", String.valueOf(cursor.getInt(0)));
        bible.setId(cursor.getInt(0));
        bible.setVerse(cursor.getString(1));
        bible.setContent(cursor.getString(2));
        bible.setNum(cursor.getInt(3));
        bible.setCnum(cursor.getInt(4));
        bible.setVnum(cursor.getInt(5));
        bible.setImage(cursor.getBlob(6));
        bible.setTimestamp(cursor.getString(7));
        Log.d("content", bible.getContent());
        Log.d("bible.getCnum", String.valueOf(bible.getCnum()));
        Cursor cursor1 = db.getCategoryName(bible.getCnum());
        cursor1.moveToFirst();
        categoryName = cursor1.getString(0);
        toolbar.setTitle(categoryName);

        return rootView;
      }

    }
}
John
  • 83
  • 1
  • 2
  • 9
  • 1
    you have a fragment not activity, and you can add a toolbar in your fragment and remove the main toolbar from your activity. – Umair Jul 16 '20 at 10:45
  • You shouldn't try to pass views between activities. What is your exact user case? You can always hide ActionBar in activity and show toolbar. – bongo Jul 16 '20 at 10:51
  • @Umair, I am sorry I edit my post. If I remove toolbar from activity then it will affect other fragment on the menu bar of navigation drawer...Is there any other way to resolve this? – John Jul 16 '20 at 11:08
  • @John yes what you should do is give every fragment their individual toolbars and make your activity without one. – Umair Jul 16 '20 at 11:16
  • 1
    @Umair, ah, why didn't I think of that. Thank you:) – John Jul 16 '20 at 11:33
  • @Umair, shoot, but there is a problem. I have 16 different fragments.... – John Jul 16 '20 at 11:43
  • no problem, you can make a single fragment and then extend rest of them with that fragment. Just like we do with activities. – Umair Jul 16 '20 at 11:53
  • @Umair, I tried but when I erase toolbar from MainActivity, Navigation drawer is gone... – John Jul 16 '20 at 13:36
  • @John oh yes sorry, you should go through these I believe you will be able to fix your issue. https://stackoverflow.com/questions/29020935/using-toolbar-with-fragments and https://medium.com/@programmerr47/give-toolbar-to-each-fragment-52c3a996deb5 – Umair Jul 17 '20 at 05:39
  • 1
    @Umair, Thank you very much for your help! but it is too hard for me to change it according to the doc, so I decided to do it other way to resolve the issue. Thank you very much for your help though. – John Jul 17 '20 at 14:42
  • happy that you were able to resolve your issue. Happy coding :) – Umair Jul 20 '20 at 05:13

0 Answers0