If-Else for getIntent - Android - java

I have 2 activies. In first code, you see 2 rows and their click listeners. When first row clicked, I want to send "0" index to 2nd activity. As same, when I clicked second row, I want to send "1". So like that.
How can I do that?
Sender Side:
TableRow tricepsRow1;
tricepsRow1=(TableRow) findViewById(R.id.arm_exercise_1);
tricepsRow1.setClickable(true);
tricepsRow1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
indexValueStandart=0;
Intent tricepsRowIntent = new Intent(getApplicationContext(),exercise_arm_triceps.class);
tricepsRowIntent.putExtra("extra_arm_triceps",indexValueStandart);
startActivity(tricepsRowIntent);
}
});
TableRow tricepsRow2;
tricepsRow2=(TableRow) findViewById(R.id.arm_exercise_2);
tricepsRow2.setClickable(true);
tricepsRow2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
indexValueStandart=1;
Intent tricepsRowIntent = new Intent(getApplicationContext(),exercise_arm_triceps.class);
tricepsRowIntent.putExtra("extra_arm_triceps2",indexValueStandart);
startActivity(tricepsRowIntent);
}
});
Receiver Side:
int getVIndexValue= getIntent().getIntExtra("extra_arm_triceps",0);
deneme.setText(String.valueOf(getVIndexValue));

try this:
int value = getIntent().getExtras().getInt("extra_arm_triceps");

Related

Disable onclicklistener on rows when clicked Android Studio

I'm very new to java and Android studio, and I'm making one card game for practice as well as to do something practical while learning (as I'm learning by my own).
So to explain in short, there will be 2 rows of 4 cards at top and bottom (I used linear layout to fit those), and these cards will be clickable, which will move them to ImageView at central area when they are clicked. Up to this I have done everything fine.
But now I want to make things such that whenever card gets clicked in one row, that whole row will be disabled till card in another row is clicked, then again remaining cards in that row will be disabled and first row would be made clickable.
In short I want to make those rows as different players which would be clickable in turns. I had one idea (which might be very dumb as I'm new to whole thing) in which I have set onclicklisteners on both linearlayouts containing 4 cards each, and in Onclick method in them I have added Onclicklisteners on each of that card. I was thinking that by doing this, if I disable Onclicklistener on linearlayout after clicking, those on all cards in that layout will be disabled too, but I don't know how to disable the button.
If my approach is correct then pls tell me how do I disable listener on layouts on clicking. If not suggest other logic. Thanks
In below code usercards are imageviews,on clicking they disappear (invisible) from there place and move on to central place as described before
layout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
usercard1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(0), card1ondeck);
usercard1.setVisibility(View.INVISIBLE);
}
});
usercard2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(1), card1ondeck);
usercard2.setVisibility(View.INVISIBLE);
}
});
usercard3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(2), card1ondeck);
usercard3.setVisibility(View.INVISIBLE);
}
});
usercard4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(3), card1ondeck);
usercard4.setVisibility(View.INVISIBLE);
}
});
}
});
layout2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user2card1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(4), card2ondeck);
user2card1.setVisibility(View.INVISIBLE);
}
});
user2card2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(5), card2ondeck);
user2card2.setVisibility(View.INVISIBLE);
}
});
user2card3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(6), card2ondeck);
user2card3.setVisibility(View.INVISIBLE);
}
});
user2card4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setCardImage(cards.get(7), card2ondeck);
user2card4.setVisibility(View.INVISIBLE);
}
});
}
});

Setting Listeners programmatically to an array of CheckBoxes

So far, I've encountered the issue "variable x is accessed within inner class,needs to be declared final. I am able to initialize the CheckBox's but I am unable to set a listener to them after initialization in the loop. Below is my code so far.
for(int i=0;i<checkBox_fiber_ID.length;i++){
int temp=getResources().getIdentifier(checkBox_fiber_ID[i],"id",getPackageName());
checkBoxes_fiber[i]=findViewById(temp);
checkBoxes_fiber[i].setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if(checkBoxes_fiber[i].isChecked()){
//do something
}
}
});
}
Any tips on how to solve this?
Take final String[] x={"defaultvalue Emptry"}
Then after inside onclick Listener set value of x using below code.
x[0]="new value"
and use this value in different function.
as per your code it look likes blow:
final String x[] ={""}
for(int i=0;i<checkBox_fiber_ID.length;i++){
int temp=getResources().getIdentifier(checkBox_fiber_ID[i],"id",getPackageName());
checkBoxes_fiber[i]=findViewById(temp);
checkBoxes_fiber[i].setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if(checkBoxes_fiber[i].isChecked()){
x[0]=checkBoxes_fiber[i].getvalue==> value name
}
}
});
}
Outside function get value of x using
String name=x[0]
You can try to create separate class of listener
private View.OnClickListener mCheckboxListener = new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)view).isChecked())
{
int checkBoxId = (int)v.getTag(); //You can get Id for specific checkbox
//do other stuff with checkBoxId
}
}
};
And set Id to each checkbox like
for(int i=0;i<checkBox_fiber_ID.length;i++){
int temp=getResources().getIdentifier(checkBox_fiber_ID[i],"id",getPackageName());
checkBoxes_fiber[i]=findViewById(temp);
checkBoxes.setTag(i); //set check box id as tag for later usage
checkBoxes_fiber[i].setOnClickListener(mCheckboxListener);
}
I guess you are trying to do something base on the checkbox IDs. You can set a tag for a checkBox and get back the tag in future. Also, the view object in method void onClick(View view) is now an CheckBox. Just change a little in your code:
for(int i=0;i<checkBox_fiber_ID.length;i++){
int temp=getResources().getIdentifier(checkBox_fiber_ID[i],"id",getPackageName());
checkBoxes_fiber[i]=findViewById(temp);
checkBoxes_fiber[i].setTag(i); //mark the check box id for later usage
checkBoxes_fiber[i].setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if(((CheckBox)view).isChecked()){
int checkBoxId = (int)view.getTag();
doSomething(checkBoxId);
}
}
});
}
}
And write a new method for business code:
public void doSomething(int no){
if(no==1){
//do something
}
else if(no==2){
//do something
}
//...
}

