Android App - Radio Buttons, TextView, and Layout Options - java

I'm creating my first android app. It seems fairly simple. But I'm a total noob with Java and Android (I'm more familiar with C, C++ and the like). I'm sorry if this is the dumbest question ever. Anyway, I followed the steps on the android dev website.
The app is supposed to have the person enter their Name and click on 1st, 2nd, or 3rd shift radio buttons and when they click on Downtime Button, they'll be brought to another page (activity) that displays their name and the shift they picked and then displays another textbox and a time input.
So far, I got the MainActivity.java done like this:
package com.cyapps.downtime;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.cyapps.downtime.MESSAGE";
public void clickedButton1(View view) {
Intent intent = new Intent(this, WinderDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void clickedButton2(View view) {
Intent intent = new Intent(this, ClamperDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void clickedButton3(View view) {
Intent intent = new Intent(this, OtherDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and the activity_main.xml like this:
<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:orientation="horizontal" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="#string/edit_message" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edit_message"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="#string/radio_button1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton1"
android:layout_centerHorizontal="true"
android:text="#string/radio_button2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton2"
android:layout_centerHorizontal="true"
android:text="#string/radio_button3" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:text="#string/button_send1"
android:onClick="clickedButton1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_centerHorizontal="true"
android:text="#string/button_send2"
android:onClick="clickedButton2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="#string/button_send3"
android:onClick="clickedButton3" />
</RelativeLayout>
I now have another activity that shows up when you finish entering in your name and clicking on a shift. This page is supposed to show your name and the shift number and have a textbox to write some other stuff in it and a time input and a submit button. I know how to do buttons and I see the time input on the interface of Eclipse. But I don't understand how to make the radio buttons be able to be "submitted" and shown on the page and how to edit the activity to show certain stuff. I'm confused.
This is how the WinderDTActivity.java looks like:
package com.cyapps.downtime;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class WinderDTActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
}
And this is what the activity_winder_dt.xml looks like:
<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" >
</RelativeLayout>
Thank you so much in advance if you help me. You have no idea how grateful I'll be. I've been trying really hard to understand this, but I'm thoroughly confused on how to get xml and java working together. Please help!

You're already sending the contents of the text box to the second activity just fine, right? Then you only need to do two more things to send the radio button state through as well:
Implement onClick handlers for the radio buttons. You've already done that for the submit buttons, so I assume you've figured out onClick stuff. In the radio button onClick, set a variable in MainActivity.
Package the variable the radio flags set into the intent's extra data, like so:
intent.putExtra("radioButtonState", radioButtonState);
You can read the result back in your second activity using the appropriate intent.getxxxxExtra() function (getIntExtra if you saved an int, for example).

you have to not written Button b1=(Button)findViewById(R.id.Button1) in onCreate methid
this is the reason your are not getting button click event
similarly write code for button2 and button3

Related

Android Studio: cannot resolve symbol R.id.textView

I'm currently following the Android Studio "Build Your First App" tutorial (https://developer.android.com/training/basics/firstapp/starting-activity) and I can't seem to get the DisplayMessageActivity working. The variable "R.id.textView" doesn't seem to exist and I can't see any differences between the tutorial and my own code. I know this is just me being stupid somewhere but I can't pinpoint it.
Here's my code for DisplayMessageActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView); <-- Cannot resolve symbol 'textView'
textView.setText(message);
}
}
And MainActivity.java, which is trying to run it:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
When I try to run the app on the virtual device it doesn't crash, but the button which calls the DisplayMessage activity does nothing.
EDIT: As requested, the xml code for both activities:
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".DisplayMessageActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
I read the tutorial and I think they missed this part of code:
android:onClick="sendMessage"
so, the code should be like this:
<Button
android:id="#+id/button"
android:onClick="sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
and I think that's it, let me know if it helps
After looking more closely at #Vishal Naikawadi's comment, I realised what the problem was. R.id.textView is not some sort of variable, placeholder, or constant as I had assumed, it is literally getting the object with id "textView" (or so I think). I had accidentally changed the id of the object I was trying to reference in the visual editor, so there was no object with that id. I changed the id of the object back to the same as is referenced in the code and everything seems to be fine now.
Apologies to anyone whose time I've wasted with this, I'm really new to this concept in Java.

Android Studio MyFirstApp (sendMessage) issue [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 1 year ago.
I am new to this Java programming and android development and still just working with Hello World App while following a youtube video Series.
Youtube tutorial link
Need to change the interface from send Messeage Button press to Welcome Message screen.
My project have two Java claases
MainActivity.java
package com.example.helloworldapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Specify the onclick method for the button
// Access modifier must be public
// Method name is send Message
// Specify parameter and object of view class
//WHen user click the button system will invoke this method
//With in the method have to create newly created activity
//TO create a new activity have to create a object of intend
// intent is intention of doing something by the android application
// E.g. Start a new activity start a new service broadcast a message
public void sendMessage (View view){
//TO create a new activity have to create a object of intent
//Have to pass 2 parameter 1. context and 2. class name of the target activity
// Context name is this, target activity is MessageActivity
Intent intent = new Intent(this,MessageActivity.class);
//To start the activity have to call the method call start activity
//Then pass intent parameter
startActivity(intent);
}
}
MessageAcitivity.java
package com.example.helloworldapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:onClick="sendMessage"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="156dp"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="154dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="#string/button_label"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MessageActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/welcome_message"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to handle your button-click listener to navigate to the next screen.
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(v -> openMessageAcitivity());
}
public void openMessageAcitivity (View view){
String message = "Your message";
Intent intent = new Intent(this,MessageActivity.class);
intent.putExtra("STRING_YOU_NEED", message);
startActivity(intent);
}
}
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_YOU_NEED");
}
}
The button click not working could be solved via declaring sendMessage in the layout activity_main.xml in the Button code inserted android:onClick = "sendMessage"
If someone can elaborate on the above it will be very helpful since it is not mentioning in the tutorial itself. Onlick attribute in the layout has alrady as sendMessage as instructed in the tutorial.

