0

I create a programatically toolbar, and I want to add it to my activity at the top, without putting id in my xml layout.

I can add the view at the top but the problem is that it goes over the views of the layout. I want that when I add it all the layout goes below my toolbar. I try to do it in a generic way.

To summarize in my layout xml I have my Relative layout (but it could be something else linear layout, I want it to be generic). And in my code I want to add it to my relativelayout

    val toolbar = PrimaryToolbar(context = this, title = "Title")
    val viewGroup: ViewGroup = this.findViewById(android.R.id.content)
    viewGroup.addView(toolbar, 0)

Result

Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46
  • since relative layout have layout_below attribute so you have to set that programatically too and I don't think you can have a generic method for this as different layouts have different properties – Vivek Mishra Jan 07 '19 at 09:03
  • Or for LinearLayout how to do this? – Mickael Belhassen Jan 07 '19 at 09:09
  • For linear layout if the orientation is set to vertical, any new view will be added below toolbar. You don't have to do anything special in that case – Vivek Mishra Jan 07 '19 at 09:10
  • Why wouldn't you want to put it within the XML. It'll make layouting much more easy. I would always suggest to go for the approach making less trouble. – tynn Jan 07 '19 at 10:44
  • Because I created a library to create the toolbar very easily with actions, fully customizable. The only thing I miss to close the library is to add this toolbar in a generic way programtically in the activity without having to give it an id in the xml. So if you have a solution .. – Mickael Belhassen Jan 07 '19 at 11:04
  • @MickaelBelhassen can you share the Screen Shot? `top but the problem is that it goes over the views of the layout.` you want toolbar at top of everything right? – Anmol Jan 07 '19 at 11:06
  • Yes look my edit please – Mickael Belhassen Jan 07 '19 at 11:12
  • @MickaelBelhassen is your requirement to add toolbar on based on user input? – Anmol Jan 07 '19 at 11:14
  • Yes I want to add a toolbar this way, I want my xml layout to be below the toolbar. – Mickael Belhassen Jan 07 '19 at 11:19

1 Answers1

1

Hello as per my understanding of your question,

Read this (How to add action bar to your Fragment)

add the support action bar in your activity write below code in your activity to change the title of the action bar or to show or hide the action bar

public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

public void hideActionBar() {
    getSupportActionBar().hide();
}

public void showActionBar() {
    getSupportActionBar().show();
}

And in your code use below to hide/show/change title of your action bar.

((MainFragmentActivity) getActivity())
            .setActionBarTitle("Your title");
((MainFragmentActivity) getActivity())
                .hideActionBar();
((MainFragmentActivity) getActivity())
                .showActionBar();

Note: Above code is generic and work's on any layout type.And you can show the ActionBar based on user input in Fragment

Anmol
  • 8,110
  • 9
  • 38
  • 63
  • Thanks for your answer but its not what I want, I want to use my custom PrimaryToolbar class .. – Mickael Belhassen Jan 07 '19 at 11:27
  • @MickaelBelhassen then it can't be generic as placing the child in RelativeLayout is Different From Placing the Child in FrameLayout etc.in My Solution you can use your `PrimaryToolbar` but from activity – Anmol Jan 07 '19 at 11:28