How to make a simple voting app in Android studio
Hello Guys,
Here I am going to discuss about simple use of button in android .How to use button in android.Basically I have used these button as social media voting button just to create a real life example.
Xml file:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="103dp"
android:layout_height="88dp"
android:background="#FF4B33 "
android:onClick="youtube"
android:text="YouTube"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<Button
android:id="@+id/button"
android:layout_width="103dp"
android:layout_height="88dp"
android:background="#33A6FF"
android:onClick="facebook"
android:text="facebbok"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f_score"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.082" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y_score"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="0.082" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="reset"
android:text="Reset "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="491dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:-
package abhishekraj.example.smediaupvote;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int f_score =0;
int y_score =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void facebook(View view){
f_score = f_score +1;
display(f_score);
}
public void youtube(View view){
y_score = y_score +1;
display1(y_score);
}
public void display(int name){
TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(""+name);
}
public void display1(int nameis){
TextView txt1 = (TextView) findViewById(R.id.textView2);
txt1.setText(""+nameis);
}
public void reset(View v){
int f_score =0;
int y_score =0;
display(0);
display1(0);
}
}
Note:-
Here I am declare f_score and y_score as global variable because these variable retain the value i.e f_score and y_score don't initialise with zero .
It's awesome..
ReplyDeleteIt is good way ...
ReplyDeleteJava Training In Noida
ReplyDelete