Creating a Test/Questionnaire using Radio-Buttons - Android

I am making an android application that contains a test for depression. The test consists of 9 questions and each question has 4 possible answers. The answers are in the form of radio buttons. I need to assign numeric values to the radio buttons as follows:
Answer 1 = 0 Answer 2 = 1 Answer 3 = 2 Answer 4 = 3
At the end of the test the user will receive a result, 1-10 being likely not depressed and 10-27 being likely depressed.
Each question is asked on a separate page and the result page will contain the result which is the total of each answer added.
Can someone please make a suggestion as to how I should tackle this?
res/values/strings.xml code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="int1">0</integer>
<integer name="int2">1</integer>
<integer name="int3">2</integer>
<integer name="int4">3</integer>
</resources>
"Test1" xml code:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/white"
tools:context=".Test1">
<!-- title text -->
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="#string/D1title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- subtitle text -->
<TextView
android:id="#+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:text="#string/Q1Subtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- question 1 text -->
<TextView
android:id="#+id/txtQ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtSubTitle"
android:layout_marginTop="38dp"
android:text="#string/Q1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- radio group to allow for only one radio button selection -->
<RadioGroup
android:id="#+id/radioAnswers1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQ1" >
<!-- 1st answer radio button-->
<RadioButton
android:id="#+id/radioA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:checked="false"
android:text="#string/zero"
android:tag="#integer/int1" />
<!-- 2nd answer radio button-->
<RadioButton
android:id="#+id/radioA2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one"
android:tag="#integer/int2" />
<!-- 3rd answer radio button-->
<RadioButton
android:id="#+id/radioA3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/two"
android:tag="#integer/int3" />
<!-- 4th answer radio button-->
<RadioButton
android:id="#+id/radioA4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/three"
android:tag="#integer/int4" />
</RadioGroup>
<!-- next button -->
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/txtTitle"
android:text="#string/next" />
</RelativeLayout>
"Test1" java code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class Test1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
final RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()));
static int result;
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged (RadioGroup group, int checkedId){
final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioA1);
final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioA2);
final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioA3);
if (radioButton1.isChecked()) {
displayToast("No, not at all");
} else if (radioButton2.isChecked()) {
displayToast("On some days");
}else if (radioButton3.isChecked()) {
displayToast("On more than half the days");
}else {
displayToast("Nearly everyday");
}
}
});
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
checkedButton.getTag();
Intent intent = new Intent(getBaseContext(), Test2.class);
intent.putExtra("EXTRA_RESULT", result);
startActivity(intent);
openTest2();
}
});
}
//Open test page
public void openTest2() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test2.class);
startActivity(i);
}
public void displayToast(String text){
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
"Results1" 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:background="#color/white" >
<!-- Creating Results Title -->
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="#string/resultsTitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- Creating Results Subtitle -->
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="#string/subtitleR1" />
<!-- Creating Score Label -->
<TextView
android:id="#+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/txtResults"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="#string/score1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- Creating Results Text -->
<TextView
android:id="#+id/txtResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnNext"
android:layout_alignParentLeft="true"
android:text="#string/results1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- Creating Next Button -->
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="#string/next" />
</RelativeLayout>
"Results1" java code:
package com.lifematters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class Results1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results1);
final Button nextBtn = (Button) findViewById(R.id.btnNext);
final TextView score = (TextView) findViewById(R.id.txtScore);
int result;
result = getText(Test1.result1);
//Set up listener for Next
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
open101();
}
});
}
//Open test page
public void open101() {
//create new textview
Intent i = new Intent(getApplicationContext(), Depression101.class);
startActivity(i);
}
}
Rather than checking every single radio button to see which one is checked, you can get the only checked radio button from the checked ID.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId() ))
Then you don't need that checked change listener, you can get the value on nextBtn click. Of course, you may still want the checked change listener to see if one was selected or display the toast... but that is besides the point.
Part 2, how to convert that radio button to a numeric value. Really, there are many ways to do this. I think my preferred way would be an enum, but you could do any number of methods for string to int mapping, radio button id to int mapping, etc. You could even use tags. Looks like you have some commented code in there around tags. I would just set the tags in the xml, then use checkedButton.getTag() to get the value when you are ready to move on to the next page. Simple enough.
I would definitely recommend passing the results on to the next Activity through intent extras. Each new Test page can add the result from that page to the previous page and pass the new value on, then when you get to the result page, the total result score is already calculated. It scales nicely if the user starts clicking the back button to change answers.

