Issue
I'm still newbie in Android. Help me to create sliding menu from imagebutton I've create. This is my app_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/white"
android:orientation="vertical"
android:gravity="center"
android:paddingBottom="7dp">
<ImageView
android:id="@+id/home"
android:layout_width="125dp"
android:layout_height="40dp"
android:layout_alignBottom="@+id/menuButton"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:layout_marginStart="100dp"
android:scaleType="centerInside"
android:src="@drawable/header"/>
<ImageButton
android:id="@+id/menuButton"
android:layout_width="46dp"
android:layout_height="60dp"
android:layout_alignBottom="@+id/btnLogin"
android:layout_alignParentStart="true"
android:layout_marginStart="45dp"
android:background="@color/white"
android:scaleType="centerInside"
android:onClick="showDrawer"
app:srcCompat="@drawable/menu" />
<ImageView
android:id="@+id/btnLogin"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_marginStart="-80dp"
android:layout_toEndOf="@+id/btnRegister"
android:scaleType="centerInside"
app:srcCompat="@drawable/login" />
<ImageView
android:id="@+id/btnRegister"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="80dp"
android:scaleType="centerInside"
app:srcCompat="@drawable/register" />
<ImageView
android:id="@+id/searchView"
android:layout_width="325dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/border"
android:iconifiedByDefault="false"
android:padding="10dp"
android:queryHint="Search"
android:scaleType="fitEnd"
android:src="@drawable/search_icon"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInput"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="0dp"
android:layout_marginStart="60dp"
android:paddingBottom="3dp"
android:textColorHint="@color/black">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="16dp"
android:background="@color/transparant"
android:hint="Search"
android:maxLines="1"
android:paddingBottom="10dp"
android:textSize="13dp" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
and here my MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun showDrawer(v: View) {
val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
val slide = true
if (slide) {
drawer.openDrawer(Gravity.LEFT)
} else {
drawer.closeDrawer(Gravity.LEFT)
}
}
}
When I run it, the app is suddenly crashed when I clicked the imagebutton. Here is the log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fixpage, PID: 26814
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6891)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6891)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: kotlin.TypeCastException: null cannot be cast to non-null type android.support.v4.widget.DrawerLayout
at com.example.fixpage.MainActivity.showDrawer(MainActivity.kt:25)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6891)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
And, I need help in the code use kotlin. I don't know how to create slide menu use image button. Also, I don't use action bar. Thanks in advance.
Solution
Try with extending only Activity
like below -
By extending this class your ActionBar
will be hidden.
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun showDrawer(v: View) {
val drawer = findViewById(R.id.drawer_layout) as DrawerLayout
val slide = true
if (slide) {
drawer.openDrawer(Gravity.LEFT)
} else {
drawer.closeDrawer(Gravity.LEFT)
}
}
}
Answered By - Al-Amin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.