I have this jokes app on Google Play (Punny Jokes) and to get a joke, the user must tap anywhere on the screen, then on the joke screen, they get to read the joke. But when they want another joke, they must go back to the main screen and press the screen again, which tends be annoying. I'm trying to set up another full-screen button on the joke screen activity, so they don't have to go back. The jokes are in strings in I have code that selects a random random string in a class called "StartingPoint". Thanks so much!
public class DisplayMessageActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
**//ERROR BEGINS HERE**
Button next;
Button next = (Button) = findViewById (R.id.next);
next.setOnClickListener(this);
**//ERROR ENDS HERE**
initTypeface1();
}
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.next:
IntentHandler.switchActivity(DisplayMessageActivity.this,
StartingPoint.class, false);
break;
// TODO Auto-generated method stub
}
}
};
You have declared the field next twice. And you had an equal sign in a completely wrong place.
Button next = (Button) findViewById (R.id.next);
next.setOnClickListener(this);
or
Button next;
next = (Button) findViewById (R.id.next);
next.setOnClickListener(this);
and solution is also in this block
//ERROR BEGINS HERE
Button next;
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);
//ERROR ENDS HERE
do replace your code as above
Well you should modify code like this
Button next = (Button) findViewById (R.id.next);
and why do you use next value twice??
Related
i have made one app to get ascii value of my character that i am going to put in my edit text. i have made the code as-> `
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
TextView textview=findViewById(R.id.textView2);
EditText edittext=findViewById(R.id.editTextTextPersonName);
String sipla=edittext.getText().toString();
Button button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int t=sipla.charAt(0);
textview.setText("THE ASCII value of your character = "+t);
}
});`
but when i click my button my app gets closed. one more think
when i make certain changes in my code and put String sipla=edittext.getText().toString(); in my button setonclick function it works and everything is normal please tell me why my app was not working prevously .
The reason your app is crashing is because sipla is empty, and you try to get the first character from an empty string. The reason it is empty is that you get it in onCreate, before the user has had a chance to enter anything in the EditText. Even if they enter something later, you never update the value of sipla.
The fix for this is simple - don't get the value from the EditText until you are actually ready to use it and the user would have had time to enter something - so get it inside onClick.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This code runs as the activity is getting set up
Button button=findViewById(R.id.button);
TextView textview=findViewById(R.id.textView2);
EditText edittext=findViewById(R.id.editTextTextPersonName);
Button button2 = findViewById(R.id.button2);
// if you get the value of edittext here it will always be empty,
// at this point the user hasn't even seen the screen yet
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// This code runs much later, when the user
// clicks the button
// Don't get the string until you are ready to process it, this code
// doesn't run until the user clicks the button, at which
// time edittext may also have a user value in it
String sipla = edittext.getText().toString();
if( !sipla.isEmpty() ) {
int t=sipla.charAt(0);
textview.setText("THE ASCII value of your character = "+t);
}
else {
textview.setText("You didn't enter anything");
}
}
});
}
Also, if your app crashes, look in the Logcat tab to see the full error message and stack trace. Learn to read that - it is an invaluable debugging skill. Also, learn to use Log/print statements in your code to help you understand what values are being used and what order things are run in when you are confused.
I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}
i am new in android programming, and i am working on my first application, so i just want to know how every time when i click same button it does some action, for example if i have a button called ( next ) and i want to click on it and an image will appear, this one i did it, but i want to click on the same button and show another image view in the same activity.
i have tried some code but with no results
so please if anyone can post a code that explain how i can do it.
Here is a quick example of code...
Rather than show a random image, it will show a random String. All you need to do is just modify it to show images instead.
public class MainActivity extends Activity {
private String[] names = {"Joe", "Mark", "Amanda", "Kelly", "Michael", "Jenny"};
private Button button;
private TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get our views
name = (TextView) findViewById(R.id.name);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
next(); // show random name
}
});
}
// Display random name on button click
private void next() {
name.setText(names[rand()]);
}
// Pick a random number from 0 to names.length
private int rand() {
return new Random().nextInt(names.length);
}
}
I have this code in one of the java file of my application.
public class Board_Play1 extends Activity {
int d,a=0,b=0,turn=2;
Random random = new Random();
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}turn++;
}
}
}
I come to this java file from another java file. All the problem I get is when this file doesn't have any while loop as in code it runs fine. But with including the while loop on navigating to this layout turns screen black and no further process can be done. If we press back button we have a pop out message saying Your application isn't responding. Do You want to close? Yes No.
Why is that happening. All things I included in while loop are perfect. What is causing for this problem?
I think you have an infinite loop. the condition in your while loop is always true because a and b values are never incremented.
And the reason why you're seeing black screen is that this infinite loop is blocking the Main UI Thread.
Seems that this is an infinite loop to me.
int a=0, b=0;
These values never change in your code and you are using them in your while loop's conditional.
Try something like:
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}
turn++;
// a = somevalue based on what you're trying to do.
// b = somevalue based on what you're trying to do.
}
hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};