Need help in debugging AsyncTask with layout view - android

I am using an Asynctask in the splash screen to add database items into database.
The insert process went well but the splash view just won't show up.
I am posting my code below, any advice would be appreciated. thx
splash.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash">
</RelativeLayout>
Splash.java
public class Splash extends Activity {
private ChannelDB mDB;
private TextView loading;
private String channelS_TABLE;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new SetDB().execute();
}
private class SetDB extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
if (tabIsExist(channelS_TABLE) !=true){
**some database insert**
}else{
synchronized(this){
Log.i("Splash", "the table is there");
wait(3000);}
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
}catch (Exception e) {
Log.i("Splash", "setDB exception");
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
return null;
}
protected void onPreExecute(String load) {
synchronized (this) { try {
wait(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
protected void onPostExecute(String finish) {
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
}
public boolean tabIsExist(String tableName){
boolean result = false;
if(tableName == null){
return false;
}
Cursor cursor= ChannelDB.check();
startManagingCursor(cursor);
try {
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
} catch (Exception e) {
Log.e(this.toString(),"error:"+e.toString());
Intent intent = new Intent(this,HDtvs.class);
startActivity(intent);
this.finish();
}
return result;
}
}
I changed the xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash">
<ImageView android:id="#+id/bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash"/>
</LinearLayout>
still not working and the database insert is not workong too.

RelativeLayout is a container. Its background will only show when you have some elements / views inside it. I am not sure about it though. But you can try this - remove the image from the background of the RelativeLayout, and create an ImageView inside the RelativeLayout and set the ImageView's source image as your desired image. You should get the desired result.

Related

Android videoview fullscreen

i working videoview. i wrote code witch can play video from url with asynctask.at the moment everythink is ok but i have another problem.when i run my app videoview is not full screen.
[i have like this resulit 1
this is a my source
public class Layout1 extends Fragment {
VideoView video;
MediaController media;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#SuppressWarnings("deprecation")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout1, null);
video = (VideoView) view.findViewById(R.id.video_view);
new BackgroundAsyncTask()
.execute(Global.yutubeDownloadUrl);
return view;
}
public class BackgroundAsyncTask extends AsyncTask<String, Uri, Void> {
Integer track = 0;
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, Please Wait...");
dialog.setCancelable(true);
dialog.show();
}
protected void onProgressUpdate(final Uri... uri) {
try {
media=new MediaController(getActivity());
video.setMediaController(media);
media.setVisibility(View.GONE);
media.setPrevNextListeners(new View.OnClickListener() {
#Override
public void onClick(View v) {
// next button clicked
}
}, new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
media.show();
video.setVideoURI(uri[0]);
video.requestFocus();
video.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
video.start();
dialog.dismiss();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
#Override
protected Void doInBackground(String... params) {
try {
Uri uri = Uri.parse(params[0]);
publishProgress(uri);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
and xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView
android:id="#+id/video_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
what am i doing wrong? if anyone knows solution please help me
The easy way is to change your activity theme to
#android:style/Theme.Holo.NoActionBar.Fullscreen
or
#android:style/Theme.Black.NoTitleBar.Fullscreen
(as long as it has fullscreen in the name it's fine).
if you really need the videoview to be in a fragment there might be some problems, but as i see your code you can use an activity instead of that fragment and set the theme for that activity.
By the way... you don't need to use an async.

show an image for some seconds before of the setContentView of the main activity

I want to show a "boot logo" or image before of setting the final layout of the main activity. Actually i do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authors);
mContext = this;
showAuthors();
where showAuthors run this:
private void showAuthors()
{
setContentView(R.layout.authors);
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
};
logoTimer.start();
try {
logoTimer.join();
setContentView(R.layout.activity_main);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
the file authors.xml ( R.layout.authors ) is the same of activity_main, but clean, containing just a pair of strings with my name and email:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#raw/call2"
android:src="#raw/call2"
android:scaleType = "centerCrop"
tools:context=".MainActivity" >
<TextView
android:id="#+id/authorsTV"
android:textColor="#FF6600"
android:textSize="16.5sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="33dp"
android:text="#string/serviceStatus" />
</RelativeLayout>
the problem is: all works, but the screen is all white as an empty layout.
where i'm doing wrong? thank you all if consider to reply me!
You should not call thread.join from the main thread, because you can get an ANR crash.
Here's how to do it with minimal changes to your existing code. Remove everything after logoTimer.start(), and put this inside the try block, immediately after sleep(3000):
runOnUiThread(new Runnable(){
public void run(){
setContentView(R.layout.activity_main);
}
});
But it would be cleaner to rewrite that whole method like this:
private void showAuthors()
{
setContentView(R.layout.authors);
new Handler().postDelayed( new Runnable(){
public void run(){
setContentView(R.layout.activity_main);
}
}, 3000);
}
First you have two ways of showing a splash screen,
1.with activity
public class SplashActivity extends Activity implements Runnable
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
setContentView(R.layout.act_spalsh);
hideSplashActivity();
}
private void hideSplashActivity()
{
new Handler().postDelayed(this, 3000);
}
#Override //from runnable
public void run()
{
startActivity(new Intent(this, MainActivity .class));
finish();
}
2.with dialog
public class MainActivity extends Activity implements Runnable
{
Dialog splashDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
splashDialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
splashDialog.setContentView(R.layout.dlg_splash);
splashDialog.show();
hideSplashDialog();
setContentView(R.layout.act_main);
}
private void hideSplashDialog()
{
new Handler().postDelayed(this, 3000);
}
#Override //from runnable
public void run()
{
splashDialog.dismiss();
splashDialog = null;
}
I prefer using dialog unless you have some trouble with that

Only select one checkbox of splash preference checkedboxs

I have app starting depend on user preferance with three different checkedbox :
1- start app without splash and music .
2- start app with splash only .
3- start app with spalsh and music .
with the below code its work perfectly .
But still two point to be achieved :
FIRST only one checkbox should be checked .
SECOND after you checked any one of checkboxs as you prefer then go back to mainactivity , here you can exit the app either by use back button or exit button which i already have it in my option menu , the problem is that either using back button or exit button it doesn't respond to first click , i have to click twice to exit the app.
but i cant achieve it ,
any help please will be appreciated .
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs1.getBoolean("splash_music", true);
if (music == true)
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(2000); }
catch (InterruptedException e){
e.printStackTrace(); }
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent); }}
};
timer.start(); }
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
In xml folder :prefs.xml
<?xml version="1.0" encoding="utf-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:title="without splash screen"
android:defaultValue="true"
android:key="without_splash_screen"
android:summary="Start app without splash"/>
<CheckBoxPreference android:title="splash screen"
android:defaultValue="true"
android:key="splash"
android:summary="Start app with splash only" />
<CheckBoxPreference android:title="splash screen music"
android:defaultValue="true"
android:key="splash_music"
android:summary="Start app with splash and music" />
</PreferenceScreen>
UPDATE:
i tried also the below code but it doesn't change any thing , still all chechboxs can be checked :
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs1.getBoolean("splash_music", true);
if (music == true)
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
public void getPrefs() {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
boolean splash = getPrefs.getBoolean("splash", false);
boolean music = getPrefs.getBoolean("splash_music", false);
if (without_splash_screen == true){
splash = false;
music = false;
}else if (splash == true){
music = false;
without_splash_screen = false;
}else if (music == true){
without_splash_screen = false;
splash = false;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
UPDATE:
i tried also another code but it doesn't change any thing , still all chechboxs can be checked :
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
else {
getPrefs.getBoolean("splash", false);
getPrefs.getBoolean("splash_music", false);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
else
{
getPrefs.getBoolean("without_splash_screen", false);
getPrefs.getBoolean("splash_music", false);
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
boolean splash_music = getPrefs.getBoolean("splash_music", true);
if (splash_music == true) {
ourSong.start();
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
else
{
getPrefs.getBoolean("without_splash_screen", false);
getPrefs.getBoolean("splash", false);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Any advice , thanks.
UPDATE 3 (WHOLE PROJECT):
1- one checkbox checked achieved perfectly .
2- either using back button or exit button in MainActivity , doesn't respond to first click , i have to click twice or three times to exit the app.
Splash.java
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle DrTsn) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(DrTsn);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs1.getBoolean("splash_music", true);
if (music == true)
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Prefs.java
public class Prefs extends PreferenceActivity {
CheckBoxPreference splash;
CheckBoxPreference splash_music;
CheckBoxPreference no_splash_music;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
splash = (CheckBoxPreference) findPreference("splash");
splash_music = (CheckBoxPreference) findPreference("splash_music");
no_splash_music = (CheckBoxPreference) findPreference("without_splash_screen");
splash.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(true);
splash_music.setChecked(false);
no_splash_music.setChecked(false);
return true;
}
});
splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(true);
no_splash_music.setChecked(false);
return true;
}
});
no_splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(false);
no_splash_music.setChecked(true);
return true;
}
});
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cool_menu, menu);
getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,AttributeSet attrs) {
if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
((TextView) view).setTextSize(25);
((TextView) view).setTextColor(Color.RED);
}
}
);
return view;
}
catch (InflateException e) {
}
catch (ClassNotFoundException e) {
}
}
return null;
}
}
);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.aboutUs:
Intent i = new Intent("com.example.checkbox.ABOUT");
startActivity(i);
break;
case R.id.preferences:
Intent p = new Intent("com.example.checkbox.PREFS");
startActivity(p);
break;
case R.id.exit:
finish();
break;
}
return false;
}
#Override
public void onBackPressed() {
finish();
}
}
AboutUs.java
public class AboutUs extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about);
Button button = (Button)findViewById(R.id.about_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
}
);}
}
"FIRST only one checkbox should be checked ."
Not sure how to make this a short and easy answer, but hang with me, this works.
Create a preference activity that you call from your activity.
Use something like this in your onOptionSelected:
case R.id.prefs:
Intent p = new Intent(MainActivity.this, Prefs.class);
startActivity(p);
break;
This is the Prefs.class
public class Prefs extends PreferenceActivity {
CheckBoxPreference splash;
CheckBoxPreference splash_music;
CheckBoxPreference no_splash_music;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
splash = (CheckBoxPreference) findPreference("splash");
splash_music = (CheckBoxPreference) findPreference("splash_music");
no_splash_music = (CheckBoxPreference) findPreference("without_splash_screen");
splash.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(true);
splash_music.setChecked(false);
no_splash_music.setChecked(false);
return true;
}
});
splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(true);
no_splash_music.setChecked(false);
return true;
}
});
no_splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(false);
no_splash_music.setChecked(true);
return true;
}
});
}
}
And don't forget to add it to the Manifest:
<activity
android:name="com.lordmarty.testforlools.Prefs"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.lordmarty.testforlools.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The prefs.xml file is the same as the one you posted.
Let me know if you get any errors. I've tested it, and it works fine here.
"SECOND"
Not sure why you have to click twice. But it might be caused by the splash not finishing, just calling a new intent.
Try this where ever you start your mainclass from the splash:
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
finish();
}
Now, I've looked over your project and found out why it where so buggy.
Main reason:
if (music == true)
ourSong.start();
Thread timer = new Thread(){
Here you are missing a brackets for the if statement, causing the thread to run everytime. Therefore you have two MainActivities running at the same time.
Other notes: You use "if"'s for all the values. I suggest you use "else if".
And I did a small cleanup.
Here u go. Enjoy:
Link to Splash.java
You can solve the last problem in many ways. One of them being this:
// add this below the other if else if statements in the splass.java
else if(without_splash_screen == false && splash == false && music == false){
//put whatever you want to happen if none of them are checked. This is just an example.
setContentView(R.layout.splash);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(Splash.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
};
timer.start();
You should just be able to change it to
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
boolean splash = getPrefs.getBoolean("splash", true);
boolean splash_music = getPrefs.getBoolean("splash_music", true);
if (without_splash_screen)
{
...
}
else if (splash)
{
...
}
else if (splash_music)
{
....
}
I implemented Radio buttons inside preference activity that looks like check boxes as follows..
Only one can be selected at a time.
I done this using 2 more xml files
One inside drawable(check_dyn.xml) and another inside layout(radiochecks.xml) .
check_dyn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="#android:drawable/checkbox_off_background" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="#android:drawable/checkbox_on_background" />
</selector>
radiochecks.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/check_dyn"
android:checked="true"
android:text="RadioButton1" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/check_dyn"
android:text="RadioButton2" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/check_dyn"
android:text="RadioButton3" />
</RadioGroup>
</LinearLayout>
Now, inside the preference,(here prefs.xml), add a preference and set its layout property. ie,
<Preference android:layout="#layout/radiochecks"
/>
Note: I don't prefer a practice like this, since it is hard to get the individual values.
I prefer ->

Android layout not loaded on onStart()

While showing a background I want to run some operations (without using threads).
In my activity on onCreate() I have setContentView(R.layout.splash);
When the app fires onStart(), the layout is not loaded yet. Why?
UPDATE: LogCat gives me "Activity has leaked window"
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/splash"
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</LinearLayout>
onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
onStart
#Override
protected void onStart() {
super.onStart();
if (CBConstants.ASSETS_MANAGER){
String progressMsg = this.getResources().getString(R.string.progress_dialog_assets_upgrading_message);
ProgressDialog progressDialog = ProgressDialog.show(this, null, progressMsg);
progressDialog.show();
try {
[...]
} catch(Exception e) {
Log.d("appcheck", ""+e);
Intent intent = new Intent(SplashScreen.this, CBWebViewActivity.class);
startActivity(intent);
}
}
else {
Thread noAssetManager = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {
if (LOG) Log.d("appcheck", ""+e);
}
finally {
Intent intent = new Intent(SplashScreen.this, CBWebViewActivity.class);
startActivity(intent);
}
}
};
noAssetManager.start();
}
}

