Dynamic addition of Android controls

Source: Internet
Author: User

Generally, the interface layout in Android is set in XML, that is, the number of elements on the interface cannot be changed in the program,

However, you can also dynamically add controls in the program.

Import Android. App. activity;

Import Android. content. context;

Import Android. Graphics. color;

Import Android. OS. Bundle;

Import Android. Text. layout;

Import Android. Text. format. dateformat;

Import Android. util. log;

Import Android. View. keyevent;

Import Android. View. viewgroup. layoutparams;

Import Android. widget .*;



Import java. util. calendar;



/**

* Test the dynamic use of the android Control

* @ Author gaolei by 20090827

*/

Public class fetion2009 extends Activity

{

/** Called when the activity is first created .*/

PB ssbar Pb; // progress bar control, but it is used to control and dynamically change its progress

// The background color of the chat conversation is separated.

Private Static final int [] BG = {color. White, color. Gray };

Private Static int bgindex = 0; // the background color of the chat conversation. The current color should be the index value in BG.



// The following layout parameter identifies the width and height of the current control. fill_parent = occupies all parent controls. wrap_content = only wraps the content in the control. // There are other functions such as left and right margins. Here we use the default

Private linearlayout. layoutparams lp_ff = new linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent );

Private linearlayout. layoutparams lp_fw = new linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. wrap_content );

Private linearlayout. layoutparams lp_ww = new linearlayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content );



@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );



// Scroll through the chat dialog box

Scrollview SV = new scrollview (this );

Sv. setlayoutparams (lp_ff );



Linearlayout layout = new linearlayout (this); // linear Layout

Layout. setorientation (linearlayout. Vertical); // The control is vertically arranged.

Layout. setbackgroundcolor (0xff00ffff); // you can specify a special color for the layout Board. This checks whether the color of the layout board is incorrect during the session!



// Enrich the chat page and test the Page scrolling effect, adding 10 repeated conversations

For (INT I = 0; I <10; I ++)

{

Setsendmsg (layout, this, getcurrcolor (), I + "chat content here .. ");

}



// Send File effect 1, ring progress bar, which is also the default Effect of progressbar

Setsendfile (layout, this, getcurrcolor (), "my photo .jpg ");



// Send File effect 2. The progress bar of the moment is also set to style = "? Android: ATTR/progressbarstylehorizontal "Effect

Setsendfile2 (layout, this, getcurrcolor (), "my photo .jpg ");



For (INT I = 0; I <10; I ++)

{

Setsendmsg (layout, this, getcurrcolor (), I + "chat content here .. ");

}

Sv. addview (layout); // Add the linear layout to scrollview.

Setcontentview (SV); // you can specify scrollview as the current page.

}



/**

* Obtain the background color of the current chat dialogue.

* @ Return background color of the current chat dialogue

*/

Private int getcurrcolor ()

{

Return BG [(++ bgindex) % BG. Length];

}



/**

* Add a chat content dynamically

* In order to simplify programming, put a person's description and content in a textview. You can split the design document into two textviews for display and font setting.

* @ Param layout textview control the target layout

* @ Param context: the required parameter for building a view control is the view control environment.

* @ Param bgcolur textview control background color

* @ Param MSG textview the actual text content of the control

*/

Private void setsendmsg (linearlayout layout, context, int bgcolur, string MSG)

{

Textview TV = new textview (context); // normal chat Dialog

// Obtain a global calendar instance, which is used to obtain the current system time and format it into hours: in the form of minutes. It is only used for testing. The time here should be provided by other programs

TV. settext ("someone says: [" + dateformat. Format ("KK: mm", calendar. getinstance () + "] \ n" + MSG );

TV. setbackgroundcolor (bgcolur );

Layout. addview (TV );

}



/**

* Dynamically Add a session entry for the Sending File

* As the sending progress bar and the cancel button are horizontally aligned to the method, you need to add a linearlayout

* @ Param layout the target layout to be added

* @ Param context: the required parameter for building a view control is the view control environment.

* @ Param bgcolur control background color

* @ Param MSG control refers to the actual text content

*/

Private void setsendfile (linearlayout layout, context, int bgcolur, string filename)

{

// Tell someone [time]

// All the file information to be sent must be drawn by setsendmsg!

Setsendmsg (layout, context, bgcolur, "sending" + filename );

// Horizontal arrangement two controls require a linearlayout. The default arrangement mode is horizontal arrangement.

Linearlayout mylayout = new linearlayout (context );

// You need to set the background color of the linearlayout control. Otherwise, the color of the main linearlayout is displayed, that is, 0xff00ffff.

Mylayout. setbackgroundcolor (bgcolur );



// Dynamically create a progressbar to add the default attribute to mylayout

Progressbar Pb = new progressbar (context );

PB. setlayoutparams (lp_ww );

Mylayout. addview (PB );



// Dynamically create a button to add the default attribute to mylayout

Button bt = new button (context );

Bt. setlayoutparams (lp_ww );

Bt. settext ("cancel ");

Mylayout. addview (BT );

// Add the horizontal layout linearlayout and all its controls to the master layout.

Layout. addview (mylayout );

}



/**

* Dynamically Add a session entry for the Sending File

* To ensure that the background color of the progressbar and button meets the design requirements, a linearlayout is added and its background color is set.

* @ Param layout the target layout to be added

* @ Param context: the required parameter for building a view control is the view control environment.

* @ Param bgcolur control background color

* @ Param MSG control refers to the actual text content

*/

Private void setsendfile2 (linearlayout layout, context, int bgcolur, string filename)

{

Setsendmsg (layout, context, bgcolur, "sending" + filename );



Linearlayout mylayout = new linearlayout (context );

Mylayout. setbackgroundcolor (bgcolur );

Mylayout. setorientation (linearlayout. Vertical); // The control is perpendicular to the control. The default value is horizontal.



// The default ssbar style is the ring type. Here, set her style to horizontal (horizontal line)

PB = new progressbar (context, null, Android. R. ATTR. progressbarstylehorizontal );

PB. setlayoutparams (lp_fw );

PB. setprogress (45); // set the 1st progress to 45

PB. setsecondaryprogress (0); // here we do not need 2nd Progress, so it is 0

Mylayout. addview (PB );



Button bt = new button (context );

Bt. setlayoutparams (lp_ww );

Bt. settext ("cancel ");

Mylayout. addview (BT );



Layout. addview (mylayout );

}



@ Override

Public Boolean onkeydown (INT keycode, keyevent event)

{

Log. D ("onkeydown:", "keycode =" + keycode + "keyevent =" + Event );

Switch (keycode)

{

Case keyevent. keycode_dpad_up:



Break;

Case keyevent. keycode_dpad_down:



Break;

Case keyevent. keycode_dpad_left:

// You can press the left button on the right to control the increase or decrease of the First progress.

PB. setprogress (PB. getprogress ()-5 );

Break;

Case keyevent. keycode_dpad_right:

PB. setprogress (PB. getprogress () + 5 );

Break;

Case keyevent. keycode_dpad_center:



Break;

Case keyevent. keycode_0:

Break;

}

Return super. onkeydown (keycode, event );

}

}

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