How do I implement a popup media player in Android for an mp3 file?

I have made an app that has 20 or so mp3 files in the res\raw folder, and when a button is pressed, the media is played, the button can then be pressed again and the media is stopped.
This works well, but it does not look very nice, ideally, what I would like is a little popup player that appears in the centre of the screen with a seek bar, a pause button, and the name of the song that plays. (Very much like the Google Play Music app works when opening an mp3 file from a file browser). I know I could just get the button to open an intent to use Google Play Music, but I would like my app to have it's own colour scheme of a very similar looking version of the same popup.
The current Java code that I have for one of the songs is as follows:
package com.lmarshall1995.scoutsongs;
import com.lmarshall1995.scoutsongs.R;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class Song_AliceTheCamel_Activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.song_alicethecamel);
setupNavigationButton();
Toast toast = Toast.makeText(this, "To be sung in the tune of \n . . .", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
final Button back = (Button) findViewById(R.id.back_button);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
final Button previous = (Button) findViewById(R.id.previous_button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent previousIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_ZipADeeDooDah_Activity.class);
Song_AliceTheCamel_Activity.this.startActivity(previousIntent);
finish();
}
});
final Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent nextIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_Bingo_Activity.class);
Song_AliceTheCamel_Activity.this.startActivity(nextIntent);
finish();
}
});
final ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
play_tune.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
play_tune.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp = MediaPlayer.create(Song_AliceTheCamel_Activity.this, R.raw.tune_alicethecamel);
public void onClick(View arg0) {
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
}
});
}
});
}
private void setupNavigationButton() {}
}
And the current xml code that I have for one of the songs is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/song_alicethecamel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
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=".ScoutSongs" >
<TextView
android:id="#+id/Song_AliceTheCamel_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/Song_AliceTheCamel_Title"
android:textSize="20sp"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/play_tune"
android:src="#drawable/play_tune"
android:contentDescription="#string/play_tune"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/Song_AliceTheCamel_Title"
android:layout_alignTop="#+id/Song_AliceTheCamel_Title" />
<ScrollView
android:id="#+id/Song_AliceTheCamel_Lyrics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/back_button"
android:layout_alignLeft="#+id/Song_AliceTheCamel_Title"
android:layout_alignRight="#+id/play_tune"
android:layout_below="#+id/play_tune">
<TextView
android:id="#+id/Song_A_Lyrics1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/Song_AliceTheCamel_Lyrics"
android:textColor="#FFFFFF" />
</ScrollView>
<Button
android:id="#+id/back_button"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/Song_AliceTheCamel_Lyrics"
android:text="#string/back"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/previous_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/back_button"
android:layout_alignBottom="#+id/back_button"
android:layout_alignLeft="#+id/Song_AliceTheCamel_Lyrics"
android:text="#string/previous"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/next_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/previous_button"
android:layout_alignBottom="#+id/previous_button"
android:layout_toRightOf="#+id/previous_button"
android:text="#string/next"
android:textColor="#FFFFFF" />
</RelativeLayout>
but I would rather have it looking more like this:
Any help would be much appreciated as I know the source code for this is obviously going to be protected, At the moment, all I know is I need to make an xml file for the layout, and I think that would be a good start to ask for guidance.
Once I have this information, I would like to change the background to black rather than white, the seek bar to purple rather than orange, and change what Text is displayed to the song Title, rather than the file name.
Implement the audio player as a fragment which communicates with a background service through a messaging tool like the LocalBroadcastReceiver or Otto. Then in the application when the user wants to play a song they can select it and you can create a DialogFragment with the new Fragment you created. Thats how I've done this before.

