onClick multiple button - Android - java

hello I'm new to android and I have a problem with multiple button first I implemented multiple button in my MainActivity and it is work with me now I create another activity and implement the same what i do in the mainActivity in the new activity I have a multiple button in this activity
the new Activity (Activity6.class):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Activity6 extends AppCompatActivity implements
View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminbackdoor);
Button ee = (Button) findViewById(R.id.button12);
Button ff = (Button) findViewById(R.id.button9);
Button gg = (Button) findViewById(R.id.button10);
Button hh = (Button) findViewById(R.id.button11);
ee.setOnClickListener(this);
ff.setOnClickListener(this);
gg.setOnClickListener(this);
hh.setOnClickListener(this);
}
#Override
public void onClick (View vv){
switch (vv.getId()) {
case R.id.button12:
Intent e = new Intent(Activity6.this, Admin1.class);
startActivity(e);
break;
case R.id.button9:
Intent f = new Intent(Activity6.this, User1.class);
startActivity(f);
break;
case R.id.button10:
Intent g = new Intent(Activity6.this, Teacher1.class);
startActivity(g);
break;
case R.id.button11:
Intent h = new Intent(Activity6.this, Class1.class);
startActivity(h);
break;
default:
break;
}
}
Admin1.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Admin1 extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
}
}
User1.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class User1 extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminuser);
}
}
activity Teacher1.class and Class1.class have the same with Admin1.class and User1.class but with different layout.
Note: I add all the activities in manifest like this:
<activity android:name=".Activity6"></activity>
<activity android:name=".Admin1"></activity>
<activity android:name=".Class1"></activity>
<activity android:name=".Teacher1"></activity>
<activity android:name=".User1"></activity>
adminbackdoor.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="العمليات"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:fontFamily="serif"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginTop="60dp"
android:layout_marginRight="140dp" />
<Button
android:id="#+id/button12"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="admin"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button9"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="user"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button10"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="teacher"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button11"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="class"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
and onClick does not work with me I do not know what is the problem ??
if I run the application and click on the button they did not work ( go to another layout )

First of all you should extend Activity and not AppCompatActivity.
And one thing I can recommend is to use the click listener like this in this situation:
Button ee = (Button) findViewById(R.id.button12);
ee.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent e = new Intent(Activity6.this, Admin1.class);
startActivity(e);
}
});

Related

SignupActivity won't launch from MainActivity's intent

