OnClick generate different Events when you click multiple times - java

I'm trying to get my OnClick to generate differente events when I click once or twice.
On the first click the ImageView changes, on the second it pass to a different Activity.
Here's my code for now
public static int i=0;
final ImageView srt = findViewById(R.id.imageone);
Button apply = findViewById(R.id.apply);
apply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vibrator.vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE));
if(i==0){
srt.setImageResource(R.drawable.imagetwo);
}else{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
});
}
Right now if I click once the ImageView change, but the second click does not work and doesn't change the Activity.

I think Ritu Suman Mohanty in the comments is correct. You need to increment your value with i++; Right now, i == 0 is always true. Good luck!

Related

TextView from SecondActivity become visible after a button from MainActivity is pressed?

I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.

onBindViewHolder increase position onClickListener

I have an activity with two buttons Next and Previous and a textview, I would like to update the textview content each time I click Next or Back
For example, If I click Next the textview should show me content from the next position or vice versa.
I think that I should be using a loop but it gives me an error when I try to do that and when I add 1 to the position (i+1) it works but it only gives me the second position, I want to get all the positions not only the second one. I don't really know if my question is clear, Hope it is :)
onBindViewHolder
#Override
public void onBindViewHolder(#NonNull final MyViewHolder myViewHolder, #SuppressLint("RecyclerView") final int i) {
myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nextContent = listItems.get(i).getContent();
String previousContent = listItems.get(i).getContent();
Intent intent = new Intent(v.getContext(), Main2Activity.class);
intent.putExtra("next", nextContent);
intent.putExtra("prev", previousContent);
v.getContext().startActivity(intent);
}
});
}
Main2Activity
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemTextView.setText(nextContent);
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemTextView.setText(prevContent);
}
});
It's bad practice to set click listeners in the onBindViewHolder method.
Why? Because onBindViewHolder is called each time the views are recycled for the new content to be displayed on the screen. Say you have a list of 1000 elements with 10 of them visible on the screen.
You scroll it to the end => Then onBindViewHolder would be called 990 times => 990 click listeners set.
You also want to dodge costly operations in onBindViewHolder() because your scrolling would be potentially slowed down.
More tips here:
Recyclerview(Getting item on Recyclerview)
A loop isn't the answer here, a loop is for automating something. This is an event (the user interacted) so it's not suitable.
The algorithm you probably want is basically the following:
User clicked an item
Find out the item index
Increment the index
Find the item with that index (by asking the list)
If it exists, do something with it
However, the issue in the code you posted is that your 'nextContent' is always the current item. So you need the following change (but be careful about bounds):
String nextContent = listItems.get(i+1).getContent();
String previousContent = listItems.get(i-1).getContent();
You current code isn't using a loop, so the onBind method should be called once for each value of i so it should not always be the second item
After your comments it appears you have buttons unrelated to the list, so now what you need to do is make it so every time you click an item in the RV or a Next/Prev, you store the correct index (as you have no access to i)
In the activity:
private int currentTextItem = 0;
public void setCurrentTextItem(int i) {
currentTextItem = i;
//the dots here will be how you get the text from the item
// probably recyclerView.findViewHolderForAdapterPosition(pos)
myTextView.setText( ... );
}
In your view holder code:
myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//(You should have some kind of call back from viewholder to activity)
activity.setCurrentTextItem(i);
}
});
Then your next/prev:
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCurrentTextItem(currentTextItem + 1);
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCurrentTextItem(currentTextItem - 1);
}
});

How to make a scoreboard where the results are calculated in another activity?

