Android - Display the loading text - java

I am trying to display Loading text only when its loading.
The problem is im setting it to visible and straight after to invisible once finished loading. But text never has a chance to update. Is there a way to force refresh the screen, or maybe there is another way to do this?
Thanks
This is my code
SearchBtn = (Button) findViewById(R.id.SearchButton);
SearchBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
//this is never seen because its set straight after to invisible
LoadingText.setVisibility(View.VISIBLE);
SearchFor(EditSearchField.getText().toString()); // all loading done here
LoadingText.setVisibility(View.INVISIBLE);
HideKeyboard();
}
});

You need to do the actual load in a separate thread. You'd be best off using an AsyncTask for this. Take a look at http://www.vogella.com/articles/AndroidPerformance/article.html

Related

How do I change the main xml file from another activity?

I am very new to Java. I am doing a school project at the moment and I have my main activity, then I have a settings activity. I am trying to modify the xml from the main activity with the settings activity. I am able to modify the settings xml file with the settings.java, but I would like to modify the main activity xml with settings.java
public class Settings extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Get the Intent that started this activity and extract the string
Switch switchButton;
final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_settings);
final RelativeLayout mRelativeLayoutMain = (RelativeLayout) findViewById(R.id.activity_main);
switchButton = (Switch) findViewById(R.id.switch1);
switchButton.setChecked(true);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}
}
});
if (switchButton.isChecked()) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}}
public void toast1(View view) {
android.widget.Toast.makeText(this, "Created by Cody Walls and Tommy Serfas", android.widget.Toast.LENGTH_LONG).show();
}
/*public void switch1(View view) {
ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.setBackgroundColor(Color.GRAY);
}*/
}
In the Code I am trying to change the background of the main activity xml with :
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
and when I run the app and click the intent it will crash with the error:
"java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null
object reference"
I think the easiest way is to create an PreferenceManager.SharedPreferences, in which I recommend you to store current app data. This will help you not to loose any changes in app after you exit the it. Here is short instructions:
Create button in settings activity which will change something in main activity.
Create onClickListener for your button.
Use .SharedPreferences to store was you button clicked or not. (I recommend storing boolean variables, this way you can store was button clicked or not.)
I both of your activities in onCreate method call .getSharedPreferences to read saved app values. (I mean to read was the button clicked or not.)
Use app values you got from 4. to change any element in activity. (For example if you stored that button was clicked, then change some TextView text or etc.)
I hope you understood the idea.
Link to the Android developer tutorial about App key values storing & saving
Link to the StackOverflow much easier explanation & examples
There are a couple of ways of doing this (Some of which depends on how you are switching back and forth from each activity). It also depends on what things you are changing.
From your settings page, as you are changing different settings, you'll save this content within Preferences. (You can see more how to use Preferences here: https://examples.javacodegeeks.com/android/core/ui/settings/android-settings-example/ or by just Googling it).
On you main activity, depending on how you come back to it (onStart most likely), you can setup the things you need to programmatically.
So, you may need to do a little research on the Android lifecycle and how each cycle works (https://developer.android.com/guide/components/activities/activity-lifecycle.html), how to program the UI programmatically through Java (http://startandroid.ru/en/lessons/220-lesson-16-creating-layout-programmatically-layoutparams.html), and the Preferences Android library to save certain settings.
The xml isn't meant to be "altered". You can change the UI programmatically. It's possible to build an Android app without any xml. When Android was first built, it didn't use the xml to create the UI. It was all done through Java. It was then added to use xml to create your activities or fragments or any UI component. This made things easier for more static activities or activities with very little dynamic content.

setOnClickListener doesn't work (as I want)

I have code like this below. It works (run without any error), however it seems button doesn't call method boardMaker() properly. Furthermore two previous lines doesn't work either. When I comment line with boardMaker call. It works. Any suggestions why this call paralyze my button?
(refresh is RelativeLayout)
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
refresh.setVisibility(View.INVISIBLE);
resultDisplay.setTextColor(Color.MAGENTA);
boardMaker(1);
}
});
There is no reason why the lines of code inside the onClickListener block would not work you're syntax is perfect.
Please show us boardMaker() method.
Your button getting paralyzed could be a sign of a blocking call made in boardMaker().

Is that logical and a right way to add a button to close application in java?

