I'm currently working in an Activity which saves text from three EditViews and constructs a SQL query. After that, the query is given to another activity to search and display the results.
Right now I've got two buttons, one to save the query, inside an onClickListener and another button to start the second activity:
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//First the data from the editviews is saved
EditText searchName = (EditText) findViewById(com.pedro.books.R.id.search_name);
search_name = searchName.getText().toString();
searchName.setText("");
EditText searchAut = (EditText) findViewById(com.pedro.books.R.id.search_aut);
search_aut = searchAut.getText().toString();
searchAut.setText("");
EditText searchYea = (EditText) findViewById(com.pedro.books.R.id.search_yea);
search_yea = searchYea.getText().toString();
searchYea.setText("");
//here i construct the query
});
And with an onClick in the xml code, I start the activity with another button:
public void ReadResults(View view){
//The query is given to the ReadActivity to display the results
Intent intent = new Intent(this, ReadActivity.class);
intent.putExtra(ReadActivity.EXTRA_QUERY, query);
startActivity(intent);
}
I've tried with the same button for both without changing anything and it obviously doesn't work, and I've also tried to start the activity inside the onClickListener, but I got this error: "The constructor Intent(new View.OnClickListener(){}, Class) is undefined"
Is there a way to start the activity inside the onClickListener or to stop the second activity to start until the query is saved?
Thanks in advance!!
if you are putting the intent in the onClick of a button you cannot use this you need to use YourActivity.this to properly get context.
this in your button is your OnClickListener like the error says
Where/How do you call your ReadResults method? On a side note - does it need to be public? If you call it inside the clickListener handler then that's wrong.
1) Extract your code out of the clickListener handler and have it into a private method within your activity class.
2) Your clickListener should only call that private method.
3) You could have your 3 editboxes as memebers of your activity and instantiate them onCreate() instead of getting them each time you click the button, it's expensive parsing the UI if not necessary.
Related
How can I update the editText field with a button when the button and the editText are on different layouts and classes?
I have a mainActivity class and layout but I wand to add this function to an intent (by clicking the save button update main_activity layout). I tried by calling the saveDegrees method onClick in the intent class but that didn't work.
After that I want to go back to the main_activity layout. My saveDegrees code is this:
public void saveDegrees(View view) {
LayoutInflater inflater = getLayoutInflater();
View activityView = inflater.inflate(R.layout.activity_main, null);
mCompassEditText = (EditText) activityView.findViewById(
R.id.compass_edit_text);
mCompassEditText.setText(toString().valueOf(currentDegree));
}
You cannot access an activity from another activity. Instead, you should send the data back to the first activity. You can do this by starting the second activity with startActivityForResult() rather than startActivity(). The second activity sets the result before finishing. The first activity receives the result in onActivityResult() and changes its own views. See How to manage `startActivityForResult` on Android? for more details.
I'm trying to build a quiz-like app for guessing flags in android. Basically i have an activity named SetFlagActivity which receivs intent from the startFlagActivity(View v) in MainActivity if a button is clicked . It needs to do two things: # 1 if the user inputs correct name of the flag load a new flag and # 2 display message: "correct" else just display "wrong".
When the button is pressed the correct message is displayed, but the ImageView containing the image of the flag disappears. Also when the button is clicked again application crashes with following output:
I assumed that image doesn't appear because it's displayed in MainActivity, so i used finish() to go back. That fixed the app crashing, but again when i click the button nothing happens...no message is displayed. So the question is how do i use SetFlagActivity in a correct way that it displays my message and sets a new image..
Any help will be apreciated.
This gets the input and sends an intent
public void startFlagActivity(View v){
EditText flagInput = (EditText) findViewById(R.id.inputFlag);
String message = flagInput.getText().toString();
Intent intent = new Intent(MainActivity.this, SetFlagActivity.class);
if(!message.equals(""))
intent.putExtra("flag", message);
MainActivity.this.startActivity(intent);
}
And my SetFlagActivity looks like this:
package ivve.projects.flags;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SetFlagActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startFlagActivity(v);
}
});
TextView text = (TextView) findViewById(R.id.displayAnswer);
Intent intent = getIntent();
String value = intent.getStringExtra("flag");
if(value.equals("Iceland")){
text.setText("correct");
}
else{
text.setText("wrong");
}
finish();
}
}
Edit1: StartFlagActivity is actually not an activity by itself...it is a function inside the MainActivity
Edit2: I have added the onClick handler to my SetFlagActivity as suggested, but i get an error "The method startFlagActivity(View) is undefined for the type new View.OnClickListener(){}" which doesn't allow me to call to startFlagActivity method
You have declared the button (id = button) in activity_main.xml and set the button's OnClickListener through the onClick attribute to refer to the startFlagActivity method. What you are trying to do is to check if the TextView contains the correct answer and if yes, then proceed else remain in this page. One suggestion off topic would be to initialize the TextView variable within onCreate(). Here while it works for you, you are re-initializing it every time you click.
You have now inflated this view in MainActivity.
setContentView(R.layout.activity_main.xml); // This inflates the layout.
// Now you dynamically have set the flag. This is not part of the activity_main.xml
You have declared startFlagActivity() in MainActivity() and the logic behind it is that the new Activity should be started (when the button is clicked) with the answer passed in as a parameter to the intent. The newly started intent will check if the answer is correct and if yes, will display correct.
Now to answer your first question that the image view disappears. This happens because you have inflated activity_main.xml - but the ImageView was created dynamically in MainActivity and does not exist in SetFlagActivity (there basically is no image in this screen). This is why the map disappears but you see the rest of the layout as it remains the same.
Then, when you again click the button, the onClick() method (or in this case the startFlagActivity() method) does not exist for the activity SetFlagActivity currently being displayed and that was why you received the error log that said that the onClick() method startFlagActivity() does not exist for the SetFlagActivity screen. I hope this makes sense. Clicking the button now will not produce anything as there is no code to back it up (also produces error logs). I hope you see now where your logic is flawed.
You don't need to start a new activity. You can finish it off based on if-else conditions. Your algorithm would be:
If (answer = correct)
setImageView -> new drawable;
repeat process;
else
show wrong and repeat;
I hope this helps.
03-08 20:52:59.539: E/AndroidRuntime(9399): java.lang.IllegalStateException: Could not find a method startFlagActivity(View) in the activity class ivve.projects.flags.SetFlagActivity for onClick handler on view class android.widget.Button with id 'button'
03-08 20:52:59.539: E/AndroidRuntime(9399): at android.view.View$1.onClick(View.java:2131)
you forgot to declare startFlagActivity(View) inside SetFlagActivity. Maybe you have set the wrong layout in SetFlagActivity
I am developing one simple app but i have problem.
The problem I have is that two activities, Activity1, have 26 button A-Z and Activity2 have textView,ImageView. I don't know how to set the button with default value.
Question
How can I change the value of textView, ImageView, and button in Activity2 whenever I press any button in Activity1?
Question
if i click buttonA how can i change texView display from "TextView" to "A" and imageView to other Image can u guys help me?
In simple way you can pass the value within intent like
Intent Activity2Intent = new Intent(this, Activity2.class);
Activity2Intent.putExtra("BUTTON_TEXT", yourText);
Activity2Intent.putExtra("IMAGE_VIEW", yourText);
startActivity(Activity2Intent);
Else if the data is needed later you can store that in SqliteDatabase
UPDATE
You should also need to extract the values passed from the bundle on Activity2.
You can use SharedPreferences or Intent for passing the value from Activity A and retrive the value in next Activity .
In Activity A:::
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do something and save the value and pass it using intent
// pass the button name or use boolean is_state= true;
}
});
In Activity B:::
By using getIntent() retrive the button name and change theTextViewandImageView` as per your requirement
i have a first screen in my app that the user enter his name in an editText.Then, when the user presses the button "ok",the app is going to a new activity.I would like to get the text from the first activity and move it to the second.For example,if the user fills the edittext with the name "kostas",when he goes to the second activity,to appear a textView writing "Hello kostas"..
i have tried to use putExtra, but i m thinking that i m doing it in a wrong way.In the first class i m using this
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(view.getContext(),home.class);
newActivity.putExtra("NAME", name);
startActivity(newActivity);
}
});
in order to move the name into my next activity "home".but then i dont know how to get it there...
and then in my new "home" activity i m using this:
Bundle extras = getIntent().getExtras();
String Name = extras.getString("NAME");
First, in your onClick Handler I would extract the text from the editText box and place the text into the intent.
Second, to debug the problem I would enable LogCat viewing
Third, I would log the actual values being passed (in Act 1) and extracted (
in Act 2) using a call such as:
Log.d(TAG,name);
Hope that helps,
JAL
i want to show this array as a listview in a new screen when a button is clicked.
ArrayList<String> favorite = new ArrayList<String>();
this ListView is a small part of my class. i cant seem to figure out how to implement it with my code (i can figure out how to create a listview in a separate application, and set the onitemclicklistner just for that listview)
i want to display that listview when.
case R.id.ShowFavButton:
Your question isn't entirely clear.... but you would build a separate Activity, possibly subclassing ListActivity (documentation here), and then load it when you click on the button. If your Activity was named FavoritesActivity, it would be something like this:
Button fav = (Button)findViewById(R.id.ShowFavButton);
fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, FavoritesActivity.class));
}
});
If you want to return something from your FavoritesActivity to your FirstActivity (or whatever it is called), you can use startActivityForResult instead of just startActivity.