0

Ok, so im pretty new to android and the whole app making but im just doing like a generic app rating thing just for practice. Here are the details I have just 2 generic pictures on the main activity with a rate button beside each and a text view below them both so when you click the rate button it takes you to the second activity which has the rating bar. This is my problem in the view with the rating bar I just have the display for the rating as a toast but i want to be able to take that rating and display it inside the textview in my main activity and its posing more of a problem than i thought it was going to so im looking for a bit of help

this is the code for my main activity

package com.example.brent.appmanagement;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


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

    ratingapp1();
    ratingapp2();
    getrating();
}
private void getrating(){

}
private void ratingapp1(){

    Button rateapp1 = findViewById(R.id.rateapp1);
    rateapp1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, firstapp.class));
        }
    });
}
private void ratingapp2(){
    Button rateapp2 = findViewById(R.id.rateapp2);
    rateapp2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, secondapp.class));
        }
    });
}
}

and this is the code for the activity with the rating bar

public class firstapp extends AppCompatActivity {


public RatingBar ratingBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_firstapp);
    ratingBar =  findViewById(R.id.ratingBar);
    return1();
}
public void rateMe(View view){

    Toast.makeText(getApplicationContext(),
            String.valueOf(ratingBar.getRating()), 
Toast.LENGTH_LONG).show();


}
private void return1(){
    Button returntomain1 = findViewById(R.id.returntomain1);
    returntomain1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(firstapp.this, MainActivity.class));
        }
    });
}

}

also I have the XML which i dont know if you need to see but here it is

main activity

<?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"
tools:context="com.example.brent.appmanagement.MainActivity">

<Button
    android:id="@+id/rateapp1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="96dp"
    android:layout_marginTop="124dp"
    android:text="Rate"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/rateapp2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="176dp"
    android:layout_marginEnd="96dp"
    android:text="Rate"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="96dp"
    android:layout_marginTop="112dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="164dp"
    android:layout_marginStart="96dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/savedrating1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="96dp"
    android:layout_marginTop="12dp"
    android:text=""
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView3" />

<TextView
    android:id="@+id/savedrating2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="96dp"
    android:layout_marginTop="12dp"
    android:text=""
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView4" />
</android.support.constraint.ConstraintLayout>

and the second activity

<?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"
tools:context="com.example.brent.appmanagement.firstapp">

<Button
    android:id="@+id/returntomain1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:text="Return to main menu"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/rate_me"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Rate Me"
    android:textSize="18dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rate_me"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:numStars="5"
    android:stepSize="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/rate_me" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ratingBar"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:onClick="rateMe"
    android:text="Submit"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ratingBar" />
</android.support.constraint.ConstraintLayout>

I would like to have the rating from the rating bar inside firstactivity be displayed in the textview savedrating1 inside my main activity

thanks all in advance for the help

Brent
  • 740
  • 1
  • 5
  • 11

1 Answers1

0

for primitive values you can stuff them inside the intent using key value pairsIntent intent =new Intent(); String myValue="something";intent.putExtra("keyMyValue",myValue);startActivity(intent); for passing custom objects use parcelable and stuff the parcelable into the intent as above or here

Pomagranite
  • 696
  • 5
  • 11
  • i get that and i have been trying that way too but every i try to do it that way like i declare Textview firstsavedrating in my main then i find that by the id for my textview i want to display it in then i say firstsavedrating.settext(string.valueof(ratingBar.getrating())) or something like that my app wont even open it just stops and i get a null pointer exception obviously i just dont know what im doing wrong – Brent Dec 23 '17 at 03:21