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
Related
I'm developing an Android app which lets the user draws and then checks if it has drawn inside a shape or not. The coordinates of the shape are retrieved by a .txt created before. Now, I created it on a Google Pixel C, so when the user draws the algorithm I made do not have any problem. I tried the app on a Galaxy Tab, which has a screen resolution of 1920 x 1200 and the shape its obviously in a different position. I made this method which converts the coordinates but it doesn't work:
public void loadShapePoints () {
BufferedReader reader = null;
Display display = getWindowManager(). getDefaultDisplay();
Point size = new Point();
display. getSize(size);
int width = size. x;
int height = size. y;
float tmp = 0;
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open(protocol+cornice)));
String mLine;
for (int elem=0; (mLine = reader.readLine()) != null; elem++) {
mLine = mLine.replaceAll("\\s+","");
if (elem%2==0) {
tmp = Float.parseFloat(mLine);
} else {
float x = Float.valueOf(tmp)*(width/(float)2560);
float y = Float.valueOf(Float.parseFloat(mLine))*(height/(float)1704);
Pair p1 = new Pair(x, y);
points.add(p1);
}
}
} catch (IOException e) { } finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
drawView.setShape(points);
}
I'm reading the file on this way because it is structured as well:
line 1: x1
line 2: y1
line 3: x2...
I used 2560x1740 in the conversion because it is the resolution of the Google Pixel C which I used on the emulator to develop the app.
I noticed that the only way to make it works on the Galaxy Tab A is to put 2600x2200 as coordinates (I don't know why), but obviously if I put this value it will not works as soon as I get back on the Pixel C. How can I convert them? Can the problem is that the drawing area is divided into 3 part which gets resized on different screen resolutions? This is the xml of the area:
<LinearLayout 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:background="#FCFCFC"
android:orientation="vertical"
android:layout_marginTop="8dp"
tools:context=".PaintingActivity" >
<!-- Top Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/draw_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="50dp"
android:contentDescription="#string/brush"
android:background="#drawable/rounded_corners"
android:padding="13dp"
android:layout_marginTop="5dp"
android:src="#drawable/brush" />
<ImageButton
android:id="#+id/erase_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="50dp"
android:contentDescription="#string/erase"
android:background="#drawable/rounded_corners"
android:padding="13dp"
android:layout_marginTop="5dp"
android:src="#drawable/eraser" />
<ImageButton
android:id="#+id/undo_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="Undo"
android:background="#drawable/rounded_corners"
android:padding="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/undo" />
</LinearLayout>
<!-- Custom View -->
<com.example.sample.DrawingView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<!-- Menu in basso -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<!-- Top Row -->
<LinearLayout
android:id="#+id/toprow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/form_intro"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip" />
<EditText
android:id="#+id/edittitle"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip"
android:singleLine="true" />
</LinearLayout>
<!-- Bottom Row -->
<LinearLayout
android:id="#+id/bottomrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="160dp"
android:layout_marginBottom="10dp"
android:background="#drawable/button_bg"
android:text="Avanti"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
This is how it looks like and its structure:
EDIT: the problem is that if I draw this segment on the Pixel C I get these coordinates:
1540.9375,
513.01074
If I do it on the Galaxy Tab A I get these:
1158.0,
279.0
I solved modifying the method as well:
public void loadShapePoints () {
BufferedReader reader = null;
float height = drawView.getCanvasHeight();
float width = drawView.getCanvasWidth();
float tmp = 0;
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open(protocol+cornice)));
String mLine;
for (int elem=0; (mLine = reader.readLine()) != null; elem++) {
mLine = mLine.replaceAll("\\s+","");
if (elem%2==0) {
tmp = Float.parseFloat(mLine);
} else {
float x = (width/2540)*(Float.valueOf(tmp));
float y = (height/1109)*(Float.valueOf(Float.parseFloat(mLine)));
Pair p1 = new Pair(x, y);
points.add(p1);
}
}
} catch (IOException e) { } finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
drawView.setShape(points);
}
The problem was that the drawing area was getting reduced on each device, so, since it has a different size than the screen, I had to manually calculate it and divide per the Google Pixel C drawing area size (2540x1109).
I'm trying to add a divider on top of my element in a RecyclerView.
Following google i've succeded to add a DividerItemDecoration, the code is as below:
DividerItemDecoration:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
The result:
What i'm trying to accomplish:
I've tried to play with the offset and the index but didn't get expected result.
How can I add the same item divider on top of my elements?
Also in case of necessary i've leave my layout of the row, here is the code:
Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
Explanation : In this answer i have added a divider above every item in your RecyclerView and after the RecyclerView itself. This way you wont have "overdraw", where 2 lines are painted on top of each other.I have used View tag for this purpose which draws a simple line from the xml.
Row file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<View android:layout_height="2dp"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:background="#color/colorPrimary"/>
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
Main file :
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/repository_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/tmp3"
android:layout_marginTop="30dp"/>
<View
android:id="#+id/bottomLine"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:background="#color/colorPrimary"/>
</LinearLayout>
Copy and paste DividerItemDecoration and change the following two lines. I only tested for vertical orientation.
//Change - getItemOffsets
//old
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight())
//new
outRect.set(0, mDivider.getIntrinsicHeight(), 0, 0)
//Change - drawVertical
//old
final int bottom = mBounds.bottom + Math.round(child.getTranslationY())
//new
final int bottom = mBounds.bottom + Math.round(child.getTranslationY()) - child.getHeight()
Don't add divider via Item Decoration. Instead put Two vies above and below of your row code like below.
<?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="wrap_content">
<View
android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
<View android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
</LinearLayout>
**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.
I have 4 buttons in my android app activity. This is what it looks like:
As you can see, the textViews at the bottom of the screen display the x and y coordinates. The coordinates are in reference to a relative layout. (See below given code)
Now, what I want to do is get the x and y coordinates of the 4 buttons at runtime and then figure out if my finger is touching the buttons while moving or not. In simple words, I want to press the buttons by swiping my finger over them instead of lifting and touching. How can I achieve it? I want to implement it in my piano application.
I was able to get the coordinates on screen and they change as I move my finger.
So, my question is How can I get the coordinates of the buttons and detect if my finger is swiping above them?:
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="#+id/relativelayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Y Cord : "
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="X Cord : "
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/textView2"
android:layout_above="#+id/textView"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView3"
android:textColor="#000000"
android:layout_below="#+id/textView2"
android:layout_toRightOf="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView4"
android:textColor="#000000"
android:layout_marginBottom="10dp"
android:layout_above="#+id/textView"
android:layout_toRightOf="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:textColor="#000000"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:textColor="#000000"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:textColor="#000000"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4"
android:textColor="#000000"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView xcordview = (TextView) findViewById(R.id.textView4);
final TextView ycordview = (TextView) findViewById(R.id.textView3);
RelativeLayout touchview = (RelativeLayout) findViewById(R.id.relativelayout);
touchview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
xcordview.setText(String.valueOf(event.getX()));
ycordview.setText(String.valueOf(event.getY()));
return true;
}
});
}
}
Thank you all very very much!
Update:
public class MainActivity extends Activity {
RelativeLayout touchview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView xcordview = (TextView) findViewById(R.id.textView4);
final TextView ycordview = (TextView) findViewById(R.id.textView3);
touchview = (RelativeLayout) findViewById(R.id.relativelayout);
touchview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
xcordview.setText(String.valueOf(event.getX()));
ycordview.setText(String.valueOf(event.getY()));
for(int i = 0; i < touchview.getChildCount(); i++){
if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
Button button = (Button) findViewById(R.id.button);
button.setBackgroundColor(Color.BLUE);
break;
}
}
return true;
}
});
}
private boolean checkInterSection(View view, float rawX, float rawY) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
int width = view.getWidth();
int height = view.getHeight();
//Check the intersection of point with rectangle achieved
return (!(rawX < x || rawY > x + width || rawY < y || rawY > y + height));
}
}
package com.example.touch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1, b2, b3, b4;
int b1x1, b1x2, b1y1, b1y2;
private TextView xcordview;
private TextView ycordview;
private TextView buttonIndicator;
private RelativeLayout touchview;
private static int defaultStates[];
private Button mLastButton;
private final static int[] STATE_PRESSED = {
android.R.attr.state_pressed,
android.R.attr.state_focused
| android.R.attr.state_enabled };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xcordview = (TextView) findViewById(R.id.textView4);
ycordview = (TextView) findViewById(R.id.textView3);
buttonIndicator = (TextView) findViewById(R.id.button_indicator);
touchview = (RelativeLayout) findViewById(R.id.relativelayout);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
defaultStates = b1.getBackground().getState();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
touchview.setOnTouchListener(new View.OnTouchListener() {
private boolean isInside = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
xcordview.setText(String.valueOf(x));
ycordview.setText(String.valueOf(y));
for (int i = 0; i < touchview.getChildCount(); i++) {
View current = touchview.getChildAt(i);
if (current instanceof Button) {
Button b = (Button) current;
if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
b.getBottom())) {
b.getBackground().setState(defaultStates);
}
if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
b.getBottom())) {
b.getBackground().setState(STATE_PRESSED);
if (b != mLastButton) {
mLastButton = b;
buttonIndicator.setText(mLastButton.getText());
}
}
}
}
return true;
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
}
static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
}
}
layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:text="Y Cord : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:text="X Cord : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_toRightOf="#+id/textView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/textView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="B1"
android:textColor="#000000" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="B2"
android:textColor="#000000" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button2"
android:text="B3"
android:textColor="#000000" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:text="B4"
android:textColor="#000000" />
<TextView
android:id="#+id/button_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView4"
android:layout_marginRight="33dp"
android:text="No one"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button_indicator"
android:layout_alignBottom="#+id/button_indicator"
android:layout_marginRight="29dp"
android:layout_toLeftOf="#+id/button_indicator"
android:text="Entered: "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
private boolean checkInterSection(View view, int rawX, int raxY) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
int width = view.getWidth();
int height = view.getHeight();
//Check the intersection of point with rectangle achieved
return (!(rawX < x || rawY > x + width || rawY < y || rawY > y + height));
}
for(int i = 0; i < touchview.getChildCount(); i++){
if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
((Button)touchview.getChildAt(i)).setBackgroundColor(Color.BLUE);// Type casting may not be required
}else{
((Button)touchview.getChildAt(i)).setBackgroundColor(Color.WHITE);
}
break;
}
}
getX() getY() for a OnTouchListener give coordinates relative the the cooresponding view.
If you prefer screen coordinates instead you can use one of the following
overwrite onTouchEvent for the activity and remove OnTouchListener-s for buttons,
in which case MotionEvent will report screen coordinates instead of Button coordinates
#Override
public boolean onTouchEvent (MotionEvent event) {
xcordview.setText(String.valueOf(event.getX()));
ycordview.setText(String.valueOf(event.getY()));
return true;
}
use getTop() getLeft() in OnTouchListener for a button:
touchview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
xcordview.setText(String.valueOf(event.getX()+v.getLeft()));
ycordview.setText(String.valueOf(event.getY()+v.getTop()));
return true;
}
});
You may consider using GestureDetector to detect swipes, however it isn't really necessary.
karioki 's solution works well. In order to make it work even when we start the move from inside one of the buttons, just add
android:clickable="false"
for every button in xml.
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.