How to use a ShapeDrawable with an xml layout resource - android-layout

I would like to be able to define a ShapeDrawable in my layout file and then inflate that so I can draw on it.
I have followed the tutorial on the android developers' site but I cannot see how I reference my ShapeDrawable in my main class file.
How do I actually inflate it?
I have put the following the following into my layout file:
<com.example.shapedrawable.CustomDrawableView
android:id="#+id/customDrawableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

Once you have defined the ShapeDrawable in its own XML file, you need to add the following to the layout.xml where you want it to be included:
<ImageView
android:src="#drawable/shapeDrawable_filename"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
If you want to do it programmatically, I don't have an answer (yet), but from your question, it seems this should work!
Let me know if that helps ;)

Related

EditText with hint, where hint moves

I have seen EditText that has a hint and when user taps on it the hint moves up reducing the font size and making the field editable by the user. When the user moves to another EditText leaving it blank the hint appears back in full size.
What is it and how can I add it in my activity on Android Studio using XML?
I am on Android Studio using Kotlin.
if you mean this
it is TextInputLayout
Add the dependency for the design support library inside the build.gradle (Module: app) file as shown below.
implementation 'com.google.android.material:material:<version>'
the latest version at this time is 1.2.0-alpha03 you can see latest version from mvnrepository
Then, you can use it like this in your xml layouts.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/myTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/my_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/YOUR_HINT"
android:singleLine="true"
android:textColor="#color/colorDarkestGray" />
</com.google.android.material.textfield.TextInputLayout>
This is TextInputLayout and you can use editText as child in it like this :
At first add the dependency for the design support library inside the build.gradle file as shown below.
implementation 'com.android.support:design:25.3.1'
And if you are using AndroidX use this :
implementation 'com.google.android.material:material:1.0.0'
Second ,Then implement that like this :
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/activity_horizontal_margin">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Floating Hint Enabled Default" />
</android.support.design.widget.TextInputLayout>
Android TextInputLayout Features
1-Enabling/Disabling floating hints
2-Enabling/Disabling floating hint animation
3-Displaying Error Messages
4-Showing Character Counter
5-Password Visibility Toggle and . . .

i cant add button to linear layout

I have a listActivity that displays via an adapter an xml feed fetched from the web, adn the layout file activity_list_feed.xml :
`<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
/>`
In the graphic editor i cannot drag a button into this layout, and when i try to hardcode as per this file :
`<?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" >
<Button
android:id="#+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
/>
</LinearLayout>`
i get a compile error message :
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
How can i add a button on top of the list because i want to refresh the pull.thank you.
The code that you provided actually works in its current state in Android Studio:
Make sure that in your java code, you are referencing the correct item. Make sure that you're setting the overall layout to be that layout, and THEN doing findViewById(R.layout.addBtn).
Also, try changing the Android Version in your IDE to 22 (as I have it set in the top right corner of the picture). That may solve your error.
I managed to add the button in the editor and this without setting to API 22 as per your picture. However the program didn't compile still.
The message error "ArrayAdapter requires the resource ID to be a TextView" meant i didn't provide the right argument to the adapter.
According to this answer :
"ArrayAdapter requires the resource ID to be a TextView" xml problems
it appears that the choice of the constructor is important, since i wanted to add a button in the view, i must use the constructor with 4 arguments (the additional argument being the id of the view):
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,int,java.util.List)
Using this constructor solved the problem.

android, xmlns, No resource indentifier error

In an xml layout file, the xmlns is already defined, but I get this error at compile time: No resource identifier found for attribute. Why is that? thanks
I'm following a Facebook dev tutorial on facebook site https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/ the layout file is as follows:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.facebook.widget.ProfilePictureView
android:id="#+id/profilepic"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
app:preset_size="large" app:is_cropped="true"
/>
</ScrollView>
I figured it out, have to replace /res-auto with res/com.facebook.samples.profilepicture
Your layout file looks correct. check that in your 'project.properties' file, the 'android.library.reference.1' value points to the correct location.
The res-auto is feature properly supported in API level 17. It must be applied for all layouts using custom attributes. It is automatically replaced on build time, by eclipse.
Ref Updated SDK Tools and ADT revision 17

