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
Related
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>
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);
}
}
I am new to app development, I am stuck on this following example in my book.
Can some one please help me that why my play button is not working? After clicking on the play button the game should start and it should navigate to next activity.
Main page code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packtpub.mathgamechapter3a.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Math Game"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="30sp" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="#+id/buttonPlay"
android:layout_marginTop="28dp"
android:layout_below="#+id/imageView"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High Scores"
android:id="#+id/button2"
android:layout_below="#+id/buttonPlay"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignRight="#+id/buttonPlay"
android:layout_alignEnd="#+id/buttonPlay"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i;
i = new Intent(this, GameActivity.class);
startActivity(i);
}
}
Game page where it should navigate
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packtpub.mathgamechapter3a.GameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartA"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="#+id/textOperator"
android:layout_alignTop="#+id/textPartA"
android:layout_centerHorizontal="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartB"
android:layout_alignTop="#+id/textOperator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="="
android:id="#+id/textView4"
android:layout_below="#+id/textOperator"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:textSize="70sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/buttonChoice1"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="99dp"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/buttonChoice2"
android:layout_alignTop="#+id/buttonChoice1"
android:layout_centerHorizontal="true"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/buttonChoice3"
android:layout_alignTop="#+id/buttonChoice2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="40sp" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameActivity extends Activity implements View.OnClickListener{
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//The next line loads our UI design to the screen
setContentView(R.layout.activity_game);
//Here we initialize all our variables
int partA = 9;
int partB = 9;
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 1;
int wrongAnswer2 = correctAnswer + 1;
/*Here we get a working object based on either the button
or TextView class and base as well as link our new objects
directly to the appropriate UI elements that we created previously*/
TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//Now we use the setText method of the class on our objects
//to show our variable values on the UI elements.
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partA);
//which button receives which answer, at this stage is arbitrary.
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
}//onCreate ends here
#Override
public void onClick(View view) {
//declare a new int to be used in all the cases
int answerGiven=0;
switch (view.getId()) {
case R.id.buttonChoice1:
//initialize a new int with the value contained in buttonObjectChoice1
//Remember we put it there ourselves previously
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
//is it the right answer?
if(answerGiven==correctAnswer) {//yay it's the right answer
Toast.makeText(getApplicationContext(),
"Well done!", Toast.LENGTH_LONG).show();
}else{//uh oh!
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice2:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice3:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Sorry that's wrong",
Toast.LENGTH_LONG).show();
}
break;
}
}
}
i checked your code it is correct .
I think you forget to register your activity in manifests
please check that
well you need to do a if statement on the being clicked on.
if(view.getId() == R.id.buttonPlay)
{
Intent intent = new Intent(getBaseContext(), GameActivity.class);
startActivity(intent);
}
also make sure game activity is registered in the manifest file.
Intent in = getIntent();
is missing..
the 2nd activity should contain this line
also add this
if(view.getId() == R.id.buttonPlay)
{
Intent inte = new Intent(MainActivity.this, GameActivity.class);
startActivity(intent);
}
and do register in the manifest file
I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}
Here I am creating a simple Droid LoginApp, which simply tells login successfull or not. But the if statement is not giving me the desired result.
Following is the code : MainActivity.java
package com.example.login1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
public EditText t1,t2;
public Button b1;
String user,pass;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText) findViewById(R.id.editText2);
b1= (Button) findViewById(R.id.button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0)
{
user = t1.getText().toString();
pass = t2.getText().toString();
if (((user.equals("user")) && (pass.equals("user"))))
{
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
t1.setText("");
t2.setText("");
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:layout_marginTop="28dp"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="53dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:clickable="true"
android:onClick="onClick"
android:text="Login" />
</RelativeLayout>
While I run the App, on clicking the Login button, the if statement does not give a correct result.. I simply don't know where is the error.
Well, you should compare strings with "equals()" and not with "==".
Replace:
user == "user"
With:
user.equals("user")
And also, I think there's no need for this extra line you have:
if (arg0.findViewById(R.id.button1)==b1)
^ Why are you checking this?
use this code, for click command
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
user = t1.getText().toString();
if (user.equals("user")){
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
}
}
}