I am new to Android Studio and have been pulling my hair out for the past hour trying to figure this out.
So I have a MainActivity java class which has two separate onClick methods each with an intent that opens a certain activity depending on the button (in activity_main.mxml) pressed.
For whatever reason, the newUser.setOnClickListener() will not open SignupActivity.
The strangest part is that if I change the destination activity in that same block to SplashActivity, it works. This tells me that it's a problem with my SignupActivity and its corresponding.xml file maybe.
Any help is greatly appreciated.
Here are the files in my project:
MainActvitiy.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
// Declare buttons
Button existingUser, newUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
// Define buttons
existingUser = findViewById(R.id.btnExistingUser);
newUser = findViewById(R.id.btnNewUser);
// Existing user button listener
existingUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SignupActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
// New user button listener
newUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SignupActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
}
}
SignupActivity.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class SignupActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_login);
}
// Launch MainActivity when back button is pressed
public void launchMain (View v) {
// Launch Log-in activity
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
LoginActivity.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_login);
}
// Launch MainActivity when back button is pressed
public void launchMain (View v) {
// Launch Log-in activity
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
activity_signup.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/gradient_background"
tools:context=".SignupActivity">
<!-- Welcome Back Text -->
<TextView
android:id="#+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="330dp"
android:fontFamily="sans-serif-light"
android:text="Create User"
android:textColor="#color/white"
android:textSize="60sp"
android:textStyle="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- User ID Field -->
<EditText
android:id="#+id/edtTxtPromptUserID"
android:layout_width="332dp"
android:layout_height="69dp"
android:layout_marginTop="468dp"
android:backgroundTint="#color/white"
android:hint="#string/prompt_userID"
android:inputType="text"
android:selectAllOnFocus="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Password Field -->
<EditText
android:id="#+id/edtTxtPromptPassword"
android:layout_width="332dp"
android:layout_height="69dp"
android:layout_marginTop="8dp"
android:backgroundTint="#color/white"
android:hint="#string/prompt_password"
android:inputType="textPassword"
android:selectAllOnFocus="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edtTxtPromptUserID" />
<!-- Register Button -->
<Button
android:id="#+id/btnRegister"
android:layout_width="242dp"
android:layout_height="83dp"
android:layout_marginTop="40dp"
android:backgroundTint="#00A36C"
android:text="Register"
android:textSize="30sp"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edtTxtPromptPassword" />
<!-- Back button -->
<Button
android:id="#+id/btnBack2"
android:layout_width="134dp"
android:layout_height="57dp"
android:layout_marginTop="24dp"
android:onClick="launchMain"
android:text="Back"
android:textColor="#color/white"
android:textSize="20dp"
android:backgroundTint="#color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnRegister" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have gone back and looked at all my files to make sure they are unique and consistent with naming, but I haven't been able to find the reason why it won't work.
change in signup Activity
setContentView(R.layout.activity_login);
to
setContentView(R.layout.activity_signup);
In SignupActivity > onCreate()
just replace yours with following...
setContentView(R.layout.activity_signup);
you are binding wrong xml with your java file.
'setContentView()' is used to bind layouts (xml)

ConstraintLayout braking after displaying Android Studio

I am currently working on an android app, and I'm having an issue with displaying XML layout
In Android Studio, everything looks fine, but in the VM it doesn't look too good.
This is how it looks in Android Studio:
And here is how it looks like when it's displayed in VM:
I should probably mention that it is supposed to be a pop up window
Here is XML code:
<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="350dp"
android:layout_height="265dp"
android:background="#drawable/rounded_corners_grey"
android:padding="20dp"
>
<android.widget.Button
android:text="Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Cancel"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:outlineProvider="none"
app:layout_constraintTop_toBottomOf="#+id/TimerValluesBackground"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="48dp"
android:layout_marginStart="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.0" />
<android.widget.Button
android:text="Set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/SaveTimer"
android:background="#drawable/rounded_corners_dark"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/Cancel"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="56dp"
app:layout_constraintTop_toTopOf="#+id/Cancel"
app:layout_constraintVertical_bias="0.0" />
<View
android:layout_width="300dp"
android:layout_height="100dp"
android:id="#+id/TimerValluesBackground"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"
android:clickable="false"
android:background="#drawable/rounded_corners_dark"
tools:ignore="MissingConstraints"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.32" />
<TextView
android:text="0 h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hourValue"
android:textSize="22dp"
android:textColor="#fff"
app:layout_constraintTop_toTopOf="#+id/TimerValluesBackground"
app:layout_constraintStart_toStartOf="#+id/TimerValluesBackground"
app:layout_constraintBottom_toBottomOf="#+id/TimerValluesBackground"
android:layout_marginStart="32dp"
app:layout_constraintVertical_bias="0.514" />
<TextView
android:text="0 min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minuteValue"
android:textSize="22dp"
android:textColor="#fff"
app:layout_constraintTop_toTopOf="#+id/TimerValluesBackground"
app:layout_constraintStart_toEndOf="#+id/hourValue"
app:layout_constraintEnd_toStartOf="#+id/secondValue"
app:layout_constraintBottom_toBottomOf="#+id/TimerValluesBackground" />
<TextView
android:text="0 sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/secondValue"
android:textSize="22dp"
android:textColor="#fff"
tools:layout_editor_absoluteX="231dp"
app:layout_constraintTop_toTopOf="#+id/TimerValluesBackground"
app:layout_constraintBottom_toBottomOf="#+id/TimerValluesBackground"
tools:ignore="MissingConstraints"
app:layout_constraintVertical_bias="0.514" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is code from the Main Activity:
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class MainActivity extends AppCompatActivity {
private String ipAddress = "192.168.1.59";
private int port = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting background color
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.rgb(30,30,30));
// hiding bottom navigation bar
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
// Initiation request
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"volume").execute();
// Click event for Volume Up button
ImageButton VolumeUp = (ImageButton) findViewById(R.id.VolumeUpButton);
VolumeUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"volume up").execute();
}
});
// Click event for Volume Down button
ImageButton VolumeDown = (ImageButton) findViewById(R.id.VolumeDownButton);
VolumeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"volume down").execute();
}
});
// Click event for Volume Up Lot button (lot means volume does up for about 33 %)
ImageButton VolumeUpLot = (ImageButton) findViewById(R.id.VolumeUpLotButton);
VolumeUpLot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"volume up lot").execute();
}
});
// Click event for Volume Down Lot button (lot means volume does down for about 33 %)
ImageButton VolumeDownLot = (ImageButton) findViewById(R.id.VolumeDownLotButton);
VolumeDownLot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"volume down lot").execute();
}
});
// Click event for ScreenShot Button
ImageView ScreenShotButton = (ImageView) findViewById(R.id.ScreenShotButton);
ScreenShotButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"screenshot").execute();
}
});
// Click event for Lock Button
ImageView LockScreenButton = (ImageView) findViewById(R.id.LockScreenButton);
LockScreenButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"lock").execute();
}
});
// Click event for Mute Button
ImageView MuteButton = (ImageView) findViewById(R.id.MuteButton);
MuteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"mute").execute();
}
});
// Enter press event for ippAddress input
EditText ipAddressInput = (EditText) findViewById(R.id.ipAddress);
ipAddressInput.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
ipAddress = ipAddressInput.getText().toString();
Log.i("Ip Address changed", String.valueOf(ipAddress));
return true;
}
return false;
}
});
// Enter press event for port input
EditText portInput = (EditText) findViewById(R.id.port);
portInput.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
port = Integer.parseInt(portInput.getText().toString());
Log.i("Port changed", String.valueOf(port));
return true;
}
return false;
}
});
// Click event for Shutdown scheduler
ImageView shutdownScheduler = (ImageView) findViewById(R.id.ShutdownTimer);
View parent = (View) findViewById(R.id.ParentViewFroPopUp);
parent.setClickable(false);
//Overlay animation init
///TransitionDrawable transition = (TransitionDrawable) parent.getBackground();
shutdownScheduler.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View v) {
// treba dorobit to dialogove okno a tam mat nastavenia a vlastne tlacidlo na zrusenie timeru
//int time = 2000;
//new ConnectionWithServer((Activity) MainActivity.this,MainActivity.this,ipAddress,port,"timer " + time).execute();
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup,null );
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setOverlapAnchor(true);
Button btnDismiss = (Button)popupView.findViewById(R.id.Cancel);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
parent.setBackgroundColor(Color.parseColor("#00000000"));
//transition.reverseTransition(5000);
}});
//transition.startTransition(5000);
popupWindow.showAtLocation(parent, Gravity.CENTER,0, 0);
parent.setBackgroundColor(Color.parseColor("#90000000"));
}});
}
}
Try adding,
app:layout_constraintEnd_toEndOf="#+id/TimerValluesBackground"
to your last TextView(#+id/secondValue) component.
In your implementation you have forgotten to add some contraints to your views.
In order that they are display as you would like, try to add all four constraints when possible.
<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="#color/quantum_black_100"
android:padding="20dp">
<android.widget.Button
android:id="#+id/Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginTop="48dp"
android:background="#android:color/transparent"
android:outlineProvider="none"
android:text="Cancel"
android:textColor="#color/places_text_white_alpha_26"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/SaveTimer"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TimerValluesBackground" />
<android.widget.Button
android:id="#+id/SaveTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="56dp"
android:text="Set"
android:textColor="#color/quantum_white_100"
app:layout_constraintBottom_toBottomOf="#+id/Cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/Cancel"
app:layout_constraintTop_toTopOf="#+id/Cancel" />
<View
android:id="#+id/TimerValluesBackground"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:clickable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/hourValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0 h"
android:textColor="#fff"
android:textSize="22dp"
app:layout_constraintBottom_toBottomOf="#+id/TimerValluesBackground"
app:layout_constraintEnd_toStartOf="#+id/minuteValue"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/TimerValluesBackground"
app:layout_constraintStart_toStartOf="#+id/TimerValluesBackground"
app:layout_constraintTop_toTopOf="#+id/TimerValluesBackground" />
<TextView
android:id="#+id/minuteValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0 min"
android:textColor="#fff"
android:textSize="22dp"
app:layout_constraintBottom_toBottomOf="#+id/TimerValluesBackground"
app:layout_constraintEnd_toStartOf="#+id/secondValue"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/hourValue"
app:layout_constraintTop_toTopOf="#+id/TimerValluesBackground" />
<TextView
android:id="#+id/secondValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0 sec"
android:textColor="#fff"
android:textSize="22dp"
app:layout_constraintBottom_toBottomOf="#+id/TimerValluesBackground"
app:layout_constraintEnd_toEndOf="#+id/TimerValluesBackground"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/minuteValue"
app:layout_constraintTop_toTopOf="#+id/TimerValluesBackground"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

Crash app when adding multiple activities android studio

Hello everybody I got an issue when I trying to run my app in my physical phone and just get crashed at run. its my 5 time making this app, when this happen just close at installing the app. I'm trying to make an app in the main activity two Image buttons and then open anther activity, each one, I checked myself without creating the image buttons. but when I putted the image button the app just get crashed.
here is my code.
ActivityMain.xml
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/blurred"
tools:context=".MainActivity">
<ImageView
android:id="#+id/blurbackground"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/backmain" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="62dp"
android:text="Welcome!"
android:textColor="#android:color/white"
android:textSize="22sp" />
<TextView
android:id="#+id/txt2"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginTop="114dp"
android:gravity="center"
android:text="Master all professions in the professions guide"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black"
android:textSize="18sp"
android:typeface="sans" />
<TextView
android:id="#+id/txt4"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginBottom="348dp"
android:textColorHighlight="#android:color/black"
android:text="Be part of the game and be the master of professions, these guides are collected from users who offer great help in your adventure throughout Azeroth!"
android:textColor="#android:color/white"
android:textStyle="italic"
android:typeface="sans" />
<TextView
android:id="#+id/txt7"
android:layout_width="322dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginBottom="75dp"
android:text="We have classroom guides available for you to master your character if you are fresh and new to the world of Warcraft, dominate your enemies and take the victory!"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black" />
<ImageView
android:id="#+id/imageView"
android:layout_width="227dp"
android:layout_height="103dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="66dp"
android:layout_marginLeft="66dp"
android:layout_marginTop="223dp"
app:srcCompat="#drawable/alchem"
tools:scaleType="centerCrop" />
<ImageButton
android:id="#+id/buthorde"
android:layout_width="68dp"
android:layout_height="75dp"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginBottom="155dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/horde" />
<ImageButton
android:id="#+id/butally"
android:layout_width="70dp"
android:layout_height="73dp"
android:layout_alignEnd="#+id/imageView"
android:layout_alignBottom="#+id/buthorde"
android:layout_marginEnd="5dp"
android:layout_marginBottom="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/ally" />
<Button
android:id="#+id/butshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="21dp"
android:text="Show me!"
android:textAllCaps="false"
android:textStyle="italic"
android:typeface="sans" />
ActivityMain.java
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button butonhorde;
private Button butonally;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
butonally=(Button) findViewById(R.id.butally);
butonally.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
butonhorde=(Button) findViewById(R.id.buthorde);
butonhorde.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}
Logcat
2018-11-01 23:30:05.204 1051-19358/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
2018-11-01 23:30:05.204 1051-19358/system_process D/OpenGLRenderer: Swap behavior 1
2018-11-01 23:30:05.291 1051-12094/system_process D/GraphicsStats: Buffer count: 4
2018-11-01 23:30:05.291 1051-4361/system_process I/WindowManager: WIN DEATH: Window{3382c8d u0 hordemuzzle.wowprofessions/hordemuzzle.wowprofessions.SplashScreenActivity}
2018-11-01 23:30:05.291 1051-12088/system_process I/ActivityManager: Process hordemuzzle.wowprofessions (pid 24362) has died
2018-11-01 23:30:05.291 1051-12088/system_process D/ActivityManager: cleanUpApplicationRecord -- 24362
Splash screen activity
package hordemuzzle.wowprofessions;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import gr.net.maroulis.library.EasySplashScreen;
import static android.support.v4.os.LocaleListCompat.create;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EasySplashScreen config = new EasySplashScreen(SplashScreenActivity.this)
.withFullScreen()
.withTargetActivity(MainActivity.class)
.withSplashTimeOut(4000)
.withBackgroundResource(android.R.color.background_dark)
.withLogo(R.drawable.logo);
View easysplashscreen = config.create();
setContentView(easysplashscreen);
}
}
try this
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
((ImageButton) findViewById(R.id.butally)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
((ImageButton) findViewById(R.id.buthorde)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}

On Android emulator my button is not clicking

I'm recently started to program and faced with this problem. My button is not clicking in emulator, however, I wrote onClickListener in java. It still doesn't work.
Here is my xml code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/SD"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:background="#color/blue_gray"
android:visibility="visible"
android:onClick="onClick"/>
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/SD2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:background="#color/follow"
android:visibility="gone"
android:onClick="onClick"/>
</FrameLayout>
Here is my java code:
enter code here
package com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.example.app.R;
/**
* Created by ww on 12.02.14.
*/
public class fragment_main extends Activity {
Button i1;
Button i2;
protected void onCreate (Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(R.layout.fragment_main);
i1= (Button) findViewById(R.id.btn1);
i2=(Button) findViewById(R.id.btn2);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
i1.setVisibility(View.GONE);
i2.setVisibility(View.VISIBLE);
break;
case R.id.btn2:
i1.setVisibility(View.VISIBLE);
i2.setVisibility(View.GONE);
break;
}
}
}
Here's the working code,
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sd"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:onClick="onClick"/>
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sd2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:visibility="gone"
android:onClick="onClick"/>
</RelativeLayout>
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
Button b2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
b2.setVisibility(View.VISIBLE);
b1.setVisibility(View.INVISIBLE);
break;
case R.id.btn2:
b2.setVisibility(View.INVISIBLE);
b1.setVisibility(View.VISIBLE);
break;
}
}
}
Output (Compiled & ran in Emulator):
When the app starts Button2 will be invisible since it was set as invisible in xml layout.
Once the user click button1 above, button2 will be visible & button1 will be invisible.
you have to register a listener to your buttons, e.g.:
i1 = (Button) findViewById(R.id.btn1);
i2 = (Button) findViewById(R.id.btn2);
//inside onCreate do this:
i1.setOnClickListener(myhandler1);
i2.setOnClickListener(myhandler2);
Then you have to create those listeners.
// somewhere outside onCreate do this:
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
}
};

