Tek Eye Logo

Tek Eye

Copying Code from the Articles

This beginners article discusses copy and pasting code from a web page. Many of the articles on Tek Eye will have code listings, they will appear like this piece of code here (handling a button click in an Android App using an anonymous inner class).

public class main extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                Button btn = (Button)arg0;
                TextView tv = (TextView) findViewById(R.id.textview1);
                tv.setText("You pressed " + btn.getText());

When moving the pointer over the code it should change to a text cursor (a.k.a. caret). This allows the code to be selected. Highlight the code to copy it. Use the context menu (normally right-click) on the selected code and click Copy. The code is copied to the clipboard. It can then be pasted into the code editor of your favourite Integrated Development Environment (IDE).

Code in an IDE

Some programs may not correct the line breaks from the HTML copy:

Pasted Code in Word

This can be solved by either first using a program that does use the correct line breaks:

Pasted Code in Notepad

Or using the view source code option on the web page (again via the context menu), scrolling to the code and highlighting and copying it from web page source:

Copy HTML Source

Once the code is copied it should paste into the program correctly.

Correctly Pasted Code in Word

Code on a web page is easy to copy into a IDE using standard copy and paste. Some websites will use a more elaborate plug-in to display code which will have a small toolbar with a copy or view source option. This allows for the same copy and paste functionality.

Android Studio Imports

When Java code is pasted into Android Studio it may require extra import statements to be added to the import section of the class file. Unknown objects will be shown in red (default settings). If Studio is able to detect the required import a prompt is displayed. Press Alt and Enter to automatically add the correct import statement. Occasionally more than one object is shown from which the import is chosen. If in doubt use the Andorid Developer documentation to determine the correct object import.

Automatically Adding Imports in Android Studio

Author:  Published:  Updated:  

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.

Articles on:

Android, HTML, ↓markdown↓ CMS, VPS, Computing, Computer History