Random number generator android - android

ActivityMain.java
package com.abhinav.random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public int count=0;
public int rand_num;
public int n;
EditText edt_rand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rand_num=(int)(Math.random()*100);
edt_rand=(EditText)findViewById(R.id.edt_rand);
n = Integer.parseInt(edt_rand.toString());
}
public void onclick(View v){
if(count!=5){
if(n>rand_num){
Toast toast=Toast.makeText(this,"Your number is greater", Toast.LENGTH_SHORT);
toast.show();
}
if(n<rand_num){
Toast toast=Toast.makeText(this,"Your number is greater", Toast.LENGTH_SHORT);
toast.show();
}
else{
new AlertDialog.Builder(this)
.setTitle("Task complete")
.setMessage("You Win")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
}
else{
new AlertDialog.Builder(this)
.setTitle("No Chances Left")
.setMessage("You Lose")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
}
}
main_activity.xml
enter code here
<EditText
android:id="#+id/edt_rand"
android:hint="#string/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
></EditText>
<Button
android:id="#+id/btn_rand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/edt_rand"
android:layout_below="#+id/edt_rand"
android:layout_marginTop="23dp"
android:text="#string/click"
android:onClick="onclick"/>
Whats wrong with the above code?

the problem is below line in onCreate() method;
n = Integer.parseInt(edt_rand.toString());
You are actually initializing an edittext and directly fetching a value from it. In such the case the value of n will be null or ""
modify your code like below,
public void onclick(View v){
n = Integer.parseInt(edt_rand.getText().toString()); // write this line here
...
}

i have one function for generate random number.
public String getDynamic()
{
Random r = new Random();
int i1 = r.nextInt(99 - 10) + 10;
String privateString = String.valueOf(i1) ;
return privateString;
}

Related

implementing a button inside a popup window for android

bellow you can see a script that displays the data from a SQLite database in a popup window. My question is that how can I implement a button inside that window to accomplish some tasks
here is the code:
package com.example.asus.sqlliteproject;
import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DataBaseHelper myDB;
EditText Name,LastName,Grades;
Button AddData,ViewData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new DataBaseHelper(this);
Name = (EditText)findViewById(R.id.name);
LastName = (EditText)findViewById(R.id.lastName);
Grades = (EditText)findViewById(R.id.Grades);
AddData = (Button)findViewById(R.id.addDataButton);
ViewData = (Button)findViewById(R.id.view);
addData();
}
public void addData () {
AddData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean inserted = myDB.insertData(Name.getText().toString(),
LastName.getText().toString(),
Grades.getText().toString());
if (inserted) {
Toast.makeText(MainActivity.this, "Text Inserted!", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this, "Unsuccessful!", Toast.LENGTH_SHORT).show();
}
});
}
public void viewAll () {
ViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor res = myDB.getAllData();
if (res.getCount() ==0){
// show some message
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("ID :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");
buffer.append("LastName :"+ res.getString(2)+"\n");
buffer.append("Grade :"+ res.getString(3)+"\n\n");
}
// show all data here
showMessage("Data",buffer.toString());
}
});
}
public void showMessage (String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
and here is a screen shot of the app and what I want to implement:
Click here for image
try this,
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Your code
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Your code
}
});
http://developer.android.com/guide/topics/ui/dialogs.html
builder.setPositiveButton("buttontext", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do some shit
}
it's simple.
public void showMessageDialog(String title , String Message)
{
AlertDialog dialog = new AlertDialog.Builder(Main3Activity.this)
.setTitle(title)
.setMessage(Message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//add your code here...
}
})
.show();
}
If you want to imlement your custom button - in left corner with custom properties, you have to use "custom layout" for AlertBox.
Something like this:
View tmLayout = View.inflate(ctx, R.layout.myalert, null);
TextView header= (TextView) tmLayout.findViewById(R.id.header);
Button btn= (Button) tmLayout.findViewById(R.id.btn);
btntzsetcur.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//callback
}
});
header.setText("header);
btn.setText("btn");
alerboxTimeZone = new AlertDialog.Builder(ctx).create();
alerboxTimeZone.setView(tmLayout);
Of course, you have to use XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/header"
style="#style/whitetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty" />
<LinearLayout
android:id="#+id/wrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
You can make a custom dialog with a custom view.
Dialog dialog = new Dialog(_context);
dialog.setContentView(YOUR_CUSTOM_VIEW); // put your xml file instead of YOUR_CUSTOM_VIEW
put buttons or whatever you need in your xml file and reach your buttons like this :
Button button = dialog.findViewById(BUTTON_ID); // R.id.YOUR_BUTTON
and then you can set onClickListener and whatever stuff you want for the component

Button issue eclipse

I want to create a simple app in eclipse with a button. I want to make it so that after the button is pushed 10 times a message will pop up. The problem is that when I start the app and push the button 10 times , nothing happens. Could you please tell me what I've done wrong?
Here's my activity file:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
int clicked = 0;
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
Thanks for response! I've edited the activity file this way:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
int clicked = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
Still it's not working in a proper way. Sorry for dumb questions: I'm new to android.
new edit:
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{
int clicked = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button gamebutton = (Button) findViewById(R.id.gamebutton);
gamebutton.setOnClickListener(this);
}
#Override
public void onClick(View v){
//TODO Auto-generated method stub
clicked++;
if( clicked==10){
AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
gamebuild.setMessage("Good");
gamebuild.setCancelable(false);
gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Game.this.finish();
}
});
gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
}
}
You put the counter inside the onClick function, causing it to reset every time. So, move
int clicked = 0;
from inside the onClick to before your onCreate such as:
int clicked = 0;
#Override
protected void onCreate....
This will make it set to 0 once, then stay equal to whatever it's last value was as long as the app is open and not killed.
Problem is that variable clicked is declared inside onClick. So it's always zero. You have to declare it globally, inside class Game.

