Home > database >  Click using RadioButton
Click using RadioButton

Time:10-18

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

    <RadioGroup
        android:id="@ id/G1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <RadioButton
            android:id="@ id/R1"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="By Specialist" />

        <RadioButton
            android:id="@ id/R2"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="By Doctor"/>
    </RadioGroup>


</LinearLayout>

Fragment2.java
package com.example.tabbedapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class Fragment2 extends Fragment {
    public Fragment2(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);

        Intent intent = new Intent(getActivity(), Specialist.class);
        RadioButton button = (RadioButton) rootView.findViewById(R.id.R1);
        button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                startActivity(intent);
            }
        });
        return rootView;
    }

}

By clicking R1 I need to move to Specialist.class page, clicking R2 I need to move to Doctor.class page. How do I implement the code in java file? I have implemented the above code while clicking the RadioButton, it closes the app. Please Can anyone resolve my problem? Is there any mistake in Intent. enter image description here

CodePudding user response:

You can use like this

button.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this,  
            Specialist.class);
        }

Or

final RadioGroup group= (RadioGroup) findViewById(R.id.radioGroupId);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
    int id = group.getCheckedRadioButtonId();
    switch (id) {
        case R.id.r1:
            // Your code
            break;
        case R.id.r2:
            // Your code
            break;
        case R.id.r3:
            // Your code
            break;
        case R.id.r4:
            // Your code
            break;
        default:
            // Your code
            break;
    }
}});

CodePudding user response:

I hope, that your classes like Speacialist and Doctor are Activities, not Fragments, so probably you forgot to register your activities in the manifest.

   <activity
        android:name=".Specialist"/>
   <activity
        android:name=".Doctor"/>
  • Related