Cannot switch activities in Android except one activity - java

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);
}
});
}

Related

Android Studio Application Crashes When Opening Second Activity [duplicate]

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);

Creating a back button with noActionBar

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));
}
});

Number picker only allowing zero as value

I am making an app that utilizes the numberpicker. What happens is when I try to create the numberpicker and set its attributes, it just simply won't do anything. The only value it allows is zero, and you can't scroll the numbers with it either. After a lot of google searches, I'm quite certain that I put the min/max-value methods in the right order.
I would appreciate some guidance of what I'm doing wrong.
source:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;
public class Start extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
NumberPicker minutePicker = (NumberPicker) findViewById(R.id.minutePicker);
minutePicker.setMaxValue(100);
minutePicker.setMinValue(0);
minutePicker.setWrapSelectorWheel(false);
NumberPicker secondPicker = (NumberPicker) findViewById(R.id.secondPicker);
secondPicker.setMaxValue(60);
secondPicker.setMinValue(0);
secondPicker.setWrapSelectorWheel(false);
}
}
edit:
Added XML file:
<?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:id="#+id/twoPickers"
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="com.listifymusic.listifymusic.Start">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginStart="45dp"
android:id="#+id/secondPicker"
android:layout_alignBaseline="#+id/minutePicker"
android:layout_alignBottom="#+id/minutePicker"
android:layout_toRightOf="#+id/minutePicker"
android:layout_toEndOf="#+id/minutePicker" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minutePicker"
android:layout_marginLeft="102dp"
android:layout_marginStart="102dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Make sure your activity is set as launcher activity in your manifest.xml:
<activity android:name=".Start">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
You've started some other Activity that is essentially doing only this.
public class SomeOtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
}
In other words, the code you have should work if you start the Start activity.

Too many output application using Android Studio?

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>

Opening one screen from another

I am trying to open one screen from another in an Android App. I copied the outline of the following code from a tutorial online and then tried to substitute my own screens. When I try to run the following code, I get a "Force Close" when I click the button on the first screen (the second screen never displays). Can someone tell me what I am doing wrong? There are four files: ScreenTestActivity.java, main.xml, screen2.java and screen2.xml.
ScreenTestActivity.java
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ScreenTestActivity extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ScreenTestActivity.this, screen2.class);
startActivity(i);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
</LinearLayout>
screen2.java:
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.os.Bundle;
public class screen2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
}
}
screen2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the New Screen"
/>
</LinearLayout>
<activity android:name="screen2"></activity>
<activity android:name="ScreenTestActivity"></activity>
put this 2 line of code in your androidmanifest.xml file

Categories

Resources