-4

I want to make an App with 2 Activitys, in the first Activity I need an EditText and Button and in the second Activity need to be TextView.

The app will open in the First Activity. You need to write in the EditText an int number, after you have chosen a number you need to click on the button and it will move you to the second Activity, if You have chosen a number in range 1 to 5, in the textView it needs to to be Write "Low Number" if the number is in range 5 to 10 it needs to be Write "High Number".

Activity1:

public class Mul extends AppCompatActivity {
private final String NUMBER = "Number";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mul);

}
public void btnonly(View view) {
    EditText etonly = (EditText) findViewById(R.id.etonly) ;
    Intent intent = new Intent(Mul.this, MulOnly.class);
    intent.putExtra("Number", etonly.getText().charAt(0));
    startActivity(intent);
        }

}

Activity2:

public class MulOnly extends AppCompatActivity {

int ab;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mul_only);


    ab=getIntent().getIntExtra("Number",0);
    TextView tv = (TextView) findViewById(R.id.textView);
    if (ab > 0 && ab < 6)
    {
        tv.setText("Low Number");
    }
    if (ab > 5 && ab < 11)
        tv.setText("High Number");
}

}

Why does it not work?

If I put in EditText number nvm what is the number it doesn't change the TextView in the next Activity

Dr.CPU
  • 9
  • 2
  • 2
    This looks like homework. And of what value would it be if you got the answer without looking. What have you already tried? What code have you already written? Show us your work, and we can help you on that. – AntonH Feb 27 '17 at 16:27
  • @Dr.CPU hello there we love to help you out but there is way that you need to ask a question, divide your case into parts and google it, if it does not work post here with what you have tried..You may wish to read http://stackoverflow.com/help/how-to-ask 1.get the value using edit text 2.store it in a variable 3.write onlcik for button 4.check how to send data via intent 5.pass you value to second activity 6.get the value in the second activity 7.check the value is less than or not http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – Charuක Feb 27 '17 at 16:28
  • But I mean if you really want to get that rep. – Greg432 Feb 27 '17 at 16:35
  • Sorry, but that doesn't help us help you when you say "don't work". What happens vs what you expect? If you have tried to use the `Intent.putExtra` methods, then you need to show that in your question. – OneCricketeer Feb 27 '17 at 17:55
  • Please use `Intent.putExtra`, and not `SharedPreferences` – OneCricketeer Feb 27 '17 at 18:27
  • @cricket_007 really thanks for the help bro, but its dont work again look the question i update the Code to .getExstra tell me please why its dont work now – Dr.CPU Feb 27 '17 at 19:05

1 Answers1

1

I think the problem is that you put a string as extra an read an integer. Try this:

intent.putExtra("Number", Integer.parseInt(""+etonly.getText().charAt(0)));

Plus, in the other Activity:

ab = getIntent().getIntExtra("Number", 0);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
yvi
  • 187
  • 1
  • 3
  • 12
  • `parseInt` takes a String, not a char and you are using `getStringExtra` for a put integer value. I have edited accordingly – OneCricketeer Feb 27 '17 at 19:53