getTag and setTag with button [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
If you allow, my friends. I am still new in the world of the Android. I am looking for the code of the basket button in the picture.
I have 4 activities. I want to collect the data entered in each activity through button send to the basket and store this data temporarily in the basket button until the completion of the collection of data from all activities. Then when pressing the basket button, I want to send this data to a new activity showing all the data Which was stored in the basket button.
I read a lot about this topic, and all the answers were about getTag and setTag.
But I really did not know how to handle this code.
Can anyone help me explain more about this instruction and how can I deal with it? Thank you very much.
This code that I try, does it send text and price to button??
Button fab;
Intent intent = new Intent( this, FloatingActionButton.class );
String keyIdentifer = null;
intent.putExtra( "String", text );
intent.putExtra( "Int", price );
fab.setTag( "input" );
And in the button code I used this code to send the received data to the last activity when you press the button. Is this correct???
final FloatingActionButton fab = (FloatingActionButton) findViewById( R.id.fab );
fab.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent( MainActivity.this, Main3Activity.class );
fab.getTag( Integer.parseInt( "input" ) );
startActivity( intent );
}
} );

You can pass data between activities using Intents and Parcelables
If I got your question right, my idea is to create a MainActivity that will start new activities for result, waiting for the response(if a product was added to the basket), and when the user clicks on the basket, you pass all collected data to the checkout activity using the Intents.
ps: Use the intents to get the response from the products activities.

Related

How to stop/cancel a runnable when pressing on android phone's back button?

In my project, I have an activity called "ExamMenuActivity" where I can choose between "Addition, Subtraction, Multiplication and Division" activities.
In Addition activity (called ExamAdditionActivity) I have a handler method to regenerate the question form after a given answer. Everything seems working fine, I can generate the question and give an answer and after giving an answer, a new question is generated in 2 seconds.
The issue I am having is, after I give a correct or a wrong answer to the question, within 2 seconds, if I quickly press on the back button of the phone and don't wait for the question to regenerate, I come back to the Exam Menu page as I wanted (back to ExamMenuActivity) but the screen changes back to ExamAdditionActivity and I see a new generated question again.
So I want to be able to come back to Exam Menu activity again when I press on the back button of the phone before the question regenerates and I don't want to face back the ExamAdditionActivity again with a new generated question (say I changed my mind after giving an answer to an addition question and I wanted to choose another activity from the menu and I didn't wait for at least 2 seconds).
I have tried overriding the activity with onBackPressed method:
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
but unfortunately that didn't work.
Here is how I regenerate the question. I basically restart the same activity in two seconds with a handler ( there was a runnable within my handler code but since it was showing anonymous, android studio offered me to change it to a lambda function) :
private void regenerateQuestion() {
new Handler().postDelayed(() -> {
Intent restartExamAdditionActivity = new Intent(ExamAdditionActivity.this, ExamAdditionActivity.class);
overridePendingTransition(0, 0);
startActivity(restartExamAdditionActivity);
finish();
overridePendingTransition(0, 0);
}, TIME_OUT);
}
Even though I am not sure but I think I am having the problem in button listeners area since I give an answer to the question and call regenetate() method there under each button.
I hope there can be an answer to my issue. Thank you so much for stopping by to check on my post!
Create your Handler as a member variable of your class, like this:
Handler myHandler = new Handler();
Create your Runnable as a member variable of your class, like this:
Runnable myRunnable = new Runnable() {
#Override
public void run() {
Intent restartExamAdditionActivity = new Intent(ExamAdditionActivity.this, ExamAdditionActivity.class);
overridePendingTransition(0, 0);
startActivity(restartExamAdditionActivity);
finish();
overridePendingTransition(0, 0);
}
};
Post the Runnable using the new variable:
myHandler.postDelayed(myRunnable, TIME_OUT);
When you want to cancel the regeneration, do this:
myHandler.removeCallbacks(myRunnable);

How can I change ImageButton through another activity? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I change ImageButton through another activity?
example:
I have imageButton in MainActivity.class and when I click on button in another activity changes ImageButton in the MainActivity.class
Thanks
You can implement a Local Broadcast Receiver in the Activity from where you can access the ImageButton. This receiver will be capable of hearing for a message and do something that you specify in response.
So, when you want to change the ImageButton you can send a message from the other Activity that will be received by the first one and will change the ImageButton. This message will be in an Intent form and can contain many extras.
EDIT
In your Activity #1 (The one who controls the ImageButton)
public void onCreate(Bundle savedInstanceState){
...
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcastReceiver, new IntentFilter("broadcastName"));
}
private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//MODIFY YOUR IMAGE BUTTON HERE
}
};
In your Activity #2 (The broadcast sender)
Call sendLocalBroadcast() in any part of this Activity when you want to send the message to update the ImageButton. You can add many parameters as you want, of course.
private void sendLocalBroadcast() {
Intent intent = new Intent("broadcastName");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}

How to get input from an EditText field in Android? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When coding for Android, there are two main ways I have seen to get text from an editText field. The first way seems to be very commonly used, and looks a little like this.
display = (EditText) findViewById(R.id.editText1);
displayContents = display.getText().toString();
displayTwo = (EditText) findViewById(R.id.editText2);
displayText = (Button) findViewById(R.id.button1);
displayText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayTwo.setText(displayContents);
}
This seems to use a clickListener in the mainActivity class to detect the click, then finds the value of the textfields.
However, when I was looking through Google's official Android tutorial, they used an alternative method. They first added this line of code to the button:
android:onClick="sendMessage";
and then had this method instead of the onClickListener:
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
After that, i created a new activity which made a new xml file with a different GUI, and a new class with the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
This seems to have the button broadcast a message rather than having a passive listener, and then trigger a new activity.
So after all that, I suppose my question would be which method is better to use? From both a technical and design standpoint, which one works with what situations? Like, when would I use each one?
I'm pretty new to android so I could be wrong, but pretty sure they are the same thing. When you add android:onClick="sendMessage";
it go's through the same type of listener, you just don't have to actually personally program that in yourself.
Which to use is arguably a personal preference I guess. Personally if things are easy such as click this to open an intent then I would use
public void sendMessage(View view)
But if I wanted the listener to parse variables through, or utilise variables within a specific method I guess, then it would be easier for me to create my own listener.
You can use any of the two methods mentioned above. There is no any technical difference in both of them.
And to learn about those thing go to this link af android developers
http://developer.android.com/develop/index.html
the second method in which the intent is used to pass the string to another activity is used only when you need to pass the string to a new activity.
if you have both of your edittexts in the same activity xml then go with the button clicklistener.
now to do something on button click we have to methods
with java listener
with xml onclick attribute
these methods will do the same thing , either one can be used.
you can create new activity using intent in java listener also with the same code.

Correct use of save button code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I've been learning and trying to build upon an app I made by following an online tutorial. It is a simple, bare bones, note taking application. The mainActivity simply shows note objects in a list view. The second screen/activity is the one I'm currently working on, trying to add code where I can. So far I've added a save button that will simply save the text/string value and take the user back to the main activity. I would like some feedback as to my implementation of the onButtonSave method:
public class NoteEditorActivity extends Activity {
private NoteItem note;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor);
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = this.getIntent();
note = new NoteItem();
note.setKey(intent.getStringExtra("key"));
note.setText(intent.getStringExtra("text"));
EditText et = (EditText) findViewById(R.id.noteText);
et.setText(note.getText());
et.setSelection(note.getText().length());
// I'm wondering if this is the correct way to call my onButtonSave method
onButtonSave();
}
private void saveAndFinish() {
EditText et = (EditText) findViewById(R.id.noteText);
String noteText = et.getText().toString();
Intent intent = new Intent();
intent.putExtra("key", note.getKey());
intent.putExtra("text", noteText);
setResult(RESULT_OK, intent);
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveAndFinish();
}
return false;
}
#Override
public void onBackPressed() {
saveAndFinish();
}
// This is the code I've added for the save button.
public void onButtonSave(){
final Button button = (Button) findViewById(R.id.saveButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setText("Saved!");
saveAndFinish(); }
});
}
}
I assume you are curious as to whether or not you have covered all your cases. Breaking up the setup of listeners and UI components from your onCreate into separate methods can be a good practice for easier readability when there are a lot of things being initialized.
You cover the case when the back button is used.
You cover the case when the user presses the button.
From what can be seen you also cover the case with the menu selection of leaving the screen. A couple people have talked best about how to detect whether the screen goes to the background or not. If you really want to catch all cases, you can do your save within the onPause() of an Activity. This will be fired if you press back, go home or call another activity.
Distinguishing between another activity and the home button is tough. But some people have pointed towards onUserHint() as a way to detect this. Just thought I would provide some feedback to what I can understand of your question.

How can we put more than one activity in Android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to put more than one activity in my main.java file but I don't know how to as I'm a newbie. I have inserted activity one but now I want to put activity two but unable to. Can you help me with it, the code until now on my main.java is:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstaceState) {
super.onCreate(savedInstaceState);
setContentView(R.layout.main);
TextView b = (TextView) findViewById(R.id.text1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(Main.this, One.class));
}
});
}
I have already created another activity files named Two.class and two.xml. How will the code be for next activities?
If you want two activity in a single calss use Fragments.
Fragments are mini activity in Android. They combine two or more than two activity in a single activity class
see following link for more detail
http://developer.android.com/guide/components/fragments.html
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
I think it will help you

Categories

Resources