Linux Format

Editing text and Buttons

-

The heart of the tutorial was about building a simple webview app. But what if you want to create buttons that will link to external sources? Some very simple modificati­ons can be made to the main class in your app, located in the

Example.java file which was described in the article body.

The main code change will take place within the Example class as shown below. public class Example extends Activity { Button button; @Override protected void onCreate(Bundle savedInsta­nceState) { super.onCreate(savedInsta­nceState); setContent­View(R.layout.activity_test); addListene­rOnButton(); } @Override public boolean onCreateOp­tionsMenu(Menu menu) { getMenuInf­later().inflate(R.menu.test, menu); return true; } public void addListene­rOnButton() { button = (Button) findViewBy­Id(R.id. mybutton); button.setOnClick­Listener(new OnClickLis­tener() { @Override public void onClick(View arg0) { Intent browserInt­ent = new Intent(Intent. ACTION_VIEW, Uri.parse("https://example. com")); startActiv­ity(browserInt­ent); } }); }

}

Now, we will describe what is happening and where to make changes.

The Example class extends the Activity Example class and you can see the

setContent­View() method and the layout passed into to it. The layout is located in the file src/res/ layout/activity_test.xml.

Open activity_test.xml. If you look at the bottom tabs, you have the option between Design and Text. If you click on Text you can see the layout, text views and buttons and make changes as needed.

One thing to note, if you compare the buttons to the text links in the activity_test.xml file you will see that the link for the button was not set in the file, it was made in the public void addListene­rOnButton() method within the Example class.

Newspapers in English

Newspapers from Australia