I have a dummy RelativelayoutView with progress bar.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/progressbar">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/progressBar" />
</RelativeLayout>
When my app starts I need this RelativelayoutView to appear for 5 seconds and should diappear with or without animation.
I'm not sure I understood your question, but I'd suggest you to write something like this in your activity:
private Handler mHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
public void run() {
// hide your progress bar
findViewById(R.id.progressBar).setVisibility(View.GONE);
// or finish this activity and start a new one
startActivity(new Intent(MyActivity.this, MySecondActivity.class));
}
}, 5000);
}
#Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(null);
}
And I think you should add the android:indeterminate="true" attribute to your progress bar.
Hope it helps. Cheers.
Related
I am trying to implement a thread that changes something on the UI in a Fragment. Therefore I need to refer to the main thread.
Based on my research, I've found that the following code should do the trick:
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Toast.makeText(menuActivity, "HELLO", Toast.LENGTH_SHORT).show();
}
});
This will execute only once though, even if the Looper should normally keep the thread alive. Trying to invoke Looper.prepare() inside the Handler will cause a RuntimeException as only one Looper is allowed per thread.
Edit: My goal is to update a TextView permanently each second.
I have also tried the following:
Thread t = new Thread() {
#Override
public void run() {
menuActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
System.out.println("-----------TEST");
}
});
}
};
t.start();
But this will execute only once too.
I've also read this article, but I guess my first snippet of code is just a shorter version of the code shown in the article.
Where may my mistake be in any of these snippets of code?
This question is not a duplicate, due to the fact that I presented a totally different snippet of code which is the base of the problem I had. Furthermore, the Looper is explained more in depth in this thread.
I understand that you want to update a text view repeatedly after 1 seconds. Here is a simple demo I just write.
Mockup screen
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="startUpdateTextViewMoney"
android:text="Start" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="stopUpdateTextViewMoney"
android:text="Stop" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="#+id/edit_text_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setMoney"
android:text="SET MONEY" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int UPDATE_TEXT_MONEY_INTERVAL = 1000;
private Handler mMainHandler;
private TextView mTextViewMoney;
private TextView mEditTextMoney;
private String money;
private Runnable mUpdateTextViewMoneyTask = new Runnable() {
#Override
public void run() {
if (TextUtils.isEmpty(money)) {
mTextViewMoney.setText(String.valueOf(SystemClock.elapsedRealtime()));
} else {
mTextViewMoney.setText(money);
money = null;
}
mMainHandler.postDelayed(this, UPDATE_TEXT_MONEY_INTERVAL);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextViewMoney = findViewById(R.id.text_view_money);
mEditTextMoney = findViewById(R.id.edit_text_money);
mMainHandler = new Handler(Looper.getMainLooper());
}
#Override
protected void onStop() {
super.onStop();
stopUpdateTextViewMoney(null);
}
public void startUpdateTextViewMoney(View view) {
mMainHandler.post(mUpdateTextViewMoneyTask);
}
public void stopUpdateTextViewMoney(View view) {
mMainHandler.removeCallbacks(mUpdateTextViewMoneyTask);
}
public void setMoney(View view) {
String money = mEditTextMoney.getText().toString();
this.money = !TextUtils.isEmpty(money) ? money : "";
}
}
When users press Start button, the app will start updating text view each second
When users press Stop button, the app will stop updating the text view.
If users want to set a new money to display in the next time, just enter in the edit text then press Set Money.
This will execute only once though, even if the Looper should normally
keep the thread alive.
You seem to be confused about the Looper's purpose/functionality. The Looper is keeping the thread alive. If the main thread was not kept alive, your application would exit. The Looper does not, however, provide repeated execution of the Runnables posted to the Handler/Thread which it is associated with. Each Runnable is executed exactly once. If you want to execute the same Runnable multiple times, you must post it multiple times. For example:
Handler mainThreadHandler = new Handler(Looper.getMainLooper());
Runnable doToast = new Runnable() {
#Override
public void run() {
Toast.makeText(menuActivity, "HELLO", Toast.LENGTH_SHORT).show();
}
});
mainThreadHandler.post(doToast); // Queue the first execution of the code in the Runnable's run() method.
mainThreadHandler.post(doToast); // Queue the second execution of the code in the Runnable's run() method.
I am working on splash screen for my android app. The screen starts from my MainActivity.java, in which onCreate() method contains the following code. Also the same activity class is loading maps in its onResume() method and in it i m loading a new layout which is activity_main.xml. That xml has black screen and no background screen.
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.loading_screen);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus + "/"
+ progressBar.getMax());
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
compass = new CompassSensor(this, compassHandler);
}
my layout for loading_screen.xml is as follow.
loading_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash_screen_loading" xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/progressBar1"
android:layout_marginLeft="15dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
The problem i m facing is that only black screen shows during app loading. I tried to debug the problem but all my efforts were useless. Plz help.
You should not attempt this in onCreate, if you expect visual changes. Nothing is shown to user during onCreate. I am aware of Thread.sleep(200).
You may want to try doing it in your onResume, just after calling super.onResume. The way you described it, R.layout.loading_screen is not even supposed to be shown.
Also, if you're using AsyncTask to load maps, Developers lets you know how to publish progress. Take a look at this: AsyncTask. Maybe you could add this prior work (with progress on R.layout.loading_screen) to asynchronous task, too.
I need to show splash layout file until operation is completed and than change to main layout.
Here is an example .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
//Here app is copying files or something other it doesn't mater.
RelativeLayout panel = new RelativeLayout(this);
//Here is creating of main layout
setContentView(panel);
I need to show splash screen while operations are performing (between two methods setContentView) And than I need to set main layout,
I have tried to do in this way but it shows only black screen and only than shows main layout (panel).
Where is the problem ?
Thx for help in advance.
You can do something like this, if you're not doing heavy stuff precisely on setContentView(R.layout.main)
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
handler = new Handler();
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
//Do some heavy stuff
return null;
}
#Override
public void onPostExecute(Void result){
handler.post(new Runnable(){
#Override
public void run(){
setContentView(R.layout.main);
}
});
}
}.execute();
}
EDIT You can check this True SplashScreen
Here is a sample taken from my project:
Layout page, you can customize the progress bar as you want:
`
<ProgressBar
android:id="#+id/inquiry_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminateDrawable="#drawable/loading_progress"></ProgressBar>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal|center_vertical"
android:id="#+id/panel_inquiry"
>
.....
</FrameLayout>
`
Java source code:
private ProgressBar progressBar;
private FrameLayout inquiryFrame;
inquiryFrame=(FrameLayout)findViewById(R.id.panel_inquiry);
progressBar=(ProgressBar)findViewById(R.id.inquiry_progress);
In order to show the splash screen/do work in background
private void callService() {
progressBar.setVisibility(View.VISIBLE);
inquiryFrame.setVisibility(View.GONE);
new ServerReqAsync().execute(this
, ServiceConstants.getRequestList());
}
When background job completed, just view the activity default layout
progressBar.setVisibility(View.GONE);
inquiryFrame.setVisibility(View.VISIBLE);
Thanks
i have the following code and it works great for android 4.4 KitKat.
In the other android version the animation #anim/loading_animation is showing but not moving.
Someone can help me?? Thank you so much!!
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 4000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
splash_screen.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/Black" >
<ImageView
android:id="#+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:contentDescription="#string/image"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:src="#anim/loading_animation" />
</RelativeLayout>
Just check your manifest file and minimum SK version. Change it to API 8.I hope it works.
If you still face any issue. let me know
I have a question regarding the loading of the first Activity in my App. Before loading it, app shows this screen: https://www.dropbox.com/s/r33n3u3xfmth345/Screenshot_2013-08-16-12-02-08.png , and what I would like, is to load directly my Activity.
I'll mention that Im using SherlockActivity, and I already tried setting the Theme both in Manifest or programatically in onCreate() of my Activity, with same result (pre-loads with that screen for 2-3 secs, then loads my Activity).
Any thoughts ?
You have to use the splash screen Activity and after that you have to start your own activity from that Splash screen Activity.
Here is the code for splashActivity.
public class SplashActivity extends Activity {
private int splashTime = 3000;
private Thread thread;
private ProgressBar mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mSpinner = (ProgressBar) findViewById(R.id.Splash_ProgressBar);
mSpinner.setIndeterminate(true);
thread = new Thread(runable);
thread.start();
}
public Runnable runable = new Runnable() {
public void run() {
try {
Thread.sleep(splashTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
startActivity(new Intent(SplashActivity.this,YourActivityName.class));
finish();
} catch (Exception e) {
// TODO: handle exception
}
}
};
}
Here is code for activity_spalsh.xml file .....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#48AD83"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text=" your app name "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#A52A2A" />
<ProgressBar
android:id="#+id/Splash_ProgressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerInParent="true"
android:layout_marginTop="5dp" />
</RelativeLayout>