I'm doing an Android project with Asynctask, and I can't set TextView text to be "Loading", it is just blank until the AsyncTask loads. How could I overcome this problem?
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Loading today's fixtures & results");
HTMLParser parser = null;
try {
parser = new HTMLoadTask().execute("http://www.skysports.com/football/fixtures-results").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
You should remove the get() call in the end, as this waits for the task to finish and then it retrieves the result. This will lock-up the UI-thread.
get()
Waits if necessary for the computation to complete, and then retrieves its result.
I donĀ“t know what you are doing in the AsyncTask, but in onPostExecute() you should handle the result and set the text of the textview.
Related
I've tried to set the text on SwitchCompat, but it doesn't work. It only work for the first time. But when you tried to change the text (eg. when button is clicked), it doesn't work.
For example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.switch_test);
switchCompat.setTextOn("Yes");
switchCompat.setTextOff("No");
switchCompat.setShowText(true);
Button buttonTest = (Button)findViewById(R.id.button_test);
buttonTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchCompat.setTextOn("YOO");
switchCompat.setTextOff("NAH");
//switchCompat.requestLayout(); //tried to this but has no effect
//switchCompat.invalidate(); //tried to this but has no effect
}
});
}
You will see that the text stays as Yes and No. I've tried to call requestLayout() and invalidate() with no success. Any idea?
The problem is, that SwitchCompat is not designed with that case in mind. It has private fields mOnLayout and mOffLayout, which are computed once and not recomputed later when text is being changed.
So, you have to explicitly null them out in order text change to initiate those layouts to be recreated.
buttonTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Field mOnLayout = SwitchCompat.class.getDeclaredField("mOnLayout");
Field mOffLayout = SwitchCompat.class.getDeclaredField("mOffLayout");
mOnLayout.setAccessible(true);
mOffLayout.setAccessible(true);
mOnLayout.set(switchCompat, null);
mOffLayout.set(switchCompat, null);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
switchCompat.setTextOn("YOO");
switchCompat.setTextOff("NAH");
}
});
Result:
I need may load at first bacground (setContentView(R.layout.activity_logo); ) after (6 second) Text in TextView4. but program make wrong. After start application, app wait 6 second and next build setContentView(R.layout.activity_logo); and write to TextView4
Why?
Thanks
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
try {
TimeUnit.SECONDS.sleep(6);
TextView textview4 = (TextView) findViewById(R.id.textView4);
textview4.setText("alalaalalalalalal");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
TextView textview4 = (TextView) findViewById(R.id.textView4);
textview4.setText("alalaalalalalalal");
}
}, 6000);
}
sleep() blocks the UI thread and your UI cannot draw itself.
Use e.g. a Handler and postDelayed() to post a Runnable to run at a later time without blocking the UI thread.
I am trying to do an app in which there is a splash screen in the beginning. During loading, i need to show "Loading images...Loading modules....Now you are ready to go",etc. This is similar to the way in which facebook app loads showing the splash screen. I don't know how to do this. This is my current code :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread logoTimer = new Thread(){
public void run(){
try{
int logoTimer = 0;
while (logoTimer<5000){
sleep(100);
logoTimer=logoTimer+100;
}
startActivity(new Intent("com.package.MAIN"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
}
I would also like to keep a ProgressBar along with this. Can anyone help me out with a sample code or a reference. Thanks in advance.
You could use Handler to communicate from background thread to UI thread. Handler should send messages to UI when loading of one module is finished.
I want my activity to show a screen for 3 seconds, then go back to previous screen. But when i use
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_layout);
TextView tvResult = (TextView)findViewById(R.id.textView1)
Thread.sleep(3000);
Intent i = new Intent(this,myActivity.class);
startActivity(i);
But unfortunately, this does not work. This doesent show the activity waits 3 seconds and goes back. However, i want it to show its contents before going back. How can i do it ?
You should remove this Thread.sleep(3000); which block the ui thread. You should never block the ui thred. You can use a Handler postDelayed with a delay and then startActivtiy.
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run(){
// do something
}
}, 3000);
To go back to previous Activity you can call finish().
Also if you need to go back to the previous activity for 3 seconds why do you need
Intent i = new Intent(this,myActivity.class);
startActivity(i);
Calling finish() will do the job
This is not the recommended way to do this.
Using Thread.sleep you're blocking the main UI thread for 3000 milliseconds. This means that nothing in the activity will work until 3 seconds are passed.
Instead, you could do this:
edited: now it works well.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_layout);
TextView tvResult = (TextView)findViewById(R.id.textView1)
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
Thread.sleep(3000);
Intent i = new Intent(getApplicationContext(), myActivity.class);
startActivity(i);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
Good evening.
I have an activity like this. In handleMessage I have access to largeText field and can change it, but I can't do smth whith stringLinks field, or each other field which is not a UI element(like TextView, Button, EditText, etc).
How can I add to stringLinks ?
public class AboutUsActivity extends Activity {
Handler h;
TextView largeText;
List<String> stringLinks;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringLinks = new ArrayList<String>();
largeText = (TextView) findViewById(R.id.textView1);
h = new Handler() {
public void handleMessage(android.os.Message msg) {
HtmlParser parser;
StringBuilder result = new StringBuilder();
try {
parser = new HtmlParser(String.valueOf(msg.getData()));
List<TagNode> links = parser.getContentByClassName("ab");
for (Iterator<TagNode> iterator = stringLinks.iterator(); iterator
.hasNext();) {
TagNode divElement = (TagNode) iterator.next();
result.append(divElement.getText().toString());
}
} catch (Exception e) {
e.printStackTrace();
}
largeText.setText(newhtml); //Work
stringLinks.add(newhtml); //doesn't work
}
};
MyHttpClientUsage connect = new MyHttpClientUsage(h);
try {
connect.getInfoAbout();
} catch (HttpException e) {
e.printStackTrace();
}
}
}
I dont know if I understood you correctly.
If i did, you can use AsyncTask class
Check out their main too methods, doInBackground that executes something in a separate thread and onPostExecute that runs in UI Thread. They can comunicate with each other passing objects, and you can also publish the progress of your task.
It really looks like SwingWorker (maybe you used it if you programmed Swing applications).