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

Source: Internet
Author: User

Directory (?) [-]

    1. Home Icon
      1. Source
      2. TextView's scrolling
      3. Returns the main activity or specified activity

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

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 ());//You can scroll through the mouse when you set too much textview content
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 (); Gets the object of the Actionbar, from which the action bar is a property of the 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); Arrow symbols are not displayed, allowing arrows to be onoptionsitemselected (), but in general, you should give the user an arrow tip.
}

@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
CaseAndroid. R.id.home://For the user to press home icon, this example simply closes the activity and returns to the previous activity, 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.

[HTML]View Plaincopy
  1. <TextView android:id="@+id/textviewid"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:scrollbars="vertical"
  5. android:scrollbarstyle="Insideoverlay"
  6. android:scrollbarsize="25dip"
  7. android:scrollbarfadeduration="0"
  8. />

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–> Active (if we press the back key, we return b,a, and finally 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 by tracking the activity object, a new instance of mainactivity will be generated, not the original, and the original has been killed. For example, currently c-> m->a->b-> is currently open, if M is opened, it is promoted 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);
}

The example code covered in this blog post can be downloaded in Pro Android Learning: Actionbar Small example.

RELATED Links: My Android development related articles

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

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.