Common android Methods

Source: Internet
Author: User

1. obtain resources: Resources res = getbasecontext (). getresources ();
Drawable draw = res. getdrawable (R. drawable. Icon );

2. Obtain the array: String [] ary = getresources (). getstringarray (R. array. ary );

3. Automatic prompt box:

Code

String [] arrays = new string [] {"A", "AB", "ABC", "BC", "bcde", "ee "};
Actalert = (autocompletetextview) findviewbyid (R. Id. actalert );
Arrayadapter <string> adapter = new
Arrayadapter <string> (
This,
Android. R. layout. simple_dropdown_item_1line,
Arrays );
Actalert. setadapter (adapter );

Mactalert = (multiautocompletetextview) findviewbyid (R. Id. mactvalert );
Mactalert. setadapter (adapter); // you can specify the delimiter between multiple values.
Mactalert. settokenizer (New multiautocompletetextview. commatokenizer ());

 

4. Set the data source for the spinner

 

Code

Spncolor. setprompt ("select ");
Arrayadapter <charsequence> Adapter
= Arrayadapter. createfromresource (
This, R. array. ATTR, Android. R. layout. simple_spinner_item );
Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
Spncolor. setadapter (adapter );

 

5. display the current time:

Simpledateformat SDF = new simpledateformat ("HH: mm: SS ");

// Display the current time in the textview component

Tvtime. settext ("current time:" + SDF. Format (New
Date ()));

 

6. Add layout files to the code

Code

Abslistview. layoutparams Lp = new abslistview. layoutparams (

Viewgroup. layoutparams. fill_parent, 64 );

Textview text = new textview (activity );

Text. setlayoutparams (LP );
Text. settextsize (20 );

Text. setgravity (gravity. center_vertical | gravity. Left );

Text. setpadding (36,
0, 0,
0 );
Text. settext (s );

 

7. Custom window title bar

 

Code

Requestwindowfeature (window. feature_custom_title); // custom title bar
Setcontentview (R. layout. Main );
// Set an XML layout for the title bar
Getwindow (). setfeatureint (window. feature_custom_title, R. layout. title_custom );

 

8. The activity is similar to dialog theme (adding icons and titles is actually the icon and titile of the activity)

(1) custom styles

<Style
Name = "theme. customdialog" parent = "Android: style/theme. Dialog">
<Item
Name = "Android: windowbackground"> @ drawable/filled_box </item>
</Style>

@ Drawable/filled_box: Create a drawable folder in the resource file and create a filled_box style XML file.

Code

<Shape
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Solid
Android: color = "# f0600000"/>
<Stroke
Android: width = "3dp" color = "# ffff8080"/>
<Corners
Android: radius = "3dp"/>
<Padding
Android: Left = "10dp" Android: Top = "10dp"
Android: Right = "10dp" Android: Bottom = "10dp"/>
</Shape>

(2) configure the acitivity style in manifest. xml

Code

<Activity
Android: Name = ". Main"
Android: Label = "@ string/app_name"
Android: theme = "@ Android: style/theme. Dialog">
<Intent-filter>
<Action
Android: Name = "android. Intent. Action. Main"/>
<Category
Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

(3) set icons for activity

Code

// Set the window mode (similar to the icon in the dialog, with a left icon)
Requestwindowfeature (window. feature_left_icon );

Setcontentview (R. layout. Main );
// Set image resources
Getwindow (). setfeaturedrawableresource (window. feature_left_icon, R. drawable. Icon );

9. From one activity1 to another activity2, when the second activity2 presses the return key, activity1 is disabled while activity2 is enabled.

Activity1

Intent intent = new intent (main. This, fowardwidget. Class );
Startactivity (intent );
Finish ();

10. Allow text value concatenation of textview

(1) Allow adding buffer text after textview's text value

TV. settext (TV. gettext (), textview. buffertype. editable );

(2) Add the buffer text using the Editable object

Editable text = (editable) TV. gettext ();

Text. append ("editable ");

Text. append ("/N "):

11. If the history stack contains an activity, open the activity and place it from the stack to the top of the stack rather than re-open the activity.

Intent intent = new intent (reorderfour. This, reordertwo. Class );
Intent. addflags (intent. flag_activity_reorder_to_front );
Startactivity (intent );

12. Avoid text blocking on the input method Panel. In manifest. XML, set Android: windowsoftinputmode in activity.

Android: windowsoftinputmode = "statevisible | adjustresize"

13. Get the current phone wallpaper and set the phone wallpaper (wallpaper)

(1) Get the current Wallpaper

Wallpapermanager WM = wallpapermanager. getinstance (this );
Drawable wallpaper = wpm. getdrawable ();

(2) set the current wallpaper and add the wallpaper setting permission.

Imapaper. setdrawingcacheenabled (true );
Drawable drawale = This. getresources (). getdrawable (R. drawable. bg );
Imapaper. setimagedrawable (drawale );
Wpm. setbitmap (imapaper. getdrawingcache ());

 

<Uses-Permission
Android: Name = "android. Permission. set_wallpaper"> </uses-Permission>

14. Common instantiation objects obtained through system services

Icationicationmanager Nm = (notificationmanager) getsystemservice (notification_service );

15. Check whether the network is connected

Code

// Check whether the network is connected
Publicboolean checkintent (){
Connectivitymanager mannager = (connectivitymanager)
This. getsystemservice (connectivity_service );
Networkinfo info = mannager. getactivenetworkinfo ();
If (Info = NULL |! Info. isconnected ()){
Returnfalse;
}
If (info. isroaming ()){
Returntrue;
}
Returntrue;
}

 

<Uses-Permission
Android: Name = "android. Permission. access_network_state"> </uses-Permission>

16. Read text documents from resource files (asset)

// Obtain the input stream
Inputstream in = getassets (). Open ("read_asset.txt ");
Int size = in. Available ();
// Read the input stream to the byte array (memory)
Byte [] buffer = newbyte [size];
In. Read (buffer );
In. Close ();
String text = new string (buffer );

17. Set textview, button, and other settings to scroll text. The control must obtain the focus to have the scroll effect, and the text length is greater than the control length.

Android: singleline = "true"
Android: ellipsize = "marquee"
Android: focusable = "true"
Android: marqueerepeatlimit = "marquee_forever" // marquee_forever: Always scroll down, n (integer): Scroll down n times

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.