Pro Android Learning Note ActionBar (1): Home icon Area

Source: Internet
Author: User

Pro Android Learning Note (48): ActionBar (1): Home icon AreaMarch 10, 2013 ? Integrated? A total of 3256 words? SizeXiao Zhong da ? Comments Off

Actionbar is introduced for tablets in the Android 3.0 SDK and can be used in phones in 4.0. There are three different forms of tab-and menu-like effects in title: Tabbed Action bar,list Action Bar and standard Action Bar, which we'll demonstrate in a small example.

Home Icon

At the far left of Action Bar is the area of home icon and title, such as The red circle. On the left side of home icon is a left arrow that returns, usually we click on this area and will return to the main activity of the app. The activity in the graph is triggered by the menu of the main activity, which has been used many times before and is not duplicated. The layout of the activity is simple, there is only one textview in LinearLayout, and its code is as follows:

Source Code

public class HomePressTestCase1 extends activity{
private TextView TV = null;
private ActionBar bar = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Setcontentview (r.layout.basic_test);
TV = (TextView) Findviewbyid (R.ID.TEXTVIEWID);
Tv.setmovementmethod (Scrollingmovementmethod.getinstance ()); //TextView can be scrolled by mouse when too much content is set
Tv.settext ("");
//After Android 4.0, the default is not to listen to the Home icon action, to use effectively, you must make the following settings
Bar = Getactionbar (); //Get Actionbar object, from this method also know that action Bar is a property of activity
Bar.setdisplayhomeasupenabled (TRUE);
Displays the arrow that is returned and listens through onoptionsitemselected () with a resource ID of android.r.id.home.
//Bar.sethomebuttonenabled (TRUE);//Do not display arrow symbols, allow arrows through onoptionsitemselected (), but in general, you should give an arrow to the user.
}

    @Override
    Public boolean Onoptionsitemselected (MenuItem Item) { 
        switch (Item.getitemid ( ) {
        case Android. R.id.home : //to the user by Home icon processing, this example simply close the activity, you can return to the previous activity, That is, the main activity.
            showinfo ("Home is Press");
            finish ();
            return true;
        Default:
             break;
       }
        return super.onoptionsitemselected (item);
   }

protected void Showinfo (String s) {
Tv.settext (s + "\ n" + tv.gettext ());
LOG.D (Getlocalclassname (), s);
}
}

TextView's scrolling

We can set the entire layout to ScrollView, but the TextView itself can be set to scroll, and the related properties in XML are as follows. and make it valid through setmovementmethod(scrollingmovementmethod.getinstance () ) in the code.

<textview android:id= "@+id/textviewid"      android:layout_width= "fill_parent"     android:layout_height= " Fill_parent "    android:scrollbars=" vertical "    android:scrollbarstyle=" Insideoverlay "    android: Scrollbarsize= "25dip"    android:scrollbarfadeduration= "0"    />

returns the main activity or specified activity

If the previous activity is not the primary activity and we have the hope of returning directly to the main activity, you can do the following:

public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case Android. R.id.home:
Showinfo ("Home is pressed");
Intent i = new Intent (this,mainactivity.class);
//"for Flag_activity_clear_top": assume that the current ACTIVITY's task stack is mainactivity–> a–> b–> Current activity (if we press the back key, we return to B,a and finally to mainactivity.) Now intent to evoke mainactivity and find that it exists on the stack, then it is clear that it and all before it, and after the new activity is aroused, the task stack is mainactivity. Note that bytracking activity of the object, will produce a new instance of Mainactivity, not the original, the original has been killed. Another example is the current c->
M->a->b-> Currently, if M is opened, it will be extended to c->m after execution.
i.addflags (intent.flag_activity_clear_top);
StartActivity (i);
return true;
Default:
Break
}
return super.onoptionsitemselected (item);
}

After Android 4.1 (API level 16), there is a more convenient way to set up the returned activity in Androidmanifest.xml, the fragment of XML is as follows:

<activity android:name= ". HomePressTestCase1 "android:label=" @string/test_case_1_home "
android:parentactivityname= "Cn.wei.flowingflying.pro.MainActivity"/>

We only need to set the Parentactivityname, the user presses the home icon area, does not need the manual code processing, can implement above by the set flag the intent code implementation. Java code can be further simplified:

  public boolean onoptionsitemselected (MenuItem item) {
        if (item.getitemid () = = Android. R.id.home) {
            showinfo ("Home is Press");
       }   
        return super.onoptionsitemselected (item);
    }

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.