How to customize Navigationbar in Android

Source: Internet
Author: User

A Brief introduction to Navigationbar's background in how to control the display and hiding of Navigationbar in the Android system

The Navigationbar code is placed under the \frameworks\base\packages\systemui\ path. The project under this path is mainly responsible for the display of the system-level UI in the mobile phone, such as the display of the notification bar in the box, the USB connection, the screenshot and so on.


creation of Navigationbar

The code for Navigationbar is under the path of the Systemui project Systemui/src/com/android/systemui/statusbar/phone, where Navigationbar is made up of Created by the Phonestatusbar.java class. Under the Makestatusbarview () method of this class, you can see the process of creating a Navigationbar:

try {Boolean shownav = Mwindowmanagerservice.hasnavigationbar ();            M:support Smartbook Feature.            if (true) log.v (TAG, "hasnavigationbar=" + Shownav); if (shownav) {Mnavigationbarview = (Navigationbarview) view.inflate (context, R.LAYOUT.N                Avigation_bar, NULL);                Mnavigationbarview.setdisabledflags (mdisabled);                Mnavigationbarview.setbar (this); Mnavigationbarview.setontouchlistener (New View.ontouchlistener () {@Override public                        Boolean OnTouch (View V, motionevent event) {checkuserautohide (V, event);                    return false;            }}); }} catch (RemoteException ex) {//no window manager? Good luck with that} 
Windowmanagerservice to decide whether you need to create a navigationbarview by judging whether you need to display Navigationbar, Navigationbarview is the view that we see Navigation_bar is the Navigationbarview materialized layout, which you can find under the Layout folder under the Systemui project.

Customize the UI of the Navigationbar by modifying the way the Navigation_bar layout is done. There is such a class in the layout file. Com.android.systemui.statusbar.policy.KeyButtonView, which is a system-defined button class on the Navigationbar (which is discussed later), the effect of clicking on the ripple.

The Navigationbarview Master is responsible for initializing the UI, instantiating the layout, and taking the correct picture first based on the screen orientation.

event bindings for Navigationbar buttons

The event bindings on the Navigationbar button are not implemented in Navigationbarview, but in systemui/src/com/android/systemui/statusbar/phone/ The Phonestatusbar.java class is completed.

Complete the binding of the button by Navigationbarview the Get button interface provided externally:

Recent, Home, searchlight Binding of button events

private void Preparenavigationbarview () {        mnavigationbarview.reorient ();        Mnavigationbarview.getrecentsbutton (). Setonclicklistener (Mrecentsclicklistener);        Mnavigationbarview.getrecentsbutton (). Setontouchlistener (Mrecentspreloadontouchlistener);        Mnavigationbarview.gethomebutton (). Setontouchlistener (Mhomesearchactionlistener);        Mnavigationbarview.getsearchlight (). Setontouchlistener (Mhomesearchactionlistener);        Updatesearchpanel ();    }

Menu, Home, Back button event bindings:

The above three buttons are Keybuttonview classes, and their event response processes are done within the class itself. They respond to click events through the Ontouchevent () method,

public boolean ontouchevent (Motionevent ev) {final int action = Ev.getaction ();        int x, y;                Switch (action) {case Motionevent.action_down://slog.d ("Keybuttonview", "press");                Mdowntime = Systemclock.uptimemillis ();                Setpressed (TRUE);                if (Mcode! = 0) {sendevent (keyevent.action_down, 0, Mdowntime);                    } else {//provide the same haptic feedback the system offers for virtual keys.                Performhapticfeedback (Hapticfeedbackconstants.virtual_key);                    } if (msupportslongpress) {removecallbacks (mchecklongpress);                Postdelayed (Mchecklongpress, Viewconfiguration.getlongpresstimeout ());            } break;                Case motionevent.action_move:x = (int) ev.getx ();                y = (int) ev.gety (); setpressed (x >=-mtouchSlop && x < getwidth () + mtouchslop && y >=-mtouchslo                P && y < getheight () + mtouchslop);            Break                Case MotionEvent.ACTION_CANCEL:setPressed (FALSE);                if (Mcode! = 0) {sendevent (keyevent.action_up, keyevent.flag_canceled);                } if (msupportslongpress) {removecallbacks (mchecklongpress);            } break;                Case MotionEvent.ACTION_UP:final Boolean doIt = ispressed ();                Setpressed (FALSE);                        if (Mcode! = 0) {if (doIt) {sendevent (keyevent.action_up, 0);                        Sendaccessibilityevent (accessibilityevent.type_view_clicked);                    Playsoundeffect (Soundeffectconstants.click); } else {SendeVent (keyevent.action_up, keyevent.flag_canceled);                        }} else {//No key code, just a regular ImageView if (doIt) {                    PerformClick ();                }} if (msupportslongpress) {removecallbacks (mchecklongpress);        } break;    } return true; }

Mcode is used to determine which button the touch is from, and that the keycode of the different buttons are defined in the KeyEvent class. This value is obtained in the layout file by getting the Systemui:keycode property in Navigationbar_view, and the following is the corresponding code snippet in the layout file:

<com.android.systemui.statusbar.policy.keybuttonview android:id= "@+id/back"                android:layout_width= "@dimen /navigation_key_width "                android:layout_height=" match_parent "                android:src=" @drawable/ic_sysbar_back "                systemui:keycode= "4"                android:layout_weight= "0"                android:scaletype= "center"                Systemui: glowbackground= "@drawable/ic_sysbar_highlight"                android:contentdescription= "@string/accessibility_back"                />

in Ontouch, the method uses the Sendevent () method to perform different keycode response events, which creates a KeyEvent object encapsulation that contains KeyCode, and then passes the Injectinputevent () Inserts an event into the InputManager and sends it out.

How to customize Navigationbar in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.