I am working on an app which will do the following:
- Allow the first user "Sam" to type his/her message in the editText field.
- Press the "Alex" button at the bottom of the screen to start the new activity ActivityAlex
- Allow user "Alex" to edit the editText field.
- Press the "Sam" button at the bottom of the screen to restart the activity MainActivity
So far, I think I have most of the work done but I'm getting stuck on how to fix the program so it will do what I want it to do.
Here is my logcat
Here is my MainActivity:
package com.chatfriends;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private Button alexButton; //initialize the buttons for "alex" and "sam"
public void initialize(){
alexButton = (Button) findViewById(R.id.button1); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)
alexButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
alexbutton(arg0);
}
public void alexbutton(View v) {
Intent intent = new Intent(MainActivity.this, ActivityAlex.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
startActivity(intent);
}
});
}
}
Here is my ActivityAlex:
package com.chatfriends;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityAlex extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private Button samButton; //initialize the buttons for "alex" and "sam"
public void initialize(){
samButton = (Button) findViewById(R.id.button2); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)
samButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
samButton(arg0);
}
public void samButton(View v) {
Intent intent = new Intent(ActivityAlex.this, MainActivity.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
startActivity(intent);
}
});
}
}
Here is my Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chatfriends"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" tools:ignore="MissingPrefix"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".ActivityAlex" />
</application>
</manifest>
Here are the XML layout files.
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/userName1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button1"
android:onClick="startalexbutton" />
</RelativeLayout>
activityalex.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"
tools:context=".ActivityAlex" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/userName2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button2"
android:onClick="startMainActivity" />
</RelativeLayout>