how to extend ImageView class in kotlin? - android-studio

I found strange behavior when extending ImageView in kotlin.
I have following layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<AlphaImage
android:id="#+id/imageView2"
android:layout_width="320dp"
android:layout_height="50dp"
app:srcCompat="#drawable/blitz_1st_place" />
</FrameLayout>
AlphaImage class:
class AlphaImage(context: Context,attrs:AttributeSet?=null):ImageView(context,attrs) {
}
The layout preview in android studio shows image as 100% transparent as well as in runtime.There are no errors in the layout editor.What interesting is if I change AlphaImage to ImageView image starts to display its content as expected.Why is this happening?How do I extend ImageView?

The issue here is the app:srcCompat attribute. That is a support/androidx library attribute for the AppCompatImageView class. The framework ImageView will just ignore it.
The reason this works when you use an <ImageView> tag instead is because the support/androidx LayoutInflater that AppCompatActivity uses will automatically substitute an AppCompatImageView for any <ImageView> tag it finds. The AppCompatImageView will then see that app:srcCompat attribute, and handle its value appropriately.
If you'd like your custom class to handle that attribute automatically, simply extend AppCompatImageView instead of ImageView.

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 . . .

Drawer Layout in fragment

I tried to implement a Drawer Layout in a fragment, however, if I use "android: layout_gravity="start" then my navigational just disappears.The same code works fine if I put it in an activity.
And if I try to swipe to the right, it also does not work (you can see it in the image I posted). For me, it looks like I can not swipe to the right and therefor the navigation view can not be opened?
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/bottom_navigation">
/>
</android.support.v4.widget.DrawerLayout>
Image (if I swipe to the right
By looking at this gist, you should add android:fitsSystemWindows="true" to the DrawerLayout and set its width to match_parent. My guess is, it should be the child NavigationView which can have a defined width.

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.

`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.

How to use a ShapeDrawable with an xml layout resource

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 ;)

Resources