Android Activity stop due to non-working toast

Hello Stack Overflow !
I'm working on an android application for children : Each activity is a question.
For each question, there is two buttons for clue and the answer.
I encounter two issues :
1/ I have to click on a button before change activity, do I need an override or something so I can change of activity without any click required ?
2/ On this activity (code is below), if I click on the button with the id "essai", the activity stop and I came to the previous activity.. Anybody know how I could fix this ?
Thank you in advance !
Here is the java code
package com.beryl.timewaster;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class Activity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
public void indice1(View v)
{
Toast.makeText(this, "expression", Toast.LENGTH_LONG).show();
}
public void indice2(View v)
{
Toast.makeText(this, "aguéri", Toast.LENGTH_LONG).show();
}
public void essai(View v)
{
Toast.makeText(this, "LOLILO", Toast.LENGTH_LONG).show();
final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1);
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Accueil.class);
startActivity(intent);
}
});
}}
And here the xml
<AbsoluteLayout
android:id="#+id/absoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxx"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:layout_x="-1dp"
android:layout_y="61dp"
android:text="#string/dev2"
android:textColor="#color/blanc"
android:textSize="25sp"
android:color="#color/blanc" />
<Button
android:id="#+id/Solution2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="259dp"
android:onClick="essai"
android:text="#string/solution"
android:textColor="#color/vertpomme"
android:textSize="30sp" />
<Button
android:id="#+id/indice1de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1dp"
android:layout_y="121dp"
android:onClick="indice1"
android:text="#string/indice1"
android:textColor="#color/orange"
android:textSize="30sp" />
<Button
android:id="#+id/indice2de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="190dp"
android:onClick="indice2"
android:text="#string/indice2"
android:textColor="#color/rouge"
android:textSize="30sp" />>
<ImageButton
android:id="#+id/flechedroite2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="196dp"
android:layout_y="338dp"
android:src="#drawable/flechedroite" />
<ImageButton
android:id="#+id/flechegauche2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="339dp"
android:src="#drawable/flechegauche" />
</AbsoluteLayout>
</RelativeLayout>
Change final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1); to final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche2);. You have not declared android:id="#+id/flechegauche1" in your xml

Categories

Resources