I'm a beginner in android and at the moment, I'm working on simple app. I have 7 pictures and I want to change one-by-one when user touches the screen. Here's my code, my app crashes on the second touch on the screen. If anyone can help, I'd be thankful.
public class Game extends Activity {
public TextView result;
public ImageView pirveli, meore, mesame, meotxe, mexute, meeqvse, meshvide;
int counter=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
result = (TextView) findViewById(R.id.result);
result.setText("Number of steps made: " + counter);
pirveli = (ImageView) findViewById(R.drawable.pirveli);
meore = (ImageView) findViewById(R.drawable.meore);
mesame = (ImageView) findViewById(R.drawable.mesame);
meotxe = (ImageView) findViewById(R.drawable.meotxe);
mexute = (ImageView) findViewById(R.drawable.mexute);
meeqvse = (ImageView) findViewById(R.drawable.meeqvse);
meshvide= (ImageView) findViewById(R.drawable.meshvide);
}
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==0){
result.setText("Number of steps made: " + counter);
counter++;
pirveli.setImageResource(R.drawable.meore);
}
return false;
}
AND one more question: when I touch the screen the first time, int counter isn't increasing. how can i fix it?
Assuming R.drawable.meore, R.drawable.mesame, etc. are drawable resources in your drawable folder, this should work.
public class Game extends Activity {
TextView result;
int[] drawableIds = {R.drawable.meore, R.drawablemeSame, R.drawable.meotxe, R.drawable.mexute, R.drawable.meeqvse, R.drawable.meshvide};
int counter=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
result = (TextView) findViewById(R.id.result);
result.setText("Number of steps made: " + counter);
pirveli = (ImageView) findViewById(R.drawable.pirveli);
pirveli.setOnTouchListener(this);
}
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
result.setText("Number of steps made: " + counter);
counter++;
pirveli.setImageResource( drawableIds[ drawableIds.length % counter ] );
}
return false;
}
Here, it will cycle through the resource Id's and change the ImageView every time the user touches it.
First of all you should use the MotionEvent constants as they could change in future API versions, also you need to return true after a touch event is handled.
I also suggest you add null checks for good measure.
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
if (result != null);
result.setText("Number of steps made: " + counter);
counter++;
if (pirveli != null)
pirveli.setImageResource(R.drawable.meore);
return true;
}
return false;
}
A stack trace would be of much assistance if none of those are your problems.
Related
This question already has answers here:
Android - Detect End of Long Press
(4 answers)
Closed 4 years ago.
I would like to detect when a button is not clicked. For instance, in the code above, I would like to replace the ????? with a condition indicating that the imageview is still being clicked and quit the loop as soon as the imageview is not long clicked anymore. Do you have an idea?
imageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
while(?????)
{
int number =(Integer.parseInt(hours.getText().toString())+1)%24;
String text_number= String.valueOf(number);
if(number>-1 && number<10)
{
text_number="0"+text_number;
}
hours.setText(text_number);
}
return true;
}
});
Use View.OnTouchListener.
Example: https://stackoverflow.com/a/39588668/4586742
You will get onTouch callback with different events.
MotionEvent.ACTION_DOWN: when the user starts pressing the view.
MotionEvent.ACTION_UP: when the user stops pressing the view.
What i get from your question and proposed answer for you.
`
public class Main2Activity extends AppCompatActivity {
private boolean isImageViewBeingClicked = true;
private boolean isLongPressed = false;
private ImageView imageView;
private TextView hours;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView = (ImageView) findViewById(R.id.imageView);
hours = (TextView) findViewById(R.id.textView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
while (isImageViewBeingClicked) {
int number = (Integer.parseInt(hours.getText().toString()) + 1) % 24;
String text_number = String.valueOf(number);
if (number > -1 && number < 10) {
text_number = "0" + text_number;
}
hours.setText(text_number);
}
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if (isLongPressed) {
isImageViewBeingClicked = false;
}
}
return false;
}
});
imageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
isLongPressed = true;
return false;
}
});
}
}
`
So here is the code of simple transitioning,from top_left to bottom_right, I dont understand why I have to return false for onTouch() to work properly,
1.If I set return true, one a single touch , it sets counter from 0 to 3 , or simply adds 2 or 3 to the counter and most of the time remains at its current position.
2.If I set return false, the program runs fine and does what is written ,i.e counter++;
public class MainActivity extends AppCompatActivity {
Button b;
TextView tv;
public static int counter_button=0,counter=0;
RelativeLayout.LayoutParams old_rules;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.b1);
tv= (TextView) findViewById(R.id.tv1);
rl= (RelativeLayout) findViewById(R.id.rl);
old_rules= (RelativeLayout.LayoutParams) b.getLayoutParams();
rl.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
my_changes_touch();
return false;
}
});
}
public void my_changes_touch(){
RelativeLayout.LayoutParams rules= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
if(counter%2==0) {
counter++;
tv.setText(" "+counter);
rules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
b.setLayoutParams(rules);
rules.height=400;
rules.width=400;
b.setLayoutParams(rules);
getContentTransitionManager().beginDelayedTransition(rl);
} else {
counter++;
tv.setText(" "+counter);
b.setLayoutParams(old_rules);
getContentTransitionManager().beginDelayedTransition(rl);
}
}
}
The return value determines the event is consumed by the view or not.
So true means that you are interested in other events also.
If you return false then the touch event will be passed to the next View further up in the view hierarchy and you will not receive any further calls.
Please check this answer
https://stackoverflow.com/a/3756619/2783541
In an Android app, I want to handle clicks both on a background element and on foreground elements. In my test case, clicks on the foreground elements do not get sent.
I've taken a barebones Hello World project called Test, and altered it as follows.
In activity_test.xml, I have set ids for the layout and the TextView, and I have given them both onClick properties:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:id="#+id/layout"
android:onClick="doStuff">
<TextView
...
android:id="#+id/hello_world"
android:textSize="52dp"
android:onClick="doStuff" />
</RelativeLayout>
In the main TestActivity class, I have the following class:
public class TestActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
TextView view = (TextView) findViewById(R.id.hello_world);
doStuff(view);
Log.d("onCreate", "testing " + view.getId());
}
public void doStuff(TextView textView) {
Log.d("test", "text " + textView.getId() + " " + R.id.hello_world);
}
public void doStuff(View view) {
Log.d("test", "view " + view.getId() + " " + R.id.layout);
}
}
In the log window, after launching the app, I see the following:
D/test﹕ text 2131165250 2131165250
D/onCreate﹕ testing 2131165250
This shows that the TestView version of the doStuff method is definitely triggered when a TestView parameter is sent.
However, if I now tap on the Hello World TextView, this is the line that is added to the log:
D/test﹕ view 2131165249 2131165249
In other words, tapping the Hello World text has the same effect as tapping on the background itself. What do I need to do to get the TextView element to respond to its onClick setting?
What you're missing is:
android:clickable="true"
The TextView is not clickable by default.
hi can you use setOnTouchListener? then you can refer below given code.. it may work i think..
as per my knowledg, the problem is , whenever you touch textview, it will call onTouchListener for both textview and background.. sorry for my bad english
private int textViesTouchStatus = 0;//public variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView view = (TextView) findViewById(R.id.textView1);
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
textViesTouchStatus = 1;
Log.d("touch_textview", "code that should be run on textview touch goes here");
return false;
}
});
RelativeLayout layout = (RelativeLayout) findViewById(R.id.lay);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (textViesTouchStatus == 1)
textViesTouchStatus = 0;
else
Log.d("touch_background", "code that should be run on layout touch goes here");
return false;
}
});
BELOW IS MY CODE. This code is working, but I want to get the value of the display image in array to test, if the display country is correct. Please help me . I'm stuck in this activity. Thanks for all help .
public class MainActivity extends Activity implements OnClickListener {
private boolean blocked = false;
private Handler handler = new Handler();
ViewFlipper flippy;
Button show;
TextView view;
int flags[] = { R.drawable.afghan, R.drawable.albania, R.drawable.algeria };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
show = (Button) findViewById(R.id.button1);
view = (TextView) findViewById(R.id.textView1);
for (int i = 0; i < flags.length; i++) {
setflipperimage(flags[i]);
}
}
private void setflipperimage(int i) {
// TODO Auto-generated method stub
Log.i("Set Filpper Called", i + "");
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(i);
flippy.addView(image);
}
do not call this in loop,use this on click or on touch:
i+=1;
flippy.setDisplayedChild(flags[0]);
This will get used to get the current child id
viewFlipper.getDisplayedChild();
I'am developing a sample android application to learn about drag & drop in android. On start of the app, i'am displaying few images on a grid view. Now i need to drag one image at a time over to the place of another. After dropping an image over another, the images should swap its places. How can i achieve it ? Please guide/help me.
You can easily achieve this by using thquinn's DraggableGridView
You can add your custom layout
public class DraggableGridViewSampleActivity extends Activity {
static Random random = new Random();
static String[] words = "the of and a to in is be that was he for it with as his I on have at by not they this had are but from or she an which you one we all were her would there their will when who him been has more if no out do so can what up said about other into than its time only could new them man some these then two first may any like now my such make over our even most me state after also made many did must before back see through way where get much go well your know should down work year because come people just say each those take day good how long Mr own too little use US very great still men here life both between old under last never place same another think house while high right might came off find states since used give against three himself look few general hand school part small American home during number again Mrs around thought went without however govern don't does got public United point end become head once course fact upon need system set every war put form water took".split(" ");
DraggableGridView dgv;
Button button1, button2;
ArrayList<String> poem = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dgv = ((DraggableGridView)findViewById(R.id.vgv));
button1 = ((Button)findViewById(R.id.button1));
button2 = ((Button)findViewById(R.id.button2));
setListeners();
}
private void setListeners()
{
dgv.setOnRearrangeListener(new OnRearrangeListener() {
public void onRearrange(int oldIndex, int newIndex) {
String word = poem.remove(oldIndex);
if (oldIndex < newIndex)
poem.add(newIndex, word);
else
poem.add(newIndex, word);
}
});
dgv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
Toast.makeText(getApplicationContext(), "On clicked" +position, Toast.LENGTH_SHORT).show();
dgv.removeViewAt(position);
poem.remove(position);
}
});
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String word = words[random.nextInt(words.length)];
addView();
poem.add(word);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String finishedPoem = "";
for (String s : poem)
finishedPoem += s + " ";
new AlertDialog.Builder(DraggableGridViewSampleActivity.this)
.setTitle("Here's your poem!")
.setMessage(finishedPoem).show();
}
});
}
public void addView()
{
LayoutParams mLayoutParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout mLinearLayout= new LinearLayout(DraggableGridViewSampleActivity.this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
ImageView mImageView = new ImageView(DraggableGridViewSampleActivity.this);
if(dgv.getChildCount()%2==0)
mImageView.setImageResource(R.drawable.child1);
else
mImageView.setImageResource(R.drawable.child2);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
mImageView.setLayoutParams(mLayoutParams);
mImageView.setId(dgv.getChildCount());
mImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), v.getId()+"clicked ", Toast.LENGTH_SHORT).show();
return dgv.onTouch(v, event);
}
});
TextView mTextView = new TextView(DraggableGridViewSampleActivity.this);
mTextView.setLayoutParams(mLayoutParams);
mTextView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
return dgv.onTouch(v, event);
}
});
TextView mTextViewLabel = new TextView(DraggableGridViewSampleActivity.this);
mTextViewLabel.setText(((dgv.getChildCount()+1)+""));
mTextViewLabel.setLayoutParams(mLayoutParams);
mTextViewLabel.setId(dgv.getChildCount());
mTextViewLabel.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
return dgv.onTouch(v, event);
}
});
mLinearLayout.setTag(mTextViewLabel);
mLinearLayout.addView(mTextViewLabel);
mLinearLayout.addView(mImageView);
mLinearLayout.addView(mTextView);
dgv.addView(mLinearLayout);
}
}