New activity in Android - android

I found myself in trouble with creating new activity, I get the unfortunately your app has stopped error message and as a good humanbeing I thought I'd share my misfortune with you:
I can't get my button to open new activity:
MainActivity.java:
package com.example.vogella.dev;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton =(RadioButton)findViewById(R.id.radio0);
RadioButtonfahrenheitButton=(RadioButton)findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this,getResources().getString(R.string.toast_a),
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(Strgin.valuof(ConvertFahrenheitToCelsius(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
} else {
text.setText(Strgin.valuof(ConvertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
private float ConvertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) +32;
}
public void scrollview(View v) {
Intent intent =newIntent(this,ScrollviewActivity.class);
startActivity(intent);
}
}
My activity_main.xml
<RelativeLayoutxmlns: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="#color/myColor" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/hint"
android:inputType="numberDecimal|numberSigned" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/radioGroup1"
android:onClick="onClick"
android:text="#string/calc" />
<Button
android:id="#+id/test_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="44dp"
android:layout_toRightOf="#+id/button1"
android:text="#string/test_button"
android:onClick="scrollview"/>
</RelativeLayout>
The AndroidManifest in portion
<activity
android:name="com.example.vogella.dev.Scrollview"
android:label="#string/title_activity_scrollview"
android:parentActivityName=com.example.vogella.dev.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.vogella.dev.MainActivity" />
</activity>
</application>
And the activity I'm trying to open
public class ScrollviewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview);
TextView view = (TextView) findViewById(R.id.TextView02);
String s="";
for (int i=0; i < 500; i++) {
s += "vogella.com" ;
}
view.setText(s);
}
}

Intent intent =newIntent(this,ScrollviewActivity.class);
You forgot the space between new and Intent. However, you may have other problems as well. It would help to tell us the error thrown.

What is the error you are getting in the logcat. May be you have not mentioned your ScrollViewActivity in your Manifest file.Please check

Your manifest must contain the MainActivity:
<activity android:name="com.example.vogella.dev.MainActivity" />

1)use this in onCreate()
Button button = (Button) findViewById(R.id.button1);
you did initialize your EditText then why didnt you do it for your Button named 'button1'
2) even mention your ScrollViewActivity in android manifest
3) Intent i = new Intent(getApplicationContext,ScrollViewActivity.class);
startActivity(i);

Have you declare new Activity in Manifest? If not, add it like this:
<activity android:name="com.example.vogella.dev.ScrollviewActivity" />

How even you compile does IDE not implemented an error
Intent intent =newIntent(this,ScrollviewActivity.class); // Error is here
startActivity(intent);
It should be like this
Intent intent =new Intent(this,ScrollviewActivity.class);
startActivity(intent);
And also never forget to adding your every new activity to Manifest
<activity
android:name="com.activities.ActivitySettings"
android:icon="#drawable/icon_small"
android:label="#string/title_activity_activity_settings"
android:logo="#drawable/icon_small"
android:theme="#style/MyTheme" >
</activity>
Specifically for your question
You have two activity MainActivity and ScrollViewActivity
on manifest you mention this
<activity
android:name="com.example.vogella.dev.Scrollview"
android:label="#string/title_activity_scrollview"
Scrollview is not any of your activity but ScrollViewActivity is

