Android multitouch issue - java

So I'm working on a game where two players play simultaneously on one screen. For the sake of simplicity let's say both players have one button on their side of the screen, buttons B[0] and B[1]. I want to implement multitouch so that both buttons can be pressed with no unresponsiveness, but as soon as one of them does ACTION_DOWN on his button, the other one becomes unresponsive. I read all the duplicate questions and understood that the second touch would be referenced as ACTION_POINTER_DOWN so I wrote this piece of code:
#Override
public boolean onTouch(View buttonPressed, MotionEvent action)
{
if(buttonPressed.getId()==B[0].getId())
{
if(action.getAction() == MotionEvent.ACTION_DOWN || action.getAction() == MotionEvent.ACTION_POINTER_DOWN)
B[0].setImageDrawable(BPressed[0]);
if(action.getAction() == MotionEvent.ACTION_UP || action.getAction() == MotionEvent.ACTION_POINTER_UP)
B[0].setImageDrawable(BOk[0]);
}
if(buttonPressed.getId()==B[1].getId())
{
if(action.getAction() == MotionEvent.ACTION_DOWN || action.getAction() == MotionEvent.ACTION_POINTER_DOWN)
B[1].setImageDrawable(BPressed[1]);
if(action.getAction() == MotionEvent.ACTION_UP || action.getAction() == MotionEvent.ACTION_POINTER_UP)
B[1].setImageDrawable(BOk[1]);
}
return true;
}
So as far I can tell if I put 1 finger on B[0] and leave it there (no ACTION_UP) and then put another on B[1] the listener should fire onTouch(B[1],ACTION_POINTER_DOWN) but i think it doesn't since when I do that B[0] has BPressed drawable and B[1] has BOk drawable. Where did I go wrong? Sorry if I made a duplicate, but I read everything and can't seem to find a simple solution. Every solution regarding this issue I found gives much more than I need.
Both buttons are connected with the listener like this:
B[0].setOnTouchListener(this);
B[1].setOnTouchListener(this);
I've read this:
ACTION_POINTER_DOWN and ACTION_POINTER_UP are fired whenever a secondary pointer goes down or up.
But it doesn't seem to be true.
EDIT:
The new code, still the same result:
#Override
public boolean onTouch(View buttonPressed, MotionEvent action)
{
if( buttonPressed.getId()==B[1].getId() )
Toast.makeText(context, "One", 500).show();
int act = action.getAction() & MotionEvent.ACTION_MASK;
if(buttonPressed.getId()==B[0].getId())
{
if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_POINTER_DOWN)
B[0].setImageDrawable(BPressed[0]);
if(act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_POINTER_UP)
B[0].setImageDrawable(BOk[0]);
}
if(buttonPressed.getId()==B[1].getId())
{
if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_POINTER_DOWN)
B[1].setImageDrawable(BPressed[1]);
if(act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_POINTER_UP)
B[1].setImageDrawable(BOk[1]);
}
return true;
}
Also, the toast:
if( buttonPressed.getId()==B[1].getId() )
Toast.makeText(context, "One", 500).show();
can never be seen if I keep my finger on B[0].
After masking:
Something changed. Let's say I hold my finger on B[0]. Before the masking when I press B[1] nothing happens. After masking if I keep my finger on B[0], and I tap my finger ANYWHERE on the screen it cycles the B[0]. So if I go:
finger1_DOWN on B[0] it changes to Pressed drawable
while(1)
{
finger2_DOWN anywhere nothing changes
finger2_UP anywhere B[0] changes to Ok drawable
}
I create my imagebuttons in OnCreate:
B[0] = (ImageButton) findViewById(R.id.ImageButton05);
B[0].setOnTouchListener(this);
B[1] = (ImageButton) findViewById(R.id.ImageButton15);
B[1].setOnTouchListener(this);
I tried jboi's approach. I don't care if the button was pressed first or second so I just did this:
private class MPListener implements OnTouchListener
{
#Override
public boolean onTouch(View buttonPressed, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(buttonPressed.getId()==B[0].getId())
B[0].setImageDrawable(BPressed[0]);
if(buttonPressed.getId()==B[1].getId())
B[1].setImageDrawable(BPressed[1]);
}
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(buttonPressed.getId()==B[0].getId())
B[0].setImageDrawable(BOk[0]);
if(buttonPressed.getId()==B[1].getId())
B[1].setImageDrawable(BOk[1]);
}
return true;
}
}
I removed implements onTouchListener from my activity and added
B[0].setOnTouchListener(new MPListener());
B[1].setOnTouchListener(new MPListener());
Nothing changed. I can still press both buttons independently, but not at the same time.
EDIT:
So I made a fresh new activity in case something else was the problem. Here's my new activity's full code:
package com.example.projectname;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class About extends Activity
{
private class MPListener implements OnTouchListener
{
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(v.getId()==B[0].getId())
B[0].setImageDrawable(Bok[0]);
if(v.getId()==B[1].getId())
B[1].setImageDrawable(Bok[1]);
}
else if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(v.getId()==B[0].getId())
B[0].setImageDrawable(Bpressed[0]);
if(v.getId()==B[1].getId())
B[1].setImageDrawable(Bpressed[1]);
}
return true;
}
}
ImageView[] B;
Drawable[] Bok,Bpressed;
#Override
protected void onCreate(Bundle savedInstanceState)
{
//standard oncreate stuff
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_about);
Context context=getApplicationContext();
B=new ImageView[2];
Bok=new Drawable[2];
Bpressed=new Drawable[2];
Bok[0] = context.getResources().getDrawable(R.drawable.bblue);
Bok[1] = context.getResources().getDrawable(R.drawable.bpurp);
Bpressed[0] = context.getResources().getDrawable(R.drawable.bpressedblue);
Bpressed[1] = context.getResources().getDrawable(R.drawable.bpressedpurp);
B[0] = (ImageView) findViewById(R.id.imageView1);
B[1] = (ImageView) findViewById(R.id.imageView2);
B[0].setImageDrawable(Bok[0]);
B[1].setImageDrawable(Bok[1]);
B[0].setOnTouchListener(new MPListener());
B[1].setOnTouchListener(new MPListener());
}
}
Everything is the same as always. As soon as I press one button the other one goes unresponsive.

