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 modifications 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 savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); addListenerOnButton(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.test, menu); return true; } public void addListenerOnButton() { button = (Button) findViewById(R.id. mybutton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent browserIntent = new Intent(Intent. ACTION_VIEW, Uri.parse("https://example. com")); startActivity(browserIntent); } }); }
}
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
setContentView() 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 addListenerOnButton() method within the Example class.