i downloaded zxing library and installed it on my device. now i tried to make a simple app the extends zxing library. in this simple app I created a new layout for it and when i tried to setmycontentview to be this layout, eclipse highlits it with red and when i move the mouse pointer on the highlighted layout, eclipse suggests the following:
1-create the field "activity_firstactivity" in type layout
OR
2-create constant "activity_firstactivity" in type layout
now, my question is:why eclipse can not reach my xml file for this layout, i created the layout inside the layout folder but eclipse can't recognise it.
UPDATE:
even when i press ctrl+space the layout name is not listed
Java Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstactivity);
probably Eclipse is trying to find the layout in the android package. Try to access your layout through the full name of your package:
your.package.name.R.layout.activity_firstactivity
Related
Recently I came across the use-case of using tools:context in my layout files for activities.
I learned that I can use it to associate an Activity class file to my layout file.
But how can I use it to associate a Fragment class file to any layout?
Bacuase as I have tried, it is only showing Activity list in autocomplete suggestion.
There is a bug in Android Studio, you have to write it manually, it will appear red(indicating it's unresolved) until you finish typing, then it will appear in green
Have you tried it doing like this:
Let's say Relative layout is your parent layout in your fragment's layout:
<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"
tools:context=".Folder.Fragment" >
Now this .xml file will be associated with your .Fragment class which is a fragment.
Key point: Autocomplete is not showing name, manually enter it and it will compile.
Create a fragment with a layout file and see the layout file for tools:context.
I just had the same problem and typing in the fragment name didn't get the fragment name recognized in the tools:context=".NameFragment" statement.
What did work was first deleting both the class and xml files for that fragment and then:
Right click on the package name and choose New->Fragment->Fragment (Blank)
Enter in your fragment name (the layout name is automatically created on the same form) and click Finish.
This method created both the class and xml files at the same time. and at this point, the tools:context statement in my xml file had my fragment name automatically and was not red but green text (indicating that it was recognized). I'm seeing this on Android Studio Bumblebee 2021.1.1.
Earlier I had created the xml file first and then created the class file (and had problems with the fragment class file name not being recognized by the tools:context statement).
I have just begun learning Android App development. I have Android Studio 1.4. In my layout folder I have two XML files (content_main.xml and activity_main.xml). I use online tutorials to learn but they have only activity_main.xml in them. So what i want to know is what are the functions that should be used in these respective files. can i just use activity_main.xml and just let the other be ? and vice versa.
The modern Android approach is based on Fragments, which are, in a way, "small Activities", which you can put in Activities, gaining lots of flexibility.
Therefore, activity_main.xml is simply the Activity layout containing a container (FrameLayout most probably) and content_main.xml is the layout for a Fragment put into this container somewhere within MainActivity.java. You should study the code in there to understand it better :)
As I know, there must be include statement in your activity_main.xml file as follows :
<include layout="#layout/content_main" />
that means it is calling the content_main.xml which has actual elements to be hold.
There will be no problem if you cut and paste all the content of content_main.xml file and paste it in activity_main.xml file in place of include statement(tag).
You can delete your content_main.xml after doing as above.
In your activity setContentView() statement should be look like as below :
setContentView(R.layout.activity_main);
According to new design pattern in android studio activity_main.xml will determine how the look of the main activity should be. And on the other hand content_main.xml will determine the contents in the activity_main.xml. That is content_main.xml will contain the textview, edittext, button etc component. And the content_main.xml will be included by the activity_main.xml .
So we can think of content_main.xml just like partial in HTML.
activity_main.xml will contain your activity global design, and
content_main.xml the contents.
What is the role of content_main.xml in android studio 1.4?
So it seems the content_main.xml is a part of a new design pattern introduced in Android Studio 1.4.
For the moment, to get along with the tutorials you can find pick the 'empty activity' when creating a new project. It will not contain the content_main.xml.
As mentioned before, the layout file used for your activity is set with setContentView(R.layout.activity_main); in the onCreate function of the activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have not seen Android Studio creating two layout files for one activity. Perhaps the content_main.xml was generated for a previous activity, wasn't it?
Anyway, it doesn't matter what is the name the layout file. Choose one and go for it. Just remember to set the right one in your Activity:
#Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.your_layout_here);
}
use that one which is set in Activity class. i mean set to setContentView(). or please provide your code if you want more description.
I updated my android studio to latest version that is android studio1.4.
By default in a new project, there is a file content_main.xml in layout folder.
What is the use of this file?
According to new design pattern in android studio activity_main.xml will determine how the global UI of the Activity should be. And on the other hand content_main.xml will determine the contents in the activity_main.xml.
That is content_main.xml will contain the textview, edittext, button etc component. And it will be included by the activity_main.xml.
So we can think of content_main.xml just like partial in HTML. activity_main.xml will contain your activity global design, and content_main.xml will contain the contents.
From view of what they contain:
activity_main : Co-ordinator layout, ViewPager etc
content_main : Developer choosen elements. textview, edittext, button etc.
What the android developer website says about them is
activity_main:
This XML layout file is for the activity you added when you created
the project with Android Studio. Following the New Project workflow,
Android Studio presents this file with both a text view and a preview
of the screen UI. The file contains some default interface elements
from the material design library, including the app bar and a floating
action button. It also includes a separate layout file with the main
content
content_main:
This XML layout file resides in activity_my.xml (activity_main) ,
and contains some settings and Textview(other) element.
The difference between content_main.xml and activity_main.xml (for the class MainActivity) exists since the API 23. The difference between them is the following:
The content_main.xml is used for displaying the things that the
user should see. So it contains the elements which are for the user.
As you can see in the name, the content_main.xml determines the
contents you can find in your Activity (MainActivity in this
case). You use this xml-file to add new contents (Views) to your
Activity.
The activity_main.xml has some special tasks. It contains for
example: The floatingActionButton (the small round button you use in
gmail for example) The tabLayout The coordinatorLayout So the
activity_main.xml determines how the Activity (MainActivity in this
case) should look. It determines its design.The content_main.xml is
a part of the activity_main.xml.
There are similar questions, but none address my specific issue.
I'm rather new to Android development and Eclipse but was able to follow the tutorials A OK.
In fact, I'm able to edit activity_main.xml from a tutorial, but now, in a test project I'm working on, I can't edit my new activity_main.xml (again, different project, same filename).
I created the file by right-clicking on the layout folder then selecting NEW > OTHER > Android XML Layout File. I was able to copy some of the other file so that my file now looks essentially like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
When I type, the cursor moves around (looping through the text) but no characters appear on the screen. Again, in the other XML file I can edit with no problem.
I'm hoping that this is just some newbie problem.
I have had similar problems, though not this exact problem. Sometimes simply restarting Eclipse, or whatever editor you use, can help. Occasionally, Eclipse will not detect changes in your xml so you need to clean and rebuild the project. You can do this by
Winodow --> Project --> Clean...
and choose your project
I know there was an issue that I had which was similar with the editor jumping/replacing lines and such which I believe was fixed in a newer version of Eclipse. If I can find a source later I will edit
Clear all of text in layout activity_main.xml (in code view)
Drag and drop new LinearLayout in layout (in graphical view)
Go to Project -> Clean
I'm trying to set a relative layout's background with,
relativeLayout1.setBackgroundColor(0x00000000);
My program keeps crashing though. Here's the logcat.
Code:
RelativeLayout window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);
That's the only stuff apart from the regular code setContentView(R.layout.something); and super.onCreate(savedInstanceState);
Entire code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invisible);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);
}
From the logcat attached, I can say that most likely your window pointer is null at the time you are trying to set background color. It can be caused by different types of problems:
Your something.xml layout does NOT contain element with android:id="#+id/window" attribute
Your something.xml layout DOES contain element with android:id="#+id/window" attribute, but this element is not RelativeLayout
Your project resource data got messed up. Try to do Project->Clean to rebuild resources
Is there is your R.layout.something a layout with the id R.id.window ?
Maybe you've missed something?
You can use this instead
relative layout.setBackgroundDrawable(get resources().get drawable(R.drawable.bg);
Or you could define it in XML
android: background="#drawable/bg"
This was another of Eclipse's moments. It didn't compile my code, so I was executing the old code. Restarted Eclipse and my app works fine now.