Hi im new to android and I have a program that has a global variable define and it works, so I can set it and get it in every activity, BUT it dosnt like to be changed in an on click listener. I made it so on the screen there is an edittext and when someone presses a button I want the edittext text to be put into the global variable. here is my code:
Button SiteButton = (Button) findViewById(R.id.SiteButton);
SiteButton.setOnClickListener(new View.OnClickListener() {
TextView textviewS = (TextView) findViewById(R.id.SiteIdT);
EditText edittextS = (EditText) findViewById(R.id.SiteIdE);
TextView textviewB = (TextView) findViewById(R.id.BusIdT);
EditText edittextB = (EditText) findViewById(R.id.BusIdE);
public void onClick(View v) {
textviewS.setText(edittextS.getText());
((Global) this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) this.getApplication()).setgVehicleId(textviewB.getText().toString());
}
});
but the getApplication() part is showing an error. can anyone help?
You should refer to your activity this, since View.OnClickListener doesn't have such a method:
// Bad code! read below
((Global) MyActivityClassName.this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) MyActivityClassName.this.getApplication()).setgVehicleId(textviewB.getText().toString());
By the way, how do you cast the return from getApplication() to Global? You will get a class cast exception there.
Related
I am really new to android. And my English speaking is not good. So,I was going to get a username using an EditText and set it to a TextView But the problem is that the EditText is in the first class (MyActivity),and the TextView is in the second class(MyAcyivity2).
I did everything such as FindViewById and....
but when I set on a click listener :
Textview1.setText(EditText1.getText())
and open the app, by clicking on button it says: Unfortunately app has stopped.
what to do?
In MyActivity :
EditText et = (EditText) findViewById(R.id.editText1);
On button click, inside onClickListener :
void onClick(View view) {
Intent intent = new Intent(MyActivity.this, MyActivity2.class);
intent.putExtra("myString", et.getText().toString());
startActivity(intent);
}
In MyActivity2 inside onCreate()
String myString = getIntent().getStringExtra("myString");
TextView tv = findViewById(R.id.textView1);
tv.setText(myString);
Hello guys right now I'm working on an app in which there are many quotes, in a TextView and I want to change the size of TextView on button click, like if Small button is clicked then the TextView should become small, if Large is clicked it should be large, etc. But each time I press the Button app force closes! What's the problem? Here's OnClickListener for the Button:
small = (Button) findViewById(R.id.small);
small.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.quotes);
tv.setTextSize(20);
}
});
P.S: The TextView and the Button are in different Activities.
There is no way to change the TextView size by Button from one Activity in other Activity. You will get NullPointerException because the findViewById method will not find view and will return null. You should use SharedPreferences. You should save text size value when you click Button and read it in the second Activity.
In your setting Activity:
small.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this.getBaseContext());
sharedPreferences.edit()
.putFloat("FONT_SIZE", 22)
.apply();
}
});
In your activity where you have TextView
TextView tv = (TextView)findViewById(R.id.quotes);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this.getBaseContext());
tv.setTextSize(sharedPreferences.getFloat("FONT_SIZE", 13/*DEFAULT VALUE*/));
I'm developing an android app, but I don't have experience with android and I need some help.
I'm going to simplify my code, here is my problem:
I have declared: TextView test = (TextView) findViewById(R.id.test);
After, i have a setOnClickListener:
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here i declare: TextView test = (TextView) findViewById(R.id.test2);
When i click test for first time it corresponds to findViewById(R.id.test) like it should, but when I click for second time it still corresponds findViewById(R.id.test).
Any idea why?
Thanks in advance! :)
It's because inside the click listener you're defining a new TextView. It's not the same test variable.
When you do TextView test = (TextView) findViewById(R.id.test2); inside the click listener, a new TextView test variable is defined which is not the same as the one you had defined previously.
If you just want to reassign a value, you should use test = (TextView) findViewById(R.id.test2); inside the onClickListener();
I got null data while fetch data from text Box.
My Code is:
EditText msg=(EditText)findViewById(R.id.Et_Msg);
String setMsg=msg.getText().toString();
Log.v("Messge","Message::"+setMsg);
Please tell me where I am wrong ?
This is your code,
EditText msg=(EditText)findViewById(R.id.Et_Msg);
String setMsg=msg.getText().toString();
Log.v("Messge","Message::"+setMsg);
The first line is initializing the EditText. When it does by default there is no value ( string ) in the edittext.
In the second line you are trying to fetch a string from a blank edittext, that's why it is giving you NullPointerException.
Solution : I suggest you to move your line String setMsg=msg.getText().toString(); to somewhere else where you are actually going to use the value of EditText.
While getting your data from EditText, you must create a listener orelse you get the value as null for an example button click listener..
For an example:
public class A extends Activity implements OnClickListener{
Button btn;
EditText edt;
#Override
public void onCreate(Bundle saved){
super.onCreate(saved);
edt = (EditText) findViewById(R.id.your_id);
btn = (Button) findViewById(R.id.your_id);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v){
if(v == btn){
String setMsg=edt.getText().toString();
Log.v("Messge","Message::"+setMsg);
}
}
}
see.. what you are doing.. immediately after obtaining and EditText's object you are calling getText() on it.. think logically.. obviously there is nothing (it should return blank though not sure why it is returing null) in the EditText unless you have it from the xml.. something like this;
<EditText
...
android:text="Hey there"
...
/>
try this.. or move getText() call under some button click..
Please replace your below line
String setMsg=msg.getText().toString();
with
String setMsg = String.valueOf(msg.getText());
Please try above line. Your problem will be solved.
Well, the title says everything, but there's a little problem, I can't use a fragment or a loader, because I'm developing for android 2.3, so, how can I update just a part of the activity?
You can still use a fragment even in 2.3 by using the android-support-v4 jar
You can reference the views by ID. As an example to change a EditText you can:
EditText text = (EditText) findViewById(R.id.input);
test.setText("New Text");
Just change the cast to the proper type, so for a button:
Button btn = (Button) findViewById(R.id.button);
So for your code if you have a button with the xml element android:onclick="increment" you can have a function such as:
public void increment(View view) {
TextView number = (TextView) findViewById(R.id.number);
number.setText(Integer.toString(Integer.parseInt(number.getText()) + 1));
}