Public void launchActivity(View view){
Intent intent = new Intent(this, youractivityname
Class);
startActivity(intent);

Related

StartActivityForResult not working properly from second activity

I am a noob at this, so going to keep it short.
This is a practice app which sends data from first activity to second activity as a message and then the first activity receives data from the second activity as a message. The tutorial I'm following instructed me to use the StartActivityForResult() function to extract data from a second activity to the first activity. I have two questions:
What is wrong with this code:
I. The MainActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
//LOG_TAG contains the name of the class package, encapsulated for ease
public static final String EXTRA_MESSAGE =
"com.example.android.twoactivities.extra.MESSAGE";
//This will be used as the unique key to send data to the second Activity
public static final int TEXT_REQUEST = 1;
//This is used to define the key for a particular type of response that
you're interested in.
private EditText mMessageEditText;
//This EditText is used to send the message to the second activity
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
//These private variables hold the reply header and the reply TextViews
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageEditText = (EditText) findViewById(R.id.editText_main);
mReplyHeadTextView = (TextView) findViewById(R.id.textHeaderReply);
mReplyTextView = (TextView) findViewById(R.id.textMessageReply);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button Clicked!");
Intent intent = new Intent(this, SecondActivity.class);
String message = mMessageEditText.getText().toString();
//Gets the data from EditText, converts it to String, and stores it in message
intent.putExtra(EXTRA_MESSAGE,message);
startActivityForResult(intent, TEXT_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == TEXT_REQUEST){
if(resultCode == RESULT_OK){
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
}
II. The SecondActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
//Gets the intent that activated this activity
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//This extracts the extra text sent along with this key String, thus
receiving the text
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
//Do not reuse the intent from the first activity, create a new one
Intent replyIntent = new Intent();
setResult(RESULT_OK,replyIntent);
//RESULT_OK has the value -1 and is used as a Result code in the Activity
class
//to check that the data is send without a complication
finish();
}
public void returnReply(View view) {
}
}
III. The activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="#id/button_main"
android:layout_toLeftOf="#+id/button_main"
android:hint="Enter Your Message Here"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textHeaderReply"
android:text="#string/textHeaderReply"
android:visibility="invisible"/>
<!--The visibility mode is used to select how the attribute will be before data is passed to it.-->
<!--Invisible means that the attribute will be invisible before data is passed to it, hence not confusing the user-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textMessageReply"
android:layout_below="#+id/textHeaderReply"
android:visibility="invisible"/>
IV. The activity_second.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:onClick="returnReply"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>
V. The AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.twoactivitiesredo">
<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=".SecondActivity"
android:label="#string/secondActivity"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.twoactivitiesredo.MainActivity"/>
</activity>
</application>
</manifest>
I am sorry if the code is taking a lot of space, but I really needed help in clarifying my doubts and problems here, about why this code isn't working.
Thanks in advance to all the answers.
Replace your secondActivity.java with this:
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
}
public void returnReply(View view) {
Intent replyIntent = new Intent();
String replyMessage = mReply.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, replyMessage);
setResult(RESULT_OK,replyIntent);
finish();
}
}
In activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Reply Button"
android:onClick="returnReply"
/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>

assign button press to evoke a new page from sliding menu

I've implemented a sliding menu android framework by following a tutorial video, the menu works well but I can't figure out how to make the buttons generate new "fresh", i.e. distinct, pages wherein I can place subsequent components/"activities".
Right now all the buttons do is "toggle" back and forth between having the menu exposed, and hiding the menu, making visible the complete "landing page".
This is how it looks:
In my layout file, I think this is responsible for assigning functionality to the buttons:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 3" />
So as you can see the buttons evoke the method toggleMenu which looks like this:
public void toggleMenu() {
switch (this.menuCurrentState){
case CLOSED:
this.menuCurrentState = MenuState.OPENING;
this.menu.setVisibility(View.VISIBLE);
this.menuAnimatonScroller.startScroll(0, 0, this.getMenuWidth(),
0, menuAnimationDuration);
break;
case OPEN:
this.menuCurrentState = MenuState.CLOSING;
this.menuAnimatonScroller.startScroll(this.currentContentOffset,
0, -this.currentContentOffset, 0, menuAnimationDuration);
break;
default:
return;
}
this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable, menuAnimationPollingInterval);
this.invalidate();
}
I guess I need to make a new method for generating fresh pages and then assign that to the button push rather than toggle menu, I've tried this a few times using intent but I've not been able to quite figure it out.
What do I need to account for in such an operation?
How should such a function look?
The complete code can be found on my github page.
Thank you for your consideration.
dig this tutorial, it's one of the only helpful ones, in my opinion:
http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/
or check the pertinent info below"
XML Layouts
Create following two XML layout files in “res/layout/” folder :
res/layout/main.xml – Represent screen 1
res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
File : res/layout/main2.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
2. Activities
Create two activity classes :
AppActivity.java –> main.xml
App2Activity.java –> main2.xml
To navigate from one screen to another screen, use following code :
Intent intent = new Intent(context, anotherActivity.class);
startActivity(intent);
File : AppActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
File : App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
3. AndroidManifest.xml
Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".App2Activity" >
</activity>
</application>

How to set dynamic string as user select in android?

I'm beginner in Android. I'm trying to make an app where when user tap on button he should go to another activity let say if user tap on button 1 he see new activity with information-A and if he press button 2 he see information-B in same activity he saw information A.
How can I set that.
If I am understanding your question correctly, here's a solution.
You can use the putExtras method of Intent to pass data in key/value pairs to the next activity.
Add the following to activity 1 layout:
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"
android:onClick="goToActivityFromButtonA" />
<Button
android:id="#+id/btnB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btnA"
android:text="Button B"
android:onClick="goToActivityFromButtonB" />
Add the following methods to Activity 1:
public void goToActivityFromButtonA(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button A");
startActivity(intent);
}
public void goToActivityFromButtonB(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button B");
startActivity(intent);
}
Add the following to Activity 2 layout:
<TextView
android:id="#+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add the following to the onCreate method of activity 2:
TextView txtText = (TextView)findViewById(R.id.txtText);
txtText.setText(getIntent().getExtras().getString("buttonData"));
I just created a sample project for you:
https://drive.google.com/file/d/0B0S6sddMC_rMSUNieUxkR2lFRzQ/view?usp=sharing
you have first create ui for first activity like this
<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=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="77dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="110dp"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/button1"
android:text="Button 2" />
</RelativeLayout>
ui of second activity like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second actvity" />
</LinearLayout>
activity first code where i set index with intent for both button click(you also put string double ling and message with intent)
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id){
case R.id.button1:
Intent i1=new Intent(FirstActivity.this,secondactivity.class);
i1.putExtra("one", 1);
startActivity(i1);
break;
case R.id.button2:
Intent i2=new Intent(FirstActivity.this,secondactivity.class);
i2.putExtra("one", 2);
startActivity(i2);
break;
}
}
}
and second activity i get that integer value to identify which button is clicked and fire operation on that condition
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class secondactivity extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
tv=(TextView)findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
int index = extras.getInt("one");
if(index==1){
tv.setText("nformation-A "+index);
}else if(index==2){
tv.setText("nformation-B "+index);
}
}
}

