Android App button press not functioning correctly - java

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));
}
}

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)));
}

i am having error with open and closing keys

I am creating a profile for my user but i dont know why i am getting this error
pls can you look ate the code and tell me what is wrong thank you very much
public class Profile extends AppCompatActivity {
EditText editText;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText =(EditText)findViewById(R.id.nome);
imageView=(ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
};
}
}
Try this:
public class Profile extends AppCompatActivity {
EditText editText;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText =(EditText)findViewById(R.id.nome);
imageView=(ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
// the closing parathesis ')' of setOnClickListener was missing
}
}

Calling a method immediately after starting intent in android?

Starting Intent then calling Method?
In the two classes, the second one has a StartIntent. Right now it simply starts the intent to the first class. I am wanting to know if it is possible from that same onClickListener to essentially StartIntent for the first class as usual, but then immediately call the defaultMap() method within it.
Sometimes I want to simply start the intent normally, and other times I want to start the intent and then call that method. 1) therefore, I can't just make so that OnCreate of the first class it calls defaultMap, because i don't always want to call it. But also 2) I don't want to JUST call the defaultMap() class. I need to call the full class so that it runs through the onCreate functions THEN goes to the defaultMap
FIRST CLASS USED
public class Daily_Schedule extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily__schedule);
......
.......
......
}
public void defaultMap(){
......
.......
......
}
SECOND CLASS USED
public class InRouteDisplay extends AppCompatActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_route_display);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(InRouteDisplay.this, DailySchedule.class);
InRouteDisplay.this.startActivity(myIntent);
}
});
.....
....
.....
}
Try the following: Two ways: 1) Using putExtra() -------- 2) Using SharedPreferences
1)
Demo4.class:-----------
public class Demo4 extends AppCompatActivity {
private Button b;
private final String CALL_DEFAULT_MAP = "call_default_map";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily__schedule);
if(getIntent() != null) {//1
if(getIntent().getStringExtra(CALL_DEFAULT_MAP) != null) {
if (getIntent().getStringExtra(CALL_DEFAULT_MAP).equals("true")) {
defaultMap();
}
}
}
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Demo4.this, Demo5.class);
finish();
startActivity(myIntent);
}
});
}
public void defaultMap() {
Toast.makeText(getApplicationContext(),"defaultMap()---called",Toast.LENGTH_LONG).show();
}
}
Demo5.class------
public class Demo5 extends AppCompatActivity {
private Button home;
private final String CALL_DEFAULT_MAP = "call_default_map";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_route_display);
home = (Button) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Demo5.this, Demo4.class);
myIntent.putExtra(CALL_DEFAULT_MAP,"true");//1
finish();
startActivity(myIntent);
}
});
}
}
2)
Demo4.class---------
public class Demo4 extends AppCompatActivity {
private Button b;
private final String CALL_DEFAULT_MAP = "call_default_map";
private SharedPreferences p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily__schedule);
p = getApplicationContext().getSharedPreferences("p_key",
0);//2
if(p != null){//2
if(p.getBoolean(CALL_DEFAULT_MAP , false)){
defaultMap();
}
}
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Demo4.this, Demo5.class);
finish();
startActivity(myIntent);
}
});
}
public void defaultMap() {
setBoolean(CALL_DEFAULT_MAP , false);//2
Toast.makeText(getApplicationContext(),"defaultMap()---called",Toast.LENGTH_LONG).show();
}
public void setBoolean(String Name, boolean value)
{
if(p != null){
SharedPreferences.Editor editor = p.edit();
editor.putBoolean(Name, value);
editor.apply();
}
}
}
Demo5.class:----------------
public class Demo5 extends AppCompatActivity {
private Button home;
private final String CALL_DEFAULT_MAP = "call_default_map";
private SharedPreferences p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_route_display);
p = getApplicationContext().getSharedPreferences("p_key",
0);
home = (Button) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setBoolean(CALL_DEFAULT_MAP , true);//2
Intent myIntent = new Intent(Demo5.this, Demo4.class);
finish();
startActivity(myIntent);
}
});
}
public void setBoolean(String Name, boolean value)
{
if(p != null){
SharedPreferences.Editor editor = p.edit();
editor.putBoolean(Name, value);
editor.apply();
}
}
}
No. The sending class doesn't get an instance of the Activity to call it on. What you can do is set a parameter in the intent, USE_DEFAULT_MAP to 1. The activity you launch can look for that variable, and use that to know that it should call defaultMap.
Use if statement inside the Daily_Schedule activity and check the extra whether they are set or null. Use getIntent() method. check the answer of this
From InRouteDisplay activity pass the intent data using putextra before calling InRouteDisplay.this.startActivity(myIntent);
Use this link to how to putextra data to the intent
Use this link's answer to know how to putextra data to the intent

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

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));
}

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