I have just created a new resource folder in
\src\main\res\layout\layout-large
that contains the activity_main-large.xml with the following code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LARGE" />
But when I run the app on a large screen device it still loads the defalut activity and shows "Hello World!" that I have put in
\src\main\res\layout\activity_main.xml
How should I fix that? I already read about this on the internet but I wrote the code in the correct way so what is the problem?
To have different ressources
they need to have the same name, and
they need to be in the correct folder
Your other configurations are on the same level, so rename your large layout and move it to
\src\main\res\layout-large\activity_main.xml
This will make it available for large devices.
Related
Background
Sometimes, you wish to put some placeholders to be shown only on the IDE, in layout files.
As an example, you can use this:
<ImageView tools:src="#tools:sample/avatars" ... />
And get this in the preview:
Such files are not part of the APK you get when you build your app, so it's safe to use just for developing.
This is what I was told from here:
With sample data in 3.0, you can now have placeholder images that are
not part of the compiled apk. You just need a sampledata directory in
your project with a subdirectory containing all the images you want to
use as placeholders. You can refer those images from "tools"
attributes. Also, there are predefined stock images like
#sample/avatars or #sample/background/scenic
The problem
I can't find how to add more of such images into the project (so that they will only be used in the IDE, and not a part of the APK), and if there is a way to put other resources, other than images.
In fact I can't find the docs of this feature.
What I tried
I tried to put an image on "res/sampledata" and tried on "res/sample", but I wasn't able to reach it in both cases.
The questions
What's even the name of this feature?
How can I put the image file into the project, and use it as a placeholder this way ? In which folder?
Is it possible to add more images to be used this way?
Is it possible to add other resources? Layouts? Strings?
Are there more capabilities of this new feature?
What's even the name of this feature?
Unconfirmed by an official source, but it's likely called "Sample Data".
How can I put the image file into the project, and use it as a placeholder this way ? In which folder?
Unlike resources like images, fonts, etc. The sample data does not go in /res/ (they are not compiled with the app, hence. It is probably easier to filter them out by putting them in a totally separate directory). They go in /app/sampledata/, for example: /app/sampledata/image.png.
You can create the sampledata folder by right clicking on app and do New > Sample Data directory:
You can then reference them like this, with #sample/
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
tools:src="#sample/test.png" />
While this does not give any errors, sadly the feature seems bugged right now, since the images do not show up in the preview, regardless of them being placed in a subdirectory or not (tried png, jpeg, jpg, xml).
Interestingly, placing a single image in a subdirectory and referring to that subdirectory instead of the specific image seems to work:
A structure of this
Combined with these references
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:background="#sample/image">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
tools:src="#sample/avatar" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
tools:src="#sample/jpeg" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
tools:src="#sample/vector" />
</LinearLayout>
Produces this preview. Notice how I utilized tools:background to set the layout background to a sample image.
Is it possible to add more images to be used this way?
Yeah, just add them to the folder.
Is it possible to add other resources? Layouts? Strings?
It does not appear to support either. If you try to define some other type of resource, you either get a bunch of syntax errors due to keywords not being recognized, or you cannot refer to them with the tools:src="#sample/ notation.
Are there more capabilities of this new feature?
Unsure at this time.
On my side I tried to use a image file as a placeholder for an Imageview using tools:src="...": I did put my PNG file in a sampledatasubdirectory but it either wouldn't show or appear as a broken image.
After a bit of fiddlings I found something that fixed my issue:
create a sampledata subdirectory named: drawable
place your png/jpg file in this directory and append the file name with "_drawable". Ex: image.png -> image_drawable.png
Only then did the placeholder showed up.
You can create subdirectories from drawable as you see fit to keep your sample images organized, but the important thing seems to be that drawable remains a parent directory.
I do not have any documentation or sources to back me up, but I figure it could be useful for other in the same situation.
I guess you have got the answer to other questions precisely by people who answered before me, but I can give you a workaround solution for particularly this question
Is it possible to add more images to be used this way?
I have faced this problem for quite a bit of time and found a workaround for it.
My issue was that I couldn't use a placeHolderImage in a SimpleDraweeView which is a part of Fresco library of Facebook, if you didn't knew. But since an ImageView is not much complex like a SimpleDraweeView, and as the workaround worked with SimpleDraweeView, even though I haven't tried it with an ImageView, it might surely work with it too.
Here is the solution.
You just have to create a json file inside the sampledata folder, but this time, you have to do it this way.
{
"data": [
{
"image": "#drawable/ic_jam_jar"
},
{
"image": "#drawable/ic_rice_bag"
},
{
"image": "#drawable/store_avatar"
},
{
"image": "#drawable/exclamation_icon"
}
]
}
As you can see, I have used the reference for drawable resource in place of value for image key. And it works in my case. What you get here in a way is an array of image resources which you can use inside the item of a RecyclerView to be previewed in Design View in Android Studio.
My RecyclerView code is as follows
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/product_desc_rec_view"
android:layout_width="match_parent"
android:layout_height="350dp"
tools:itemCount="6"
app:spanCount="6"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="#layout/product_desc_item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Remember not to skip any of the extra tags which I used, as it might not produce the required output in preview if you miss any. You can customize the itemCount attribute value according to your requirement of number of images you are using.
Also, orientation might be vertical or horizontal according to your use-case.
You can simply use tools:src="#sample/your_json_file.json/data/image inside your recyclerview item xml file's ImageView to load sample images there.
Try it and let me know if this works in your case or not.
tools:src="#tools:sample/avatars[5]"
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
tools:src="#tools:sample/avatars[5]"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
Tested with Android Studio Dolphin | 2021.3.1 Patch 1
(be patient with it, often it does not do immediately what you typed in...)
Following covers
Drawables (both bitmaps and vectors)
Strings
Directory/files structure:
<project workspace dir>
├───app (=your module name, defaults to app)
└───sampleData
├───drawable
│ ├───some_name_drawable.png
│ ├───some_othername_drawable.png
│ └───some_3rdname_drawable.xml // SVG vector
├───some_name.json // strings
├───some_other_name.json // other strings
Use of sample images
<ImageView
...
tools:src="#sample/drawable" />
Important
folder with images must be named "drawable"
file name of image/vector must end with "..._drawable.png" (.jpeg not tested) or "..._drawable.xml"
Use of sample strings
<TextView
...
tools:text="#sample/some_name.json/data/something" />
<TextView
...
tools:text="#sample/some_other_name.json/data/something_else"
<TextView
...
tools:text="#sample/some_other_name.json/data/something_different"
where some_name.json contains:
{
"data": [
{ "something":"Nice sample text" },
{ "something":"Nicer other text" },
{ "something":"Awesome text" }
]
}
and some_other_name.json:
{
"data": [
{ "something_else":"your text here" },
{ "something_else":"your text here" },
{ "something_else":"your text here" },
{ "something_different": "also here your text" },
{ "something_different": "also here your text" },
{ "something_different": "also here your text" }
]
}
Important
Android Studio requires the "data" as only root element in the .json file
Interesting and really nice: One .json file can have >1 sorts of strings
I've just finished to do the layout I want, and even although I saved everything, when I try to find by id the button, the button I'm looking for is missing.. Both the button's I have.. I've checked in the R class, under the ID, and it seems it doesn't sees' the buttons. Doesn't generate them? I dunno.. Any way, it happens every time I use relative layout!! Is there any connection? It works with TableLayout and with linear layout.. Only with relative layout it make's problems.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/Background" >
<TextView
android:id="#+id/corpTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/corpTag"
android:textColor="#color/White" />
<Button
android:id="#+id/addBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="208dp"
android:text="#string/addYourBusiness" />
<Button
android:id="#+id/searchBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignBottom="#+id/addBusinessButton"
android:layout_alignLeft="#+id/addBusinessButton"
android:layout_marginBottom="36dp"
android:text="#string/searchBusiness" />
I found this happening to me with a broken layout. No need to be worry. I am trying my best to giving you the solution
Solution
Make sure that anything the R. links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, R will not regenerate.
If you somehow hit something and created import android.R in your activity, remove it.
Run Project -> Clean. This will delete and regenerate R and BuildConfig.
Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
Wait a few seconds for the errors to disappear.
This problem isn't related to relative layout, but is actually set on the background because Android drwable is case-sensitive and now allows you any capital letters for image or layout. Rename your image name or drwable name to "background" instead of Background."
Here is the problem with set background name:
android:background="#drawable/Background"
It might be the Android Style ButtonBar. It isn't available for every API level. So if you have a low minSDK, it's not going to build and Eclipse/IntelliJ should give you an error message in its error pane.
The rule given by Google, is that you should always define your own resources. If you want to use a resource that is in the Android name space, that's fine, but you should copy it first and package it with your apk, because that's the only way you can be assured that it will always be bundled with your application.
And yes, I realize many people and many tutorials on the web don't do it that way, but that's because most people are lazy.
i have any troubles with switching between orientation from portrait to landscape.
i made this page cause i don't have permission to add images
http://www.guitart.comyr.com/
There are my codes and two images.
I've tried to make some small application to test switching, i made same folders as you can see at the page and this small test app works fine. But my original app doesnt work.
Sorry for English.
Do you have
<Button
android:id="#+id/btn_menu_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/info64"
android:src="#drawable/info64" />
in both menu.xml layouts(layout|layout-land) folders? If you change the orientation to landscape, your app automatically uses the xmls from your layout-land folder. Eclipse don't recognize if the ID exists in both orientations. Eclipse can't stop you from building because it's no error but it could at least give you a warning imo.
No problem :)
Have you tried to just copy the layout file from layout folder and paste it in "layout-land". check the id's of the views because logcat is showing nullPointerException in onCreate.
<Button
android:id="#+id/btn_menu_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/info64"
android:src="#drawable/info64" />
remove
android:src="#drawable/info64"
line from this button's property.thats the problem.and you are done.
I Imported one image inside the drawable-mdpi, then implemented the image from button, but an error occurs no resource found here. How do I fix this issue?
I tried this:
main.xml
<Button
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable-mdpi/button_focused_orange"/>
All drawables are compiled under a single resource name, i.e. drawable. Android automatically chooses from which folder to take the drawable depending on the screen size, and hence you do not need to specifically point it out. Also, hard coding Android to use resources from a particular folder kind of defeats the purpose of having multiple folders for Android to choose from. To solve this issue, simply change:
android:background="#drawable-mdbi/button_focused_orange"/>
To
android:background="#drawable/button_focused_orange"/>
Should be #drawable/button_focused_orange
Not #drawable-mdpi/button_focused_orange
Try to clean and rebuild your project.
If you are usuing Eclipse you can do this by clicking project -> clean and then project -> Build project
You don't have to mention -mdpi to add background images, simply use drawable only. Here is you revised code. Try this.
<Button
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_focused_orange"/>
I have an image in portrait mode, which is displayed correctly. When I change the configuration to landscape, the image is not displayed.
Note that the LinearLayout, enclosing the ImageView is displayed right(I checked it by changing the background). But when I changed the background for the ImageView, the background was also not shown.
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:layout_weight="3">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:minHeight="48dp" android:minWidth="48dp" android:src="#drawable/xyz" android:id="#+id/img"/>
Do you have different files specified for portrait and landscape more? I.e., do you have a layout file with the same name in the layout folder as well as the layout-land folder? If so, make sure both files include the ImageView.
You are most likely specifying the file you want to display like
myImageView.setImage("myfile.jpg");
in your onCreate method.
Move this code to onResume (create onResume with the #Override if you do not already have it) This will fix your problem
OK, your layout file is unnecessarily large and complex. So let's try a couple of things-
First, try creating a simple version of your XML file that contains the barebones for displaying the image. If that works in both portrait and landscape, you know that the problem is not your activity or your image.
If the first step works, the next step would be to work on simplifying your layout file, there is a lot that could be pruned. It goes seven layers deep at points, that should almost never happen. Here is a good blog post by an Android engineer on better layout design. You can find similar resources elsewhere.