Changing an Activity in Android

I'm just learning android here, and Java to be honest. I'm just trying to switch to a different activity via a button click, however, it keeps crashing. It crashes when I click the button and go to make the switch. Can someone please help me figure where I'm going wrong?
First Activity:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class TicTacToe extends Activity {//Start TicTactToe Class
//CONSTANTS
//Variables
Button mainButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
mainButton = (Button) findViewById(R.id.bPlayNow);
mainButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), playerMenu.class);
startActivity(i);
}
});
}
}//End TicTacToe Class
Activity I'm jumping to:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class playerMenu extends Activity {
Button bOnePlayer, bTwoPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bTwoPlayer = (Button) findViewById(R.id.bTwoPlayer);
bTwoPlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the XML for the first activity:
<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="#000000"
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=".TicTacToe" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="50dp"
android:text="Welcome to \nTic-Tac-Toe"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bPlayNow"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textSize="34dp"
android:text="Play now!" />
</RelativeLayout>
XML for the second activity:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_x="78dp"
android:layout_y="30dp"
android:text="GAME MODE"
android:textColor="#FFFFFF"
android:textSize="50dp" />
<Button
android:id="#+id/bOnePlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="160dp"
android:text="Single Player"
android:textSize="25dp" />
<Button
android:id="#+id/bTwoPlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="220dp"
android:text="Two Player"
android:textSize="25dp" />
</AbsoluteLayout>
For starting a new activity, it is necessary to add it in the manifest file.
<application >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
then in onCreate(), for starting a new activity when the button is pressed, you can do:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(TicTacToe.this, playerMenu.class);
startActivity(intent);
}
});
}
Make sure that you define playerMenu in the AndroidManifest file.
<application ... >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
Check this page for more information
http://developer.android.com/training/basics/firstapp/starting-activity.html
Are you sure that you have register your PlayerMenu activity in AndroidManifest.xml ?
...
please paste the error log.
use this code in your first Activity to jump to another
onClick(View v){
Intent ps = new Intent (TicTacToe.this,
PlayerMenu.class);
startActivity(ps);
}
Manifest File :
<Activity
android:name="com.example.killacatoe.TicTacToe">
</Activity>

Public class must be defined in its own file

