Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7937619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:33:58+00:00 2026-06-03T22:33:58+00:00

Hey i am using the code below to use as a press a button

  • 0

Hey i am using the code below to use as a press a button to get new text in to a textview,
But its laggy and doesn’t work half of the times i press it, but the other half of the time it works fine, anyone have any idé?

final Button button = (Button) findViewById(R.id.slumpaovrigt);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    Random myRandom5 = new Random();
    TextView textovrigt = (TextView) findViewById(R.id.textovrigt);
    switch(myRandom5.nextInt() %151) {
    case 0:
        textovrigt.setText("text 1");
        break;
    case 1:
        textovrigt.setText("text 2");
        break;
    case 2:
        textovrigt.setText("text 3");
        break;
    case 3:
        textovrigt.setText("text 4");
        break;
    }
    }
    });
    }
    }

Also, i use 4 different xml views whit the same code but for different buttons and textviews and also whit different texts, each of the contains around 150 different texts (cases).

Changed the code abit and it got better, however still a bit slow

this is the complete code in the java file

import java.util.Random;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Ovrigtskamt extends Activity {

    Button slumpaovrigt;
TextView textovrigt;
Random slumpaovrigt2 = new Random();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ovrigttext);
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



    slumpaovrigt = (Button) findViewById(R.id.slumpaovrigt);
    textovrigt = (TextView)findViewById(R.id.textovrigt);

    loadDoc();
}

private void loadDoc(){




    String s = "";

    for(int x=0;x<=100;x++){
        s += "Line: "+String.valueOf(x)+"\n";
    }

    textovrigt.setMovementMethod(new ScrollingMovementMethod());

    textovrigt.setText("Klicka på Slumpa");




    slumpaovrigt.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {




    switch(slumpaovrigt2.nextInt() %4) {
    case 0:
  case 0:
    textovrigt.setText("text 1");
    break;
case 1:
    textovrigt.setText("text 2");
    break;
case 2:
    textovrigt.setText("text 3");
    break;
case 3:
    textovrigt.setText("text 4");
    break;
}
}
});
}
}

but as said whit 151 different texts

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:33:59+00:00Added an answer on June 3, 2026 at 10:33 pm

    Update: well you are doing % 151 do you have that many elements in your code? Not in this code example. Use % 4 instead.
    Update2: Or random.nextInt(4).

    Two optimizations we can see directly, but sounds strange if that would cause that much lag really.

    1. Don’t create the Random object every time, create it once at startup and re-use.
    2. Don’t find the view over and over, just do it at start up and save a reference to it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I am currently using the following code Dim flickr As New Flickr(apikey) Dim
Hey guys I get no errors from my code but nothing seems to happen
Hey I'm pretty new to XSLT's and trying to get my head around using
Hey all i am currently using this code below to gather my returned XML
Hey everyone. The following is the code that I am using to play random
Below is the code i tried using python >>> import string >>> def main():
using the script code below, this returns the images from facebook vertically. ive removed
Hey so i've made a text game using the pdCurses library and microsoft opperating
Hey I just wrote a Hello World code for ASP. But it is not
hey everyone, my code is listed below. as you can see, I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.