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);
}
...
}
@Mahmoudtarek59
Copy link

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

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

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

public void showMeVotes(View view) {
    display(udacicolaVotes + " vs. " + pepcityVotes);
}
...

}

@lixrg
Copy link

lixrg commented Nov 14, 2017

if someone is still looking for a good explanation of local vs. global variables, I really liked this short, easy to understand explanation https://www.codecademy.com/en/forum_questions/514900b642e721e65d0003f1

@freryksl
Copy link

freryksl commented Jan 6, 2018

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

@mathsways
Copy link

good job thank you

@Nogaeman
Copy link

Nogaeman commented Feb 3, 2018

This Code have some error
1- int pepcityVotes; not set initial value of variable name
2- udacicolaVotes & pepcityVotes are local variable not Global variable so make error

The Correct Code Is

int udacicolaVotes = 0, pepcityVotes = 0;

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

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

public void showMeVotes(View view) {
    display(udacicolaVotes + " vs. " + pepcityVotes);
}

@burobot2016
Copy link

globbal is in the begining
local is in lines

@abdahma01
Copy link

abdahma01 commented Feb 13, 2018

Errors:

  • local variables out of scope.
  • local variables cannot be used as counters.
  • one of the local variables is not initialized.

@oddmentiusmaximus
Copy link

thanks

@first-hero
Copy link

both variables are local and they should be global for all methods to know about them

@louayeldin
Copy link

louayeldin commented Mar 16, 2018

Udacicola and Pepcity should be both global to be used in showMeVotes() method. Cool example

@ahmedalfalahi
Copy link

thanks

@Erikcht
Copy link

Erikcht commented May 2, 2018

soo... but how will become this code on
MainActivity.java and how will become the acitivityMain.xml with this ?

@Mohhmd
Copy link

Mohhmd commented May 7, 2018

it gives me error

@SaeedAlbaradie
Copy link

should be global Variable so other method can use it . As example here ShowMeVotes , otherwise it will give us error.

@mohammed74
Copy link

im need the xml code of voting ...

@OluwatobilobaLight
Copy link

@mohammed74 you can use mine below but you need to change android:src to your own image
`

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WHICH DO YOU PREFER?"
    android:textColor="#000"
    android:shadowColor="#3e0909"
    android:layout_marginBottom="30dp"
    android:textSize="45sp" />

    </LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="#000"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/cocacola"
        android:layout_alignParentLeft="true"
        android:src="@drawable/cola"
        android:layout_marginBottom="20dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cocacola"
        android:gravity="center"
        android:text="Vote"
        android:layout_alignParentLeft="true"
        android:background="#b71f1f"
        android:onClick="voteUdacicola"
        android:textColor="#fff"
        android:layout_marginLeft="35dp"/>

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/pepsi"
        android:id="@+id/pepsi"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="20dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/pepsi"
        android:gravity="center"
        android:text="Vote"
        android:layout_alignParentRight="true"
        android:background="#285fbf"
        android:textColor="#fff"
        android:onClick="votePepcity"
        android:layout_marginRight="30dp"/>

</RelativeLayout>

`

image

@tooptooptoop
Copy link

no

@rdgmh777
Copy link

rdgmh777 commented Jun 2, 2018

@Haapiiiii
Copy link

very nice .. thanx

@find1010
Copy link

thank you

@mhd73383
Copy link

mhd73383 commented Jul 3, 2018

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <TextView
        android:id="@+id/display_text_view1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textSize="45sp" />

    <TextView
        android:id="@+id/display_text_view2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".55"
        android:gravity="center"
        android:text="vs"
        android:textSize="45sp" />

    <TextView
        android:id="@+id/display_text_view3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="0"
        android:textSize="45sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="votePepcity"
        android:text="pepcity" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="voteUdacicola"
        android:text="udacicola" />
</LinearLayout>

@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