1. Acticity_main.java
(DemoAppProject\DemoApp\src\main\java\com\example\demoapp\MainActivity.java)
package com.example.demoapp;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button)findViewById(R.id.btnStart);
startBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
finish();
Intent myIntent = new Intent(MainActivity.this,MatchActivity.class);
startActivity(myIntent);
}
});
Button exitBtn = (Button)findViewById(R.id.btnExit);
exitBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.XML
(DemoAppProject\DemoApp\src\main\res\layout\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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="250dip"
android:layout_height="60dip"
android:text="Start"
android:id="@+id/btnStart"
android:layout_marginTop="58dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="250dip"
android:layout_height="60dip"
android:text="Exit"
android:id="@+id/btnExit"
android:layout_marginTop="21dp"
android:layout_below="@+id/btnStart"
android:layout_alignLeft="@+id/btnStart"/>
</RelativeLayout>
Match_activity.java
package com.example.demoapp;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Random;
/**
* Created by ABHA on 6/16/13.
*/
public class MatchActivity extends Activity {
View circleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match);
circleView = (View)findViewById(R.id.circleId);
Random rand = new Random();
int x = rand.nextInt(3);
switch (x) {
case 0:
circleView.setBackgroundColor(getResources().getColor(R.color.green));
circleView.setTag("Green");
break;
case 1:
circleView.setBackgroundColor(getResources().getColor(R.color.red));
circleView.setTag("Red");
break;
case 2:
circleView.setBackgroundColor(getResources().getColor(R.color.blue));
circleView.setTag("Blue");
break;
}
Button redBtn = (Button)findViewById(R.id.btnRed);
Button blueBtn = (Button)findViewById(R.id.btnBlue);
Button greenBtn = (Button)findViewById(R.id.btnGreen);
redBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String c = (String)circleView.getTag();
Intent myIntent = new Intent(MatchActivity.this,ResultActivity.class);
if(c.equalsIgnoreCase("Red")){
myIntent.putExtra("Result",true );
//Toast.makeText(MatchActivity.this, "Correct",Toast.LENGTH_SHORT).show();
}else{
myIntent.putExtra("Result",false );
//Toast.makeText(MatchActivity.this, "Wrong",Toast.LENGTH_SHORT).show();
}
finish();
startActivity(myIntent);
}
});
blueBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String c = (String)circleView.getTag();
Intent myIntent = new Intent(MatchActivity.this,ResultActivity.class);
if(c.equalsIgnoreCase("Blue")){
myIntent.putExtra("Result",true );
//Toast.makeText(MatchActivity.this, "Correct",Toast.LENGTH_SHORT).show();
}else{
myIntent.putExtra("Result",false );
//Toast.makeText(MatchActivity.this, "Wrong",Toast.LENGTH_SHORT).show();
}
finish();
startActivity(myIntent);
}
});
greenBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String c = (String)circleView.getTag();
Intent myIntent = new Intent(MatchActivity.this,ResultActivity.class);
if(c.equalsIgnoreCase("Green")){
myIntent.putExtra("Result",true );
//Toast.makeText(MatchActivity.this, "Correct",Toast.LENGTH_SHORT).show();
}else{
myIntent.putExtra("Result",false );
//Toast.makeText(MatchActivity.this, "Wrong",Toast.LENGTH_SHORT).show();
}
finish();
startActivity(myIntent);
}
});
}
}
Activity_match.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<View android:layout_width="fill_parent"
android:layout_height="200dip"
android:id="@+id/circleId"
android:background="@drawable/circle"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="Red"
android:id="@+id/btnRed"
android:layout_marginTop="50dp"
android:layout_below="@+id/circleId"
android:layout_alignLeft="@+id/circleId"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="Green"
android:id="@+id/btnGreen"
android:layout_marginTop="21dp"
android:layout_below="@+id/btnRed"
android:layout_alignLeft="@+id/btnRed"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="Blue"
android:id="@+id/btnBlue"
android:layout_marginTop="21dp"
android:layout_below="@+id/btnGreen"
android:layout_alignLeft="@+id/btnGreen"
/>
</LinearLayout>
Result_activity.java
package com.example.demoapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by ABHA on 6/16/13.
*/
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView textView = (TextView)findViewById(R.id.textView);
boolean result = getIntent().getBooleanExtra("Result",false);
if(result){
textView.setText(getResources().getString(R.string.win));
}else{
textView.setText(getResources().getString(R.string.failed));
}
Button exitBtn2 = (Button)findViewById(R.id.btnExit2);
exitBtn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
});
}
}
Result_Activity.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:baselineAligned="true">
<TextView
android:layout_width="250dip"
android:layout_height="250dip"
android:text="New Text"
android:id="@+id/textView"
android:layout_marginTop="40dp"
android:layout_gravity="center"
/>
<Button
android:layout_width="250dip"
android:layout_height="60dip"
android:text="Exit"
android:id="@+id/btnExit2"
android:layout_marginTop="58dp"
android:layout_gravity="center"/>
</LinearLayout>
Circle.XML//some error
(DemoAppProject\DemoApp\src\main\res\drawable-hdpi\circle.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
</shape>
Color.XML (DemoAppProject\DemoApp\src\main\res\values\color.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>
No comments:
Post a Comment