0

I want to built a simple android application that uses spinner and a button. The spinner contains two items. ITEM1->Google ITEM2-> Yahoo I want: If i select Item1(Google) from spinner and click the button, it redirects to google.com in WebView in android, and IF I select Item2(Yahoo) from spinner and click the same button, it redirects to yahoo.com.

Here is my xml code:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="158dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="72dp"
        android:onClick="onClick"
        android:text="@string/buttn" />

</RelativeLayout>

Value item to spinner:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="section">
        <item>Google</item>
        <item>Yahoo</item>

    </string-array>
</resources>

Java Code:

public class FirstPage extends ActionBarActivity implements OnItemSelectedListener
{


        Spinner sp;
        Button buttn;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.firstpage);
            buttn= (Button) findViewById(R.id.button1);
            sp = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<CharSequence> ar = ArrayAdapter.createFromResource(this, R.array.section, android.R.layout.simple_list_item_1);
            ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            sp.setAdapter(ar);

            addListenerOnButton();
        }


        private void addListenerOnButton() 
        {
            buttn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    try{
                    sp = (Spinner) findViewById(R.id.spinner1);
                    buttn= (Button) findViewById(R.id.button1);
                    }
                }
            });

        }

            public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {

            }

            public void onNothingSelected(AdapterView<?> 
            }
        }

I don't know how to do that. Anyone please help me out. Thanks.

Anonymous
  • 9
  • 4

2 Answers2

0

If you are asking how to start the browsing,and your listener is already working, then I would try this:

   Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    startActivity(browse);

You can also see more questions about your problem

Furthermore, the spinner does not do well with onClickListener, as see on this post , I would recommend to use another method, such as OnItemSelectedListener().

Community
  • 1
  • 1
Raphaël
  • 173
  • 11
  • Thanks for replying @Raphaël , My problem was with spinner not with browsing. Thanks anyway. – Anonymous Jan 16 '15 at 20:32
  • hey, can you please help me in one more thing? I want on selecting an item from spinner and clicking button takes me to a new activity where I have a webView. I want webpages to open in app's webview rather than in mobile browser. Thanks. – Anonymous Jan 16 '15 at 21:05
0

Do this

private void addListenerOnButton() 
{
    buttn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String val = sp.getSelectedItem().toString();
            String url = "";
            if(val.equalsIgnoreCase("google")){
                url = "http://www.google.com";
            else if (val.equalsIgnoreCase("yahoo"))
                url = "http://www.yahoo.com"; 

            if(url.length() > 0){
                Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                YourActivity.this.startActivity(browse);
            } 
        }
    });
}

EDIT

To open the url in your acitivity webview do this

Intent intent = new Intent(YourCurrentActivity.this, MyWebViewActivity.class); // Change the activity name as yours
intent.putExtra("url", url);
YourCurrentActivity.this.startActivity(intent); // Change the activity name as yours

And do this in next activity where webview is

String url = getIntent().getStringExtra("url");
WebView webView = (WebView) findViewById(R.id.web_view); // Change to your id
webView.loadUrl(url);
Rohit5k2
  • 16,859
  • 8
  • 38
  • 56
  • Thanks for answering. It helps me. – Anonymous Jan 16 '15 at 20:30
  • hey, can you please help me in one more thing? I want on selecting an item from spinner and clicking button takes me to a new activity where I have a webView. I want webpages to open in apps webview rather than in mobile browser. thanks. – Anonymous Jan 16 '15 at 21:01
  • Do i need to make two different activities having webView, one for google, and one for yahoo or it will run in same activity?? If in same then how will it selects item from spinner to browse? – Anonymous Jan 16 '15 at 21:51
  • Its same activity, no need of two different activities. See in my edited part I am passing a url to the web view activity which will access the url and open it. All you need to do is put the first part of edited code block in the`if(url.length() > 0)` statement (remove the old one). – Rohit5k2 Jan 16 '15 at 21:53