I'm very new to code, and I met a lot of problems while building my first app. I want to create a drawer that I can swipe from left to right from anywhere to open. (not swipe from the edge) so the component tree of my MainActivity view is:
- DrawerLayout
- Navigationview (drawer layout)
- ConstraintLayout (mainpage layout)
THIS IS MainActivity.Java:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.view.GestureDetectorCompat;
import android.os.Bundle;
import android.view.GestureDetector;
import android.widget.TextView;
import android.view.MotionEvent;
public class MainActivity extends Activity {
private GestureDetectorCompat gestureObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureObject = new GestureDetectorCompat(this, new LearnGesture());
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureObject.onTouchEvent(event);
return super.onTouchEvent(event);
}
private class LearnGesture extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
if (event2.getX() > event1.getX()) {
}
else if (event2.getX() < event1.getX()){
Intent intent = new Intent(
MainActivity.this, UpcomingActivity.class);
finish();
startActivity(intent);
overridePendingTransition(R.anim.right_in,R.anim.left_out);
}
return true;
}
}
}
This is AndroidManifest.xml
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".UpcomingActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_upcoming"
android:screenOrientation="landscape"
android:theme="@style/FullscreenTheme" />
</application>
</manifest>
the code can run but has following render problems:
1.using the design library requires using Theme.AppCompat or a desecendant (Select Theme.AppCompat or a desecendant in the theme selector.) 2.Failed to instantiate one or more classes
Actually I even not so sure if I'm in the right direction, I don't know whether I should use fragment or drawerlayout to complete this activity ? Can somebody do me a favor or share with me some related tutorials?
Thanks in advance.