AlertDialog has not come out because cannot getText of a button

My Alert dialog message doesn't come out because I cannot get the text of the button. After that the program will directly go to my EventActivity. How can settle this problem?
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == insertButton.getId()) {
if(colorButton.getText().toString().equals("Color")){
colorAlert.show();
}
}
}
This is variable
AlertDialog colorAlert;
AlertDialog in the OnCreate()
AlertDialog.Builder CA = new AlertDialog.Builder(this);
CA.setTitle("Alert Message!");
CA.setMessage("Please insert the level of important for the event.");
CA.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
colorAlert.dismiss();
startActivity(new Intent(this, EventActivity.class));
}
});
colorAlert = CA.create();
When you compare a String in Java, use YOUR_STRING.equals("Color");
Update:
change your while into if , because your are running it once.
The move your
startActivity(new Intent(this, EventActivity.class));
in your alertbox positive button handler.
Comparing Strings in Java.
Ok look like you need more help, here is one solution... i let you deal with the EventActivity ;)
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
final Context context=this;
public static String EXTRA_MESSAGE;
Button colorButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
colorButton = (Button) findViewById(R.id.button1);
colorButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(colorButton.getText().toString().equals("Color")){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setMessage("myDialog").setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(MainActivity.this, EventActivity.class);
String message = colorButton.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog myAlert = alertBuilder.create();
myAlert.show();
}
}
});
}
Adding to wtsang02 answer, you shouldn't put your alert.show in a while like this one... I see coming the infinite loop ;)

how to use alertdialog

package com.progme.wallkon;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class NextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ImageView im1;
im1 = (ImageView)findViewById(R.id.a_01_b);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im2;
im2 = (ImageView)findViewById(R.id.a_02_b);
im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im3;
im3 = (ImageView)findViewById(R.id.a_03_b);
im3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Gmelon");
builder.setMessage("setting?");
builder.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
});
builder.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
});
return builder.create();
}
}
I wrote code in activity.java like this..
I want to use dialog in im1, im2, im3, and each have to get another event.
Then, I have to write 3 dialog?
and how I can set [//TODO Auto...] here that I use is like..
first dialog for im1,
second dialog for im2,
third dialog for im3..
Please help..
You can write a private variable for the alert dialog and reuse it, but not at the same time
private AlertDialog mDialog = new AlertDialog.Builder(this)
.setTitle("Gmelon")
.setMessage("setting?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
})
.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
}).create();
now you can show the dialog where ever you want in your code.
It looks like you could just use showDialog(x) to me unless there is more to this question.

Android, what can i do with that listener i click on the button and doesnt responce

i don't know what is wrong with that code ..the emulator works but the button does not work with the listener.i did everything i cleaned the project and also i made a listener and it supposed to work.
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
CharSequence[] items = { "Goolge", "Apple", "MaC Os" };
boolean[] checkedItems = new boolean[items.length];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button btn= (Button)findViewById(R.id.dialog);
// btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
showDialog(0);
}
protected Dialog onCreatDialog(int id) {
switch (id) {
case 0 :
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("this is a List of items..");
builder.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getBaseContext(), "Ok clicked",
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "cancel cliked",
Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked"
: " unchecked"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="#string/click_me" />
</RelativeLayout>
uncomment this code:
Button btn = (Button)findViewById(R.id.dialog);
btn.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v) {
//Do your stuff
}
});
If you want to use your Activity as onClickListener, keep "this" on setOnClickListener, but your activity must implements OnClickListener interface, and implements its methods to work.

Categories

Resources