I'm a couple days into learning Android development and I'll admit my code is not pretty, as I have a few weeks of Java under my belt on top of this. I'm trying to create a back button on my 2nd main activity page to get back to the 1st main activity, but the only solutions I've come across are while using the toolbar. How would you go about adding this function while using noActionBar?
Main Activity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/clearwater"
android:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:background="#drawable/btn"
android:fontFamily="cursive"
android:text="DIVE"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
Main Activity 2 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">
<Button
android:id="#+id/Button2"
style="#style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:background="#drawable/timebutton"
android:fontFamily="serif-monospace"
android:text="Time Interval Between Dives"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButton"
style="#android:style/Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="11dp"
android:adjustViewBounds="false"
android:background="#android:color/background_light"
android:cropToPadding="false"
android:tint="#android:color/background_dark"
app:srcCompat="?android:attr/actionModeCloseDrawable" />
</RelativeLayout>
Main Activity Java
package com.example.goodvibes.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button but1;
public void init(){
but1= (Button)findViewById(R.id.button);
but1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent toy = new
Intent(MainActivity.this,activity_main_2.class);
startActivity(toy);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();{
}
}
}
Main Activity 2 Java
package com.example.goodvibes.helloworld;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ViewAnimator;
public class activity_main_2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goodvibes.helloworld">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".activity_main_2"></activity>
</application>
</manifest>
I had a lot of great people giving me answers I had to research. In the end here is what solved my problem: I inserted "android:onClick="ImageButton" under my button code in the xml fine THEN under the corresponding java file for my second activity I inserted:
public void ImageButton(View v)
{
// some code
finish();
}
Add this to your layout file:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_acashmemoreport"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarColoredBackArrow" />
In your activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_acashmemoreport);
setSupportActionBar(toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
First of all you have to add a required toolbar and after making the toolbar you put the button inside the toolbar like following;
Update your xml layout file as,
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<ImageButton
android:id="#+id/btn_back_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"
/>
</android.support.v7.widget.Toolbar>
Also include the below java code inside your Activity Class,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageButton back=findViewById(R.id.btn_back_toolbar);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,AnotherActivity.class));
}
});
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
findViewById() returns null when I call it in onCreate()
(3 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
I am trying to open up the second activity (MainActivity2) via a button but the application crashes every time, I am not quite sure how to solve this. I want to be able to open to the second screen to use the second activity. Thank you for your help. (Having trouble with separating the code, so I have used lines)
Activity main xml + Main activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="241dp"
android:text="Activity1"
android:id="#+id/textView"/>
<Button
android:id="#+id/button"
android:layout_width="375dp"
android:layout_height="101dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="275dp"
android:text="open password generator" />
</RelativeLayout>
**Main activity**
package com.example.menutest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
activity main2 + activity main 2 XML + manifest
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="#+id/textView3"
android:layout_width="203dp"
android:layout_height="85dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="67dp"
android:layout_marginLeft="67dp"
android:layout_marginTop="264dp"
android:hapticFeedbackEnabled="false"
android:textColor="#070505"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.653"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="19dp"
android:layout_marginLeft="19dp"
android:layout_marginBottom="304dp"
android:hapticFeedbackEnabled="true"
android:soundEffectsEnabled="false"
android:text="Generate strong password"
android:textSize="18sp"
android:textStyle="bold"
app:backgroundTint="#36A13A"
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.59" />
</RelativeLayout>
***Mainactivity2***
package com.example.menutest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity2 extends AppCompatActivity {
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(generateString(16));
}
});
}
private String generateString(int length) {
char[] chars ="/.,MNBVCXZ;LKJHGFDSAPOIUYTREWQ[]#=-)0987654321!£$*&qwertyuiopzxcvbnmasdfghjkl(".toCharArray();
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
for ( int i=0; i<length;i++)
{
char c = chars[random.nextInt(chars.length)];
stringBuilder.append(c);
}
return stringBuilder.toString();
}
}
***AndroidManifest xml***
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.menutest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MENUTEST">
<activity android:name=".MainActivity2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
..........................................................................
Mainactivity2
button = (Button)findViewById(R.id.button4);
textView = (TextView)findViewById(R.id.textView3);
I was trying to use user input name for later use but Once i run the app this is closed by saying unfortunately your app has stopped. I have provided Main activity,Layout xml and android manifest codes. Please anyone answer this . I am newbie in android and cant figure out the error.
Main_activity
package rupakthapa.droiddynasty.com.interactivestory2;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.R.attr.duration;
import static android.R.attr.name;
public class MainActivity extends AppCompatActivity {
private EditText mNameField;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameField = (EditText) findViewById(R.id.nameField);
mButton = (Button) findViewById(R.id.startButton);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String a= mNameField.getText().toString();
Context context = getApplicationContext();
int length = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, a, length);
toast.show();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
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="rupakthapa.droiddynasty.com.interactivestory2.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="149dp"
android:contentDescription="startImage"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<Button
android:text="Start your adventure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/startButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="rupakthapa.droiddynasty.com.interactivestory2"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
There is a problem here. You dont have EditText in your layout.
mNameField = (EditText) findViewById(R.id.nameField);
In your layout you should change TextView to EditText
<EditText
android:layout_width="match_parent"
android:id="#+id/nameField"
android:hint="Enter YOur Name"
android:layout_above="#+id/startButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:maxLength="30"
android:layout_marginLeft="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content"
android:textSize="16sp"/>
Running the app cause no problem but it shows a warning
Too many Output!
Surprisingly when I check my phone because I use it as my deployment output instead of a virtual device, there are 3 application that was installed. Here's the screen shot.
phone screenshot
Here's my Java Codes
MainActivity.Java
package com.faacts.jayson.faactsv004;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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);
Button button;
button = (Button) findViewById(R.id.btnEnglish);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), Index.class);
startActivity(intent);
}
});
}
}
Splash.Java
package com.faacts.jayson.faactsv004;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by Jayson on 5/22/2016.
*/
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.anim.rotate);
final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(),R.anim.abc_fade_out);
iv.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iv.startAnimation(an2);
finish();
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
Index.Java
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
/**
* Created by Jayson on 5/22/2016.
*/
public class Index extends AppCompatActivity {
EditText txtpassword;
CheckBox chkbox;
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.index);
Button button;
button = (Button) findViewById(R.id.btnRegister);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), Registration.class);
startActivity(intent);
}
});
txtpassword = (EditText) findViewById(R.id.txtPassword);
chkbox = (CheckBox) findViewById(R.id.chkBox);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
txtpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
chkbox.setText("Reveal Password");
} else {
// hide password
txtpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
chkbox.setText("Hide Password");
}
}
});
}
}
Registration.Java
package com.faacts.jayson.faactsv004;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
/**
* Created by Jayson on 5/22/2016.
*/
public class Registration extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.gender_array,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
}
}
This is the example of my xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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: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"
android:background="#33ccff"
android:clickable="false"
style="#style/AppTheme">
<ImageView
android:layout_width="600dp"
android:layout_height="700dp"
android:id="#+id/imageView3"
android:src="#drawable/welcomepage"
android:layout_marginBottom="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="#string/welcomepage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Choose your language:"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="165dp"
android:textColor="#003333"
android:textStyle="bold" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/btnFilipino"
android:id="#+id/btnFilipino"
android:layout_alignTop="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#drawable/bkg"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/btnEnglish"
android:id="#+id/btnEnglish"
android:background="#drawable/bkg"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
index.xml
<?xml version="1.0" encoding="utf-8"?>
<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: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=".Index"
android:background="#33ccff"
android:clickable="false"
style="#style/AppTheme">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33ccff">
<ImageView
android:layout_width="380dp"
android:layout_height="90dp"
android:layout_marginTop="50dp"
android:id="#+id/imgULogo"
android:layout_gravity="center_horizontal"
android:src="#drawable/upperlogo2"
android:contentDescription="#string/upperlogo" />
<EditText
android:layout_width="318dp"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:layout_gravity="center_horizontal"
android:id="#+id/txtUsername"
android:background="#ffffff"
android:hint="#string/username"
android:textColorHint="#color/dim_foreground_material_dark"
android:textColor="#000000"
android:inputType="text" />
<EditText
android:layout_width="318dp"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/txtPassword"
android:background="#ffffff"
android:hint="#string/password"
android:textColorHint="#color/dim_foreground_material_dark"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginLeft="26dp"
android:text="#string/reveal_password"
android:id="#+id/chkBox" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/login"
android:id="#+id/btnLogin"
android:layout_marginTop="90dp"
android:background="#drawable/bkg"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/register"
android:id="#+id/btnRegister"
android:background="#drawable/bkg"
android:layout_marginTop="10dp"
android:textColor="#000066"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
Please hoping for your feedback Anyway, I am new in android studio.
Anyway, there are two xmls that I did not post I just only pasted the example two those are the registration.xml and splash.xml.
PROBLEM SOLVED: I put a android.intent.category.LAUNCHER instead of just a DEFAULT in every Java class on manifest declaration.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.faacts.jayson.faactsv004">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Index">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Registration">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
So I have been making a simple four button app for my IT 1 class and have been having a problem with some code. I have four buttons that are supposed to call a startActivity and change to a different screen. The first button, labeled fire, works perfectly fine and works 100% of the time. The next button, labeled Earthquake doesn't work, even though the code is the same (without the parts that need to change). The other 2 buttons also do not work. I have tried making a new project, copying the working xml, copying the exact code. I cannot figure out why it doesn't work.
MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<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:background="#81848b"
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="com.example.carrieowen.testproject.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:id="#+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#eff2f9"
android:text="Emergency Drills"
android:gravity="center"
android:textSize="32sp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="40dp"
android:text="Fire"
android:background="#e71e1e"
android:id="#+id/button"
android:onClick="Fire"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#009c12"
android:padding="40dp"
android:text="Earthquake"
android:id="#+id/button2"
android:onClick="Earthquake"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#eef035"
android:padding="40dp"
android:text="Tornado"
android:id="#+id/button3"
android:onClick="Tornado"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#1b4fdd"
android:padding="40dp"
android:text="Code Blue"
android:id="#+id/button4"
android:onClick="CodeBlue"/>
</LinearLayout>
</RelativeLayout>
The MainActivity Java
package com.example.carrieowen.testproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Fire (View view){
Intent changeScreenFire = new Intent(this, firescreen.class);
startActivity(changeScreenFire);
}
public void Earthquake (View view){
Intent changeScreenEarthquake = new Intent(this, earthquake.class);
startActivity(changeScreenEarthquake);
}
public void Tornado (View view){
Intent changeScreenTornado = new Intent(this, tornado.class);
startActivity(changeScreenTornado);
}
public void CodeBlue (View view){
Intent changeScreenCodeBlue = new Intent(this, codeblue.class);
startActivity(changeScreenCodeBlue);
}
}
The FireScreen Java
package com.example.carrieowen.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class firescreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firescreen);
}
}
The Earthquake Java
package com.example.carrieowen.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class earthquake extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_earthquake);
}
}
The XML for the Fire and Earthquake are the same other than the Text boxes being different. If needed will add.
Make sure your activities are listed in the manifest? The manifest file is called AndroidManifest.xml and is listed in the project at the top, in android studio. This is what the content should look like:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Tornado">
</activity>
<activity android:name=".CodeBlue">
</activity>
</application>
I have something like that as well, but my code is a bit different. Try it like this. The button that you used to create open the new activity is labeled in the line starting with Button Pause, then i set a onclicklistner so that when you click on it, something happens, and i have it to currently oped a new activity in the method past the #Override part.
public class GameWindow extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_window);
Button pause = (Button) findViewById(R.id.pausebutton);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
Intent intent = new Intent(GameWindow.this, PauseMenu.class);
startActivity(intent);
}
});
}
I have this problem that just seems strange.
I have this Android View with six buttons and when i click on one of them(the only one with the action defined apart from the exit button), it gives me a RuntimeError.
The Activity is declared on the manifest, and this is why i don't understand my error.
Can someone please help me with this.
I would really appreciate it :)
So here's my code:
package com.example.calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View createButton = findViewById(R.id.btn_create);
View deleteButton = findViewById(R.id.btn_delete);
View moveButton = findViewById(R.id.btn_move);
View searchButton = findViewById(R.id.btn_search);
View translateButton = findViewById(R.id.btn_translate);
View exitButton = findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_create:
Intent i = new Intent(this, CreateAppointment.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
My layout is like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="225dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin" >
<CalendarView
android:id="#+id/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="2dip"
android:layout_marginTop="2dip"
android:stretchColumns="*" >
<TableRow>
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
<Button
android:id="#+id/btn_viewEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/viewEdit" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/delete" />
<Button
android:id="#+id/btn_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/move" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search" />
<Button
android:id="#+id/btn_translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/translate" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/exit"/>
</LinearLayout>
And the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calendar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.calendar.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CreateAppointment"
android:theme="#android:style/Theme.Dialog"
android:label="#string/create" >
</activity>
</application>
</manifest>
You have to define correctly what kind of views ares using in onCreate()
View createButton = findViewById(R.id.btn_create);
for example:
since you are using a Button:
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
Make the cast as button:
Button createButton = (Button)findViewById(R.id.btn_create);
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createButton = (Button)findViewById(R.id.btn_create);
Button deleteButton = (Button)findViewById(R.id.btn_delete);
Button moveButton = (Button)findViewById(R.id.btn_move);
Button searchButton = (Button)findViewById(R.id.btn_search);
Button translateButton = (Button)findViewById(R.id.btn_translate);
Button exitButton = (Button)findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}