I have been working on an Android Application project recently, and I've notice that some of my code IDs within a XML file will not register within the R file, and I have not been able to access the IDs within my classes. The code within the XML file will look like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_height="250dp"
android:layout_gravity="center"
android:src="#drawable/icon"
android:id="#+id/ivReturnedPic"
android:layout_width="250dp"></ImageView>
<ImageButton android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/ibTakePic"
android:layout_width="125dp"
android:layout_gravity="center"></ImageButton>
<Button android:text="Set Wallpaper"
android:id="#+id/bSetWall"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_gravity="center"></Button>
</LinearLayout>
None of theses IDs will register within the resource file.
There's problem in your xml that's why R is not being generated automatically
Remove one of extra </LinearLayout>
you must be sure that the image "icon.png" or "icon.jpg" is under your res/drawable folder.
android:src="#drawable/icon"
Related
My program is running correctly, but this error is being displayed in the compiler. How to fix it?
No answer regarding this can be found on Google, I checked quite many times.
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- username-->
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/username_TIL"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:elevation="2dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/username_TIET"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/username" />
</com.google.android.material.textfield.TextInputLayout>
<!-- password-->
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password_TIL"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="#id/username_TIL">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/password_TIET"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="password"/>
</com.google.android.material.textfield.TextInputLayout>
<!-- confirmPassword-->
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/confirmPassword_TIL"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="#id/password_TIL">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/confirmPassword_TIET"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/confirm_password"/>
</com.google.android.material.textfield.TextInputLayout>
<!-- SignIn-->
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/signIn_BTN"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/sign_in"
android:textColor="#android:color/white"
app:cornerRadius="50dp"
android:elevation="2dp"
android:layout_below="#id/confirmPassword_TIL"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_blue_light"/>
</RelativeLayout>
MainActivity has nothing in it - initialised
Manifest File also has nothing extra added to it. Any explanations towards why is this error occurring. Also one more error is being displayed.
E/InputTransport: ro.sf.lcd_density must be defined as a build property
I had the same problem but mine was because of screen density. You may need to revise your images or icons size if any in the drawable folder then generate a copy to following directories.
res/
drawable-xxxhdpi/
example.png
drawable-xxhdpi/
example.png
drawable-xhdpi/
example.png
drawable-hdpi/
example.png
drawable-mdpi/
example.png
For more info read here
To eliminate this error message, add the following line to the build.prop file located in the /system folder on the device:
ro.sf.lcd_density=160
This sets the display density to 160 dpi, which is the lowest among currently supported devices. You can adjust it to match the specific density of your device.
Note: You need to have root access to the device in order to edit the build.prop file. Since this can cause issues on some devices, it is to make a backup of the original file before making any changes.
I tried using AndroidTreeView
https://github.com/bmelnychuk/AndroidTreeView
But after following the directions and importing it, i now receive the following errors in resource layout_selectable_item.xml which i copied into my project from the demo that came with the package.
Error:(37) No resource identifier found for attribute 'iconText' in package 'android'
Error:(37) No resource identifier found for attribute 'iconColor' in package 'android'
Error:(37) No resource identifier found for attribute 'iconFont' in package 'android'
Error:(37) No resource identifier found for attribute 'iconSize' in package 'android'
If i comment out the lines with the error from the xml file, the treeView works, but there are no icons in it, and it looks crappy, so I think these lines are needed but of course the errors...
To import it all i did was add this line to the gradle then started using the tree view in my code, and it worked (but looks crappy)..
compile 'com.github.bmelnychuk:atv:1.2.+'
Here is the xml file, can anybody help????
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:minHeight="48dp"
android:paddingLeft="10dp"
android:background="?android:attr/selectableItemBackground"
android:layout_height="match_parent">
<CheckBox
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:id="#+id/node_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_centerHorizontal="true"
android:layout_marginLeft="28dp"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/node_selector"
android:orientation="vertical"
android:id="#+id/picture_wrapper"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_height="wrap_content">
<View
android:id="#+id/top_line"
android:background="#12558C"
android:layout_gravity="center_horizontal"
android:layout_width="1dp"
android:layout_height="16dp" />
<com.github.johnkil.print.PrintView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconText="#string/ic_check_circle_blank"
android:iconColor="#12558C"
android:iconFont="fonts/material-icon-font.ttf"
android:iconSize="16dp" />
<View
android:id="#+id/bot_line"
android:background="#12558C"
android:layout_gravity="center_horizontal"
android:layout_width="1dp"
android:layout_height="16dp" />
</LinearLayout>
<TextView
android:layout_centerVertical="true"
android:textSize="16sp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/picture_wrapper"
android:id="#+id/node_value"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
From github page: https://github.com/johnkil/Print
Custom views
Use PrintView as single icon in your layout.
<com.github.johnkil.print.PrintView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:print_iconText="#string/ic_android"
app:print_iconColor="#color/icon_color"
app:print_iconSize="#dimen/icon_size"/>
You need to use the app namespace, not android:
<com.github.johnkil.print.PrintView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:iconText="#string/ic_check_circle_blank"
app:iconColor="#12558C"
app:iconFont="fonts/material-icon-font.ttf"
app:iconSize="16dp" />
Add implementation 'com.github.johnkil.print:print:1.3.1' into your build.gradle file.
Also don't forget to add material-icon-font.ttf to main/assets/fonts
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
PrintConfig.initDefault(getAssets(), "fonts/material-icon-font.ttf");
...
}
I got it to work, just kept trying different things dont know what combination of things made it work unfortunatly.
I have 2 layouts for my application
but I have written all my Logic in MainActivity.JAVA
I want to use MainActivity.JAVA for my other layout (XML) file also
How can I do that?
This is my MainActivity.XML (Layout File)
Now I want Blue Button on Right to resize the new layout for that
New XML (with no Java file) only Layout file
Now I want to use the same Java file (MainActivity.JAVA)
For this new layout file
You have to use a Fragment or another solution is that include layout. You said you have one common activity and in this activity you need to include two XML layout then you can set like this:-
You can use the include element to have sub-XML files:
main.xml
<LinearLayout ... >
<include layout="#layout/file1" />
<include layout="#layout/file2" />
</LinearLayout>
file1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/toolbar_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:id="#+id/iv_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:visibility="gone" />
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center_horizontal"
android:padding="10dp"
android:singleLine="true"
android:text="MyApp"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
file2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#color/colorPrimaryDark"
tools:context="com.inducesmile.androidmusicplayer.SplashActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logomusic"
android:contentDescription="#string/app_name"/>
</LinearLayout>
</LinearLayout>
In your main activity layout, you could use a FrameLayout, which you can then add a fragment to. Then in the Java file, you can use getSupportFragmentManager().beginTransaction().replace() method to inflate the layout XMLs as and when you like.
If you have two XML files, you can merge both of them by including one into another.
For example: if you have two files named a.xml and b.xml, then you can include the b.xml in a.xml using include tag like
<include layout="#layout/b.xml" />
Then call setContentView (R.layout.a) in your MainActivity.java
Or, you can make use of Fragments.
I'm trying to use the AndroidSlidingUpPanel library (in Eclipse) and I have this error:
04-19 20:09:48.413: E/AndroidRuntime(13760): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mendozatourapp/com.mendozatourapp.activitys.MapsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.sothree.slidinguppanel.library.SlidingUpPanelLayout
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<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=".DemoActivity" >
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:sothree="http://schemas.android.com/apk/lib/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:panelHeight="68dp"
sothree:shadowHeight="4dp"
sothree:paralaxOffset="100dp"
sothree:dragView="#+id/name">
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>
I downloaded the entire project and imported "main" as library. Then I add this library to my project's java build path. I have the feeling that is a java build path problem. Any idea?
Thank you so much for reading me and take your time to do it.
I just got it working in Eclipse.
The important part is to get your library project created correctly and then link it up to your main project.
For the library project, I created a project.properties file, and also added the jar files for the RecyclerView library, the support v4 library, and nineoldandroids.
I also created a .classpath file in order to make it automatically put the java folder in the build path.
I uploaded the library project that I got working to github: https://github.com/dmnugent80/SlidingUpPanel-for-Eclipse/tree/master
Then, import the library project into your workspace:
Set it up as a dependency of the main project:
So, you end up with something like this:
Then, using this test xml (your xml threw an error about needing a minimum of two children):
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoPanelHeight="68dp"
sothree:umanoShadowHeight="4dp"
sothree:umanoParallaxOffset="100dp"
sothree:umanoDragView="#+id/dragView"
sothree:umanoOverlay="true"
sothree:umanoScrollableView="#+id/list">
<!-- MAIN CONTENT -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Main Content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="true"
android:textSize="16sp" />
</FrameLayout>
<!-- SLIDING LAYOUT -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:clickable="true"
android:focusable="false"
android:id="#+id/dragView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="14sp"
android:gravity="center_vertical"
android:paddingLeft="10dp"/>
<Button
android:id="#+id/follow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="14sp"
android:gravity="center_vertical|right"
android:paddingRight="10dp"
android:paddingLeft="10dp"/>
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
It works!
My layouts and views' ids, eclipse can not recognize them and underscore them with red line.
I built all the projects and cleaned them as well, but the problem still exists. Please provide any suggestions.
UPDATE:
The updated problem is, the R class is highlighted with red
JavaCode:
setContentView(R.layout.mplmenu_activity);
mMainMenuListView = (ListView) findViewById(R.id.ListView_Menu);
String [] items = {
getResources().getString(R.string.menuItem_AddNewLocation),
getResources().getString(R.string.menuItem_MySavedLocation),
getResources().getString(R.string.menuItem_GPSReadings),
getResources().getString(R.string.menuItem_OpenCameraSurface)
};
ArrayAdapter<String> adapt =new ArrayAdapter<String>(this,
R.layout.mainMenuItems, items);
mplmenu_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/mplMenuActivityBackGroundColor" >
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal">
<TextView
android:id="#+id/TextView01"
android:layout_height="wrap_content"
android:text="#string/menu"
android:textSize="#dimen/mainMenuTextSize"
android:layout_marginLeft="18dp"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_width="wrap_content"
android:shadowColor="#android:color/white"
android:textColor="#color/mainMenuTextColor">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center">
<ListView
android:layout_height="wrap_content"
android:id="#+id/ListView_Menu"
android:layout_width="wrap_content"
android:divider="#drawable/dividerline"
android:layout_alignParentTop="true">
</ListView>
</RelativeLayout>
</LinearLayout>
Check that the generated R.java file actually exists. You may have errors in some of your XML resource files that prevent the Android build tool aapt from generating the R.java file. In my experience, that is typically why resource IDs are not found.
To fix this, you will have to fix the XML errors.
If you have something like import your_package_name.R in your Activity class, remove it, and clean the project, then build it again.
Make sure that you are identifying them in your xml with the +id and not just id.
Like this:
android:id="+id/btn1"
not this:
android:id="id/btn1"