I have done an immense amount of research and for whatever reason whatever I try I cannot get an ImageButton to be clickable in Android studio. I have tried numerous things but I must be missing something. I will past XML file below and then Java below that. When I put setOnClickListener method I get a cannot resolve message and same for when I do onClickListener. I would like the button to link to a webpage. Please help!
<?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.autismacademyed.www.autismacademy.AutismAcademy">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.173" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageButton
android:id="#+id/imageButtonYellow"
android:layout_width="109dp"
android:layout_height="125dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#null"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:srcCompat="#mipmap/ic_yellowpuzzlepiece"
android:onClick="onClick"/>
</android.support.constraint.ConstraintLayout>
Here is Java:
package com.autismacademyed.www.autismacademy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
ImageButton imageButtonYellow = (ImageButton)findViewById(R.id.imageButtonYellow);
imageButtonYellow.setOnClickListener(new View.onClickListener()
public void onClick (View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Remove the setOnClickListener since you already specify in your view that the onclick function for the button is onClick. To avoid confusion rename your button android:onClick="onClick", like android:onClick="imageButtonOnClick".
And in your java code you can just use this
package com.autismacademyed.www.autismacademy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
public void imageButtonOnClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Just copy and paste this two file will work for you. You declare two click listener that's why its not working.
xml file
<?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.autismacademyed.www.autismacademy.AutismAcademy">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.173" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageButton
android:id="#+id/imageButtonYellow"
android:layout_width="109dp"
android:layout_height="125dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#null"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:srcCompat="#mipmap/ic_yellowpuzzlepiece"
android:onClick="buttonClick"/>
</android.support.constraint.ConstraintLayout>
jAVAFILE
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
public void buttonClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Related
I am new to Android Studio. I am following a video tutorial to make a TODO app. I was following this video
https://www.youtube.com/watch?v=kpHVtSvdeOI
At 14:44, I typed "fab", and it is in red color. I tried "Show Context Actions" but nothing works. I followed the exact same steps that he did but I still have that problem.
Here is the code:
package com.example.medicinereminder;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import com.example.medicinereminder.Utils.DataBaseHelper;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class ReminderActivity extends AppCompatActivity {
private RecyclerView mRecyclerview;
private FloatingActionButton fab;
private DataBaseHelper myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
mRecyclerview = findViewById(R.id.recyclerView);
fab = findViewById(R.id.fab);
myDB = new DataBaseHelper(ReminderActivity.this);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
And a screenshot
Here is the XML code
<?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=".ReminderActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medicine Reminder List"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.042" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<Button
android:id="#+id/addBtn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:foreground="#drawable/plus"
android:text="Button"
app:backgroundTint="#00FDFDFD"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.977"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recyclerView"
app:layout_constraintVertical_bias="0.957" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you are using the Floating Action button(dependency needed) then there must be floating action in XML
then only this line work
private FloatingActionButton fab;
And use this instead of Button in XML
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_my_icon"
android:contentDescription="#string/submit"
android:layout_margin="16dp" />
But if you are using the normal button in XML
and you are giving
<Button
android:id="#+id/addBtn"
so you need to declare like this
private Button btnadd;
then to read that button you can do
btnadd = findViewById(R.id.addtn);
For better understand refer this
I've 3 activity in my app. I set setOnClickListener on all the imagebutton in all the 3 activity. When I run the application and on clicking the first button in the first activity the app crash, but if I comment out addListenerOnButton code in the second activity the app works just fine. Any help is appreciated
This is the code of MainActivity.java
package com.example.caa;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageButton;
import android.view.View;
public class MainActivity extends AppCompatActivity {
ImageButton maschio, femmina;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
maschio = (ImageButton) findViewById(R.id.Uomo);
femmina = (ImageButton) findViewById(R.id.Donna);
maschio.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(MainActivity.this, Scelta_Simbolo.class);
String genere = "maschio";
i.putExtra("genere", genere);
startActivity(i);
/*SERVE PER VEDERE DIRETTAMENTE SU SCHERMO UN MESSAGGIO
Toast.makeText(MainActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();*/
}
});
femmina.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(MainActivity.this, Scelta_Simbolo.class);
String genere = "femmina";
i.putExtra("genere", genere);
startActivity(i);
}
});
}
}
And 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"
android:background="#drawable/sfondo"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pagina 1 - TEST"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
<ImageButton
android:id="#+id/Donna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.826"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.661"
app:srcCompat="#drawable/woman" />
<ImageButton
android:id="#+id/Uomo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.793"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238"
app:srcCompat="#drawable/man" />
<TextView
android:id="#+id/Voce_Uomo"
android:layout_width="157dp"
android:layout_height="35dp"
android:text="Voce Maschile "
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.208"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.28" />
<TextView
android:id="#+id/Voce_Donna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Voce Femminile"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.218"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.629" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is second class code: Scelta_Simbolo.java (without comment on setOnClickListener so in this case crash)
package com.example.caa;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class Scelta_Simbolo extends MainActivity {
ImageButton si_no, su_giu, ok_nope;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scelta_simbolo);
addListenerOnButton();
}
public void addListenerOnButton() {
si_no = (ImageButton) findViewById(R.id.si_no_button);
// su_giu = (ImageButton) findViewById(R.id.su_giu_button);
// ok_nope = (ImageButton) findViewById(R.id.ok_nope_button);
Bundle receiveBundle = this.getIntent().getExtras();
final String receiveValue = receiveBundle.getString("genere");
si_no.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
/* Intent i = new Intent(Scelta_Simbolo.this, Ripr_Audio.class);
String scelta = "si_no";
i.putExtra("scelta", receiveValue);
startActivity(i);*/
}
});
/* su_giu.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(Scelta_Simbolo.this, Ripr_Audio.class);
String scelta = "su_giu";
i.putExtra("scelta", scelta);
startActivity(i);
}
});
ok_nope.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(Scelta_Simbolo.this, Ripr_Audio.class);
String scelta = "ok_nope";
i.putExtra("scelta", scelta);
startActivity(i);
}
});
*/
}
}
And the xml activity_scelta_simbolo.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"
android:background="#drawable/sfondo">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pagina 2 - TEST"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scegli quale tipologia di simboli vuoi utlizzare:"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.149" />
<ImageButton
android:id="#+id/si_no_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/textView2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.336"
app:srcCompat="#drawable/si_no" />
<ImageButton
android:id="#+id/su_giu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/si_no_button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/si_no_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.531"
app:srcCompat="#drawable/su_giu" />
<ImageButton
android:id="#+id/ok_nope_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/su_giu_button"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/su_giu_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.728"
app:srcCompat="#drawable/ok_nope" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thank you
I wanted to add Stopwatch in a fragment with bottom navigation activity. I got no error at all but the app crashes on onclicking the image buttons I have created for start pause and reset. Please Help!
package com.example.chatapp;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageButton;
public class StopwatchFragment extends Fragment {
private boolean running;
private long pauseoffset;
private Chronometer chronometer;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState ){
View view = inflater.inflate(R.layout.fragment_stopwatch, null);
chronometer = view.findViewById(R.id.cm);
return view;
}
public void startcm (View view){
if (!running){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseoffset);
chronometer.start();
running = true;
}
}
public void pausecm (View view){
if (running){
chronometer.stop();
pauseoffset = SystemClock.elapsedRealtime() - chronometer.getBase();
running = false;
}
}
public void resetcm (View view){
chronometer.setBase(SystemClock.elapsedRealtime());
pauseoffset = 0;
}
}
This is my java code of that fragment. Android Studio shows no error in this.
But here in my xml code of that fragment. In front of onclick it shows
cannot resolve symbol name "startcm"
cannot resolve symbol name "pausecm"
cannot resolve symbol name "restartcm"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".StopwatchFragment">
<TextView
android:id="#+id/text_stopwatch"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_stopwatch"
android:fontFamily="#font/bold"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center_horizontal" />
<Chronometer
android:id="#+id/cm"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="68dp"
android:fontFamily="#font/bold"
android:textSize="60sp"
android:textColor="#color/colorPrimary"
android:textAlignment="center"/>
<ImageButton
android:id="#+id/start"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_play_arrow_black_24dp"
android:background="#drawable/circular_button"
android:tint="#fff"
android:layout_marginTop="175dp"
android:layout_marginLeft="50dp"
android:onClick="startcm"/>
<TextView
android:id="#+id/text_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start"
android:layout_marginTop="225dp"
android:layout_marginLeft="60dp"
android:textColor="#color/black"
android:fontFamily="#font/regular"
/>
<ImageButton
android:id="#+id/pause"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_pause_black_24dp"
android:background="#drawable/circular_button"
android:tint="#fff"
android:layout_marginTop="175dp"
android:layout_marginLeft="150dp"
android:onClick="pausecm"/>
<TextView
android:id="#+id/text_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pause"
android:layout_marginTop="225dp"
android:layout_marginLeft="157dp"
android:textColor="#color/black"
android:fontFamily="#font/regular"
/>
<ImageButton
android:id="#+id/reset"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/restart"
android:background="#drawable/circular_button"
android:tint="#fff"
android:layout_marginTop="175dp"
android:layout_marginLeft="250dp"
android:onClick="resetcm"/>
<TextView
android:id="#+id/text_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_marginTop="225dp"
android:layout_marginLeft="258dp"
android:textColor="#color/black"
android:fontFamily="#font/regular"
/>
</RelativeLayout>
Please Help!!!
The xml onClick tag looks for your functions in your Activity, not in your fragment. Probably 3 solutions:
Move these functions to your Activity
Add view.findViewById(R.id.cmstart).setOnClickListener() to your fragment, and remove the xml tag.
You might can add a reference to your fragment in the xml tag, but I don't know how by heart.
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
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
I am trying to make a projects which reminds users for their vaccines. I have made some progress and have created birth date forms using EditText. I just can't seem to find how to save those strings to a file so when the user closes the app they are saved. Here is the code. There are two activities. I am trying to save dita1 muaji 1 and viti1 which are on the second activity.
---------------------------------First Activity----------------------------------------------------------------------------------
package al.programming.erlisciko.vaksinimi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyVaccines extends AppCompatActivity {
public Button vendosni_ditelindjen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_vaccines);
OnClickButtonListener();
}
public void OnClickButtonListener(){
vendosni_ditelindjen = (Button)findViewById(R.id.vendosni_ditelindjen);
vendosni_ditelindjen.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent set = new Intent ("al.programming.erlisciko.vaksinimi.SetDate");
startActivity(set);
}
}
);
}
}
----------------------------Second Activity-------------------------------------
package al.programming.erlisciko.vaksinimi;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class SetDate extends AppCompatActivity {
public Button ruani;
public EditText dita, muaji, viti;
public String dita1, muaji1, viti1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_date);
ruani = (Button)findViewById(R.id.ruani);
dita = (EditText)findViewById(R.id.dita);
muaji = (EditText)findViewById(R.id.muaji);
viti = (EditText)findViewById(R.id.viti);
ruani.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Log.v("dita1", dita.getText().toString());
Log.v("muaji1", muaji.getText().toString());
Log.v("viti1", viti.getText().toString());
}
});
System.out.print(dita1);
}
}
-------------------------------XML 1--------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="al.programming.erlisciko.vaksinimi.MyVaccines">
<Button
android:id="#+id/vendosni_ditelindjen"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:text="Vendosni Ditëlindjen"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.481"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.984" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ju lutem vendosni ditëlindjen"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
--------------------------------XML 2-------------------------------------------
<?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="al.programming.erlisciko.vaksinimi.SetDate">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vendosni Ditëlindjen"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.497" />
<EditText
android:id="#+id/dita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Dita"
android:inputType="date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.229" />
<EditText
android:id="#+id/muaji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Muaji"
android:inputType="date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.431" />
<EditText
android:id="#+id/viti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Viti"
android:inputType="date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.647" />
<Button
android:id="#+id/ruani"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Ruani Datën"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.914" />
</android.support.constraint.ConstraintLayout>
Thank You
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences
Write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Name", yourname);
editor.commit();
Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String name = sharedPref.getString("Name", "");