I am making an app for a local card game for three players, where there are two activities. One, which is kind of a calculator which calculates the results, and second, where the mentioned results are (to be) displayed. However, as I am not expert with java and programming in general, it is not working.
What now happens in my app is that the result of player one (Tom in my case), is taken from the calculator activity and displayed in the first textView und Player One's name. But when I repeat this operation, with the intent of displaying the second result in the second textView, it just overwrites the first textView. I have already tried the following:
using startActivity
using startActivityForResult
finishing the calculator activity (called MainActivity) with finish()
using Bundle instead of Intent, but
none of these did work though.
In the calculator activity (called MainActivity):
final Button zapsat = (Button)findViewById(R.id.button3);
zapsat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TextView tomVysledek = (TextView)findViewById(R.id.textView_tom2);
//final TextView tataVysledek = (TextView)findViewById(R.id.textView_tom3);
//final TextView kubaVysledek = (TextView)findViewById(R.id.textView_tom4);
Intent intent = new Intent(MainActivity.this, Results.class);
intent.putExtra("tomVysledek", tomVysledek.getText().toString());
startActivity(intent);
}
});
In onCreate of the Results activity (where I want to display the results):
TextView textView_tomV1 = (TextView) findViewById(R.id.textView_tomV1);
TextView textView_tomV2 = (TextView) findViewById(R.id.textView_tomV2);
.
.
.
TextView textView_tomV5 = (TextView) findViewById(R.id.textView_tomV5);
int tomZapis = 0;
Intent intent = getIntent();
try {
tomZapis = Integer.parseInt(intent.getStringExtra("tomVysledek"));
} catch (Exception e){}
String tomZapis_tv = Integer.toString(tomZapis);
if (textView_tomV1.getText().equals("0")) {
textView_tomV1.setText(tomZapis_tv);
} else if (textView_tomV2.getText().equals("0")){
textView_tomV2.setText(tomZapis_tv);
} else if (textView_tomV3.getText().equals("0")){
textView_tomV3.setText(tomZapis_tv);
} else if (textView_tomV4.getText().equals("0")) {
textView_tomV4.setText(tomZapis_tv);
} else if (textView_tomV5.getText().equals("0")) {
textView_tomV5.setText(tomZapis_tv);
} else {
Toast.makeText(Results.this, "Už není místo :(", Toast.LENGTH_LONG).show();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentres = new Intent(Results.this, MainActivity.class);
startActivityForResult(intentres, 1);
}
});
As I said, it always displays the result in the first textView (textView_tomV1), instead of finding the first empty textView and filling it with the variable. I believe that is because the startActivity actually restarts the activity, but I am not sure about that, and even if it was the case, I wouldn't know what to do with it, either.
I am not sure if it will solve your problem but when you write this
else if (textView_tomV2.getText().equals("0"))
you compare "Editable" with a String...
You should add toString() like so
textView_tomV2.getText().toString().equals("0")
and it will compare Strings

Multiple activities and buttons?

I want to make buttons once I click on the button I go to another activity?
and the problem is only the first button is working!
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageOne.class);
v.getContext().startActivity(myIntent);
Button PageTwo = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageTwo.class);
v.getContext().startActivity(myIntent);
}
{}
});
}
});
}
}
Think it is because most of your code is closed inside the scope of the first onClickListener, try something like this.
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
});
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
});
Using v.getContext() should be ok, this is just how I usually would do as the Activity itself is indeed a valid context. I guess it just seems more readable to me.
Edit:
Just as a clarification to the current state of your code. The second button is assigned a onClickListener only after the first button is pressed. But since the first button takes the app to a new Activity, inherently destroying the Main Activity, the second button will never have a chance to reach it's onClickListener.
Hope it makes sense, nevertheless the code above should fix the issue.
There are a couple of issues currently in your code. The first issue is that your second button is being defined inside the first button's declaration. The next issue is that you're setting the second OnClickListener to the wrongly named button. You've made a typo and instead of PageTwo, which you've called the Button (presumably you wanted to call it PageTwoButton in accordance with the first Button) and then set the OnClickListener to PageTwoButton instead. Seeing as you're also using multiple Buttons, it's a lot cleaner and more efficient to use a GroupOnClickListener. I'd probably also suggest using 'this' instead of 'v.getContext()' as well when setting up your Intents. Change your code to be like so:
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageOneButton.setOnClickListener(addGroupOnClickListener);
PageTwoButton.setOnClickListener(addGroupOnClickListener);
private OnClickListener addGroupOnClickListener = new OnClickListener() {
public void onClick(View v) {
if (v == PageOneButton) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
} else if (v == PageTwoButton) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
}
}
};
Hope this helps!
Two words: Code Indentation
Were you to indent your code properly, you would have noticed that you're setting OnClickListener INSIDE your first buttons' listener. Move it outside your first listener, as has already been advised by others.
There's also an extra pair of {}, which is redundant.
Also, #edwoollard noticed that for the second button, you're using two different names, PageTwo and PageTwoButton. Keep that in mind, unless it's a typo.

Android Notification Area Customization

I don't know whether this question get minus points, but I searched every where and my last resort is stackoverflow.
I need to add five buttons to notification area in horizontally. And each button I need to add even listener. I know it is possible to do with RemoteViews. But I never seen anyone adding event listener to each element.
These are the references if anyone need to refer.
Notifications Documentation
How to create a custom notification on android
SlidingDrawer API
You can add 5 anonymous listeners, or a single named listener.
Anonymous:
Button b1 = new Button(...);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// first listener's code goes here
}
});
Button b2 = new Button(...);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// second listener's code goes here
}
});
...
named is much the same, but contains a switch statement to differentiate what happens:
View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
String buttonTitle = ((Button)v).getText();
if ("title1".equals(buttonTitle)) {
// do things for the first button's click
} else if ("title2".equals(buttonTitle)) {
// do things for the second button's click
}
...
}
});
...

Categories

Resources