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.
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 3 years ago.
Improve this question
In my app, I want to show user some animation when background work is in progress(e.g. like FIle downloading/getting surrent location etc).
How I can do that?
Use a asyncTask like below.
class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.show();
}
#Override
protected Void doInBackground(Void... params) {
// Do your request , eg: file download etc
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pd != null)
{
pd.dismiss();
}
}
}
call this class like below when you need it like below,
new MyTask().execute();
You can use progress bar for this purpose. You can create a progress bar and place in a custom dialog box. Or you can use a library for that purpose. I can suggest you some library. Here is some progress bar by Awesome Android UI -
https://github.com/wasabeef/awesome-android-ui#progress
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 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 6 years ago.
Improve this question
I was wondering if there was a way to add a vertical and horizontal yellow lines in the middle of the width and length respectively on the camera, so that the user has a rough guideline when making the photo. I can post code if needed. Thanks in advance.
This code may help your:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.guideline);
dialog.setCanceledOnTouchOutside(true);
//for dismissing anywhere you touch
View masterView = dialog.findViewById(R.id.guidelineView);
masterView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
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.
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