Android can sort out for you, what was pressed in what order and if it is still touched or not. Just give the two Buttons different listeners. Something like this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("TEST", "Begin onCreate");
super.onCreate(savedInstanceState);
Log.d("TEST", "End onCreate");
setContentView(R.layout.test);
findViewById(R.id.upperTouchable).setOnTouchListener(new MyTouchListener());
findViewById(R.id.lowerTouchable).setOnTouchListener(new MyTouchListener());
}
private boolean lowerIsTouched = false;
private boolean upperIsTouched = false;
private void setInfo() {
if(lowerIsTouched && upperIsTouched)
((TextView) findViewById(R.id.info)).setText("both touched");
else if(lowerIsTouched)
((TextView) findViewById(R.id.info)).setText("only lower is touched");
else if(upperIsTouched)
((TextView) findViewById(R.id.info)).setText("only upper is touched");
else
((TextView) findViewById(R.id.info)).setText("non is touched");
((TextView) findViewById(R.id.lowerTouchable)).setText(lowerIsTouched? "touched":"not touched");
((TextView) findViewById(R.id.upperTouchable)).setText(upperIsTouched? "touched":"not touched");
}
private class MyTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(v.getId() == R.id.lowerTouchable)
lowerIsTouched = true;
else if(v.getId() == R.id.upperTouchable)
upperIsTouched = true;
setInfo();
}
else if(event.getAction() == MotionEvent.ACTION_UP) {
if(v.getId() == R.id.lowerTouchable)
lowerIsTouched = false;
else if(v.getId() == R.id.upperTouchable)
upperIsTouched = false;
setInfo();
}
return true;
}
}
and the corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FFFFFFFF"
android:text="Non touched"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/upperTouchable"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Not touched"
android:background="#FFF0F0F0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lowerTouchable"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Not touched"
android:background="#FFFFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

Related

Android app crashes when touched outside the ImageView

When i touched the ImageView it display the color from the image but when i touched outside the image the app crashes.
Here's my xml code:
<ImageView
android:id="#+id/colorimage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="#drawable/color" />
<ImageView
android:id="#+id/displaycolor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/colorimage"/>
And here is the main code:
mImageView.setDrawingCacheEnabled(true);
mImageView.buildDrawingCache(true);
mImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE){
Bitmap bitmap = mImageView.getDrawingCache();
int pixel = bitmap.getPixel((int)event.getX(), (int)event.getY());
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
display.setBackgroundColor(Color.rgb(r , g , b));
}
return true;
}
});
Logcat error message:
Error Logs
Change the line if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) to :
if(v.getId() == R.id.colorimage){
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE)
{........}
}
The on touch listener has been set to the whole activity not a particular view and the bitmap operation cannot be done on the other views except this one so the view so it is crashing.

