Android Practical Programming Tips Code Summary _android

Source: Internet
Author: User
Tags add numbers stub

This example summarizes the practical Android programming techniques. Share to everyone for your reference, specific as follows:

1. Make a picture transparent:

Bitmap buffer = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_4444);
Buffer.erasecolor (color.transparent);

2. Send mail directly:

Intent Intent = new Intent (Intent.action_sendto, Uri. Fromparts ("mailto", "", null));
Intent.setflags (intent.flag_activity_new_task);
Context.startactivity (Intent);

3. Program control screen to lighten:

Windowmanager.layoutparams LP = GetWindow (). GetAttributes ();
lp.screenbrightness = 100/100.0f;
GetWindow (). SetAttributes (LP);

4. Filter specific text

Filter filter = Myadapter.getfilter ();
Filter.filter (Mysearchtext);

5.scrollView Scroll Stop Event

Setonscrolllistener (New Onscrolllistener () {public
void onscroll (abslistview view, int firstvisibleitem, int VisibleItemCount, int totalitemcount) {
//TODO auto-generated method stub  } public
void Onscrollstatechanged (abslistview view, int scrollstate) {
//TODO auto-generated method Stub
if (Scrollstate = 0 ) log.i ("A", "scrolling stopped ...");}  

6. Initiating an association for a particular program to open

Bitmap bmp = Getimagebitmap (jpg);
String path = Getfilesdir (). GetAbsolutePath () + "/test.png";
File File = new file (path);
FileOutputStream fos = new FileOutputStream (file);
Bmp.compress (compressformat.png, FOS);
 Fos.close ();
  Intent Intent = new Intent ();
  Intent.setaction (Android. Content. Intent.action_view);
  Intent.setdataandtype (Uri. FromFile (New File (path)), "Image/png");
  StartActivity (Intent);

There will be an error for the non applicable index format at the top of the picture.

Intent Intent = new Intent ();
Intent.setaction (Android. Content. Intent.action_view);
File File = new file ("/sdcard/test.mp4");
Intent.setdataandtype (Uri. FromFile (file), "video/*");
StartActivity (intent);
Intent Intent = new Intent ();
Intent.setaction (Android. Content. Intent.action_view);
File File = new file ("/sdcard/test.mp3");
Intent.setdataandtype (Uri. FromFile (file), "audio/*");
StartActivity (Intent);

7. Set Text appearance

Settextappearance (Context, Android.);
Android:textappearance= "Android:attr/textappearancemedium"?

8. Set up a separate initiation mode:

 android:name= ". Artistactivity "
 android:label=" Artist "
 android:launchmode=" Singletop ">

Intent i = new Intent ();
I.putextra (extra_key_artist, id);
I.setclass (this, artistactivity.class);
I.addflags (intent.flag_activity_single_top);
StartActivity (i);

9. Create a fillet picture

The main principle of this is to use the mask, first create a rounded box and then put the picture below:

Bitmap Mycoolbitmap = ...;
   int w = mycoolbitmap.getwidth (), H = mycoolbitmap.getheight ();
   Bitmap rounder = Bitmap.createbitmap (w,h,bitmap.config.argb_8888);
   Canvas Canvas = new Canvas (rounder);
   Paint xferpaint = new Paint (paint.anti_alias_flag);
   Xferpaint.setcolor (;
   Canvas.drawroundrect (New RECTF (0,0,w,h), 20.0f, 20.0f, xferpaint);
   Xferpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN));
And then what? Implement
Canvas.drawbitmap (Mycoolbitmap, 0,0, null);
Canvas.drawbitmap (Rounder, 0, 0, xferpaint);

10. Add numbers to the icon on the notification to indicate how many unread

Notification Notification = new Notification (icon, Tickertext, when);
Notification. Number = 4;

11. Background gradient:

First set up file Drawable/shape.xml

<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android
= " com/apk/res/ Android "android:shape=" Rectangle ">
  <gradient android:startcolor=" #FFFFFFFF "android:endcolor=" FFFF0000 "
      android:angle=" 270 "/>

Set the start color (startcolor), End color (endcolor), and angle (angle) of the gradient in this file

Then create a theme Values/style.xml

<?xml version= "1.0" encoding= "Utf-8"?> <resources> <style name= "Newtheme" parent= "
Android : Theme ">
<item name=" Android:background "> @drawable/shape</item>

The topic is then introduced in the application or activity in the Androidmanifest.xml file, such as:

<activity android:name= ". Shapedemo "Android:theme=" @style/newtheme ">

The same method applies to controls

<?php XML version= "1.0"?>
<message>invalid url.</message>
</ Response>

12. Storing data when you save static data in an instance, this example closes and the next instance wants to refer to static data as NULL, where you must rewrite applition

public class MyApplication extends application{
  private String thing = null;
  Public String getthing () {return
  public void setthing (String thing) {
   this.thing = thing;
public class MyActivity extends activity {
   private MyApplication app;
   public void OnCreate (Bundle savedinstancestate) {
     super.oncreate (savedinstancestate);
     App = ((MyApplication) getapplication ());
     String thing = app.getthing ();

More interested readers of Android-related content can view this site: Android Development Primer and Advanced tutorials, Android view summary, and Android layout layout tips, Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

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: 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.