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
Related
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at com.example.books.MainActivity.onOptionsItemSelected(MainActivity.java:245)
The terminal shows that the error is with the onOptionsItemSelected function
I have referred many StackOverflow posts regarding errors with side navigation drawer but I couldn't find the solution
Logcat
2022-05-08 17:13:15.175 867-867/com.example.books E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.books, PID: 867
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at androidx.drawerlayout.widget.DrawerLayout.openDrawer(DrawerLayout.java:1736)
at androidx.drawerlayout.widget.DrawerLayout.openDrawer(DrawerLayout.java:1722)
at androidx.appcompat.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:287)
androidx.appcompat.app.ActionBarDrawerToggle.onOptionsItemSelected(MainActivity.java:245)
at com.example.books.MainActivity.onOptionsItemSelected(MainActivity.java:245)
at android.app.Activity.onMenuItemSelected(Activity.java:4324)
at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:352)
at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected
at androidx.appcompat.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:188)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30250)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/dl">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SearchView
android:id="#+id/searchView"
android:layout_width="277dp"
android:layout_height="34dp"
android:layout_alignParentTop="true"
android:background="#drawable/bg_white_rounded"
android:iconifiedByDefault="false"
android:queryHint="Search book,title,author, etc.."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.035"
app:queryBackground="#drawable/bg_white_rounded"
app:submitBackground="#drawable/bg_white_rounded"
tools:ignore="MissingConstraints">
</SearchView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:contentPadding="#dimen/cardview_default_elevation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131">
<ImageView
android:id="#+id/idIVCourseImage5"
android:layout_marginLeft="10dp"
android:layout_width="76dp"
android:layout_height="100dp"
android:src="#drawable/bookdonation" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.136"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131">
<ImageView
android:id="#+id/idIVCourseImage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:clickable="true"
android:src="#drawable/onlinepurchase" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView3"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.865"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131" >
<ImageView
android:id="#+id/idIVCourseImage2"
android:layout_marginTop="10dp"
android:layout_width="96dp"
android:layout_height="80dp"
android:src="#drawable/prouddonars" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FICTION AND NON-FICTION BOOKS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.115"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.519" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idRVCourse"
android:layout_width="403dp"
android:layout_height="333dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
tools:ignore="MissingConstraints">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/inter_medium"
android:text="Purchase books"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.122"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/inter_medium"
android:text="Donate books"
android:textSize="12sp"
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.276"
android:clickable="true"/>
<TextView
android:id="#+id/textView11"
android:layout_width="89dp"
android:layout_height="17dp"
android:fontFamily="#font/inter_medium"
android:text="Proud donars"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.869"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.275" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="407dp"
android:layout_height="148dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.401"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="99dp"
android:layout_height="99dp"
android:src="#drawable/medical"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="MEDICAL HEALTH AND SCIENCE BOOKS"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Biochemistry| Physiotherapy | Phathalogy"></TextView>
<Button
android:id="#+id/buttonmed"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order "
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:layout_marginBottom="10dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/fictionandnonfiction"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="EXPLORE THE WIDE RANGE OF FICTION AND NON FICTION"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Science Fiction| Short stories | Teens"></TextView>
<Button
android:id="#+id/buttoneng"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/ncert"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="NCERT BOOKS FOR CLASS 1-12"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Physics| Chemistry | Mathematics"></TextView>
<Button
android:id="#+id/buttonfiction"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/competative"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text=" WIDE RANGE OF AVAILABLE COMPITATIVE EXAM BOOKS"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Banking| UPSC| SSC"></TextView>
<Button
android:id="#+id/buttonfinance"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.777"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:srcCompat="#drawable/ic_favorite" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="36dp"
android:layout_height="29dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.045"
app:srcCompat="#drawable/cart1" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="32dp"
android:layout_height="27dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/linearLayout"
app:layout_constraintHorizontal_bias="0.889"
app:layout_constraintStart_toStartOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:srcCompat="#drawable/ic_location" />
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idnav_bar"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
tools:ignore="InvalidId,MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Mainactivity.java
package com.example.books;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationView;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageClickListener;
import com.synnapps.carouselview.ImageListener;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.view.MenuItem.OnMenuItemClickListener;
import java.util.Objects;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button medbutton;
private RecyclerView courseRV;
public DrawerLayout dl;
private Toolbar toolbar;
public ActionBarDrawerToggle abdt;
public TextView TVdonars;
public TextView TVdonatebooks;
public TextView TVpurchasebooks;
public ImageView wishlistimg;
private ArrayList<CourseModel> courseModelArrayList;
private int[] mImages = new int[]{
R.drawable.academic, R.drawable.engineering, R.drawable.medical, R.drawable.fiction
};
private String[] mImagesTitle = new String[]{
"Academic", "Engineering", "Medical", "Fiction"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Classic Books Donar");
medbutton = findViewById(R.id.buttonmed);
wishlistimg = findViewById(R.id.imageView5);
//button = (Button) findViewById(R.id.button2);
courseRV = findViewById(R.id.idRVCourse);
TVpurchasebooks = findViewById(R.id.textView9);
TVdonatebooks = findViewById(R.id.textView10);
TVdonars = findViewById(R.id.textView11);
TVdonatebooks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Register.class);
startActivity(intent);
}
});
TVdonars.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Prouddonars.class);
startActivity(intent);
}
});
TVpurchasebooks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Purchasebooks.class);
startActivity(intent);
}
});
final NavigationView nav_view = (NavigationView) findViewById(R.id.idnav_bar);
nav_view.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.select_catogery) {
Toast.makeText(MainActivity.this, "Select Catogery",
Toast.LENGTH_SHORT).show();
}
if (id == R.id.engineering) {
Toast.makeText(MainActivity.this, "Engineering", Toast.LENGTH_SHORT).show();
}
if (id == R.id.medical) {
Toast.makeText(MainActivity.this, "Medical", Toast.LENGTH_SHORT).show();
}
return true;
}
});
dl = (DrawerLayout) findViewById(R.id.dl);
abdt = new ActionBarDrawerToggle(this, dl, toolbar, R.string.open, R.string.close);
abdt.setDrawerIndicatorEnabled(true);
//Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
dl.addDrawerListener(abdt);
abdt.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
courseModelArrayList = new ArrayList<>();
courseModelArrayList.add(new CourseModel("Design of Machine Elements, Fourth Edition", "V
B Bhandari", R.drawable.designofmachineelements));
courseModelArrayList.add(new CourseModel("Fluid Mechanics, Sixth Edition", "Munsun,
Okilshi,Huebsch & Rothmayer", R.drawable.fluidmechanics));
courseModelArrayList.add(new CourseModel("Fundamentals of Thermodynamics", "Claus
Borgnakke & Richard E Sonntag", R.drawable.thermodynamics));
courseModelArrayList.add(new CourseModel("Shigley's Mechanical Engineering Design",
"Richard G Budynas & JJ Keith Nisbett", R.drawable.mechanicalengineeringdesign));
courseModelArrayList.add(new CourseModel("Atlas of Human Anatomy, Seventh Edition", "Frank
H. Netter, MD ", R.drawable.atlasofhumananatomy));
courseModelArrayList.add(new CourseModel("Data structures and algorithms", " Alfred V.Aho,
John E.Hopcroft, Jeffrey D.Ullman", R.drawable.datastructures));
courseModelArrayList.add(new CourseModel("Textbook of Clinical Neuroanatomy, Third
Edition", "Vishram Singh ", R.drawable.clinicalneuroanatomy));
// we are initializing our adapter class and passing our arraylist to it.
CourseAdapter courseAdapter = new CourseAdapter(this, courseModelArrayList);
// below line is for setting a layout manager for our recycler view.
// here we are creating vertical list so we will provide orientation as vertical
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
// in below two lines we are setting layoutmanager and adapter to our recycler view.
courseRV.setLayoutManager(linearLayoutManager);
courseRV.setAdapter(courseAdapter);
medbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Medical.class);
startActivity(intent);
}
});
wishlistimg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Wishlist.class);
startActivity(intent);
}
});
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (abdt.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
So I have a settings fragment which contains, well, settings.
On line 22 I have a findview which finds a textview. It worked wonders until today (Didn't change a thing). Now I imagine it will also fail the rest of the finds but since it crashes on this one, we will never know.
Why is that?
XML file:
<?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">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="ACCOUNT"
android:textColor="#2b2a29"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/SettingsFragmentAccountSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Account Settings"
android:textColor="#2b2a29"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="THEME"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentAccountSettings" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Dark Mode"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<Switch
android:id="#+id/SettingsFragmentDarkModeSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:scaleX="1.25"
android:scaleY="1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentAccountSettings" />
<TextView
android:id="#+id/r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="OTHER"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentDarkModeSwitch" />
<TextView
android:id="#+id/SettingsFragmentFAQ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="FAQ"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/r" />
<TextView
android:id="#+id/SettingsFragmentSupport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Support"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentFAQ" />
<TextView
android:id="#+id/SettingsFragmentPolicy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Content Policy & EULA"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentSupport" />
<TextView
android:id="#+id/SettingsFragmentUSerAgreement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="User Agreement"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentPolicy" />
<TextView
android:id="#+id/SettingsFragmentBugReport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Report A Bug"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentUSerAgreement" />
<TextView
android:id="#+id/Build"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="BUILD INFORMATION"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentBugReport" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="1.0.0"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.042"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/Build" />
</androidx.constraintlayout.widget.ConstraintLayout>
The fragment code:
package com.example.create4me;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Settings extends Fragment {
TextView accSettings, FAQ, BugReport, Policy, UserAgg, Support;
Switch darkModeSwitch;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
accSettings = (TextView) getView().findViewById(R.id.SettingsFragmentAccountSettings); <--- This one
FAQ = (TextView) getView().findViewById(R.id.SettingsFragmentFAQ);
BugReport = (TextView) getView().findViewById(R.id.SettingsFragmentBugReport);
Policy = (TextView) getView().findViewById(R.id.SettingsFragmentPolicy);
UserAgg = (TextView) getView().findViewById(R.id.SettingsFragmentUSerAgreement);
Support = (TextView) getView().findViewById(R.id.SettingsFragmentSupport);
darkModeSwitch = (Switch) getView().findViewById(R.id.SettingsFragmentDarkModeSwitch);
accSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), AccountSettings.class);
startActivity(intent);
}
});
BugReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), BugReport.class);
startActivity(intent);
}
});
FAQ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
Policy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
UserAgg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
darkModeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*
In here, put 0 into MySqli DB if the isChecked value = False.
Put 1 into MySqli DB if the isChecked value = 1.
*/
}
});
return inflater.inflate(R.layout.settings, container, false);
}
void alertDiag(){
new AlertDialog.Builder(getContext())
.setTitle("Well here we are")
.setMessage("Normally, the user by now would've gone into a website explaining the content of the pressed TextView.\n"
+ "Since we do not have a website or a domain, we show this AlertDialog. Sorry for the turnout!")
.setNegativeButton(android.R.string.no, null)
.show();
}
}
You need to do on the first line of onCreateView:
View view = inflater.inflate(R.layout.settings, container, false);
Then do for each of your findViewById:
view.findViewById(R.id.your_view);
You're inflating the view last instead of first.
Then at the end of onCreateView():
return view;
Please try below way to initialize and return in your oncreateview
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.settings, container, false);
//Init all here like
accSettings = (TextView) view. findViewById(R.id.SettingsFragmentAccountSettings)
//and then return like
return view;
}
Hope it may help you
So....i am trying to make this button change from main activity to Disciplinas_Activity. But whenever I try to run the app and click the button, the app crashes.
Keep in mind i have other 3 button completly identical ,(other then the fact that they direct to different activities), in the same activity and they all work with the same base code.
Here is the .xml code for Main Activity
<TextView
android:id="#+id/textView_emailnotverified"
android:layout_width="172dp"
android:layout_height="34dp"
android:background="#color/white"
android:text="Email não Verificado!"
android:textColor="#E41F1F"
android:textSize="18dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.066"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.516" />
<TextView
android:id="#+id/textView_studentEmail"
android:layout_width="237dp"
android:layout_height="40dp"
android:text="Email do aluno"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.856"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.395" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="332dp"
android:layout_height="168dp"
android:scaleX="2"
android:scaleY="1.3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.028"
app:srcCompat="#drawable/etpr" />
<TextView
android:id="#+id/welcommingtextview5"
android:layout_width="337dp"
android:layout_height="39dp"
android:text="ESCOLA TÉCNICA E PROFISSIONAL DO RIBATEJO"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.459"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15" />
<TextView
android:id="#+id/ETPRTitle5"
android:layout_width="258dp"
android:layout_height="30dp"
android:text="BEM-VINDO À ETPR"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.105" />
<Button
android:id="#+id/Button_LOGOUT"
android:layout_width="114dp"
android:layout_height="59dp"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<Button
android:id="#+id/button_Testes"
android:layout_width="108dp"
android:layout_height="47dp"
android:text="Testes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.867"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644" />
<Button
android:id="#+id/button_pdf"
android:layout_width="121dp"
android:layout_height="41dp"
android:text="PDF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.894"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.753" />
<Button
android:id="#+id/button_historia"
android:layout_width="191dp"
android:layout_height="47dp"
android:text="Sobre o criador"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.149"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.76" />
<ImageView
android:id="#+id/imageView_perfil"
android:layout_width="122dp"
android:layout_height="118dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.055"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.376"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView_studentname"
android:layout_width="236dp"
android:layout_height="33dp"
android:text="Nome do aluno"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.857"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.329" />
<Button
android:id="#+id/button_emailverification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verificar agora"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.88"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.527" />
<Button
android:id="#+id/changeprofileBTN"
android:layout_width="245dp"
android:layout_height="41dp"
android:text="Mude a imagem de perfil"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.897"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.453" />
<Button
android:id="#+id/button_disciplinas"
android:layout_width="194dp"
android:layout_height="55dp"
android:text="Disciplinas"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.106"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644" />
Here is my .java code for main activity
public class MainActivity extends AppCompatActivity {
TextView fullname, email, verifymessage;
FirebaseAuth fAuth;
FirebaseFirestore fstore;
String userID;
Button resendVerification, LogoutBTN, changeprofileBTN, tests, createrInfo, pdfdatabase, buttonDisciplinas;
ImageView profileImage;
StorageReference storageReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullname = findViewById(R.id.textView_studentname);
email = findViewById(R.id.textView_studentEmail);
LogoutBTN = (Button) findViewById(R.id.Button_LOGOUT);
tests =(Button) findViewById(R.id.button_Testes);
createrInfo =(Button) findViewById(R.id.button_historia);
pdfdatabase =(Button) findViewById(R.id.button_pdf);
buttonDisciplinas = findViewById(R.id.button_disciplinas);
buttonDisciplinas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Disciplinas_Activity.class));
}
});
tests.setOnClickListener(new View.OnClickListener() {// menu dos testes
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Testes_Activity.class));
}
});
createrInfo.setOnClickListener(new View.OnClickListener() {//página sobre a história da app
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),AboutMe_Activity.class));
}
});
pdfdatabase.setOnClickListener(new View.OnClickListener() {//pdf database
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),PDF_Activity.class));
}
});
LogoutBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginScreen.class));
finish();
}
});
profileImage = findViewById(R.id.imageView_perfil);
changeprofileBTN = findViewById(R.id.changeprofileBTN);
fAuth = FirebaseAuth.getInstance();
fstore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
StorageReference profileRef = storageReference.child("utilizadores/"+fAuth.getCurrentUser().getUid()+"/perfil.jpg");
profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});
resendVerification = findViewById(R.id.button_emailverification);
verifymessage = findViewById(R.id.textView_emailnotverified);
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fstore.collection("utilizadores").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot, #Nullable FirebaseFirestoreException error) {
fullname.setText(documentSnapshot.getString("NomeCompleto"));
email.setText(documentSnapshot.getString("Email"));
}
});
FirebaseUser user = fAuth.getCurrentUser();
if (!user.isEmailVerified()){
verifymessage.setVisibility(View.VISIBLE);
resendVerification.setVisibility(View.VISIBLE);
resendVerification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(v.getContext(),"Email de verificação enviado.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d("tag","Erro: Email de verificação não enviado " + e.getMessage());
}
});
}
});
}
changeprofileBTN.setOnClickListener(new View.OnClickListener() { //mudar imagem de perfil
#Override
public void onClick(View v) {
Intent openGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGalleryIntent,1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 ){
if (resultCode == Activity.RESULT_OK){//se o resultado for igual então abre galeria
Uri imageUri = data.getData();
//profileImage.setImageURI(imageUri); //insere imagem escolhida na galeria
uploadImageToFirebase(imageUri);
}
}
}
private void uploadImageToFirebase(Uri imageUri) { //upload da imagem para base de dados
StorageReference fileReference = storageReference.child("utilizadores/"+fAuth.getCurrentUser().getUid()+"/perfil.jpg");
fileReference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this,"Erro", Toast.LENGTH_SHORT).show();
}
});
}
}
Here is .xml code for DisciplinasActivity
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/Relative_Layout_Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="32dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/Menu_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disciplinas"
android:textColor="#color/white"
android:textSize="22sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Menu_Title"
android:layout_marginTop="6dp"
android:text="ETPR"
android:textColor="#color/white"
android:textSize="14sp">
</TextView>
<TextView
android:id="#+id/Name_Data"
android:textColor="#color/white"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/user_icon"
android:text="Nome">
</TextView>
<TextView
android:id="#+id/Email_Data"
android:textColor="#color/white"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Name_Data"
android:layout_toStartOf="#id/user_icon"
android:text="Email">
</TextView>
<ImageView
android:id="#+id/user_icon"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/cara">
</ImageView>
</RelativeLayout>
<GridLayout
android:id="#+id/MainGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#+id/Relative_Layout_Title"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="2">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutSdac"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/sdac_icon"/>
<TextView
android:layout_width="56dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="SDAC"
android:textColor="#color/black"
android:textSize="18sp">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutEletronica"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/eletronica_icon"
app:srcCompat="#drawable/eletronica_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="Eletrónica"
android:textColor="#color/black"
android:textSize="18sp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutCD"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/cd_icon"
app:srcCompat="#drawable/cd_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="CD"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
android:layout_marginTop="70dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp">
<LinearLayout
android:id="#+id/LinearLayoutIMEI"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:src="#drawable/imei_icon"
app:srcCompat="#drawable/imei_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:text="IMEI"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:id="#+id/ExitButton"
android:layout_marginTop="20dp"
android:layout_width="121dp"
android:layout_height="69dp"
android:layout_gravity="center"
android:layout_marginBottom="#+id/RelativeLayout"
android:text="Sair"
android:textColor="#color/white"
android:textSize="16dp" />
</LinearLayout>
Here is my .Java code for DisciplinasActivity
public class Disciplinas_Activity extends AppCompatActivity {
GridLayout gridPrincipal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disciplinas);
gridPrincipal = (GridLayout)findViewById(R.id.MainGrid); //identifica qual a grelha
//ação
setSingleEvent(gridPrincipal);
}
private void setSingleEvent(GridLayout gridPrincipal) {
for (int i =0;i<gridPrincipal.getChildCount();i++)
{
CardView cardView = (CardView)gridPrincipal.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),TeacherLoginScreen.class));//muda para Disciplina de Sdac
}
});
}
}
}
What am i missing...? if all the other 3 buttons work...why doesnt this one work? (I am a total newbie but i
I really need the help)
Consider your code
button = (Button)findViewById(R.id.button1);
You do not need to explicitly cast widgets anymore unless it's a special case like a RadioButton from a resource ID.
Please check that the button you're clicking is actually the button you set all this up for.
Add
android:clickable="true";
android:focusable="true";
to your buttons in the XML layout.
I'm guessing ConstraintLayout is creating this problem, you shouldn't come across this issue if you use LinearLayout.
In your .xml code for DisciplinasActivity you have some mistakes on the dimensions that you are using:
<GridLayout
...
android:id="#+id/MainGrid"
...
android:layout_marginBottom="#+id/Relative_Layout_Title" <!-- E.G 20dp -->
>
<Button
android:id="#+id/ExitButton"
...
android:layout_marginBottom="#+id/RelativeLayout" <!-- E.G 20dp -->
... />
For margins, padding, etc you have to use a dimension, like dp,in,mm,etc not a reference to other view, or you could use dimmens.xml maybe you are confusing android:layout_marginBottom with android:layout_constraintBottom_toBottomOf or another property. Check for this in all the elements in your .xml
This answer is due to this log that you posted:
ComponentInfo{studying.app.tkappv6/studying.app.tkappv6.Disciplinas_Activity}:
java.lang.UnsupportedOperationException: Can't convert to dimension:
type=0x12
In the MainActivity
Change the to "v.getContext()" istead of "MainActivity.this"
tests.setOnClickListener(new View.OnClickListener() {// menu dos testes
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), Testes_Activity.class));
}
});
createrInfo.setOnClickListener(new View.OnClickListener() {//página sobre a história da app
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), AboutMe_Activity.class));
}
});
pdfdatabase.setOnClickListener(new View.OnClickListener() {//pdf database
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),PDF_Activity.class));
}
});
LogoutBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(v.getContext(),LoginScreen.class));
finish();
}
});
I was Looking into the code and i've never used this method, but it's strange for me so it's good to pay attention, I think is wrong to set a "Void", if the method is not going to receive any value, but since is a #Override method if it was that way from the beginning you don't need to change;
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(v.getContext(),"Email de verificação enviado.", Toast.LENGTH_SHORT).show();
}
Give a try and let's see if works!
I'm trying to make an android app which will show a Toast on button click. That Toast contains the number entered by the user in edittext field. The problem is that i am entering text to edittext(Numeric) field and on button click, Toast isn't showing the text entered by me. Toast is completely blank.
Here is my code:-
Activity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class login extends AppCompatActivity {
String username, password;
Button payNGO, payGO;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_login);
payNGO = (Button) findViewById(R.id.payngo);
payGO = (Button) findViewById(R.id.paygo);
usernameField = (EditText) findViewById(R.id.forno);
passwordField = (EditText) findViewById(R.id.dob);
username = usernameField.getText().toString();
password = passwordField.getText().toString();
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
}
}
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".login"
android:background="#drawable/back">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/forno"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.438"
app:srcCompat="#drawable/ico" />
<EditText
android:id="#+id/forno"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="20dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Number"
android:inputType="number"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/dob"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/dob"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="124dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Date of Birth"
android:inputType="numberPassword"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="92dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/payngo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="20dip"
android:layout_marginStart="15dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (NGO)"
tools:layout_editor_absoluteX="204dp"
tools:layout_editor_absoluteY="440dp" />
<Button
android:id="#+id/paygo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="15dip"
android:layout_marginStart="20dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (GO)"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="440dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="#drawable/rect_back_text"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dip"
android:layout_weight="1"
android:text="#string/linef"
android:textAlignment="textEnd"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="30dp"
tools:layout_editor_absoluteY="505dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/heart"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dip"
android:layout_weight="1"
android:text="#string/linee"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="171dp"
tools:layout_editor_absoluteY="505dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Since you are getting the value in onCreate which will be executed even before you enter anything, your Toast shows blank data.
Move the getText() methods to onClick to achieve expected result as follows:
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
Toast.makeText(login.this, usernameField.getText().toString(), Toast.LENGTH_LONG).show();
you can use this code it definitely work..
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