How do i create the above view in my android application?

Because i'm create an app similar to what was shown below but i have no idea how to create a view similar to what was shown above after i click a buttton to navigate to this page where all the video and map log file is shown..
I'm kinna new in android/java can someone guide me on this?
EDIT: This is a series of code for creating a directory to store my video files but these files can only be seen outside the application but i wanted it to be seen in the application where when a button is pressed it navigates to the VideoList to browse the various videa file i have filmed and when press it display a custom screen seen here. But what are the things should be done to achieve this?
File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");
if(!(dirlist.exists()))
dirlist.mkdir();
File TempFile = new File(Environment.getExternalStorageDirectory() + "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());
Hay Zack its a custom Dialog check out this... code
main.xml(Your main page layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text.">
</TextView>
<Button android:layout_height="wrap_content"
android:layout_below="#+id/TextView01"
android:layout_width="wrap_content"
android:id="#+id/Button01main"
android:text="Hey! There is more..."></Button>
</RelativeLayout>
maindialog.xml(Your Dialog layout page)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_below="#+id/ImageView01"
android:layout_height="200px">
<TextView android:text="#+id/TextView01" android:id="#+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
<Button android:id="#+id/Button01" android:layout_below="#id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:text="Cancel" />
</RelativeLayout>
dilo.java(Display dialog on click button code)
package com.example.dilo;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class dilo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//set up dialog
final Dialog dialog = new Dialog(dilo.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.lots_of_text);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.icon);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
});
}
}
i think it should help you .... ok... N joy
zack it is custom dialog u need to create custom dialog
for more info.. go this link 1) link 2

Categories

Resources