Button OnClick Listener

I have a button with the "btnadd" id ,
Button add = (Button) findViewById(R.id.btnadd);
And a function with two inputs ,
public void CheckNumber (int i , int j) { if (i != j) Toast.makeText(getBaseContext,"i is not equal to j"); }
And I want to set this function for the click event of this button
add.setOnClickListener(CheckNumber(2,4));
This code is not correct, but how can I do this?
You need to define a new View.OnClickListener inside of setOnClickListener.
Try this:
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
checkNumber(2, 4);
}
});
Complete Solution:
Try something like this:
First, Create a method: (Consider your case)
public void CheckNumber (int i , int j) {
if (i != j)
Toast.makeText(this, "i is not equal to j", Toast.LENGTH_SHORT);
}
Second, declare the button inside onCreate():
Button add = findViewById(R.id.btnadd);
Then finally, add a click listener:
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckNumber(2, 4);
}
});
That's it. Hope it helps.

changing text on textview with button's onClickListener

Hi I'm working at my first bigger app in android studio "FlashCards". I would like it to work so after you click on the button the flashcard's textview changes its text to next random flashcard untill you see all of the them how can i do something like 'continue' to my loop from inside onClick method.
here's the loop's code:
while(i < mTestDeck.size()) {
// generates random number which will represent position in deck.
int random = randomGenerator.nextInt() % mTestDeck.size();
// if random flashcard was already shown create random number again
if (mTestDeck.get(random).wasShown())
continue;
//text view that we will operate on
TextView deckTextView = (TextView) findViewById(R.id.flashcard_text_view);
// set text
deckTextView.setText(mTestDeck.get(random).getFront());
// set mWasShown to true
mTestDeck.get(random).flashcardShown();
Button myButton = (Button) findViewById(R.id.know_answer);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mTestDeck.correctAnswer();
}
});
myButton = (Button) findViewById(R.id.dont_know_answer);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
First, this way, you have a potential infinite loop. And if it can happens, it will happens! It's not a good idea to "get random item and check if it's ok or try again".
I think that it's better to keep a list with all items in a random order. You just have to iterate over it.
Something like:
int currentPosition = 0;
List<Card> items = new ArrayList<Card>(mTestDeck).shuffle();
// Call this method once in onCreate or anywhere you initialize the UI
private void function setCurrentCard() {
Card currentItem = items.get(currentPosition);
[...] // Set all UI views here
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (currentPosition > items.size) {
// TODO? End?
return;
}
currentPosition++;
setCurrentCard();
}
});
}

How to clear the intent text?

I have a reset button in Activity A and it works fine since it can clear all the text and display null when the save button in Activity B is clicked. But this only works if there are nothing in the textView before pass to B.
It does not works in below cases.
In Activity A, type " Project 123', click next button go to B. Then I back to Activity A again and click the reset button to clear "Project 123". After done go to Activity B and click submit button. It shows "Project 123" instead of "null"...
Activity A
private TextView c;
String result; // 123
String name; // project
reset=(Button)claims.findViewById(R.id.button14); // reset button
Button button = (Button) claims.findViewById(R.id.button8); //next button
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
c.setText("");
d.setText("");
e.setText("");
f.setText("");
g.setText("");
h.setText("");
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), B.class);
if(c!=null){
intent.putExtra("name", name);
intent.putExtra("result", result);
}
});
return A;
}
Activity B
Name=getIntent().getExtras().getString("name");
Result=getIntent().getExtras().getString("result");
save=(Button)findViewById(R.id.button8);
save.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if((Name!=null)&&(Result!=null)){
Toast.makeText(getApplicationContext(), Name+Result, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"null", Toast.LENGTH_LONG).show();
}
}
});
This is because you only clear the setText but not the string.
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
c.setText("");
name="";
result="";
}
});
You have to add name="" and result="" in your reset method. It should works.
Kindly refer clear a value from a string android
Actually, the name is not null but with empty string ""
Try to take replace the Name != null with !TextUtils.isEmpty() in B activity.

Categories