0

I have a fairly basic page which contains several buttons external websites which I want to be launched in the browser. What I am not sure is exactly how I should store the URL of the pages.

I am quite new to Android, so if I am taking the wrong approach please guide me.

I have several buttons like so:

<Button
    style="?android:attr/buttonBarButtonStyle"
    android:onClick="openUrlFromButton"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"             
    android:layout_weight=".3" 
    android:gravity="center_horizontal"
    android:id="@+id/btn_contact_page"
    android:text="@string/contact_page" />

My callback is like so:

public void openUrlFromButton(View view){       
    Uri uriUrl;

    switch(view.getId()){
        case R.id.btn_contact_facebook:
            uriUrl = Uri.parse("http://google.com/"); 
        default:
            uriUrl = Uri.parse("http://stackoverflow.com/"); 
    }
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);       
}

What I am unsure about, is how would be best to store the URL.

I'd like to be able to directly reference it in the same format as the ID used to access the button (i.e., from the view), or at least a consistent way that isnt hard coded in the callback (store it in the XML defining the button directly).

Can anyone point me to a proper way of achieving this?

Many thanks

xceph
  • 1,036
  • 2
  • 13
  • 28

1 Answers1

2

You can use a tag:

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

Use

android:tag="http://www.google.com"

in your Button, and get it in your method via

(String)view.getTag()
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134