Java how to open material navigation view on button click

Java how to open material navigation view on button click

To open a Material Navigation View on a button click in Android, you can use the openDrawer method of the DrawerLayout class.

Here is an example of how you can open a Material Navigation View on a button click in Android:

refer to‮ttual:‬uri.com
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        drawerLayout = findViewById(R.id.drawer_layout);
        button = findViewById(R.id.button);
        
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }
}

In this example, the MainActivity activity has a DrawerLayout and a Button view. The DrawerLayout is used to display the Material Navigation View, and the Button is used to open the Material Navigation View.

When the Button is clicked, the openDrawer method of the DrawerLayout is called with the START gravity to open the Material Navigation View. The DrawerLayout and the Button views are initialized in the onCreate method of the activity using the findViewById method.

Keep in mind that the DrawerLayout class and the openDrawer method are part of the Android Support Library, and you need to import them and add them to the dependencies of your project to use them in your Android app. You also need to include the Material Navigation View in your layout file and set the DrawerLayout as the root view of the layout.

Created Time:2017-11-03 23:27:11  Author:lautturi