White screen before splashscreen

I have an issue with my SplashScreenActivity, when I start my application on my phone it shows a white screen for about 0,5 seconds. The MainActitivy extends FragmentActivity and in the AndroidManifest I declare the SplashScreenActivity as launcher and portrait mode as screenOrientation.
The code:
public class SplashScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
randomSplash();
Thread splashscreen = new Thread() {
public void run() {
try {
Thread.sleep(1000);
Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
startActivity(mainScreen);
} catch (InterruptedException e) {
} finally {
finish();
}
}
};
splashscreen.start();
}
private void randomSplash(){
Random random = new Random();
int i = random.nextInt(4);
LinearLayout ln = (LinearLayout) findViewById(R.id.splashscreen);
switch(i){
case 1:
ln.setBackgroundResource(R.drawable.splash1);
break;
case 2:
ln.setBackgroundResource(R.drawable.splash2);
break;
case 3:
ln.setBackgroundResource(R.drawable.splash3);
break;
default:
ln.setBackgroundResource(R.drawable.splash0);
break;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Thread splashscreen = new Thread() {
public void run() {
try {
Thread.sleep(1000);
Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
startActivity(mainScreen);
} catch (InterruptedException e) {
} finally {
finish();
}
}
};
splashscreen.start();
this is your problem UI thread to sleep not a very good idea use handler instead
and I think it may cause an exception too.
Handler h=new Handler();
h.postDelayed(new Runnable() {
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(Splash_Activity.this,Main_Activity.class));
finish();
}
}, 2000);
}
You need to run this two actions in an AsyncTask:
setContentView(R.layout.splashscreen);
randomSplash();
put the setContentView in the doInBackground-method and in the postExecute method you run randomSplash.
Change SplashActivity theme in the AndroidManifest.xml file to this.
android:theme="#android:style/Theme.Translucent.NoTitleBar"

Categories

Resources