Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active October 1, 2021 05:47
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
  • Save udacityandroid/797532fc79eed21d801e to your computer and use it in GitHub Desktop.
Save udacityandroid/797532fc79eed21d801e to your computer and use it in GitHub Desktop.
Android Development for Beginners : Cola Wars Example
public class MainActivity extends AppCompatActivity {
...
public void voteUdacicola(View view) {
int udacicolaVotes = 0;
udacicolaVotes = udacicolaVotes + 1;
}
public void votePepcity(View view) {
int pepcityVotes;
pepcityVotes = pepcityVotes + 1;
}
public void showMeVotes(View view) {
display(udacicolaVotes + " vs. " + pepcityVotes);
}
...
}
@mhd73383
Copy link

mhd73383 commented Jul 3, 2018

public class MainActivity extends AppCompatActivity {
int pepcityVotes = 0;
int udacicolaVotes = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void votePepcity(View view) {
    pepcityVotes = pepcityVotes + 1;
    display(pepcityVotes);
    return;
}

public void voteUdacicola(View view) {
    udacicolaVotes = udacicolaVotes + 1;
    display1(udacicolaVotes);
    return;
}

public void display(int p) {
    TextView t1 = (TextView) findViewById(R.id.display_text_view1);
    t1.setText("" + p);
}

public void display1(int c) {
    TextView t2 = (TextView) findViewById(R.id.display_text_view3);
    t2.setText("" + c);
}

}

@mhd73383
Copy link

mhd73383 commented Jul 3, 2018

the parent layout is linearLayout vertical

@Dharmesh40
Copy link

can somebody please provide full code of xml and java for these examples............... i am getting very confused in this lesson 3

@waelfareed
Copy link

Both of variables are local and both of them must be global for reachable from anypoint.

@UmmuRasul
Copy link

having error

package com.example.ummurasul.practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int pepsiVotes = 0;
int spriteVotes =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

public void voteUdacicola(View view) {
    spriteVotes = spriteVotes + 1;
    display1(spriteVotes);
    return;
}

public void votePepcity(View view) {
    pepsiVotes =pepsiVotes + 1;
    display(pepsiVotes);
    return;
}

}

@UmmuRasul
Copy link

**public void display(int p) {
TextView t1 = (TextView) findViewById(R.id.cocacola);
t1.setText("" + p);
}

public void display1(int c) {
    TextView t2 = (TextView) findViewById(R.id.pepsi);
    t2.setText("" + c);
}**

the error is on de above code

@herpriz2
Copy link

@Kukkulum
A global variable is initialized at the starting of the code it is global means other methods can be used this variable but local variable is initialized inside a method and there local members can used it not all the methods.

Thank you for your explanation. It is helpful to understand the difference.

@Ashishkumar-sys
Copy link

best android course on this planet

@sudhir72350999
Copy link

very nice course

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment