Skip to main content

Android Custom Horizontal Icons Menu

https://www.viralandroid.com/2016/01/android-custom-horizontal-icons-menu.html

Adding custom horizontal menu in android is tricky part but not much difficult. In this tutorial, I am going to show how to implement custom horizontal icons menu in android application with one of the best and easy ways. For this you not need to have good knowledge of java code.

To add custom icons menu in your android application here I am working with XML code and few java code also. Here I have used Buttons, ImageButtons, TextViews, RelativeLayout and LinearLayout in XML activity layout file and also created a XML drawable file inside res/drawable directory to change the background color of icons/buttons when user click/press the icon.

Related:
Android Custom Vertical Dropdown Icons Menu
Changing Background and Text Color of Android ActionBar Option Menu
Android Spinner Example

Android Example: How to Make Custom Horizontal Icons Menu in Android


Following is the complete necessary XML and Java code of different file.

XML Layout File


This is the main XML layout file where I have added text view, button and image buttons inside linear and relative layout.

res/layout/custom_horizontal_icons_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c2ec97"
android:padding="16dp"
tools:context="viralandroid.com.androidxmluserinterfacetutorial.MainActivity">
<Button
android:id="@+id/dropdown_custom_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5bace6"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="horizontalDropDownIconMenu"
android:padding="16dp"
android:text="DropDown\t"
android:textColor="#eee" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dropdown_custom_icon_menu"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Custom Horizontal Dropdown Menu Icons Horizontal Icon on Dropdown Android : Custom spinner with custom object! Break line after Icon in Menu Item Android
Google design guidelines Android Vertical ScrollBar Styling android popup menu with icons android custom popup menu example popup menu with icon in android example android popup menu style listpopupwindow" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Android Custom Horizontal Dropdown Menu Icons" />
</LinearLayout>
<LinearLayout
android:id="@+id/horizontal_dropdown_icon_menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dropdown_custom_icon_menu"
android:layout_marginTop="5dp"
android:background="#333"
android:orientation="horizontal"
android:padding="3dp"
android:visibility="invisible">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_send"
android:text="Android Custom Horizontal Dropdown Menu Icons" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_attach"
android:text="Android Custom Horizontal Dropdown Menu Icons" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_mail"
android:text="Android Custom Horizontal Dropdown Menu Icons" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_refresh"
android:text="Android Custom Horizontal Dropdown Menu Icons" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_attach"
android:text="Android Custom Horizontal Dropdown Menu Icons" />
</LinearLayout>
</RelativeLayout>


Java Activity File


In java activity file we are going to control on dropdown button click event to show and hide dropdown menu items when button is click.

src/HorizontalDropDownIconMenu.java
// Creating Android Custom Horizontal Dropdown Icons Menu Using XML
package viralandroid.com.androidxmluserinterfacetutorial;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
public class HorizontalDropDownIconMenu extends AppCompatActivity {
LinearLayout androidDropDownMenuIconItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_horizontal_icons_menu);
androidDropDownMenuIconItem = (LinearLayout) findViewById(R.id.horizontal_dropdown_icon_menu_items);
}
public void horizontalDropDownIconMenu(View view) {
if (androidDropDownMenuIconItem.getVisibility() == View.VISIBLE) {
androidDropDownMenuIconItem.setVisibility(View.INVISIBLE);
} else {
androidDropDownMenuIconItem.setVisibility(View.VISIBLE);
}
}
public void menuItemClick(View view) {
androidDropDownMenuIconItem.setVisibility(View.INVISIBLE);
}
}

XML Drawable File


Create a new XML file onclick_press_color.xml in res/drawable of your application project and add the following content to change the background color of dropdown items when users click on dropdown items.

res/drawable/onclick_press_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="@color/black" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="@color/light_black" />
</selector>

And following is the different color value of res/values/colors.xml file.

res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="light_black">#504f4f</color>
<color name="black">#000000</color>
</resources>
view rawcolors.xml hosted with ❤ by GitHub

Android Example: How to Make Custom Horizontal Icons Menu in Android

Now, run your Android Custom Horizontal Icons Menu application and click the dropdown button which will look like the above screenshot.

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap then to bytes. Also

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App §   2.4.7  Set the color §   2.4.8  Adding Animations o    2.5  To plot grouped bar chart §   2.5.1  Creating Dataset o    2.6  Get the best of Android plus exclusive deals and freebies in your inbox!