Using NumberPicker to change TextView on a different activity [android] - java

I'm trying to change a textview value on one activity by using a numberpicker on the previous activity. Any help would be appreciated.
Here's the relevant part of my Java from activity1
public class activity_game extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final NumberPicker fizzNumberPkr = (NumberPicker)findViewById(fizzNumberPicker);
fizzNumberPkr.setValue(3);
fizzNumberPkr.setMinValue(1);
fizzNumberPkr.setMaxValue(20);
fizzNumberPkr.setWrapSelectorWheel(true);
final NumberPicker buzzNumberPkr = (NumberPicker)findViewById(buzzNumberPicker);
buzzNumberPkr.setValue(5);
buzzNumberPkr.setMinValue(1);
buzzNumberPkr.setMaxValue(20);
buzzNumberPkr.setWrapSelectorWheel(true);
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
toActivityPlay.putExtra("fizzNumber", fizzNumber);
toActivityPlay.putExtra("buzzNumber", buzzNumber);
startActivity(toActivityPlay);
}
And my relevant java from activity2
public class activity_play extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
TextView fizzNumberText = (TextView)findViewById(R.id.fizzNumber);
fizzNumberText.setText(getIntent().getExtras().getString("fizzNumber"));
if(getIntent().hasExtra("fizzNumber")) {
fizzNumber = getIntent().getIntArrayExtra();
} else {
throw new IllegalArgumentException("Error: Fizz number not found");
}
If there's any relevant code that I may not have posted please let me know and i'll edit my post.

Some ideas come to me now to communicate two activities..
You can create a method setValuePicker()/getValueFromPicker() in the first Activity, and call getValue() in the other activity.
public void setValuePicker(Parameter value){
number = value;
}
public Parameter getValuePicker(){
return value;
}
If you variable have been set global only need implement getValue() method.
If you someday want use fragments need implement callback.
EDIT:
Using your code is something like this:
- FirstActivity
public class MainActivity extends AppCompatActivity {
private int number;
public static final String FIZZ_TAG = "fizz_numer";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final NumberPicker picker = (NumberPicker) findViewById(R.id.numberPicker);
picker.setMinValue(0);
picker.setMaxValue(10);
picker.setValue(5);
picker.setWrapSelectorWheel(true);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
number = picker.getValue();
Intent intent = new Intent(getApplicationContext(), Main2Activity.class)
.putExtra(FIZZ_TAG,number);
startActivity(intent);
}
});
}
}
Second Activity need be something like this.
.
.
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
int s = getIntent().getIntExtra(MainActivity.FIZZ_TAG,0);
TextView textView = (TextView) findViewById(R.id.text_test);
textView.setText(String.format("%s = %d","number ",s));
}

Related

Using keys for passing values between activities

I'm trying to create an app that checks which number out of 2 numbers you input, is the bigger one, and display the result in another activity.
I tried to use getIntExtra() too but it didn't work and there is no error its just not working.
Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button check=findViewById(R.id.checkButton);
check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent Check=new Intent(getApplicationContext(), BiggerNum.class);
EditText N1EditText=findViewById(R.id.editTextN1);
int num1=Integer.parseInt(N1EditText.getText().toString());
EditText N2EditText=findViewById(R.id.editTextN2);
int num2=Integer.parseInt(N2EditText.getText().toString());
Check.putExtra("com.example.testapp.num1",num1);
Check.putExtra("com.example.testapp.num2",num2);
startActivity(Check);
}
});
}
}
Second Activity:
public class BiggerNum extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (getIntent().hasExtra("com.example.testapp.num1")){
int num1=getIntent().getExtras().getInt("num1");
int num2=getIntent().getExtras().getInt("num2");
TextView bigger=findViewById(R.id.biggerNView);
bigger.setText((Math.max(num1,num2)));
}
}
}
Edit: The problem in my code was as forpas explained. Anyways, the better way to use keys is to create the keys as static finals in the class you would pass them to, and then use them:
public class BiggerNum extends AppCompatActivity {
public static final String FIRST_NUM_KEY = "num1";
public static final String SECOND_NUM_KEY= "num2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
int num1= 0;
int num2 = 0;
if (getIntent().hasExtra(FIRST_NUM_KEY)){
int num1 = getIntent().getExtras().getInt(FIRST_NUM_KEY);
}
if (getIntent().hasExtra(SECOND_NUM_KEY)){
int num1 = getIntent().getExtras().getInt(SECOND_NUM_KEY);
}
TextView bigger=findViewById(R.id.biggerNView);
bigger.setText((Math.max(num1,num2)));
}
}
The keys of the 2 extras are "com.example.testapp.num1" and "com.example.testapp.num2", so you must use these to retrieve them and not "num1" and "num2":
if (getIntent().hasExtra("com.example.testapp.num1") && getIntent().hasExtra("com.example.testapp.num2")){
int num1=getIntent().getExtras().getInt("com.example.testapp.num1");
int num2=getIntent().getExtras().getInt("com.example.testapp.num2");
TextView bigger=findViewById(R.id.biggerNView);
bigger.setText(String.valueOf(Math.max(num1,num2)));
}