`VideoView.start()` cause other views to break

When my activity receives a new intent via onNewIntent, it updates the data of three Views, an ImageView, a TextView, and a VideoView. The problem is, the two other views just flash, then disappear when my VideoView comes on. After scattering a few breakpoints, I discovered that they appear when their content is set, but disappear when VideoView.onStart() is called in my MediaPlayer.onPrepared() method. I also have an AlertDialog show up when the menu button is pressed. After pressing the menu button, it shows up. I'm on Android API 9, as this is the API on the device I'm working on. I really need help, so I'd appreciate any advice.
Here's the layout. I don't think it's the issue though:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
<TextView
android:id="#+id/marquee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#android:color/white" >
</TextView>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#id/marquee"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical" />
<VideoView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/marquee"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="#+id/image" />
</RelativeLayout>
Take note that the TextView is meant to be a marquee; it's supposed to keep scrolling sideways, until it is disposed of. I discovered that when the text is too short for it to start the marquee, the TextView AND the ImageView disappear (as stated earlier, they appear for a split second, then disappear). However, when the text causes the marquee feature to activate, everything works.
CURRENTLY:
I got it to work by calling postInvalidateDelayed(500) on my ImageView and TextView after calling VideoView.start(). I think the start() method is causing the problem, and requires that other views call invalidate(). Also, for some reason, there needs to be a small delay in-between the call to start() and the call to invalidate().
The layout is the issue.
Your VideoView is declared as match_parent, match_parent, allowing it to consume the whole width and height of the screen. Since you declared it last on the xml file (You used RelativeLayout. Ordering matters), the TextView and the ImageView would be covered by the VideoView.
If you're confused,
match_parent is basically the same as fill_parent. It's just another name for fill_parent in android 2.3+
Now what can you do about it?
Reorder your views in such a way that the largest is declared first. In this case, VideoView, then ImageView, then TextView.
Also note that your ImageView has height set to fill_parent - you may not want that.
I created a method, but it seems very bad:
private void startVideo() {
this.videoView.start();
if (this.imageView != null)
this.imageView.postInvalidateDelay(500);
if (this.textView != null)
this.textView.postInvalidateDelay(500);
}
It works, but it makes me feel dirty.

Where is a reference to the android XML UI layout?

I am looking for a spec or reference of all the possible options for the various XML layout attribute settings that typically come with an android UI. Google seem to be good at burying it. This is similar to this question but remains in-effectively answered.
Such as what are my options available to me for the TextView layout_width definition ? There must be a complete definition published ... somehwere....
layout_* attributes aren't directly part of the view they appear on, which is why you won't find them in TextView's documentation. (TextView is not a ViewGroup.) They are arguments to the parent view, also known as LayoutParams. Take a look at the "Known Subclasses" sections at the top of the linked page for a list of them. They're instructions about how a ViewGroup should arrange each child view, and each parent type can recognize different ones depending on what kinds of layout options it supports.
For example, LinearLayout.LayoutParams supports the android:layout_weight parameter. Children of a LinearLayout can specify weight to request a proportion of the remaining space after all children have been measured. You can give equal weight to two sibling TextViews with a base width of 0 to give them each half of the available space within the parent.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="World" />
</LinearLayout>
Normally developer.android.com is your site. Maybe this helps:
http://developer.android.com/reference/android/view/View.html
If you use Eclipse, then the autocomplete suggestions may help you as well in adding the right parameter.
...and the options you have for layout_width are
wrap_content (as large as the content of the View)
fill_parent (extends to the whole size - width or height - of its parent)
Layout parameters are pretty well described in the documentation for ViewGroup.LayoutParams and its subclasses. For the truly strong of heart, you can always browse the source for attr.xml.

Resources