JAVA CODE: import android.app.Activity; import android.support.v7.app.AppCompatA
ID: 3731650 • Letter: J
Question
JAVA CODE:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity
{
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
findViewById (R.id.hi).setOnClickListener (new View.OnClickListener ()
{
@Override
public void onClick (View view)
{
Toast.makeText (MainActivity.
this,
"Calories Burned",
Toast.
LENGTH_LONG).
show ();}
});
findViewById (R.id.by).setOnClickListener (new View.OnClickListener ()
{
@Override
public void onClick (View view)
{
Toast.makeText (MainActivity.
this,
"Heart Rate",
Toast.
LENGTH_LONG).
show ();}
});
final CheckBox c1 = findViewById (R.id.checkbox);
final CheckBox c2 = findViewById (R.id.checkbox);
c1.setOnCheckedChangeListener (new CompoundButton.
OnCheckedChangeListener ()
{
@Override
public void
onCheckedChanged (CompoundButton
compoundButton,
boolean b)
{
if (c1.isChecked ())
{
c1.setText ("Weight");}
else
{
c1.setText ("Height");}
}
}
);
c2.setOnCheckedChangeListener (new CompoundButton.
OnCheckedChangeListener ()
{
@Override
public void
onCheckedChanged (CompoundButton
compoundButton,
boolean b)
{
if (c2.isChecked ())
{
c2.setText ("Height");}
else
{
c2.setText ("Weight");}
}
}
);
}
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
final ProgressBar simpleProgressBar =
(ProgressBar) findViewById (R.id.simpleProgressBar);
simpleProgressBar.setOnClickListener ();
}
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
editTxt = (EditText) findViewById (R.id.editText);
btn = (Button) findViewById (R.id.button);
list = (ListView) findViewById (R.id.listView);
arrayList = new ArrayList ("Road", "Track", "Gravel", "Other");
adapter =
new ArrayAdapter (getApplicationContext (),
android.R.layout.simple_spinner_item, arrayList);
list.setAdapter (adapter);
btn.setOnClickListener (new View.OnClickListener ()
{
@Override public void onClick (View view)
{
arrayList.add (editTxt.getText ().toString ());
adapter.notifyDataSetChanged ();}
});
}
public void onClick (View view)
{
DatePicker datePicker =
(DatePicker) dialogView.findViewById (R.id.date_picker);
TimePicker timePicker =
(TimePicker) dialogView.findViewById (R.id.time_picker);
Calendar calendar = new GregorianCalendar (datePicker.getYear (),
datePicker.getMonth (),
datePicker.getDayOfMonth (),
timePicker.getCurrentHour (),
timePicker.
getCurrentMinute ());
time = calendar.getTimeInMillis ();
alertDialog.dismiss ();
alertDialog.setView (dialogView);
alertDialog.show ();
}
}
-------------------------------------------------------------------------------------------
GIVEN THE JAVA CODE ABOVE CORRECT ALL ERRORS IN THE XML CODE PROVIDED BELOW
XML CODE:
<?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"
android:background="#ffffff"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calories Burned"
android:id="@+id/hi"
app:layout_constraintRight_toLeftOf="@+id/by"
app:layout_constraintLeft_toLeftOf="parent"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heart Rate"
android:id="@+id/by"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/hi"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/hi"
android:layout_marginTop="20dp"
android:id="@+id/rb">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="@+id/male"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/female"
app:layout_constraintTop_toBottomOf="@id/male"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Others"
app:layout_constraintTop_toBottomOf="@+id/female"
android:id="@+id/others"
/>
RadioGroup>
<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
tools:layout_editor_absoluteY="204dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c2"
app:layout_constraintTop_toBottomOf="@id/c1"
android:text="Height"
/>
<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="4"
android:layout_height="0dp" />
<TimePicker
android:id="@+id/time_picker"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
android:android.support.constraint.ConstraintLayout>
Explanation / Answer
JAVA CODE:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ProgressBar;
public class HW4 extends AppCompatActivity
{
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
findViewById (R.id.hi).setOnClickListener (new View.OnClickListener ()
{
@Override
public void onClick (View view)
{
Toast.makeText (HW4.
this,
"Calories Burned",
Toast.
LENGTH_LONG).
show ();}
});
findViewById (R.id.by).setOnClickListener (new View.OnClickListener ()
{
@Override
public void onClick (View view)
{
Toast.makeText (HW4.
this,
"Heart Rate",
Toast.
LENGTH_LONG).
show ();}
});
final CheckBox c1 = findViewById (R.id.checkbox);
final CheckBox c2 = findViewById (R.id.checkbox);
c1.setOnCheckedChangeListener (new CompoundButton.
OnCheckedChangeListener ()
{
@Override
public void
onCheckedChanged (CompoundButton
compoundButton,
boolean b)
{
if (c1.isChecked ())
{
c1.setText ("Weight");}
else
{
c1.setText ("Height");}
}
}
);
c2.setOnCheckedChangeListener (new CompoundButton.
OnCheckedChangeListener ()
{
@Override
public void
onCheckedChanged (CompoundButton
compoundButton,
boolean b)
{
if (c2.isChecked ())
{
c2.setText ("Height");}
else
{
c2.setText ("Weight");}
}
}
);
}
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
final ProgressBar simpleProgressBar =
(ProgressBar) findViewById (R.id.simpleProgressBar);
simpleProgressBar.setOnClickListener ();
}
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
editTxt = (EditText) findViewById (R.id.editText);
btn = (Button) findViewById (R.id.button);
list = (ListView) findViewById (R.id.listView);
arrayList = new ArrayList ("Road", "Track", "Gravel", "Other");
adapter =
new ArrayAdapter (getApplicationContext (),
android.R.layout.simple_spinner_item, arrayList);
list.setAdapter (adapter);
btn.setOnClickListener (new View.OnClickListener ()
{
@Override public void onClick (View view)
{
arrayList.add (editTxt.getText ().toString ());
adapter.notifyDataSetChanged ();}
});
}
public void onClick (View view)
{
DatePicker datePicker =
(DatePicker) dialogView.findViewById (R.id.date_picker);
TimePicker timePicker =
(TimePicker) dialogView.findViewById (R.id.time_picker);
Calendar calendar = new GregorianCalendar (datePicker.getYear (),
datePicker.getMonth (),
datePicker.getDayOfMonth (),
timePicker.getCurrentHour (),
timePicker.
getCurrentMinute ());
time = calendar.getTimeInMillis ();
alertDialog.dismiss ();
alertDialog.setView (dialogView);
alertDialog.show ();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.