Add a font to an Intent to send to another activity

I've got 2 activities (well actually 4 but only 2 matter for now), the main activity and the font changing activity. The main activity sends text that the user has typed in to the font changing activity using an intent and startActivityForResult. Within the font changing activity the user can change the font of the text, as expected. What I'm trying to do is after the user has changed the font, send the text and the new font back to the main activity. But I'm not sure how to do this. I'm thinking of using some kind of bundle but I am stuck. If anyone could help me that would be greatly appreciated.
Here's my (relevant) Main Activity code:
public class MainActivity extends AppCompatActivity
{
RelativeLayout move_group;
ImageView view;
String backgroundImageName;
SeekBar seekSize;
TextView user_text;
Button button;
SeekBar tiltChange;
String temp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_text = (TextView) findViewById(R.id.user_text);
static final int REQUEST_FONT = 4;
public void FontChange(View view)
{
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}
//There's supposed to be an accompanying onActivityResult method as well
//but i haven't figured that part out yet
}
Here's my (relevant) Font Activity code:
public class Main4Activity extends AppCompatActivity
{
TextView user_font_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
user_font_text = (TextView) findViewById(R.id.user_font_text);
}
public void boldText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_bold);
user_font_text.setTypeface(custom_font);
}
public void italicText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_italic);
user_font_text.setTypeface(custom_font);
}
public void regularText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_regular);
user_font_text.setTypeface(custom_font);
}
public void thinText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_thin);
user_font_text.setTypeface(custom_font);
}
public void OK(View view)
{
//The intent and/or bundle would go here but I don't know what to do
}
}
public void FontChange(View view)
{
temp = user_text.getText().toString();
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}
this will fetch your string from textview to fontActivity.
Now in your FontActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
Intent intent = getIntent();
String text = intent.getStringExtra("temp");
user_font_text = (TextView) findViewById(R.id.user_font_text);
user_font_text.setText(text);
}
public void OK(View view)
{
Intent returnIntent = new Intent();
returnIntent.putExtra("result",
user_font_text.getText().toString());
returnIntent.putExtra("font",
ypur_font_type);
setResult(Activity.RESULT_OK, returnIntent);
}
Remaining part of setting font style can be done using SpannableStringBuilder. here you can check SpannableStringBuilder . Hope this helps.

Android App button press not functioning correctly

Being brand new to Java I'm not sure why this when called with android:onClick="changeInfo" isn't changing the text view back to the original value.
public class JavaTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_test);
}
public void changeInfo(View view){
setContentView(R.layout.activity_java_test);
TextView t = (TextView)findViewById(R.id.textSpace1);
if (t.getText()==getResources().getString(R.string.lorem_ipsum)){
t.setText("[Email Here]");
}
else{
t.setText(getResources().getString(R.string.lorem_ipsum));
}
}
Could anyone help, thank you.
Try removing setContentView(R.layout.activity_java_test); from the changeInfo(View view) method.
setContentView(R.layout.activity_java_test);
You don't have to call setContentView inside changeInfo method
t.getText()==getResources().getString(R.string.lorem_ipsum)
it should be t.getText().toString().equeals(getResources().getString(R.string.lorem_ipsum))
Try this
public class JavaTest extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_test);
}
public void changeInfo(View view){
TextView t = (TextView)findViewById(R.id.textSpace1);
if (t.getText().toString().equeals(getResources().getString(R.string.lorem_ipsum))){
t.setText("[Email Here]");
}
else{
t.setText(getResources().getString(R.string.lorem_ipsum));
}
}

Want to display stored name/value data in intent to new activity screen

I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation. What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class. Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):
MainActivity from which I want to store data to send to ThirdActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a2_button = (Button) findViewById(R.id.a2_button);
a2_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a2intent = new Intent("com.tester.lab.x");
startActivityForResult(a2intent, 1);
}
});
Button a3_button = (Button) findViewById(R.id.a3_button);
a3_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);
a3intent.putExtra("greeting","my Name");
startActivity(a3intent);
}
});
}
}
ThirdActivity:
public class ThirdActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
getIntent().getStringExtra("greeting");
}
}
How would I display the string "my Name" once the user clicks ThirdActivity button? Any help is wonderful, thanks!
Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
Or by using Button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
Button three = (Button) findViewById(R.id.your_button_id);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
});
}

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}

Categories

Resources