Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create an app with 3 radio buttons for pizza sauce choices: Tomato, BBQ, Sweet Chilli Use a textview to display a message such as your
Create an app with 3 radio buttons for pizza sauce choices: Tomato, BBQ, Sweet Chilli
Use a textview to display a message such as your sauce is tomato in response to a user clicking that radiobutton.
I have coded but I need some help executing it properly (using cases most likely). When the user selects tomato and bbq (for e.g) , both the radio buttons get coloured. The user should only be able to select one radio button.
//content_main.xml
xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintBottom_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <RadioButton android:id="@+id/tomato" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="252dp" android:layout_marginStart="26dp" android:layout_marginTop="49dp" android:text="Tomato" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <RadioButton android:id="@+id/bbq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="26dp" android:layout_marginTop="34dp" android:text="BBQ" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tomato" /> <RadioButton android:id="@+id/sweetchilli" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="26dp" android:layout_marginTop="47dp" android:text="SweetChilli" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/bbq" /> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="47dp" android:layout_marginEnd="36dp" android:layout_marginStart="36dp" android:text="" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tomato" /> android.support.constraint.ConstraintLayout>
//MainActivity.java
package com.user.qs2; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.view.View.OnClickListener; public class MainActivity extends AppCompatActivity { TextView textView; RadioButton tomato, bbq, sweetchilli; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=(TextView)this.findViewById(R.id.textView); tomato=(RadioButton)this.findViewById(R.id.tomato); bbq=(RadioButton)this.findViewById(R.id.bbq); sweetchilli=(RadioButton)this.findViewById(R.id.sweetchilli); tomato.setOnClickListener(myRadioListener); bbq.setOnClickListener(myRadioListener); sweetchilli.setOnClickListener(myRadioListener); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } private View.OnClickListener myRadioListener = new OnClickListener() { public void onClick(View v) { //if selected tomato, textview code //if selected bbq, textview code //if selected sweetchilli, textview code RadioButton rb = (RadioButton) v; textView.setText("Your sauce is: " + rb.getText()); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started