Android - Detecting when user touches a drawable left

I have a TextView that contains a DrawableRight, what I want to do is detecting when the user presses that icon in drawableRight, is that possible ? and if it is how can I do it ?
PS: I am working inside a fragment
TextView XML
<TextView
android:id="#+id/mTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="22dp"
android:drawableRight="#mipmap/icn" //this is the drawable
/>
mTitle.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (mTitle.getRight() - mTitle.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
//drawable pressed
return true;
}
}
return false;
}
});

Have a disabled onClick?

I want to be able to respond to a click event on a disabled switch, is that possible?
I have a switch that is not enabled until the user fills in some information, so it looks like this:
I want to prompt the user to fill out the information if they click on the disabled switch with a dialog, like so:
mySwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!userInfo.isFilled){
new AlertDialog.Builder(MainActivity.this)
.setTitle("Fill out info first!")
.setMessage("You must first fill out info before turning on this featurel")
.setNeutralButton("Okay", null)
.show();
}
}
});
However, the onClick() is not triggered when I click on the disabled switch, so how do I get when the user clicks on it?
You could place a transparent View on top of the Switch and toggle its enabled state opposite the Switch, and show the message when this overlaid View is clicked.
From the View.java source code,
public boolean dispatchTouchEvent(MotionEvent event) {
// If the event should be handled by accessibility focus first.
if (event.isTargetAccessibilityFocus()) {
// We don't have focus or no virtual descendant has it, do not handle the event.
if (!isAccessibilityFocusedViewOrHost()) {
return false;
}
// We have focus and got the event, then use normal event dispatch.
event.setTargetAccessibilityFocus(false);
}
boolean result = false;
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
}
final int actionMasked = event.getActionMasked();
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
}
if (onFilterTouchEventForSecurity(event)) {
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
if (!result && onTouchEvent(event)) {
result = true;
}
}
if (!result && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
}
// Clean up after nested scrolls if this is the end of a gesture;
// also cancel it if we tried an ACTION_DOWN but we didn't want the rest
// of the gesture.
if (actionMasked == MotionEvent.ACTION_UP ||
actionMasked == MotionEvent.ACTION_CANCEL ||
(actionMasked == MotionEvent.ACTION_DOWN && !result)) {
stopNestedScroll();
}
return result;
}
the enabled flag ensures the UnhandledEvents are consumed however not passed along to the listeners,thereby bypassing all your possible code.So it is not possible to listen to events on a disabled view.
That said, your options are,
Change the style to mimic that of a disabled view as mentioned here,and then add your required functionality.
Add a overlay invisible view to perform your required functionality which you can set to Gone once the view should be enabled.
Use something apart from enabled,(you could setClickable(false) and consume touch events)
You can set onTouchListener and react to boolean (e.g isToggleEnable) reference with respect to the user's previous actions:
mySwitch.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(!isToggleEnable){
//Taost here
}
//If isToggleEnable = false on return OnClickListener won't be called
return isToggleEnable;
}
});
When it is disabled, setEnabled(false), these listeners won't work.
Try this way: don't disable it, use the setOnCheckedChangeListener and check against your is-entry-filled in there:
use setOnCheckedChangeListener
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isEntryFilled) {
buttonView.setChecked(false);
// your alert dialog
} else {
}
}
});
this will re-check it back to off and pop your alert, until isEntryFilled is met.
EDIT
OR instead of setEnabled(false), use setClickable(false) or android:clickable="false" since docs say setClickable() is tied to click-events.
and instead of OnClickListener, try OnTouchListener. It will register your on-down-touch (and ignore your on-up-touch), since a click consists of down+up.
switch.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!isEntryFilled) {
buttonView.setChecked(false);
// your alert dialog
}
return false;
}
});
then somewhere else, where you check for isEntryFilled, reactivate your switch with switch.setClickable(true)
Try setting setFocusable(false) and setEnabled(true) on your switch. That way, click events will be fired while the switch still being "disabled". Taken from this answer.
mySwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isClick()){
//Your Valid Code
}else{
//Make our switch to false
new AlertDialog.Builder(MainActivity.this)
.setTitle("Fill out info first!")
.setMessage("You must first fill out info before turning on this featurel")
.setNeutralButton("Okay", null)
.show();
}
}
});
public Boolean isClick(){
//check condition that user fill details or not
//if yes then return true
// else return false
}
Let the Parent View intercept ClickEvents or TouchEvents, when its detected check if the receiving View is disabled, and do what you have to do.
Edit
"it doesn't work when disabled?"
try these codes, Im use LinearLayout for easy aligment. but overall it should give you an example
this is a full example
XML
<FrameLayout 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:layout_marginTop="70dp"
android:background="#273746">
<FrameLayout
android:layout_width="match_parent"
android:id="#+id/ass"
android:background="#drawable/abc_popup_background_mtrl_mult"
android:layout_height="match_parent">
</FrameLayout>
MainActivity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry_screen);
FrameLayout fl = (FrameLayout)findViewById(R.id.ass);
Test t = new Test(this);
FrameLayout.LayoutParams lp = (LayoutParams) fl.getLayoutParams();
lp.height = LayoutParams.MATCH_PARENT;
lp.width = LayoutParams.MATCH_PARENT;
t.setOrientation(LinearLayout.VERTICAL);
t.setLayoutParams(lp);
fl.addView(t);
t.setBackgroundColor(Color.YELLOW);
Button b = new Button(this);
b.setText("patricia");
t.addView(b);
b = new Button(this);
b.setText("monica");
t.addView(b);
b = new Button(this);
b.setText("rebecca");
t.addView(b);
}
Test.java
public class Test extends LinearLayout {
public Test(Context context) {
super(context);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
StringBuilder sb = new StringBuilder();
sb.append("intercept \n\r");
int x = (int)event.getX(),
y= (int)event.getY();
for(int i =0; i< getChildCount(); i++){
int[] pos = new int[]{getChildAt(i).getLeft(),getChildAt(i).getTop(),
getChildAt(i).getMeasuredWidth(),
getChildAt(i).getMeasuredHeight()};
sb.append(getChildAt(i).getLeft()+", ");
sb.append(getChildAt(i).getTop()+", ");
sb.append(getChildAt(i).getMeasuredWidth()+", ");
sb.append(getChildAt(i).getMeasuredHeight());
sb.append("\n\r");
sb.append(isInBounds(pos, x, y));
sb.append("\n\r");
}
sb.append("x is ");
sb.append(x);
sb.append("y is ");
sb.append(y);
Toast.makeText(getContext(),sb.toString() , Toast.LENGTH_LONG).show();
return super.onInterceptTouchEvent(event);
}
private boolean isInBounds(int[] dimen, int x, int y){
return ((x >= dimen[0] && x < (dimen[0] + dimen[2]))
&& (y >= dimen[1] && y < (dimen[1] + dimen[3])));
}
}
Now The one you click will check out to be true, that is the child, now when it checks out to be true you can do something like this
View v = getchildAt(pos);
//its the one that is tapped or clicked
if(!v.isEnabled()){
//this is the guy you want now, do what you want to do
for click event i am not try this, but you could just do View.performClick() or put your Dialog in the ViewGroup class and call it
actually you could use the View..getClipBounds() to save yourself from int array
Set the disable switches on click listener to change the listeners of the other switches. For example:
Switch s = (Switch) findViewById(R.id.SwitchID);
if (s != null) {
s.setOnCheckedChangeListener(this);
}
/* ... */
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "The Switch is " + (isChecked ? "on" : "off"),
Toast.LENGTH_SHORT).show();
if(isChecked) {
//do stuff when Switch is ON
//this is where you set your normal state OnClickListner
} else {
mySwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!userInfo.isFilled){
new AlertDialog.Builder(MainActivity.this)
.setTitle("Fill out info first!")
.setMessage("You must first fill out info before turning on this featurel")
.setNeutralButton("Okay", null)
.show();
}
}
});
}
}
I'm guessing you've disabled the switch using switch.setEnabled(false). If so, the onclick event will not trigger. If you still want to handle a click action when the switch is disabled, you can use .setOnTouchListener()...
You're best bet however would be to use .setOnCheckedChangeListener() and keeping the switch enabled. Basically when onCheckChanged() gets called, you can popup your dialog if the switch value is on and when the user click ok, you default the switch back to off.
mSwitched.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked && !userInfo.isFilled){
new AlertDialog.Builder(Activity.this)
.setTitle("Fill out info first!")
.setMessage("You must first fill out info before turning on this featurel")
.setNeutralButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mSwitched.setChecked(false);
}
})
.show();
}
}
});
You can do this in a different way,Give a root layout to toggle button with same width and height of toggle button
<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"
>
<!--Root layout to toggle button with same height and width
of toggle button-->
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/linear"
android:layout_height="wrap_content">
<ToggleButton
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
/>
</LinearLayout>
</RelativeLayout>
When you disable the button,make the button as not focasable and clickable .Then os will handover touch functionality to rootlayout.In the root layout click listner we can write the click logic when the button is not enabled
public class MainActivity extends AppCompatActivity {
ToggleButton button;
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (ToggleButton) findViewById(R.id.button);
linearLayout= (LinearLayout) findViewById(R.id.linear);
//disabling button
button.setEnabled(false);
button.setClickable(false);
button.setFocusableInTouchMode(false);
button.setFocusable(false);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//write the logic here which will execute when button is enabled
}
});
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//write the logic here which will execute when button is disabled
}
});
}
}
When you enable the button,make button to clickable and focausable.
//enabling button
button.setEnabled(true);
button.setClickable(true);
button.setFocusableInTouchMode(true);
button.setFocusable(true);

