has anyone succeeded in setting drawerLayout with ...
# anko
a
has anyone succeeded in setting drawerLayout with navigationView header? If so, has anyone succeeded in showing drawer menu OVER the action bar? so far this is what i got:
Copy code
coordinatorLayout {
			drawerLayout {
				relativeLayout {
					MY MAIN CONTENT
				}
				navigationView {
					inflateHeaderView(R.layout.header) <- UGLY HACK, I CANNOT GET IT TO WORK ANY OTHER WAY
				}
			}

			appBarLayout(R.style.AppTheme_AppBarOverlay) {
				toolbar()
			}
	}
but still it shows under the action bar (it's overlapped by it)
m
I use this scheme:
Copy code
drawerLayout {
    coordinatorLayout {
        appBarLayout {
            toolbar()
            tabLayout()
        }

        frameLayout() /* content */
    }
    navigationView()
}
but DrawerLayout works with fitsSystemWindows incorrectly if it is specified not from AttributeSet, and AppBarLayout behaves poorly if created from code. So I inflate a part of layout from XML. But I can use
NavigationView#addHeaderView
without any problems, and I also can create menu from code.
1
👍 1
a
thank you! It works wonderfully with the scheme. Still I've no success with addHeaderView - how do you prepare it? with
Copy code
val navHeader = relativeLayout {
			imageView(R.drawable.menu_tile)
		}

		navigationView {
			addHeaderView(navHeader)
			...
		}
I get
The specified child already has a parent. You must call removeView() on the child's parent first.
Could you please elaborate on "DrawerLayout works with fitsSystemWindows incorrectly if it is specified not from AttributeSet, and AppBarLayout behaves poorly if created from code."? I can't see anything suspicious now.
m
Anko functions add views to parent instantly, so the relative layout being attached to a parent. Try this:
Copy code
addHeaderView(UI{ /* create a new Anko context */
    relativeLayout { ... } /* root view is not attached, so you can use it */
}.view)
a
Could you please post a more detailed example? I tried to use
UI
but I can't reach it;
AnkoContext.create()
is not a candidate here. I mingled with imports as well (
import org.jetbrains.anko.UI
,
import org.jetbrains.anko.UI as UIHelper
) to no avail. On the other hand, using
with(ui)
causes the same exception (regardless whether I use
}.view()
or not). Not using
with(ui)
at all makes no difference.
m
but I can’t reach it
what do you mean?
a
I mean Android Studio offers me no import for this (i've got
import org.jetbrains.anko.*
already) nor does it interpret UI as anko's UI. I am in a class inheriting from AnkoComponent, if that matters (that's why I tried to use
with(ui)
)
m
I have
inline fun Context.UI(init: AnkoContext<Context>.() -> Unit): AnkoContext<Context>
in AnkoContext.kt, using Anko
0.10.0-beta-2
.
a
Copy code
addHeaderView(context.UI { relativeLayout {
				...
			}}.view())
compiles, but throws
IllegalStateException: View is already set: org.jetbrains.anko._RelativeLayout
. What do I do wrong? 😶
(using
UI(setContentBoolean = false)
makes no difference, btw)
m
I don’t know what’s the problem here (we’re using different versions), but you can try a workaround:
Copy code
addHeaderView(_RelativeLayout(context).apply {
    // DSL here
})
Actually, I’ve got this:
Copy code
addHeaderView(
  object : _RelativeLayout(context) { // sad crutch, fixes bottomPadding when keyboard is open
    @TargetApi(20) override fun onApplyWindowInsets(insets: WindowInsets) = insets.consumeSystemWindowInsets()
  }.apply { /* header view */
👍 1
a
hmm, both compile and run, but the content itself isn't shown (I guess the view isn't evaluated and added to the parent). I'll try with upgrading to 0.10.0-beta2 (from 0.9.1) later - at first try it breaks something and I didn't want to fix for something which I'm not sure will work. Any ideas? Nevertheless, big thank you for your time!
m
addHeaderView adds a view to parent, so it should work. Changes, including breaking ones, are listed here: https://github.com/Kotlin/anko/releases
a
tadam! It works! You, sir, are a hero 🙂 It was just an imageView which needed imageResource instead of passing it as a constructor parametre.
there's a hanging question on stackoverflow with exactly same problem, you can answer it and gain some rep points! (at least I will vote 😉 ) http://stackoverflow.com/questions/41687871/how-to-set-navigationviews-headerview-with-anko-dsl
m
Done. simple smile