You are on page 1of 10

EMMA SAFWANAH BINTI SAHIRIN (D20141067249)

GROUP C

MTS3072- MOBILE APP DEVELOPMENT

ASSIGNMENT 2 (Question 2)

MainActivity.java
package emma.displaydata2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button btnClose;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnClose = (Button)findViewById(R.id.close);
btnClose.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory( Intent.CATEGORY_HOME );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case R.id.menu1: {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),"Menu 1
selected",Toast.LENGTH_LONG).show();
return true;
}

case R.id.menu2: {
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
startActivityForResult(intent, 1);
Toast.makeText(getApplicationContext(), "Menu 2 selected",
Toast.LENGTH_LONG).show();
return true;
}

default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
TextView op1 = (TextView)findViewById(R.id.displayName);

String nama = data.getStringExtra("name");


op1.setText(nama);
}
}
}
}
activity_main.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:background="@color/colorAccent"
android:orientation="vertical"
android:weightSum="1">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Homepage"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="24sp" />

<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.14" />

<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Displayed Name:"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />

<TextView
android:id="@+id/displayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:background="@android:color/background_light"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />

<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.61" />

<Button
android:id="@+id/close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="Close" />

</LinearLayout>
SecondActivity.java
package emma.displaydata2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {


Button back;

protected void onCreate(Bundle savedInstanceState){


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

ImageView picture1 =(ImageView) findViewById(R.id.pic1);


registerForContextMenu(picture1);

ImageView picture2 =(ImageView) findViewById(R.id.pic2);


registerForContextMenu(picture2);

ImageView picture3 =(ImageView) findViewById(R.id.pic3);


registerForContextMenu(picture3);

back = (Button)findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
}
});
}

@Override

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo


menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select your option");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Rename");
menu.add(0, v.getId(), 0, "Delete");
}

@Override

public boolean onContextItemSelected(MenuItem item) {


if(item.getTitle()=="Edit"){
Toast.makeText(getApplicationContext(),"Option 'Edit' has been
choose",Toast.LENGTH_LONG).show();
}

else if(item.getTitle()=="Rename") {
Toast.makeText(getApplicationContext(), "Option 'Rename' has been
choose", Toast.LENGTH_LONG).show();
}
else if(item.getTitle()=="Delete"){
Toast.makeText(getApplicationContext(),"Option 'Delete' has been
choose",Toast.LENGTH_LONG).show();
}

else{
return false;
}
return true;
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:orientation="vertical"
android:weightSum="1">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Menu 1"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="24sp" />

<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.51" />

<ImageView
android:id="@+id/pic1"
android:layout_width="match_parent"
android:layout_height="98dp"
app:srcCompat="@mipmap/image1" />

<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/pic2"
android:layout_width="375dp"
android:layout_height="121dp"
app:srcCompat="@mipmap/image2" />

<TextView
android:id="@+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/pic3"
android:layout_width="382dp"
android:layout_height="111dp"
app:srcCompat="@mipmap/image3" />

<TextView
android:id="@+id/textView15"
android:layout_width="match_parent"
android:layout_height="5dp" />

<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.51" />

<Button
android:id="@+id/btnBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to Homepage" />

</LinearLayout>
ThirdActivity.java
package emma.displaydata2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ThirdActivity extends AppCompatActivity {


Button btnSubmit,back;

protected void onCreate(Bundle savedInstanceState){


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
final EditText etN = (EditText)findViewById(R.id.NameEnter);

btnSubmit = (Button)findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
Intent intent = new Intent();

String name = etN.getText().toString();


intent.putExtra("name", name);
setResult(Activity.RESULT_OK, intent);
Toast.makeText(getApplicationContext(),"Your name has been
submited",Toast.LENGTH_LONG).show();
finish();
}
});

back = (Button)findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(ThirdActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
activity_third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Menu 2"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="24sp" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12" />

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />

<EditText
android:id="@+id/NameEnter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Please enter your name"
android:inputType="textPersonName" />

<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_purple"
android:text="Submit and back to Homepage" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.88" />

<Button
android:id="@+id/btnBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to Homepage" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="emma.displaydata2">

<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" >
</activity>
<activity android:name=".ThirdActivity">
</activity>
</application>

</manifest>

You might also like