I have an integer ArrayList and several buttons. Each button corresponds to an index of the ArrayList and I want to display the value of each button's corresponding index as its text. I have everything working with button behavior, etc. The problem is when I go to swap a button's value (index value) with another it will start to display 0s for every value I swap it with. I am always going to be swapping the 0 with something else.
So if I have buttons: b1, b2, b3 with values 0, 1, 2 respectively and I swap the values of b1 and b2. The result becomes 0, 0, 2. This is the swapping function:
public static void swap(ArrayList<Integer> list, int firstInd, int secondInd) {
int temp = list.get(firstInd);
list.set(firstInd, list.get(secondInd));
list.set(secondInd, temp);
}
This swap method works, I have tested it independently using print statements, etc. and there are no duplicate 0s and all of the rest of the numbers remain in the list.
Here is the relevant code:
// class declaration
static ArrayList<Integer> numList = new ArrayList<Integer>();
// onCreate
// initialize ArrayList
// displays initial values on buttons
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.first: {
switchButtonValues(R.id.first);
b1.setText(String.valueOf(numList.get(0)));
updateButtonStates();
break;
}
case R.id.second: {
switchButtonValues(R.id.second);
b2.setText(String.valueOf(numList.get(1)));
updateButtonStates();
break;
}
// etc for all buttons
public static void switchButtonValues(int buttonNum) {
switch(buttonNum) {
case R.id.first: {
if(numList.get(1) == 0) {
swap(numList, 0, 1);
} else if (numList.get(3) == 0) {
swap(numList, 0, 3);
} else {
}
break;
}
case R.id.second: {
// etc. for the buttons
The buttons displaying the initial values is correct. So I know the ArrayList is getting initialized properly. The problem occurs when the swapping occurs. All of the values are getting overwritten with 0 somehow.
Even though 0s are overwriting the other values, the button behavior is working correctly because it knows where the "real" 0 is. I have cleaned the project too.
Why is this happening? Anyone have any ideas?
I did an example, I hope it's near what you expect:
package br.com.bertan.swap.buttons;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SwapButtonsActivity extends Activity {
// class declaration
static ArrayList<Integer> numList = new ArrayList<Integer>();
static ArrayList<Button> btnList = new ArrayList<Button>();
public static void swap(ArrayList<Integer> list, int firstInd, int secondInd) {
int temp = list.get(firstInd);
list.set(firstInd, list.get(secondInd));
list.set(secondInd, temp);
}
private void updateButtonStates(){
int index_0 = -1;
int index_up = -1;
int index_down = -1;
int index_left = -1;
int index_right = -1;
for(int i = 0; i < 16; i++){
if(index_0 >= 0){
if(i != index_up
&& i != index_down
&& i != index_left
&& i != index_right
// && i != index_0
){
btnList.get(i).setEnabled(false);
} else {
btnList.get(i).setEnabled(true);
}
} else {
if(numList.get(i) == 0){
index_0 = i;
if(index_0 > 3){
index_up = index_0 - 4;
}
if(index_0 < 12){
index_down = index_0 + 4;
}
if(index_0 != 0
&& index_0 != 4
&& index_0 != 8
&& index_0 != 12){
index_left = index_0 - 1;
}
if(index_0 != 3
&& index_0 != 7
&& index_0 != 11
&& index_0 != 15){
index_right = index_0 + 1;
}
i = -1;
}
}
}
}
public static void switchButtonValues(int buttonNum) {
int index_0 = -1;
int index_btn = -1;
for(int i = 0; i < 16; i++){
if(index_0 < 0){
if(numList.get(i) == 0){
index_0 = i;
}
}
if(index_btn < 0){
if(buttonNum == btnList.get(i).getId()){
index_btn = i;
}
}
if(index_0 >= 0 && index_btn >=0){
break;
}
}
btnList.get(index_0).setText(btnList.get(index_btn).getText().toString());
btnList.get(index_btn).setText("0");
numList.set(index_0, numList.get(index_btn));
numList.set(index_btn, 0);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initialize ArrayList
for(int i = 0; i < 16; i++){
numList.add(i);
}
final Button btn_01 = (Button) findViewById(R.id.btn_01);
final Button btn_02 = (Button) findViewById(R.id.btn_02);
final Button btn_03 = (Button) findViewById(R.id.btn_03);
final Button btn_04 = (Button) findViewById(R.id.btn_04);
final Button btn_05 = (Button) findViewById(R.id.btn_05);
final Button btn_06 = (Button) findViewById(R.id.btn_06);
final Button btn_07 = (Button) findViewById(R.id.btn_07);
final Button btn_08 = (Button) findViewById(R.id.btn_08);
final Button btn_09 = (Button) findViewById(R.id.btn_09);
final Button btn_10 = (Button) findViewById(R.id.btn_10);
final Button btn_11 = (Button) findViewById(R.id.btn_11);
final Button btn_12 = (Button) findViewById(R.id.btn_12);
final Button btn_13 = (Button) findViewById(R.id.btn_13);
final Button btn_14 = (Button) findViewById(R.id.btn_14);
final Button btn_15 = (Button) findViewById(R.id.btn_15);
final Button btn_16 = (Button) findViewById(R.id.btn_16);
btnList.add(btn_01);
btnList.add(btn_02);
btnList.add(btn_03);
btnList.add(btn_04);
btnList.add(btn_05);
btnList.add(btn_06);
btnList.add(btn_07);
btnList.add(btn_08);
btnList.add(btn_09);
btnList.add(btn_10);
btnList.add(btn_11);
btnList.add(btn_12);
btnList.add(btn_13);
btnList.add(btn_14);
btnList.add(btn_15);
btnList.add(btn_16);
OnClickListener click_listener = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_01: {
switchButtonValues(R.id.btn_01);
btn_01.setText(String.valueOf(numList.get(0)));
updateButtonStates();
break;
}
case R.id.btn_02: {
switchButtonValues(R.id.btn_02);
btn_02.setText(String.valueOf(numList.get(1)));
updateButtonStates();
break;
}
case R.id.btn_03: {
switchButtonValues(R.id.btn_03);
btn_03.setText(String.valueOf(numList.get(2)));
updateButtonStates();
break;
}
case R.id.btn_04: {
switchButtonValues(R.id.btn_04);
btn_04.setText(String.valueOf(numList.get(3)));
updateButtonStates();
break;
}
case R.id.btn_05: {
switchButtonValues(R.id.btn_05);
btn_05.setText(String.valueOf(numList.get(4)));
updateButtonStates();
break;
}
case R.id.btn_06: {
switchButtonValues(R.id.btn_06);
btn_06.setText(String.valueOf(numList.get(5)));
updateButtonStates();
break;
}
case R.id.btn_07: {
switchButtonValues(R.id.btn_07);
btn_07.setText(String.valueOf(numList.get(6)));
updateButtonStates();
break;
}
case R.id.btn_08: {
switchButtonValues(R.id.btn_08);
btn_08.setText(String.valueOf(numList.get(7)));
updateButtonStates();
break;
}
case R.id.btn_09: {
switchButtonValues(R.id.btn_09);
btn_09.setText(String.valueOf(numList.get(8)));
updateButtonStates();
break;
}
case R.id.btn_10: {
switchButtonValues(R.id.btn_10);
btn_10.setText(String.valueOf(numList.get(9)));
updateButtonStates();
break;
}
case R.id.btn_11: {
switchButtonValues(R.id.btn_11);
btn_11.setText(String.valueOf(numList.get(10)));
updateButtonStates();
break;
}
case R.id.btn_12: {
switchButtonValues(R.id.btn_12);
btn_12.setText(String.valueOf(numList.get(11)));
updateButtonStates();
break;
}
case R.id.btn_13: {
switchButtonValues(R.id.btn_13);
btn_13.setText(String.valueOf(numList.get(12)));
updateButtonStates();
break;
}
case R.id.btn_14: {
switchButtonValues(R.id.btn_14);
btn_14.setText(String.valueOf(numList.get(13)));
updateButtonStates();
break;
}
case R.id.btn_15: {
switchButtonValues(R.id.btn_15);
btn_15.setText(String.valueOf(numList.get(14)));
updateButtonStates();
break;
}
case R.id.btn_16: {
switchButtonValues(R.id.btn_16);
btn_16.setText(String.valueOf(numList.get(15)));
updateButtonStates();
break;
}
default:
break;
}
}
};
// displays initial values on buttons
for(int i = 0; i < 16; i++){
btnList.get(i).setText(String.valueOf(numList.get(i)));
btnList.get(i).setOnClickListener(click_listener);
}
updateButtonStates();
}
}
And here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_01"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="0"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_02"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_03"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_04"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3"
android:textSize="50dip"
android:textStyle="bold"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_05"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_06"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="5"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_07"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="6"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_08"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
android:textSize="50dip"
android:textStyle="bold"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_09"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="8"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_10"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="9"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_11"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="10"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_12"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="11"
android:textSize="50dip"
android:textStyle="bold"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_13"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="12"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_14"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="13"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_15"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="14"
android:textSize="50dip"
android:textStyle="bold"
/>
<Button
android:id="#+id/btn_16"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="15"
android:textSize="50dip"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
</LinearLayout>
that's worked just fine, and really quite a funny when you shuffle the places and try to put then in order... hehehe
It's simple and was one good idea for a game :D (I will do some sort of this genre and let see what happens)
I think that's possible to better the code, I had suspect of this part of your code:
case R.id.btn_01: {
switchButtonValues(R.id.btn_01);
btn_01.setText(String.valueOf(numList.get(0)));
updateButtonStates();
break;
}
I thinked you switch the values, and next set the text with the numList value of that position, but, you had changed with the 0, isn't? Maybe it's your problem, try put the line that set the text before the switch...
cya,
Bertan.
Related
The problem i'm having is the inability for the app to perform the task i designed it to.
i run the app successfully but when i click any of the spinners the list doesn't drop down at all. None of the spinners is responding at all and i don't know the problem.
I've tried everything to sort this out but i can't.
i run the code many times i don't get an error message but still the spinners don't work.
please can anyone help out?
This app is suppose to convert length, area and temperature.
String.xml file
<resources>
<string name="spinner_1">From</string>
<string name="spinner_2">To</string>
<string-array name="conversion_type">
<item>Length</item>
<item>Area</item>
<item>Temperature</item>
</string-array>
<string-array name="area">
<item>Square kilometre</item>
<item>Square metre</item>
<item>Square yard</item>
<item>Square foot</item>
<item>Square inch</item>
<item>Square mile</item>
<item>Hectare</item>
<item>Acre</item>
</string-array>
<string-array name="length">
<item>Kilometre</item>
<item>Metre</item>
<item>Centimetre</item>
<item>Millimetre</item>
<item>Micrometer</item>
<item>Mile</item>
<item>Yard</item>
<item>Foot</item>
<item>Inch</item>
<item>Nautical Mile</item>
</string-array>
<string-array name="temperature">
<item>Celsius</item>
<item>Kelvin</item>
<item>Fahrenheit</item>
</string-array>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Conversion Type"
android:layout_marginTop="10dp"
android:layout_marginLeft="12dp"
android:textSize="16sp"
android:textColor="#607D8B"
android:textStyle="bold"
android:id="#+id/text"/>
<LinearLayout
android:layout_below="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/first_linear"
android:layout_marginRight="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:background="#cbcbcb"/>
<Spinner
android:id="#+id/conversion_type"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_height="1dp"
android:id="#+id/text_some"
android:background="#cbcbcb"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_below="#id/first_linear"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:id="#+id/second_linear"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_from"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:text="From"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#607D8B"
android:layout_below="#id/first_linear"/>
<Spinner
android:id="#+id/from_spinner"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:hint="edit_text_from"
android:id="#+id/from_edit"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:inputType="numberSigned|numberDecimal"
android:layout_marginLeft="30dp" />
<Button
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="Clear"
android:layout_marginBottom="2dp"
android:id="#+id/clear_text_unit"
android:onClick="clear_unit"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="30dp"
android:id="#+id/third_text"
android:layout_below="#+id/second_linear"
android:layout_marginBottom="0dp"
android:background="#FFCBCBCB"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/third_text"
android:id="#+id/fourth_linear"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_to"
android:layout_marginTop="20dp"
android:layout_marginLeft="6dp"
android:text="To"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#607D8B" />
<Spinner
android:id="#+id/to_spinner"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:background="#e6e5e5"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:maxLines="1"
android:scrollHorizontally="true"
android:layout_marginTop="20dp"
android:id="#+id/to_text"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginRight="30dp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="40dp"
android:layout_below="#+id/fourth_linear"
android:layout_marginBottom="10dp"
android:background="#FFCBCBCB"/>
</RelativeLayout>
MainActivity.java file
package com.example.learnspinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
int spinner_cycle_pos = 0, spinner_duration_pos = 0;
Spinner from_spinner, to_spinner, conversion_type, duration_spinner, cycle_spinner;
String from_spinner_string, to_spinner_string, choice_spinner;
int from_spinner_position, to_spinner_position, choice_spinner_pos;
Button btnConvert;
TextView result;
EditText quantity;
ArrayAdapter<CharSequence> adapter_length, adapter_temperature, adapter_area;
BigDecimal[][] array = {{new BigDecimal("1"), new BigDecimal("1000000.0"), new BigDecimal("1195990.05"), new BigDecimal("10763910.4"), new BigDecimal("1550000000"), new BigDecimal("0.386102"), new BigDecimal("100"), new BigDecimal("247.105")},
{new BigDecimal("1"), new BigDecimal("0.125"), new BigDecimal("0.000122073125"), new BigDecimal("0.00000011920992"), new BigDecimal("0.0000000001164153"), new BigDecimal("0.0000000000001136868377"), new BigDecimal("0.000000000000001110223024")},
{new BigDecimal("1"), new BigDecimal("0.001"), new BigDecimal("0.000239006"), new BigDecimal("0.00000027778"), new BigDecimal("0.737562")},
{new BigDecimal("1"), new BigDecimal("0.001"), new BigDecimal("0.000001"), new BigDecimal("0.000000001")},
{new BigDecimal("1"), new BigDecimal("1000"), new BigDecimal("1000000"), new BigDecimal("1000000000"), new BigDecimal("1000000000000"), new BigDecimal("157.473"), new BigDecimal("2204.62"), new BigDecimal("35274")},
{new BigDecimal("1"), new BigDecimal("1000"), new BigDecimal("100000"), new BigDecimal("10000000000"), new BigDecimal("10000000000000"), new BigDecimal("0.621371"), new BigDecimal("1093.61"), new BigDecimal("3280.84"), new BigDecimal("39370.1"), new BigDecimal("0.539953")},
{new BigDecimal("1"), new BigDecimal("1.1111111300619"), new BigDecimal("0.0174533"), new BigDecimal("17.453300")},
{new BigDecimal("1"), new BigDecimal("1.01325"), new BigDecimal("101325"), new BigDecimal("760")},
{new BigDecimal("1"), new BigDecimal("1.46667"), new BigDecimal("0.44704"), new BigDecimal("1.60934"), new BigDecimal("0.868976")},
{}, // keep blank for temperature
{new BigDecimal("1"), new BigDecimal("0.001"), new BigDecimal("0.000016667"), new BigDecimal("0.00000027778"), new BigDecimal("0.000000011574"), new BigDecimal("0.0000000016534"), new BigDecimal("0.00000000038052"), new BigDecimal("0.00000000003171"), new BigDecimal("0.000000000003171"), new BigDecimal("0.0000000000003171")},
{new BigDecimal("1"), new BigDecimal("1000"), new BigDecimal("0.219969"), new BigDecimal("0.879877"), new BigDecimal("3.51951"), new BigDecimal("0.0353147"), new BigDecimal("61.0237"), new BigDecimal("61023.7")}};
String val = "0";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void initialise_adapters() {
adapter_temperature = ArrayAdapter.createFromResource(getApplication(), R.array.temperature, android.R.layout.simple_spinner_item);
adapter_length = ArrayAdapter.createFromResource(getApplication(), R.array.length, android.R.layout.simple_spinner_item);
adapter_area = ArrayAdapter.createFromResource(getApplication(), R.array.area, android.R.layout.simple_spinner_item);
}
private void unit_converter() {
from_spinner = findViewById(R.id.from_spinner);
to_spinner = findViewById(R.id.to_spinner);
result = findViewById(R.id.to_text);
quantity = findViewById(R.id.from_edit);
conversion_type = (Spinner) findViewById(R.id.conversion_type);
// Initialise the main spinner
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplication(), R.array.conversion_type, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
conversion_type.setAdapter(adapter);
conversion_type.setOnItemSelectedListener(new spinner());
from_spinner = (Spinner) findViewById(R.id.from_spinner);
to_spinner = (Spinner) findViewById(R.id.to_spinner);
initialise_adapters();
//initally set default to area adapter
adapter_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_area);
to_spinner.setAdapter(adapter_area);
from_spinner_string = "Square kilometer";
to_spinner_string = "Square kilometer";
choice_spinner = "Area";
//Default values
from_spinner_position = 0;
to_spinner_position = 0;
choice_spinner_pos = 0;
int spinner_cycle_pos = 0, spinner_duration_pos = 0;
from_spinner.setOnItemSelectedListener(new spinner());
to_spinner.setOnItemSelectedListener(new spinner());
quantity.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
val = s.toString();
int flag = 0;
if (val.equals("")) {
flag = 1;
val = "0";
}
if (val.equals("-") || val.equals(".")) flag = 1;
if (choice_spinner.equals("Temperature")) {
if (flag == 0) {
double celcius = Double.parseDouble(val);
if (from_spinner_position == 2) {
celcius = celcius - Double.parseDouble("32");
celcius = celcius * Double.parseDouble("0.55555");
} else if (from_spinner_position == 1)
celcius = celcius - Double.parseDouble("273.15");
if (to_spinner_position == 2) {
celcius = celcius * Double.parseDouble("1.8");
celcius = celcius + Double.parseDouble("32");
} else if (to_spinner_position == 1)
celcius = celcius + Double.parseDouble("273.15");
print_exponent(result, celcius);
} else {
result.setText("");
}
} else {
if (flag == 0) {
double temp = Double.parseDouble(val);
temp = temp / Double.parseDouble(array[choice_spinner_pos][from_spinner_position].toString());
temp = temp * Double.parseDouble(array[choice_spinner_pos][to_spinner_position].toString());
temp = temp / Double.parseDouble(array[choice_spinner_pos][0].toString());
print_exponent(result, temp);
} else {
result.setText("");
}
}
}
});
}
//Method to print the numbers in exponent form
public void print_exponent(TextView view, double temp) {
String temp1 = "" + temp;
String temp2 = "";
int bds = 0;
for (char c : temp1.toCharArray()) {
if (c == 'E') {
temp2 += " e ";
bds = 1;
} else {
if (bds == 1) {
if (c == '-')
temp2 += c;
else
temp2 = temp2 + "+" + c;
} else
temp2 += c;
bds = 0;
}
}
view.setText(temp2);
}
//This method takes a big decimal number and convert that to the exponent from and scale is mantissa
private static String format(BigDecimal x, int scale) {
NumberFormat formatter = new DecimalFormat("0.0E0");
formatter.setRoundingMode(RoundingMode.HALF_UP);
formatter.setMinimumFractionDigits(scale);
return formatter.format(x);
}
//Spinner class to select spinner and also add gender
class spinner implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.conversion_type) {
quantity.getText().clear();
result.setText("");
choice_spinner = parent.getItemAtPosition(position).toString();
choice_spinner_pos = position;
from_spinner.setEnabled(true);
to_spinner.setEnabled(true);
if (choice_spinner.equals("Area") == true) {
adapter_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_area);
to_spinner.setAdapter(adapter_area);
} else if (choice_spinner.equals("Length") == true) {
adapter_length.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_length);
to_spinner.setAdapter(adapter_length);
} else if (choice_spinner.equals("Temperature") == true) {
adapter_temperature.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_temperature);
to_spinner.setAdapter(adapter_temperature);
} else if (parent.getId() == R.id.from_spinner) {
quantity.getText().clear();
result.setText("");
from_spinner_string = parent.getItemAtPosition(position).toString();
from_spinner_position = position;
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(), "Please Select a category", Toast.LENGTH_SHORT).show();
from_spinner.setEnabled(false);
to_spinner.setEnabled(false);
}
public void clear_unit (View view){
quantity.setText("");
result.setText("");
val = "";
}
}
}
Root cause
In the MainActivity, the unit_converter() method is responsible for initializing Spinner, set adapters, set listeners for the Spinner, but you forgot to call this method when the activity is starting.
Solution
Call the unit_converter() method when the activity is starting.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unit_converter();
}
Update
When users select from_spinner and to_spinner, you do not update from_spinner_position and to_spinner_position value. Change your code to:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.conversion_type) {
quantity.getText().clear();
result.setText("");
choice_spinner = parent.getItemAtPosition(position).toString();
choice_spinner_pos = position;
from_spinner.setEnabled(true);
to_spinner.setEnabled(true);
if (choice_spinner.equals("Area") == true) {
adapter_area.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_area);
to_spinner.setAdapter(adapter_area);
} else if (choice_spinner.equals("Length") == true) {
adapter_length.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_length);
to_spinner.setAdapter(adapter_length);
} else if (choice_spinner.equals("Temperature") == true) {
adapter_temperature.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
from_spinner.setAdapter(adapter_temperature);
to_spinner.setAdapter(adapter_temperature);
} else if (parent.getId() == R.id.from_spinner) {
quantity.getText().clear();
result.setText("");
from_spinner_string = parent.getItemAtPosition(position).toString();
from_spinner_position = position;
}
} else if (parent.getId() == R.id.from_spinner) {
from_spinner_position = position;
} else if (parent.getId() == R.id.to_spinner) {
to_spinner_position = position;
}
}
**activity_main 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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.hynes.equations.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please select what type of equation to solve"
android:id="#+id/textView2" />
<Button
android:text="Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3"
android:layout_marginRight="230dp"
android:layout_marginEnd="230dp"
android:id="#+id/button2" />
<TextView
android:layout_centerVertical="true"
android:id="#+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="50dp"
android:layout_width="250dp"
android:layout_alignRight="#+id/textView2"
android:layout_alignEnd="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_above="#+id/button2"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginRight="170dp"
android:layout_marginEnd="170dp"
android:layout_marginBottom="36dp"
android:id="#+id/editText2" />
<RadioGroup android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:text="Addition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="26dp"
android:id="#+id/addition"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Subtraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="#+id/subtraction"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:id="#+id/division"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Multiplication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:id="#+id/multiplication"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
`
MAIN ACTIVITY
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;`
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int x;
private int y;
public int a;
public int b;
Random random = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View view){
TextView display = (TextView) findViewById(R.id.textView5);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int btnID = radioGroup.getCheckedRadioButtonId();
switch(btnID){
case R.id.addition:
x = random.nextInt(100);
y = random.nextInt(100);
display.setText(x + "+" + y + "=");
break;
case R.id.subtraction:
x = random.nextInt(100);
y = random.nextInt(100);
display.setText(x + "-" + y + "=");
break;
case R.id.division:
y = random.nextInt(5);
x = y*2;
display.setText(x + "/" + y + "=");
break;
case R.id.multiplication:
x = random.nextInt(10);
y = random.nextInt(10);
display.setText(x + "*" + y + "=");
break;
}
public void onClickCheck(View view){
TextView result = (TextView) findViewById(R.id.textView5);
EditText ans = (EditText) findViewById(R.id.editText2);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int btnID = radioGroup.getCheckedRadioButtonId();
if(boolean(RadioButton) findViewById(R.id.addition)) ){
int key = x+y;
int try = 0;
while(!ans.equals(key)){
try = try + 1;
if(try ==0){
result.setText("Incorrect, try again!");
try++
}else if (try>2){
result.setText("Incorrect, the answer is : " + key);
}
if(ans.equals(key)){
result.setText("You are correct !");
}
}
}
}
}
}
}
This is a android studio app that's suppose to randomly generate a math equation upon creation which I've done. There's addition, subtraction, division, and multiplication. My random equation generator works perfectly. But I'm stuck at my onClickCheck method which i want to get the user's answer/input and check to see if it's correct or incorrect and display a message accordingly. My onClick method is wrong but it's a start. I don't know what methods i need to get my code to accept inputs properly. Any help would be appreciated.
It looks like you aren't assigning an OnClickListener to the button.
You need to put:(in onCreate)
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(onClickCheck);
or
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickCheck(view);
}
});
Same for the radio button.
This app translates form specific graffiti shapes to text.
I have 3 categories, letters, numbers and other characters.
The starting point defines the category to choose the output from. It works will on the emulator but with changing phones (resolution) it starts to get wrong.
This is my activity for drawing the graffiti and the TextView that show the result.
This is the activity 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.soltan.app1.MainActivity"
android:background="#0e004e"
android:animateLayoutChanges="false"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/res"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffffff"
android:textDirection="anyRtl"
android:hint="#string/mess"
android:layout_above="#+id/chars" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="105dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff"
android:layout_marginTop="5dp"
android:id="#+id/letters"
android
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/txt3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textDirection="anyRtl"
android:hint="#string/txt3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="175dp"
android:layout_height="90dp"
android:layout_above="#+id/letters"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff"
android:layout_marginTop="5dp"
android:id="#+id/chars"
android:layout_alignParentRight="false"
android:layout_marginRight="5dp"
android:layout_alignParentEnd="false"
android:focusable="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/txt1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textDirection="anyRtl"
android:hint="#string/txt1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="175dp"
android:layout_height="90dp"
android:layout_above="#+id/letters"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#fff"
android:layout_toRightOf="#+id/chars"
android:id="#+id/numbers">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/txt2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textDirection="anyRtl"
android:hint="#string/txt2" />
</RelativeLayout>
</RelativeLayout>
This is how I choose the category for now i the java file:
private List<Point> input; // a list contains the drawn graffiti shape coordinates
private Input inserted; // instance of a class
String section; // the category from which we receive the output
boolean proceed ; // if the starting point in the allowed range
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onTouchEvent(MotionEvent touchEvent)
{
super.onTouchEvent(touchEvent);
switch(touchEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
input = new ArrayList<>();
if(touchEvent.getY() < 1216)
{
proceed = false;
}
else if(touchEvent.getY() > 1465)
{
proceed = true;
section = "letter";
}
else if(touchEvent.getX() < 506)
{
proceed = true;
section = "char";
}
else
{
proceed = true;
section = "number";
}
//Inserting the touch event points into the array list of points
for (int h = 0; h < touchEvent.getHistorySize(); h++)
{
for (int p = 0; p < touchEvent.getPointerCount(); p++)
{
float x = touchEvent.getHistoricalX(p,h);
float y = touchEvent.getHistoricalY(p,h);
input.add(new Point(x,y));
}
}
break;
}
case MotionEvent.ACTION_MOVE:
{
//Inserting the touch event points into the array list of points
for (int h = 0; h < touchEvent.getHistorySize(); h++)
{
for (int p = 0; p < touchEvent.getPointerCount(); p++)
{
float x = touchEvent.getHistoricalX(p,h);
float y = touchEvent.getHistoricalY(p,h);
input.add(new Point(x,y));
}
}
break;
}
case MotionEvent.ACTION_UP:
{
//Inserting the touch event points into the array list of points
for (int h = 0; h < touchEvent.getHistorySize(); h++)
{
for (int p = 0; p < touchEvent.getPointerCount(); p++)
{
float x = touchEvent.getHistoricalX(p,h);
float y = touchEvent.getHistoricalY(p,h);
input.add(new Point(x,y));
}
}
if(proceed)
{
inserted = new Input();
String letter =inserted.checkPoint(input,section);
if(letter.equals(""))
{
Toast.makeText(this,"No Such Graffiti, check the our dictionary!",Toast.LENGTH_SHORT).show();
}
TextView myText =(TextView) findViewById(R.id.res);
String text = myText.getText().toString();
myText.setText(text+letter);
}
break;
}
}
return true;
}
it looks to me that you're trying to get the touchEvents from the topmost view and according to the coordinates determine which child is touched? If so this is very wrong. The way to go is to register for the children themselves the onClick or onTouch listener and just check the view's id in there to determine which one was touched.
Edit:
The easiest way to do this is to add this to all three RelativeLayouts that you gave an id:
android:onClick="myMethod"
And then add this in your activity
public void myMethod(View view) {
switch(view.getId()) {
case R.id.letters:
//do here what you wanna do with letters
break;
case R.id.chars:
//do here what you wanna do with chars
break;
case R.id.numbers:
//do here what you wanna do with numbers
break;
}
}
you can of course rename the method as you like as long as it is the same as you specified in the xml
This is the code.
What more information do i need to put here?
I'm trying to find out why my text and button views are widening and then returning to normal during running of this code.
It keeps saying i need more details so i guess im just gonna talk about some nonsense here.
package com.notesquirrel.johnald.memorymagic;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GameActivity extends AppCompatActivity implements View.OnClickListener {
Animation wobble;
SharedPreferences prefs;
SharedPreferences.Editor editor;
String dataName = "MyData";
String intName = "MyInt";
int defaultInt = 0;
int highScore;
TextView textScore;
TextView textDifficulty;
TextView textWatchGo;
Button button1;
Button button2;
Button button3;
Button button4;
Button buttonRestart;
int difficultyLevel = 3;
int[] sequenceToCopy = new int[100];
private Handler myHandler;
boolean playSequence = false;
int elementToPlay = 0;
int playerResponses;
int playerScore;
boolean isResponding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
wobble = AnimationUtils.loadAnimation(this, R.anim.wobble);
prefs = getSharedPreferences(dataName, MODE_PRIVATE);
editor = prefs.edit();
highScore = prefs.getInt(intName, defaultInt);
final MediaPlayer powerup7 = MediaPlayer.create(this, R.raw.poweru);
final MediaPlayer powerup8 = MediaPlayer.create(this, R.raw.poweru2);
final MediaPlayer powerup9 = MediaPlayer.create(this, R.raw.poweru3);
final MediaPlayer powerup10 = MediaPlayer.create(this, R.raw.poweru4);
textScore = (TextView) findViewById(R.id.textScore);
assert textScore != null;
textScore.setText("Score: " + playerScore);
textDifficulty = (TextView) findViewById(R.id.textDifficulty);
assert textDifficulty != null;
textDifficulty.setText("Level: " + difficultyLevel);
textWatchGo = (TextView) findViewById(R.id.textWatchGo);
button1 = (Button) findViewById(R.id.button21);
button2 = (Button) findViewById(R.id.button32);
button3 = (Button) findViewById(R.id.button43);
button4 = (Button) findViewById(R.id.button54);
buttonRestart = (Button) findViewById(R.id.buttonRestart);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
buttonRestart.setOnClickListener(this);
//thread
myHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (playSequence) {
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
button4.setVisibility(View.VISIBLE);
switch (sequenceToCopy[elementToPlay]) {
case 1:
// button1.setVisibility(View.INVISIBLE);
button1.startAnimation(wobble);
powerup7.start();
break;
case 2:
//button2.setVisibility(View.INVISIBLE);
button2.startAnimation(wobble);
powerup8.start();
break;
case 3:
//button3.setVisibility(View.INVISIBLE);
button3.startAnimation(wobble);
powerup9.start();
break;
case 4:
// button4.setVisibility(View.INVISIBLE);
button4.startAnimation(wobble);
powerup10.start();
break;
}
elementToPlay++;
if (elementToPlay == difficultyLevel) {
sequenceFinished();
}
}
myHandler.sendEmptyMessageDelayed(0, 900);
}
};
myHandler.sendEmptyMessage(0);
playASequence();
}
#Override
public void onClick(View v) {
final MediaPlayer powerup7 = MediaPlayer.create(this, R.raw.poweru);
final MediaPlayer powerup8 = MediaPlayer.create(this, R.raw.poweru2);
final MediaPlayer powerup9 = MediaPlayer.create(this, R.raw.poweru3);
final MediaPlayer powerup10 = MediaPlayer.create(this, R.raw.poweru4);
if (!playSequence) {
switch (v.getId()) {
case R.id.button21:
powerup7.start();
checkElement(1);
break;
case R.id.button32:
powerup8.start();
checkElement(2);
break;
case R.id.button43:
powerup9.start();
checkElement(3);
break;
case R.id.button54:
powerup10.start();
checkElement(4);
break;
case R.id.buttonRestart:
difficultyLevel = 3;
playerScore = 0;
textScore.setText("Score: " + playerScore);
playASequence();
break;
}
}
}
public void createSequence() {
//For choosing a random button
Random randInt = new Random();
int ourRandom;
for (int i = 0; i < difficultyLevel; i++) {
//get a random number between 1 and 4
ourRandom = randInt.nextInt(4);
ourRandom++;//make sure it is not zero
//Save that number to our array
sequenceToCopy[i] = ourRandom;
}
}
public void playASequence() {
createSequence();
isResponding = false;
elementToPlay = 0;
playerResponses = 0;
textWatchGo.setText("WATCH!");
playSequence = true;
}
public void sequenceFinished() {
playSequence = false;
//make sure all the buttons are made visible
// button1.setVisibility(View.VISIBLE);
// button2.setVisibility(View.VISIBLE);
// button3.setVisibility(View.VISIBLE);
// button4.setVisibility(View.VISIBLE);
textWatchGo.setText("GO!");
isResponding = true;
}
public void checkElement(int thisElement) {
if (isResponding) {
playerResponses++;
if (sequenceToCopy[playerResponses - 1] == thisElement) {//Correct
playerScore = playerScore + ((thisElement + 1) * 2);
textScore.setText("Score: " + playerScore);
if (playerResponses == difficultyLevel) {//got the whole sequence
//don't checkElelment anymore
isResponding = false;
//now raise the difficulty
difficultyLevel++;
//and play another sequence
playASequence();
}
} else {//wrong answer
textWatchGo.setText("FAILED!");
//don't checkElelment anymore
isResponding = false;
if (playerScore > highScore) {
highScore = playerScore;
editor.putInt(intName, highScore);
editor.commit();
Toast.makeText(getApplicationContext(), "New high score!!", Toast.LENGTH_LONG).show();
}
}
}
}
}
XML layout:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.notesquirrel.johnald.memorymagic.GameActivity"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score: 999"
android:textSize="40sp"
android:id="#+id/textScore"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Difficulty: 4"
android:textSize="25sp"
android:id="#+id/textDifficulty"
android:layout_below="#+id/textScore"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Watch/Go"
android:id="#+id/textWatchGo"
android:layout_marginTop="48dp"
android:textSize="25sp"
android:layout_below="#+id/textDifficulty"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button21"
android:layout_marginTop="50dp"
android:layout_below="#+id/textWatchGo"
android:layout_alignRight="#+id/textScore"
android:layout_alignEnd="#+id/textScore"
android:layout_alignLeft="#+id/textScore"
android:layout_alignStart="#+id/textScore" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button32"
android:layout_marginTop="10dp"
android:layout_below="#+id/button21"
android:layout_alignRight="#+id/button21"
android:layout_alignEnd="#+id/button21"
android:layout_alignLeft="#+id/button21"
android:layout_alignStart="#+id/button21" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_marginTop="10dp"
android:id="#+id/button43"
android:layout_below="#+id/button32"
android:layout_alignRight="#+id/button32"
android:layout_alignEnd="#+id/button32"
android:layout_alignLeft="#+id/button32"
android:layout_alignStart="#+id/button32" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_marginTop="10dp"
android:id="#+id/button54"
android:layout_below="#+id/button43"
android:layout_alignRight="#+id/button43"
android:layout_alignEnd="#+id/button43"
android:layout_alignLeft="#+id/button43"
android:layout_alignStart="#+id/button43" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
android:id="#+id/buttonRestart"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I have no problem with my device. But I removed some constraints.
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.notesquirrel.johnald.memorymagic.GameActivity"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score: 999"
android:textSize="40sp"
android:id="#+id/textScore"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Difficulty: 4"
android:textSize="25sp"
android:id="#+id/textDifficulty"
android:layout_below="#+id/textScore"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Watch/Go"
android:id="#+id/textWatchGo"
android:layout_marginTop="48dp"
android:textSize="25sp"
android:layout_below="#+id/textDifficulty"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button21"
android:layout_marginTop="50dp"
android:layout_below="#+id/textWatchGo"
android:layout_alignRight="#+id/textScore"
android:layout_alignLeft="#+id/textScore"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button32"
android:layout_marginTop="10dp"
android:layout_below="#+id/button21"
android:layout_alignRight="#+id/button21"
android:layout_alignLeft="#+id/button21"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_marginTop="10dp"
android:id="#+id/button43"
android:layout_below="#+id/button32"
android:layout_alignRight="#+id/button32"
android:layout_alignLeft="#+id/button32"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_marginTop="10dp"
android:id="#+id/button54"
android:layout_below="#+id/button43"
android:layout_alignRight="#+id/button43"
android:layout_alignLeft="#+id/button43"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
android:id="#+id/buttonRestart"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
I am developing an android lock screen app. But now I am stuck. Basically I have a whole bunch of buttons on the screen, and I need to be able to register when they drag their finger from one button to the next, and in which order.
How can I do this?
I am trying to use the MotionEvent.ACTION_MOVE in the OnTouch method, but it isn't working. (It only works for button 1, as I am printing out to logcat the ID of the button that is being hovered over, but it wont print for any other button than button 1)
Please advise on how I can do this?
i do this one time, you need get all position of you view ( x and y with yourButton.getLeft() and yourButton.getTop() but be careful because you can get after layout creating don't use this in onCreate()) then in onTouch() method you have id of your button, in onTouch() method you can get position of finger with me.getX(), me.getY(), but this value have a relative with first view, so you must have something link this.
int rightXPos = yourButton.getLeft()+ me.getX() // yourButton is Button that
int rightYPos = yourButton.getTop()+ me.getY() // Toached firstTime
use log for catching me.getX(), me.getY() you understand your self.
then check the rightXPos and rightYPos with your list of position, then you can find out witch button touched
I hope that this useful for you
Edit
this is a sample code that do this, try this.
MainActivity.class
package activity;
import java.util.ArrayList;
import shayan.pourvatan.lowandhigh.R;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import farsiConverter.FarsiDigit;
public class MainActivity extends FragmentActivity implements OnClickListener , OnTouchListener {
ArrayList<Integer> _clickedPos;
ArrayList<Integer> _leftDestanceLine , _topDestanceLine;
TextView tv1 , tv2 , tv3 , tv4 , tv5 , tv6 , tv7 , tv8 , tv9;
TextView line1 , line2 , line3 , line4 , line5 , line6 , line7, line8;
TextView titleEn , titleFa;
boolean _firstTime;
int _count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
_clickedPos = new ArrayList<Integer>();
_leftDestanceLine = new ArrayList<Integer>();
_topDestanceLine = new ArrayList<Integer>();
InitializeTextView();
InitilizeLine();
// detector = new GestureDetector(this,this);
DefaultBackground();
_count = 0;
_firstTime = true;
}
private void InitilizeLine() {
line1 = (TextView) findViewById(R.id.textView21);
line2 = (TextView) findViewById(R.id.textView22);
line3 = (TextView) findViewById(R.id.textView23);
line4 = (TextView) findViewById(R.id.textView24);
line5 = (TextView) findViewById(R.id.textView25);
line6 = (TextView) findViewById(R.id.textView26);
line7 = (TextView) findViewById(R.id.textView27);
line8 = (TextView) findViewById(R.id.textView28);
}
private void InitializeTextView() {
tv1 = (TextView) findViewById(R.id.textView41);
tv2 = (TextView) findViewById(R.id.textView42);
tv3 = (TextView) findViewById(R.id.textView43);
tv4 = (TextView) findViewById(R.id.textView44);
tv5 = (TextView) findViewById(R.id.textView45);
tv6 = (TextView) findViewById(R.id.textView46);
tv7 = (TextView) findViewById(R.id.textView47);
tv8 = (TextView) findViewById(R.id.textView48);
tv9 = (TextView) findViewById(R.id.textView49);
titleEn = (TextView) findViewById(R.id.TitleEn);
titleFa = (TextView) findViewById(R.id.TitleFa);
tv1.setTag(1);
tv2.setTag(2);
tv3.setTag(3);
tv4.setTag(4);
tv5.setTag(5);
tv6.setTag(6);
tv7.setTag(7);
tv8.setTag(8);
tv9.setTag(9);
View main = findViewById(R.id.RelativeLayout1);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this);
tv5.setOnClickListener(this);
tv6.setOnClickListener(this);
tv7.setOnClickListener(this);
tv8.setOnClickListener(this);
tv9.setOnClickListener(this);
tv1.setOnTouchListener(this);
tv2.setOnTouchListener(this);
tv3.setOnTouchListener(this);
tv4.setOnTouchListener(this);
tv5.setOnTouchListener(this);
tv6.setOnTouchListener(this);
tv7.setOnTouchListener(this);
tv8.setOnTouchListener(this);
tv9.setOnTouchListener(this);
main.setOnTouchListener(this);
}
#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;
}
private void ChangingBackGround(int colorFrom , int colorTo, final View v) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
v.setBackgroundColor((Integer)animator.getAnimatedValue());
}
});
colorAnimation.start();
}
#Override
protected void onResume() {
overridePendingTransition(0,0);
super.onResume();
}
private void DefaultBackground() {
tv1.setBackgroundColor(Color.parseColor("#000000"));
tv3.setBackgroundColor(Color.parseColor("#000000"));
tv5.setBackgroundColor(Color.parseColor("#000000"));
tv7.setBackgroundColor(Color.parseColor("#000000"));
tv9.setBackgroundColor(Color.parseColor("#000000"));
tv2.setBackgroundColor(Color.parseColor("#ffffff"));
tv4.setBackgroundColor(Color.parseColor("#ffffff"));
tv6.setBackgroundColor(Color.parseColor("#ffffff"));
tv8.setBackgroundColor(Color.parseColor("#ffffff"));
}
private View GetViewPos(int _tempPos) {
switch (_tempPos) {
case 1: return tv1;
case 2: return tv2;
case 3: return tv3;
case 4: return tv4;
case 5: return tv5;
case 6: return tv6;
case 7: return tv7;
case 8: return tv8;
case 9: return tv9;
default:
break;
}
return null;
}
private int CheckPosition(float X, float Y) {
int pos = -1;
pos = Position1checked(X , Y);
pos = Position2checked(X , Y , pos);
pos = Position3checked(X , Y, pos);
pos = Position4checked(X , Y, pos);
pos = Position5checked(X , Y, pos);
pos = Position6checked(X , Y, pos);
pos = Position7checked(X , Y, pos);
pos = Position8checked(X , Y, pos);
pos = Position9checked(X , Y, pos);
return pos;
}
private int Position9checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(2) && x < _leftDestanceLine.get(3) &&
y > _topDestanceLine.get(2) && y < _topDestanceLine.get(3))
return 5;
return -1;
}
private int Position8checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(1) && x < _leftDestanceLine.get(2) &&
y > _topDestanceLine.get(2) && y < _topDestanceLine.get(3))
return 6;
return -1;
}
private int Position7checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(0) && x < _leftDestanceLine.get(1) &&
y > _topDestanceLine.get(2) && y < _topDestanceLine.get(3))
return 7;
return -1;
}
private int Position6checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(2) && x < _leftDestanceLine.get(3) &&
y > _topDestanceLine.get(1) && y < _topDestanceLine.get(2))
return 4;
return -1;
}
private int Position5checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(1) && x < _leftDestanceLine.get(2) &&
y > _topDestanceLine.get(1) && y < _topDestanceLine.get(2))
return 9;
return -1;
}
private int Position4checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(0) && x < _leftDestanceLine.get(1) &&
y > _topDestanceLine.get(1) && y < _topDestanceLine.get(2))
return 8;
return -1;
}
private int Position3checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(2) && x < _leftDestanceLine.get(3) &&
y > _topDestanceLine.get(0) && y < _topDestanceLine.get(1))
return 3;
return -1;
}
private int Position2checked(float x, float y, int pos) {
if (pos > -1)
return pos;
else
if (x > _leftDestanceLine.get(1) && x < _leftDestanceLine.get(2) &&
y > _topDestanceLine.get(0) && y < _topDestanceLine.get(1))
return 2;
return -1;
}
private int Position1checked(float x, float y) {
if (x > _leftDestanceLine.get(0) && x < _leftDestanceLine.get(1) &&
y > _topDestanceLine.get(0) && y < _topDestanceLine.get(1))
return 1;
return -1;
}
private void FillArrayPos() {
_leftDestanceLine.add(line1.getLeft());
_leftDestanceLine.add(line5.getLeft());
_leftDestanceLine.add(line6.getLeft());
_leftDestanceLine.add(line3.getLeft());
_topDestanceLine.add(line2.getTop());
_topDestanceLine.add(line7.getTop());
_topDestanceLine.add(line8.getTop());
_topDestanceLine.add(line4.getTop());
for (int i = 0 ; i < 4 ; i++)
{
Log.d(""+_leftDestanceLine.get(i),""+_topDestanceLine.get(i) );
}
_firstTime = false;
}
#Override
public boolean onTouch(View v, MotionEvent me) {
if (_firstTime)
FillArrayPos();
if (me.getActionMasked() == MotionEvent.ACTION_UP)
{
Log.d("in action up", "123");
// CheckEquality(_count);
// if _count == 1 then user just
}
int _tempPos = CheckPosition(me.getX()+v.getLeft() , me.getY()+v.getTop());
if (_clickedPos.size() > 0)
{
if (_tempPos == -1){}
else if (_tempPos == _clickedPos.get(_clickedPos.size()-1)){
//change the background of current position
}
else
{
_count++;
View v1 = GetViewPos(_tempPos);
ChangingBackGround(Color.parseColor("#ffffff") , Color.parseColor("#ffa500") , v1 );
_clickedPos.add(_tempPos);
}
}
else
{
if (_tempPos == -1){}
else
{
_count++;
View v1 = GetViewPos(_tempPos);
ChangingBackGround(Color.parseColor("#ffffff") , Color.parseColor("#ffa500") , v1 );
_clickedPos.add(_tempPos);
}
}
return true;
}
}
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >
<TextView
android:id="#+id/TitleEn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TitleFa"
android:layout_below="#+id/TitleFa"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:id="#+id/TitleFa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffa500"
android:textSize="40sp" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#000" />
<TextView
android:id="#+id/textView25"
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView24"
android:layout_alignTop="#+id/textView22"
android:layout_marginRight="45dp"
android:layout_toLeftOf="#+id/textView17"
android:background="#000" />
<TextView
android:id="#+id/textView21"
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView24"
android:layout_alignTop="#+id/textView22"
android:layout_marginRight="90dp"
android:layout_toLeftOf="#+id/textView25"
android:background="#000" />
<TextView
android:id="#+id/textView26"
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView24"
android:layout_alignTop="#+id/textView22"
android:layout_marginLeft="45dp"
android:layout_toRightOf="#+id/textView17"
android:background="#000" />
<TextView
android:id="#+id/textView23"
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView24"
android:layout_alignTop="#+id/textView22"
android:layout_marginLeft="90dp"
android:layout_toRightOf="#+id/textView26"
android:background="#000" />
<TextView
android:id="#+id/textView24"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_above="#+id/textView17"
android:layout_alignLeft="#+id/textView21"
android:layout_alignRight="#+id/textView23"
android:layout_marginBottom="5dp"
android:background="#000" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_above="#+id/textView24"
android:layout_alignLeft="#+id/textView24"
android:layout_alignRight="#+id/textView23"
android:layout_marginBottom="90dp"
android:background="#000" />
<TextView
android:id="#+id/textView27"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_above="#+id/textView28"
android:layout_alignLeft="#+id/textView28"
android:layout_alignRight="#+id/textView23"
android:layout_marginBottom="90dp"
android:background="#000" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_above="#+id/textView27"
android:layout_alignLeft="#+id/textView21"
android:layout_alignRight="#+id/textView23"
android:layout_marginBottom="90dp"
android:background="#000" />
<TextView
android:id="#+id/textView41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView27"
android:layout_alignLeft="#+id/textView22"
android:layout_alignRight="#+id/textView25"
android:layout_alignTop="#+id/textView25"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_toLeftOf="#+id/textView25"
android:background="#000"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="50sp" />
<TextView
android:id="#+id/textView43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView27"
android:layout_alignLeft="#+id/textView26"
android:layout_alignRight="#+id/textView23"
android:layout_alignTop="#+id/textView23"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="#000"
android:gravity="center"
android:text="3"
android:textColor="#fff"
android:textSize="50sp" />
<TextView
android:id="#+id/textView47"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView21"
android:layout_alignLeft="#+id/textView28"
android:layout_alignRight="#+id/textView41"
android:layout_alignTop="#+id/textView45"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:background="#000"
android:gravity="center"
android:text="7"
android:textColor="#fff"
android:textSize="50sp" />
<TextView
android:id="#+id/textView45"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView26"
android:layout_alignLeft="#+id/textView43"
android:layout_alignRight="#+id/textView28"
android:layout_alignTop="#+id/textView28"
android:layout_marginBottom="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="#000"
android:gravity="center"
android:text="9"
android:textColor="#fff"
android:textSize="50sp" />
<TextView
android:id="#+id/textView42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView27"
android:layout_alignTop="#+id/textView41"
android:layout_toLeftOf="#+id/textView43"
android:layout_toRightOf="#+id/textView41"
android:background="#fff"
android:gravity="center"
android:text="2"
android:textColor="#000"
android:textSize="50sp" />
<TextView
android:id="#+id/textView48"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView28"
android:layout_alignLeft="#+id/textView41"
android:layout_alignRight="#+id/textView41"
android:layout_below="#+id/textView41"
android:background="#fff"
android:gravity="center"
android:text="4"
android:textColor="#000"
android:textSize="50sp" />
<TextView
android:id="#+id/textView49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView48"
android:layout_alignRight="#+id/textView42"
android:layout_below="#+id/textView41"
android:layout_toRightOf="#+id/textView41"
android:background="#000"
android:gravity="center"
android:text="5"
android:textColor="#fff"
android:textSize="50sp" />
<TextView
android:id="#+id/textView44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView49"
android:layout_alignLeft="#+id/textView43"
android:layout_alignRight="#+id/textView43"
android:layout_alignTop="#+id/textView49"
android:background="#fff"
android:gravity="center"
android:text="6"
android:textColor="#000"
android:textSize="50sp" />
<TextView
android:id="#+id/textView46"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView24"
android:layout_alignLeft="#+id/textView49"
android:layout_alignRight="#+id/textView49"
android:layout_alignTop="#+id/textView47"
android:background="#fff"
android:gravity="center"
android:text="8"
android:textColor="#000"
android:textSize="50sp" />
</RelativeLayout>