Skip to main content

Create drop-down menu with image icons in Android

Hello devs!
In this tutorial, we will be creating a drop-down menu on clicking a button in Android studio.




Here is an image of how our menu is going to look:


On clicking the hamburger icon, a vertical menu will appear with image icons of your choice. Above image is just to get an idea of what we are going to develop.

Let's get started.

Step 1:
Open the layout file where you want the menu. Add the hamburger icon on an image button.

<ImageButton    android:layout_width="50dp"    android:layout_height="50dp"    android:src="@drawable/ham"    android:id="@+id/ham"    android:background="#00000000"/>

 Note: #00000000 specifies 'transparent' background.

Step 2:
Add a vertical linear layout for the menu. Inside that, add nested linear layouts for horizontal rows.
<LinearLayout    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    android:layout_below="@id/ham"    android:id="@+id/dropdown"    android:visibility="invisible"
    android:padding="2dp"    >

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"
      >        <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img1"            android:scaleType="centerCrop"/>
          <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img2"            android:scaleType="centerCrop"/>

        
    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"
      >        <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img3"            android:scaleType="centerCrop"/> 
         <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img4"            android:scaleType="centerCrop"/>
        
    </LinearLayout>
</LinearLayout>
And so on..

Note: The visibility is "invisible" so that it will be visible after clicking the hamburger icon.

Step 3:
Surround this inside a scroll view.
 <ScrollView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@id/ham">
<LinearLayout    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    android:layout_below="@id/ham"    android:id="@+id/dropdown"    android:visibility="invisible"
    android:padding="2dp"    >

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"
      >        <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img1"            android:scaleType="centerCrop"/>
          <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img2"            android:scaleType="centerCrop"/>

        
    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"
      >        <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img3"            android:scaleType="centerCrop"/> 
         <ImageButton            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@drawable/img4"            android:scaleType="centerCrop"/>
        
    </LinearLayout>
</LinearLayout>
</ScrollView>

Note: The layout of the menu should be added in the XML after the other contents of the layout so that menu is above the content and not below it.

Also, the layout_below is set to the ham.

Step 4:
Let's move on to the JAVA part.

Inside the Activity class declare the ImageButtons(all including ham icon) and LinearLayout(vertical) and in the onCreate method initialize it using its IDs (findViewById method).



Step 5:
Now we need to make the menu visible on clicking the ham icon and invisible on clicking it again. For that, refer the following code.


hambtn.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View view) {
        if(dropmenu.getVisibility()==View.VISIBLE)
        {   
            dropmenu.setVisibility(View.GONE);        }
        else {          
            dropmenu.setVisibility(View.VISIBLE);        }
    }
});

Step 6:
Set onClickListener for every ImageButton and specify an action for each. It may be opening an activity or simply making a Toast.

That's all folks! Hope you find this tutorial helpful.


This idea is inspired from :https://www.viralandroid.com/2016/01/android-custom-vertical-dropdown-icons-menu.html





T

Comments

Popular posts from this blog

How to add buttons and its functionality dynamically using HTML,MySQL and PHP?

There are times when we wish to add button dynamically in a webpage based on the entries in the database. If that is true in your case, please read on. (This guide assumes you have the basic knowledge of HTML, PHP and MySQL.) Following are the steps one will need to apply to create buttons dynamically: 1. Use PHP to connect to the database. <?php  $db = mysqli_connect('localhost','root','','media') //root is the username and no password.  or die('Error connecting to MySQL server.'); ?> Here, 'media' is the database name and it is running on WAMP. 2. Use a while loop to fetch contents from the database.     For each entry add a Button.   <form method="POST">  <?php $query = "SELECT * FROM post"; mysqli_query($db, $query) or die('Error querying database.'); $result = mysqli_query($db, $query); while ($row = mysqli_fetch_array($result)) { $varid=$row['postID...

Create HTML pages using python and open automatically on execution.

This process of creation of HTML pages using python is similar to process of creation of any other file. To automatically open the pages after execution, we will use selenium webdriver. Following are the steps: 1. Open any text editor and create a python file with .py extension. 2. Import selenium packages. (Make sure you download them first.) For downloading on Ubuntu, use these commands in terminal: $ sudo apt-get install python-pip $ sudo pip install selenium Import: from selenium import webdriver 3. Write the following to create HTML file on execution. f = open ( ' helloworld.html ' , ' w ' ) 4. In order to write in this file, use the following code: f.write( """ <!DOCTYPE HTML> < html > < header > < title > This is title </ title > </ header > < body > Hello world </ body > </ html > """) 5. Close command : f.close() 6. Use the...