Android code segment 7 (1)

Source: Internet
Author: User

Preface

Here we have accumulated some uncommon and practical code, which is collected every seven updates, hoping to be useful to everyone.

 

Statement

You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com

Farmer's uncle: http://over140.cnblogs.com

 

Body 

1. Precisely obtain the screen size (for example, 3.5, 4.0, 5.0)

Public static double getScreenPhysicalSize (Activity ctx ){
DisplayMetrics dm = new DisplayMetrics ();
Ctx. getWindowManager (). getdefadisplay display (). getMetrics (dm );
Double diagonalPixels = Math. sqrt (Math. pow (dm. widthPixels, 2) + Math. pow (dm. heightPixels, 2 ));
Return diagonalPixels/(160 * dm. density );
}

Generally, a tablet is larger than 7 inches.

 

2. determine whether it is a tablet (official usage)

Public static boolean isTablet (Context context ){
Return (context. getResources (). getConfiguration (). screenLayout & Configuration. SCREENLAYOUT_SIZE_MASK)> = Configuration. SCREENLAYOUT_SIZE_LARGE;
}

 

3. Change the color of text based on the Status android: textColor

<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: color = "# 53c1bd" android: state_selected = "true"/>
<Item android: color = "# 53c1bd" android: state_focused = "true"/>
<Item android: color = "# 53c1bd" android: state_pressed = "true"/>
<Item android: color = "#777777" type = "codeph" text = "/codeph"/>
</Selector>

Put it in the res/color/directory

 

4. the background color is changed based on the status. android: backgroup

<Selector xmlns: android = "http://schemas.android.com/apk/res/android">

<Item android: state_selected = "true"> <shape>

<Gradient android: angle = "0" android: centerColor = "# 00a59f" android: endColor = "# 00a59f" android: startColor = "# 00a59f"/>
</Shape> </item>
<Item android: state_focused = "true"> <shape>
<Gradient android: angle = "0" android: centerColor = "# 00a59f" android: endColor = "# 00a59f" android: startColor = "# 00a59f"/>
</Shape> </item>
<Item android: state_pressed = "true"> <shape>
<Gradient android: angle = "0" android: centerColor = "# 00a59f" android: endColor = "# 00a59f" android: startColor = "# 00a59f"/>
</Shape> </item>
<Item> <shape>
<Gradient android: angle = "0" android: centerColor = "#00ff00" android: endColor = "00ff00" android: startColor = "00ff00"/>
</Shape> </item>

</Selector>

If the background color is directly given, an error is reported.

 

5. Start the default Activity of the APK

Public static void startApkActivity (final Context ctx, String packageName ){
PackageManager pm = ctx. getPackageManager ();
PackageInfo pi;
Try {
Pi = pm. getPackageInfo (packageName, 0 );
Intent intent = new Intent (Intent. ACTION_MAIN, null );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
Intent. setPackage (pi. packageName );

List <ResolveInfo> apps = pm. queryIntentActivities (intent, 0 );

ResolveInfo ri = apps. iterator (). next ();
If (ri! = Null ){
String className = ri. activityInfo. name;
Intent. setComponent (new ComponentName (packageName, className ));
Ctx. startActivity (intent );
}
} Catch (NameNotFoundException e ){
Log. e ("startActivity", e );
}
}

 

7. Calculate the word width

Public static float GetTextWidth (String text, float Size ){
TextPaint FontPaint = new TextPaint ();
FontPaint. setTextSize (Size );
Return FontPaint. measureText (text );
}

NOTE: If textStyle is set, you need to further set TextPaint.

 

 

 

 

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.