Change text in another activity on click. how?

I am new to this forum as an User, and I am also new to Java and Android, in developing ways. Sorry if there are any language or other mistakes.
So I am trying to make an app for a 'final' school project, which has the follow use:
The user sees a picture, a 'next' and a 'finish' button. there are 11 pictures (user only sees one) when the user clicks 'next', the pic no. 2 appears, if he clicks another time next, the picture no. 3 appears and so on.
when user clicks next 2 times (so if he made it until the 3rd picture) and then clicks finish, he'll Return to the main activity, and instead of the text 'Klicke auf start um zu beginnen' (click on start to begin) the text 'you made it until the 3rd picture' should show up.
If he clicked Next 5 times (6th picture) and clicks finish, he also returns to the main activity, but the text is 'you made it until the 5th picture'.
And so on with the others.
I've tried this, but I failed.
the red lines are those, which I made at my own but they unfortunately don't work
[B]Java Main Activity[/B]
package ch.OptiLab.visustest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
TextView textView;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.buttonSTART);
btn1.setOnClickListener(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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivityForResult(new Intent(this,VisusActivity.class), 0);
}
public void onActivityResult() {
if (resultCode == R.drawable.v1)textView.setText("you got 01");
else if (resultCode == R.drawable.v2)textView.setText("you got 02");
else if (resultCode == R.drawable.v3)textView.setText("you got 03");
else if (resultCode == R.drawable.v4)textView.setText("you got 04");
else if (resultCode == R.drawable.v5)textView.setText("you got 05");
else if (resultCode == R.drawable.v6)textView.setText("you got 06");
else if (resultCode == R.drawable.v7)textView.setText("you got 07");
else if (resultCode == R.drawable.v8)textView.setText("you got 08");
else if (resultCode == R.drawable.v9)textView.setText("you got 09");
else if (resultCode == R.drawable.v10)textView.setText("you got 10");
}
]
XML Main Activity
<RelativeLayout xmlns:android="(external link)"
xmlns:tools="(external link)"
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="ch.OptiLab.visustest.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Text1"
android:id="#+id/textView"
android:layout_marginTop="84dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Text2"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<Button
android:id="#+id/buttonSTART"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/button1"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="142dp" />
</RelativeLayout>
Java 2nd activity
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class VisusActivity extends Activity {
Button next;
ImageView testanzeige;
Button finish;
int i = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visus);
next = (Button)findViewById(R.id.next);
testanzeige = (ImageView)findViewById(R.id.testanzeige);
finish = (Button)findViewById(R.id.finish);
textView = (TextView)findViewById(R.id.textView);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
i++;
// To Set Your 1 Image Do it Thorough Layout
if (i == 1 ) {
//Your 2 Image
testanzeige.setImageResource(R.drawable.v1);
onStop();
setResult(R.drawable.v1);
}
else if (i == 2) {
//Your 3 Image
testanzeige.setImageResource(R.drawable.v2);
onStop();
setResult(R.drawable.v2);
}
else if (i == 3) {
//Your 4 Image
testanzeige.setImageResource(R.drawable.v3);
onStop();
setResult(R.drawable.v3);
}
else if (i == 4) {
// Your 5 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v4);
onStop();
setResult(R.drawable.v4);
}
else if (i == 5 ) {
//Your 6 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v5);
onStop();
setResult(R.drawable.v5);
}
else if (i == 6) {
//Your 7 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v6);
onStop();
setResult(R.drawable.v6);
}
else if (i == 7 ) {
//Your 8 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v7);
onStop();
setResult(R.drawable.v7);
}
else if (i == 8 ) {
//Your 9 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v8);
onStop();
setResult(R.drawable.v8);
}
else if (i == 9) {
//Your 10 Image
testanzeige.setImageResource(R.drawable.v9);
onStop();
setResult(R.drawable.v9);
}
else if (i == 10) {
//Image 11 Image
testanzeige.setImageResource(R.drawable.v10);
onStop();
setResult(R.drawable.v10);
//next.setText("Finish");
//Toast.makeText(getApplicationContext(), "Your Final Text", Toast.LENGTH_LONG).show();
}
finish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
});
}
}
XML 2nd Activity
<ImageView
android:id="#+id/testanzeige"
android:layout_width="231dp"
android:layout_height="231dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/v2" />
<Button
android:id="#+id/next"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="#string/NEXTPIC" />
<Button
android:id="#+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/next"
android:layout_alignBottom="#+id/next"
android:layout_alignParentLeft="true"
android:text="#string/cantread" />
</RelativeLayout>
The most important strings:
<string name="Text1">Klicke auf Start um zu beginnen.</string>
<string name="cantread">finish</string>
Thank you so much
You have to use startActivityForResult http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
You pass two parameters in this function. For the purpose of this example lets assume you pass 2 as the result code.
Now when you're about to finish your second activity, and before you call finish() you can use setResult(position) where position is the image position that you just set. After you call finish your main activity will fire onActivityResult.
On that method you will have to search for the requestCode that you passed (2) and you will get the result code that you used on your setResult.
You can also pass bundles with this method.
EDIT
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == R.drawable.v1)textView.setText("you got 01");
else if (resultCode == R.drawable.v2)textView.setText("you got 02");
else if (resultCode == R.drawable.v3)textView.setText("you got 03");
else if (resultCode == R.drawable.v4)textView.setText("you got 04");
else if (resultCode == R.drawable.v5)textView.setText("you got 05");
else if (resultCode == R.drawable.v6)textView.setText("you got 06");
else if (resultCode == R.drawable.v7)textView.setText("you got 07");
else if (resultCode == R.drawable.v8)textView.setText("you got 08");
else if (resultCode == R.drawable.v9)textView.setText("you got 09");
else if (resultCode == R.drawable.v10)textView.setText("you got 10");
}
You need to pass the last image shown's index for finish button :
Intent in = new Intent(this, MainActivity.class);
in.putExtra("index", i);
startActivity(in);
And in your MainActivity onCreate, check if the data is passed :
Bundle data = getIntent().getExtras();
if(data != null)
int lastIndex = data.getInt("index");
After this, you can do anything with lastIndex, like show it in your TextView.

Creating a Drag and Drop ImageButton

I have looked tutorials on how to make my imagebutton capable of drag and drop. With the code I have the imagebutton just disappears when I click it. and when I click anywhere else it does not come back.
Here is my code for the imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getApplicationContext(), "in the movement", Toast.LENGTH_SHORT).show();
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins((int)event.getRawX() - 25, (int) event.getRawY() - 50, 0, 0);
layout = (LinearLayout) findViewById(R.id.gllayout);
layout.removeView(mainHut);
layout.addView(mainHut, params);
}//end if
}//end else
return false;
}//end onTouch function
});
here is the xml for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgm" >
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mainhut" />
</LinearLayout>
Any help is appreciated. Thanks.
To answer my own question...this is how I fixed my issue. It took me a long time to figure this out but here it is. The parameters are a little messy when it comes to my own imagebutton so I will have to figure out what to use to keep my finger in the center of the button, but this moves the button like I was wanting:
button.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (me.getAction() == MotionEvent.ACTION_MOVE )
{
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()), (int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()));
v.setLayoutParams(params);
}
return false;
}
});

Categories

Resources