I am trying to use two views in one application to come up with the user's planetary weight. After rewriting the java several times, I finally have it working... mostly, but on the public class Planets it tells me "The public type planets must be defined in its own file." I went into the manifest and made an activity for it, but that didn't help anything. Planets is already the name of one of my xml files. How do I make the public type into its own file?
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" >
<TextView
android:id="#+id/askwtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="19dp"
android:text="#string/askwt" />
<EditText
android:id="#+id/inputwtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/askwtTextView"
android:layout_below="#+id/askwtTextView"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/inputwtEditText"
android:layout_below="#+id/inputwtEditText"
android:layout_marginTop="38dp"
android:onClick="buttonclick"
android:text="#string/enter" />
</RelativeLayout>
Planets.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/planetTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/planet" />
<TextView
android:id="#+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/mercuryRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mercury" />
<RadioButton
android:id="#+id/venusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/venus" />
<RadioButton
android:id="#+id/earthRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/earth" />
<RadioButton
android:id="#+id/marsRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mars" />
<RadioButton
android:id="#+id/jupiterRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/jupiter" />
<RadioButton
android:id="#+id/saturnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/saturn" />
<RadioButton
android:id="#+id/uranusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/uranus" />
<RadioButton
android:id="#+id/neptuneRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/neptune" />
<RadioButton
android:id="#+id/plutoRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pluto" />
</RadioGroup>
<Button
android:id="#+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonclick2"
android:text="#string/select" />
<TextView
android:id="#+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout
JAVA:
package com.deitel.planetaryweight;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends Activity {
/* You should get used to declaring everything with the correct visibility. Good practice is to make everything private and use public mutator methods */
//Global variable
private double weight;
private Button enter; // creates a button
// Views
private EditText wtEntry;
private TextView txtForm2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Start with first screen
setContentView(R.layout.activity_main);
enter = (Button) findViewById(R.id.enterButton);
//creates an editext and assigns the resource id of the xml edittext.
wtEntry = (EditText)findViewById(R.id.inputwtEditText);
txtForm2 = (TextView)findViewById(R.id.textViewform2);
}
// Button clicks shouldn't do anything but perform clicky actions. Leave field initialization, view creation, etc to the Activity.
//buttonclick for form 1
public void buttonclick(View view){
//Receives the input from the edittext, converts it to a double (number).
weight = Double.parseDouble(wtEntry.getText().toString());
TextView t2 = null;
//change the value of the textview on screen 2 to the calculation value
t2.setText(Double.toString(weight));
// If you want a new layout, it's best to start a new activity.
// It looks like you want to get information back, so use startActivityForResult().
// setContentView(R.layout.planets);
Intent dataIntent = new Intent(this, Planets.class);
dataIntent.putExtra("com.deitel.identifier.DATA_WEIGHT", weight);
startActivityForResult(dataIntent, Activity.RESULT_OK);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check that the resultCode is the same as we started the activity with
if(resultCode == Activity.RESULT_OK){
// get the double from the Intent, using the same string name (package prefixed)
// or a default value if it didn't get set.
double resultWeight = data.getDoubleExtra("com.deitel.identifier.RESULT_WEIGHT", 0.0);
// Now do something with resultWeight
}
}
}
// PlanetChooser.class
public class Planets extends Activity {
// constants, usually denoted by uppercase and declared static and final
public static final double MERCURYFORCE = 0.38;
public static final double VENUSFORCE = 0.91;
public static final double EARTHFORCE = 1.00;
public static final double MARSFORCE = 0.38;
public static final double JUPITERFORCE = 2.34;
public static final double SATURNFORCE = 1.06;
public static final double URANUSFORCE = 0.92;
public static final double NEPTUNEFORCE = 1.19;
public static final double PLUTOFORCE = 0.06;
private RadioButton mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto;
// No need to use the Double object as opposed to the primitive unless you have good reason
private double mercurypf, venuspf, earthpf, marspf, jupiterpf, saturnpf, uranuspf, neptunepf, plutopf, weight;
// One variable will suffice, it seems.
private double resultForce;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.layout.planets);
mercury = (RadioButton) findViewById(R.id.mercuryRadio);
venus = (RadioButton) findViewById(R.id.venusRadio);
earth = (RadioButton) findViewById(R.id.earthRadio);
mars = (RadioButton) findViewById(R.id.marsRadio);
jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
saturn = (RadioButton) findViewById(R.id.saturnRadio);
uranus = (RadioButton) findViewById(R.id.uranusRadio);
neptune = (RadioButton) findViewById(R.id.neptuneRadio);
pluto = (RadioButton) findViewById(R.id.plutoRadio);
}
public void buttonclick2(View view){
/*
It looks to me here you're looking to see which box is checked, and set a value based on
that planet. Since instance variables (in this case mercurypf, jupiterpf, etc) are initialized
to the default value (0), there's no need to set them manually.
*/
// Code used to determine which planet RadioButton is checked:
if(mercury.isChecked()) {
resultForce = MERCURYFORCE * weight;
}
if(venus.isChecked()){
resultForce = VENUSFORCE * weight;
}
if(earth.isChecked()){
resultForce = EARTHFORCE * weight;
}
if(mars.isChecked()){
resultForce = MARSFORCE * weight;
}
if(jupiter.isChecked()){
resultForce =JUPITERFORCE * weight;
}
if(saturn.isChecked()){
resultForce = SATURNFORCE * weight;
}
if(uranus.isChecked()){
resultForce = URANUSFORCE * weight;
}
if(neptune.isChecked()){
resultForce = NEPTUNEFORCE * weight;
}
if(pluto.isChecked()){
resultForce = PLUTOFORCE * weight;
}
// Create a new data Intent to pass back to the calling activity, set the result code,
// and manually finish() this activity.
Intent dataIntent = new Intent(this, null);
dataIntent.getDoubleExtra("com.deitel.identifier.RESULT_DATA", resultForce);
setResult(Activity.RESULT_OK, dataIntent);
finish();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deitel.planetaryweight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Planets" android:label="#string/title_planets"></activity>
</application>
</manifest>
Your Java file is named MainActivity.java (the name of the public class it defines). Remove the Planets class from it, and put that class into a Planets.java file. That's pretty much just how Java wants it to be.

Categories

Resources