Skip to content

Commit 56d7af0

Browse files
committed
Guess the Celebrity App
Guesses the name of celebrity by image
1 parent 9605629 commit 56d7af0

File tree

15 files changed

+408
-0
lines changed

15 files changed

+408
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lenovo.guessthecelebrity;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.lenovo.guessthecelebrity", appContext.getPackageName());
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.lenovo.guessthecelebrity">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<application
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
package com.lenovo.guessthecelebrity;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
import android.os.AsyncTask;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
import android.view.View;
9+
import android.widget.Button;
10+
import android.widget.ImageView;
11+
import android.widget.Toast;
12+
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.InputStreamReader;
16+
import java.net.HttpURLConnection;
17+
import java.net.MalformedURLException;
18+
import java.net.URL;
19+
import java.util.ArrayList;
20+
import java.util.Random;
21+
import java.util.concurrent.ExecutionException;
22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
24+
25+
public class MainActivity extends AppCompatActivity {
26+
27+
ArrayList<String> celebURLs = new ArrayList<String>();
28+
ArrayList<String> celebNames = new ArrayList<String>();
29+
int chosenCeleb = 0;
30+
int locationOfCorrectAnswer = 0;
31+
String[] answers = new String[4];
32+
33+
ImageView imageView;
34+
Button button0;
35+
Button button1;
36+
Button button2;
37+
Button button3;
38+
39+
public void celebChosen(View view) {
40+
41+
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))) {
42+
43+
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_LONG).show();
44+
45+
} else {
46+
47+
Toast.makeText(getApplicationContext(), "Wrong! It was " + celebNames.get(chosenCeleb), Toast.LENGTH_LONG).show();
48+
49+
}
50+
51+
createNewQuestion();
52+
53+
}
54+
55+
public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
56+
57+
58+
@Override
59+
protected Bitmap doInBackground(String... urls) {
60+
61+
try {
62+
63+
URL url = new URL(urls[0]);
64+
65+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
66+
67+
connection.connect();
68+
69+
InputStream inputStream = connection.getInputStream();
70+
71+
Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
72+
73+
return myBitmap;
74+
75+
76+
} catch (MalformedURLException e) {
77+
78+
e.printStackTrace();
79+
80+
} catch (IOException e) {
81+
82+
e.printStackTrace();
83+
84+
}
85+
86+
return null;
87+
}
88+
}
89+
90+
91+
public class DownloadTask extends AsyncTask<String, Void, String> {
92+
93+
94+
@Override
95+
protected String doInBackground(String... urls) {
96+
97+
String result = "";
98+
URL url;
99+
HttpURLConnection urlConnection = null;
100+
101+
try {
102+
103+
url = new URL(urls[0]);
104+
105+
urlConnection = (HttpURLConnection)url.openConnection();
106+
107+
InputStream in = urlConnection.getInputStream();
108+
109+
InputStreamReader reader = new InputStreamReader(in);
110+
111+
int data = reader.read();
112+
113+
while (data != -1) {
114+
115+
char current = (char) data;
116+
117+
result += current;
118+
119+
data = reader.read();
120+
}
121+
122+
return result;
123+
124+
}
125+
catch (Exception e) {
126+
127+
e.printStackTrace();
128+
129+
}
130+
131+
return null;
132+
}
133+
}
134+
135+
@Override
136+
protected void onCreate(Bundle savedInstanceState) {
137+
super.onCreate(savedInstanceState);
138+
setContentView(R.layout.activity_main);
139+
140+
imageView = (ImageView) findViewById(R.id.imageView);
141+
button0 = (Button) findViewById(R.id.button);
142+
button1 = (Button) findViewById(R.id.button2);
143+
button2 = (Button) findViewById(R.id.button3);
144+
button3 = (Button) findViewById(R.id.button4);
145+
146+
DownloadTask task = new DownloadTask();
147+
String result = null;
148+
149+
try {
150+
151+
result = task.execute("http://www.posh24.com/celebrities").get();
152+
153+
String[] splitResult = result.split("<div class=\"sidebarContainer\">");
154+
155+
Pattern p = Pattern.compile("<img src=\"(.*?)\"");
156+
Matcher m = p.matcher(splitResult[0]);
157+
158+
while (m.find()) {
159+
160+
celebURLs.add(m.group(1));
161+
162+
}
163+
164+
p = Pattern.compile("alt=\"(.*?)\"");
165+
m = p.matcher(splitResult[0]);
166+
167+
while (m.find()) {
168+
169+
celebNames.add(m.group(1));
170+
171+
}
172+
173+
174+
} catch (InterruptedException e) {
175+
176+
e.printStackTrace();
177+
178+
} catch (ExecutionException e) {
179+
180+
e.printStackTrace();
181+
182+
}
183+
184+
createNewQuestion();
185+
186+
}
187+
188+
public void createNewQuestion() {
189+
190+
Random random = new Random();
191+
chosenCeleb = random.nextInt(celebURLs.size());
192+
193+
ImageDownloader imageTask = new ImageDownloader();
194+
195+
Bitmap celebImage;
196+
197+
try {
198+
199+
celebImage = imageTask.execute(celebURLs.get(chosenCeleb)).get();
200+
201+
imageView.setImageBitmap(celebImage);
202+
203+
locationOfCorrectAnswer = random.nextInt(4);
204+
205+
int incorrectAnswerLocation;
206+
207+
for (int i=0; i<4; i++) {
208+
209+
if (i == locationOfCorrectAnswer) {
210+
211+
answers[i] = celebNames.get(chosenCeleb);
212+
213+
} else {
214+
215+
incorrectAnswerLocation = random.nextInt(celebURLs.size());
216+
217+
while (incorrectAnswerLocation == chosenCeleb) {
218+
219+
incorrectAnswerLocation = random.nextInt(celebURLs.size());
220+
221+
}
222+
223+
answers[i] = celebNames.get(incorrectAnswerLocation);
224+
225+
226+
}
227+
228+
229+
}
230+
231+
button0.setText(answers[0]);
232+
button1.setText(answers[1]);
233+
button2.setText(answers[2]);
234+
button3.setText(answers[3]);
235+
236+
237+
} catch (Exception e) {
238+
e.printStackTrace();
239+
}
240+
241+
242+
243+
244+
}
245+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/activity_main"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:paddingBottom="@dimen/activity_vertical_margin"
8+
android:paddingLeft="@dimen/activity_horizontal_margin"
9+
android:paddingRight="@dimen/activity_horizontal_margin"
10+
android:paddingTop="@dimen/activity_vertical_margin"
11+
tools:context="com.lenovo.guessthecelebrity.MainActivity">
12+
13+
<GridLayout
14+
android:layout_width="match_parent"
15+
android:layout_height="match_parent"
16+
android:layout_alignParentTop="true"
17+
android:layout_alignParentStart="true"
18+
android:layout_alignParentLeft="true">
19+
20+
<ImageView
21+
android:layout_width="fill_parent"
22+
android:layout_height="wrap_content"
23+
android:layout_column="0"
24+
android:layout_row="0"
25+
android:layout_gravity="fill"
26+
android:id="@+id/imageView" />
27+
28+
<Button
29+
android:layout_width="fill_parent"
30+
android:layout_height="wrap_content"
31+
android:layout_row="1"
32+
android:layout_column="0"
33+
android:tag="0"
34+
android:text=""
35+
android:id="@+id/button"
36+
android:onClick="celebChosen" />
37+
38+
<Button
39+
android:layout_width="fill_parent"
40+
android:layout_height="wrap_content"
41+
android:layout_row="2"
42+
android:layout_column="0"
43+
android:tag="1"
44+
android:text=""
45+
android:id="@+id/button2"
46+
android:onClick="celebChosen" />
47+
48+
<Button
49+
android:layout_width="fill_parent"
50+
android:layout_height="wrap_content"
51+
android:layout_row="3"
52+
android:layout_column="0"
53+
android:tag="2"
54+
android:text=""
55+
android:id="@+id/button3"
56+
android:onClick="celebChosen" />
57+
58+
<Button
59+
android:layout_width="fill_parent"
60+
android:layout_height="wrap_content"
61+
android:layout_row="4"
62+
android:layout_column="0"
63+
android:tag="3"
64+
android:text=""
65+
android:id="@+id/button4"
66+
android:onClick="celebChosen (MainActivity)" />
67+
</GridLayout>
68+
</RelativeLayout>
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Guess The Celebrity</string>
3+
</resources>

0 commit comments

Comments
 (0)