java - Starting Another Activity - Activity not showing my message -
i'm new android developing have experience python , java, decided want make apps, began tutorial developer.android.com.
i'm in build first app > starting activity section.
i made final step without errors, message type in not showing in second activity created when press send button!
(https://developer.android.com/training/basics/firstapp/starting-activity.html#displaymessage) if click link see tutorial, , scroll down bottom, should see message type should pop up, , bigger font size. message find when type small font, default "hello world!"
here's myactivity.java
package com.example.android.myfirstapp; import android.app.fragment; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.content.intent; import android.view.view; import android.widget.edittext; public class myactivity extends appcompatactivity { public final static string extra_message = "com.mycompany.myfirstapp.message"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /* called when user clicks send button */ public void sendmessage(view view) //view must parameter of onclick, view pressed. { // in response intent intent = new intent(this, displaymessageactivity.class); //a context first parameter (this used because activity class subclass of context) edittext edittext = (edittext) findviewbyid(r.id.edit_message); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); }
}
here displaymessageactivity.java
package com.example.android.myfirstapp; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.textview; public class displaymessageactivity extends appcompatactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // message intent intent intent = getintent(); string message = intent.getstringextra(myactivity.extra_message); // create text view textview textview = new textview(this); textview.settextsize(40); textview.settext(message); // set text view activity layout setcontentview(textview); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
}
i'm not sure if guys need androidmanifest.xml, activity_my.xml, or strings.xml it's better safe sorry!
androidmanifest.xml below:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.myfirstapp" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".myactivity" 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=".displaymessageactivity" android:label="@string/title_activity_display_message" > </activity> </application> </manifest>
activity_my.xml below:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation = "horizontal"> <edittext android:id = "@+id/edit_message" android:layout_weight = "1" android:layout_width = "0dp" android:layout_height = "wrap_content" android:hint = "@string/edit_message" /> <button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/button_send" android:onclick = "sendmessage" />
strings.xml below:
<resources> <string name="app_name">my first app!</string> <string name = "edit_message">enter message!</string> <string name = "button_send">send</string> <string name="action_settings">settings</string> <string name="title_activity_main">mainactivity</string> <string name="title_activity_display_message">displaymessageactivity</string> <string name="hello_world">hello</string> </resources>
i apologize of things not indented if didn't indent when copy pasted them on here.
if me, appreciate it, can continue becoming android developer!
again, app simple type message , send, see message pop bigger font size in activity. though mine showing default, small-font-sized "hello world!" when new activity pops when press send.
thank time, , sorry long post!
first of all, replace
intent.putextra(extra_message, message);
with
intent.putextra("extra_message", message);
in myactivity.java
as first argument putextra()
should string name, used further in application. please refer [docs](http://developer.android.com/reference/android/content/intent.html#putextra(java.lang.string, android.os.bundle))
now, in displaymessageactivity.java
write oncreate()
follows
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set parent view setcontentview(r.layout.disp_msg_layout); // message intent intent intent = getintent(); string message = intent.getstringextra("extra_message"); // reference textview , update it's content textview textview = (textview)findviewbyid(r.id.my_text_view); textview.settext(message); }
and declare xml file named disp_msg_layout
in layouts , should like
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="40sp" android:id="@+id/my_text_view"/>
Comments
Post a Comment