I was wondering what would happen if I wrote one code in XML and another in Java.
I had this code in my program:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class ActivityName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
While in my Manifest I had this:
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar">
</activity>
My application started with no issues but ran the Java code and not what I had in XML. I assume this is true for all XML edits but wanted to make sure, if I had a Java code that is similar to XML with a bit of differences, would it use my XML format or Java code?
It'll always use the Java code, since that's run second. If you're inflating from XML, though, and you inflate after doing some Java stuff (to the layout), then the XML overrides the code you wrote.
The code takes precedence over xml. You can always over-define things from the code you write. As #Cornholio pointed out if you manipulate xml-based configurations from java code it can modify things you have set from java code.
The Android framework however will never overwrite for example the xml layout files you created.
Related
I am trying to use AdMob in Android and am following tutorials. I have successfully downloaded, installed and added the Google Play Services library. Nearly everything seems fine.
However Eclipse stubbornly gives a red underline under the "adView.setAdSize" bit. The error message says "The method setAdSize(AdSize) in the type AdView is not applicable for the arguments (AdSize)". I don't see what's wrong, that code is in line with all tutorials and documentation I can find, and why would setAdSize(AdSize) not take in AdSize as an argument? That doesn't make sense to me.
Unfortunately setting the ad size is necessary for the code to run so I can't just remove that bit. Relevant code is below. Thanks to anyone that can help.
...
import com.google.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity{
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create the adView.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ID REMOVED FOR PRIVACY REASONS");
...
}
...
}
Change:
import com.google.ads.AdSize;
For:
import com.google.android.gms.ads.AdSize;
image
Well i have this problem when the code is correct, but it gives me red line error!
can anybody help me.
Main.java
package com.example.sout;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import
~
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource (R.anim.animation);
~~~~~~~~~~~~~~~~
iv.setOnClickListener(new OnClickListener());
~~~~~~~~~~~~~~~ ~
}
in (note the ~ characters above):
iv.setBackgroundResource (R.anim.animation);
iv.setOnClickListener(new OnClickListener());
The setBackgroundResource and setOnClickListener are red too.
The error in iv.setBackgroundResource (R.anim.animation); comes because that method takes integer as a parameter .So change it to the following,
iv.setBackgroundResource (R.drawable.image1); //image1 is a drawable which is inside your drawable folder.
and the second error comes because you have not imported the required package and have not overridden the methods required for that. Change it to the following,
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your task here
}
});
after changing press ctrl+shift+p to import the required packages.
Sometimes, at least with Eclipse, it gets it into it head that there's an error and it won't let it go. But, with Eclipse, you can at least put the cursor over a marker and it will explain what the problem is (or what it thinks it is). I'd try to hover over the errant lines to see if a popup box appears telling you what it thinks.
If you're sure it's wrong, often I've found that saving the file (or all files) will fix it. Sometimes adding then deleting a space on the errant line will fix it.
However, I notice that you have an incomplete import at the top of your file and this may be preventing the syntax checker from properly analysing the source file.
My advice is to fix that first, then try those other two tricks (save then, if that doesn't work, edit and undo on the errant line). Hopefully that will make it disappear.
Of course, you may want to check, just in case, your R.java file to ensure that member exists in there somewhere. I've been bitten by my own misspellings before.
I setup ActionBarSherlock with my app, and I'm trying to use the Intermediate Progress, I'm using this:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setSupportProgressBarIndeterminateVisibility(false);
In my onCreate, and then using:
setSupportProgressBarIndeterminateVisibility(true);
To enable it.
It works fine in ICS but it doesn't work at all in Gingerbread or Froyo, does anyone know how to get it to work? Thanks
I just had the same problem. Jake's solution above did not fix it for me - the method is undefined.
I found a working solution posted by Jake on the bug list for ActionBarSherlock here:
Action Bar Indeterminate Progress Bar Not Disappearing
See Jake's response to the poster - the trick is to call getSupportActionBar() first, to "trigger creation of the views".
So my onCreate() method is:
protected void onCreate(Bundle arg0)
{
super.onCreate(arg0);
// allow window to show progress spinner in the action bar
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getSupportActionBar();
setSupportProgressBarIndeterminateVisibility(false);
}
Update based on comment from Laux:
Make sure your imports reflect com.actionbarsherlock.view.Window.FEATURE_INDETERMINATE_PROGRESS for this to work.
Here is part of my import block from an app that uses this pattern:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionProvider;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.actionbarsherlock.widget.ShareActionProvider;
This is a very good thing to remember when working with ABS - many of your normal Android imports should be updated to refer to ABS instead.
It may be a good idea to revisit your import block, or possibly remove it entirely and let Eclipse rebuild it for you (CTRL-SHIFT-O) to which point Eclipse will prompt you for each import that ABS redeclares.
This was also explained by Glebbb in his answer.
I'm sure you've probably figured it out by now, but the most likely culprit is you including the wrong file because it's so easy to do automatically.
Replace any import of android.view.Window with com.actionbarsherlock.view.Window and the needed features will work.
You need to call supportRequestWindowFeature.
requestWindowFeature is a final method on Activity and couldn't be overriden.
Check, if you are using Theme.Sherlock.NoActionBar or similar no action bar theme for this activity.
In this case setSupportProgressBarIndeterminateVisibility method fails for me with
Caused by: java.lang.NullPointerException
at com.actionbarsherlock.internal.ActionBarSherlockCompat.updateProgressBars(ActionBarSherlockCompat.java:710)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.onIntChanged(ActionBarSherlockCompat.java:686)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.updateInt(ActionBarSherlockCompat.java:681)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setFeatureInt(ActionBarSherlockCompat.java:665)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setProgressBarIndeterminateVisibility(ActionBarSherlockCompat.java:637)
at com.actionbarsherlock.app.SherlockFragmentActivity.setSupportProgressBarIndeterminateVisibility(SherlockFragmentActivity.java:282)
I guess you should use a progress dialog instead to indicate loading process or regular Theme with activity title bar and then use setProgressBarIndeterminateVisibility method for older platforms.
i m new to this platform, please help me to find what is the error...
setContentView(R.layout.main); // this line shows the error.
Code:
package com.example.helloandroid;
import android.R;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Try deleting the line import android.R then clean your project.
Just adding more details on why the error is coming.
As there is an import of android.R so setContentView() is looking for a layout file 'android.R.layout.main' and there is no main.xml in the layout files that come along with SDK. So, using the correct R.java import will work.
clean Project then try To Run because i faced same problem before a month and remember layout/main.xml must be their and it must not contains any error.
you can Also Do this
import android.R;
or
import your.application.packagename.R; Now Clear Project and Run it.
You must have to simply change the
setContentView(R.layout.main);
... to:
setContentView(R.layout.activity_main);
... because Layout contains this .xml file.
I hope your problem will be solved.
First remove import android.R;
After any change on xml fiels you must clean project.
Build > Clean Project
after that every things corrects.
What I did was create two .java files. One that can compile and run on a 1.5 phone (SDK3) and then one that works on 2.0(SDK5) So for this example i'll call the 1.5 file ExampleOld and the new one Example. I was wondering if i just made activity like this if it would work sort of like a "portal" and pick the activity to load depending on the SDK so there is no crash or compile errors. Are there any changes I should make to my code? Maybe anyone out there that's had to do this before. thanks!
package com.my.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
public class ExamplePortal extends Activity {
int sdk=new Integer(Build.VERSION.SDK).intValue();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (sdk<5) {
Intent v = new Intent(this, ExampleOld.class);
startActivity(v);
}
else {
Intent v = new Intent(this, Example.class);
startActivity(v);
}
}
}
What you're doing (correct me if I'm wrong) is trying to maintain backwards compatibility while making use of new APIs if the user is running a newer android version. The best way to do this is to follow the tutorial Google posted here. This avoids any verification issues and is really the best way to do stuff imho.
I would put this decision in a Factory Class to avoid having these if-else statements all over the codebase.