简体   繁体   中英

an activity button to open navigation drawer

i want to start a normal activity with a Button inside and when this button pressed it will open navigation drawer.

i build default navigation drawer from android studio and created a normal activity with a button

i want this button to open the navigation drawer i changed the start activity from AndroidManifest.xml

AndroidManifest.xml

    <activity
        android:name=".MainActivity0"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

MainActivity0 the normal activity i created

public class MainActivity0 extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main0);
        button= findViewById(R.id.button0);


    }



    public void act(View view){
        Intent toFragment=new Intent(this, MainActivity.class);
        startActivity(toFragment);
    }

I did not understand your problem. But your source had the bug. like this

 public class MainActivity0 extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main0);
        button= findViewById(R.id.button0);
        View view;
       act(view);

    }



    public void act(View view){
        Intent toFragment=new Intent(this, MainActivity.class);
        startActivity(toFragment);
    }
}
  final DrawerLayout drawer = findViewById(R.id.drawer_layout);
   button= findViewById(R.id.button0);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            drawer.openDrawer(Gravity.LEFT);
        }
    });
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

but you should create all xml layouts first i need a full code let me knew and i will sent it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM