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
Related
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 1 year ago.
Improve this question
My app crashes. I look throught most are using this approach public class MainActivity extends
where is the errors in this short code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
radioGroup=findViewById(R.id.radioGroup);
textView=findViewById(R.id.text_view);
Button ok=findViewById(R.id.ok);
ok.setOnClickListener(new view.onClicklisenn){
public void onClick(view view){
int radioId=radioGroup.getCheckedRadioButtonId();
radioButton=findViewById(radioId);
textView.setText("choise"+radioButton.getText());
}
}
}
Use this in your code if you have initialised all variables correctly
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int radioId=radioGroup.getCheckedRadioButtonId();
radioButton=findViewById(radioId);
textView.setText("choise"+radioButton.getText());
}
});
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 6 years ago.
Improve this question
I was trying to make a code that while filling a ListView there is a Loading View.
An example?
Thank you very much
This is exactly what I wanted when I am creating my music player app. Here is how I showed another screen while loading list view using AsyncTask (runs in background).
1) Create an activity (launcher) - LoadActivity.
2) Load list view contents using an AsyncTask while showing LoadActivity.
3) Open your MainActivity after loading is complete and close LoadActivity.
In an AsyncTask,
onPreExecute - Before background task.
doInBackground - background task.
onPostExecute - After background task.
private class MyTask extends AsyncTask<Params, Progress, Result> {
protected void onPreExecute() {
// Before your task
}
protected Long doInBackground(Params... params) {
// Your Task
return result;
}
protected void onPostExecute(Result result) {
// After your task
startActivity(new Intent(LoadActivity.this, MainActivity.class));
finish();
}
}
Then, in your onCreate() of LoadActivity.java,
new MyTask().execute(Params... params);
For more on AsyncTask, refer the documentation.
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);
}
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.
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.