Open websites after selection is made via radio buttons

140 views Asked by At

Trying to open websites after they get selected via radio buttons and I am not having much success. Tried WebView but realized that I need to use fragments, and I am not very comfortable with how to do that. I am new to Java/android programming, so I suspect I am making many mistakes. Thanks in advance.

My MainActivity.Java is: -

package apps101.dcinfo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.net.Uri;



public class MainActivity extends Activity {

    RadioGroup rg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup rg = (RadioGroup) findViewById(R.id.infoDC);

        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.radioButton1:
                    public void openNews(View v) {
                        String url = "http://www.washingtonpost.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    }
                    break;
                case R.id.radioButton2:
                    public void openNews(View v) {
                        String url = "http://washington.org";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    };
                    break;
                case R.id.radioButton3:
                    public void openWeather(View v) {
                        String url = "http://www.weaather.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        startActivity(i);
                    };
                    Website = "http://www.weather.com";
                    break;
                case R.id.radioButton4:
                    Toast.makeText(MainActivity.this, "Ok. Have a great day!",
                            Toast.LENGTH_SHORT).show();
                    break;

                }
                };

        });

    }

    }

XML looks like below: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="apps101.dcinfo.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Greeting"
        android:textSize="22sp"
        android:textStyle="italic" />


    <RadioGroup
        android:id="@+id/infoDC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:textSize="30sp"
        android:textStyle="bold" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:text="@string/news"
            android:onClick="openNews" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:text="@string/tourist_attractions"
            android:onClick="openTourist" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:text="@string/weather"
            android:onClick="openWeather" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:text="@string/not_now" />
    </RadioGroup>


</RelativeLayout>

I am getting multiple syntax errors on MainActivity.java in the below lines..not sure how to proceed.. " public void openNews(View v) {"

1

There are 1 answers

1
Hadi Tok On

first thing I see is you need to remove method decleration in the switch cases like

                case R.id.radioButton1:
                    String url = "http://www.washingtonpost.com";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                    break;