I'm using Android Studio.
In the MainActivity inside the onCreate I did:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTime = SystemClock.uptimeMillis();
serverChecksThread.start();
status1 = (TextView) findViewById(R.id.textView3);
timerValue = (TextView) findViewById(R.id.timerValue);
uploadedfilescount = (TextView) findViewById(R.id.numberofuploadedFiles);
uploadedfilescount.setText("Uploaded Files: 0");
addListenerOnButton();
initTTS();
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
And in the activity_main.xml I added:
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/btn1"
android:text="Close App" />
I have two questions.
The main is that if it's logical and good thing to do to add a button that will close the application so it will not leave anything behind? This way when i'm running the application over again on my android device it's starting clean reseted.
The sub question is when I click the button and exit the app then when I'm running it again for a millisecond the app blink. And it happen after I added this button code it wasn't before. I'm not getting error or exception but it's blinking for a very short time.
"Running" and "closed" are fuzzy concepts in Android. When an app is in the background, it may or may not actually be running. When an activity is in the backstack, an in-memory instance of it may or may not exist. When your last activity finishes, the framework may or may not kill the process. And when you start the app again, the framework may or may not create a new instance of your Application class.
Calling System.exit(0) is a bad idea because it short-circuits the Android framework. It may result in unspecified behavior (read: really strange bugs.) Better to just finish your last activity and let the framework do as it likes.
Whether it's good UX to show a close button is a matter of opinion. Google recommends against it. The preferred way to "close" an activity is by pressing the back button.
It is not necessary that you add a close button to an application on android because, there is usually a, either software button, or b, a hardware button on the device to close (minimize) applications. so it wouldn't be a good thing to add a button, and it would also be illogical
And for your second question, I did not quite understand your point.
Even if you close your app with a button, it won't be closed permanently, it still will be shown on the users device as a 'running on background app'. Because android is not working like windows, so its not so useful to add such a button.
As far as i know the only apps that using this button is using it to be sure in 100% that the user left the app and that the connection that he had will be closed, so no one else will be able to use his login or password...

Hiding ActionBar on touching surface

I'm writing a very simple android app. When the user touches the surface and moves their finger around, pictures are drawn below their fingers. When their finger is raised, this stops.
I want the action bar to hide when the finger is down and reappear when it is raised.
The ActionBar is overlayed. I thought using ActionBar.hide() and .show() in my view's onTouchEvent would work fine, but I've hit some problems.
I've tried a few things. First I tried passing the ActionBar created in the Activity into the view, but this didn't work as the view has to be created before the ActionBar is "got", so I would just be passing in a null value.
I then tried to make a static ActionBar which I can call from my view, but then I get this error: "Only the original thread that created a view hierarchy can touch its views.”
Anyone know of a way around this?
Take a look at one of the examples that is part of the jfeinstein10's Sliding Menu library
In it, in the BirdActivity.java, you will see how the ActionBar is shown and hidden after 2 seconds. You will naturally have to adapt the code yourself. But this should get you started:
Excerpt of the code from Line No. 53 - 58:
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getSupportActionBar().show();
hideActionBarDelayed(mHandler);
}
});
The hideActionBarDelayed() from Line No. 80 - 86:
private void hideActionBarDelayed(Handler handler) {
handler.postDelayed(new Runnable() {
public void run() {
getSupportActionBar().hide();
}
}, 2000);
}
UPDATE
For the lack of code, most of the problem has been assumed. It may be wrong simply for the lack of data from the OP. That being said, if sorting out the "Only the original thread that created a view hierarchy can touch its views." problem will fix it, perhaps this may do it for you:
Runnable run = new Runnable() {
#Override
public void run() {
// RUN YOUR CODE TO HIDE / SHOW THE ACTIONBAR HERE
}
}; YOUR_ACTIVITY.this.runOnUiThread(run); // REPLACE WITH getActivity().runOnUiThread(run); IF THIS IS A FRAGMENT
NOTE: In the example, the activity in question uses ActionBarSherlock and extends SherlockActivity and therefore, the use of getSupportActionBar(). If you are not using ABS, you will have to use getActionBar() instead of the former.

TranslateAnimation Not Working As Expected

I wanted to move some linear layout using TranslateAnimation. I have 2 problems. My base SDK is Android 2.2.
Even when the animation is finished, the touchable area in the linear layout was not moved at all.
The screen flashed for a couple of frames right after the animation was finished.
At first, I didn't use AnimationListener and LinearLayout.layout(). When I fnished the animation using the following code, the position of the view was indeed changed. But it seemed that the touchable area was not moved with the view during animation. As a result, when I tried to clicked any of the buttons on the view after animation, nothing happened. If I clicked the original area of the buttons (the original area before the animation took place), the on_click_listener was triggerred.
Then i deleted this line of code,
tmpAnimation.setFillAfter(true);
and tried AnimationListener and LinearLayout.layout(). It did help and sovled the 1st problem.
But there came the 2 problem. After the animation, some of my linear layouts would flash for a couple of frames and then back to order.
I've tried midLinearlayout.requestLayout(), it doesn't work.I tried implemented Animation.AnimationListener and override onAnimationEnd like someone said,but it doesn't work either.
TranslateAnimation tmpAnimation = new TranslateAnimation(midLinearlayout.getLeft(),midLinearlayout.getLeft(),midLinearlayout.getTop(),midLinearlayout.getTop()+100);
//tmpAnimation.setFillAfter(true);
tmpAnimation.setDuration(2000);
tmpAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("onflingTest","top="+midLinearlayout.getTop()+" left="+midLinearlayout.getLeft()+" right" + midLinearlayout.getRight());
midLinearlayout.layout(midLinearlayout.getLeft(), midLinearlayout.getTop()+100, midLinearlayout.getLeft() + midLinearlayout.getMeasuredWidth(), midLinearlayout.getTop()+ 100 + midLinearlayout.getMeasuredHeight());
}
#Override
public void onAnimationRepeat(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
I've solve this by the code below:
linearlayout.clearAnimation();
see the link:
EditText stucks after animation and alive back on scrolling......?
I solved the issue with the help from post View.GONE in animation complete
The problem is after layout B completes the animation, i missed to make the view state as View.GONE. Adding View.GONE brought back the controls.

Categories