Android DataBinding new button setOnClickListener - android

I need to use the Android Camera.
Therefore I am using following Template: https://github.com/android/camera-samples/tree/main/Camera2SlowMotion
I added a Butten to the fragment_camera.xml
https://github.com/android/camera-samples/blob/main/Camera2SlowMotion/app/src/main/res/layout/fragment_camera.xml
<Button
android:id="#+id/change_focus_button"
android:layout_width="115dp"
android:layout_height="81dp"
android:text="#string/focusButtonText"
tools:layout_editor_absoluteX="13dp"
tools:layout_editor_absoluteY="473dp" />
In the CameraFragment.kt I now want to call a function when the button is clicked.
In my understanding I therefore need to create a setOnClickListener.
https://github.com/android/camera-samples/blob/main/Camera2SlowMotion/app/src/main/java/com/example/android/camera2/slowmo/fragments/CameraFragment.kt
I thought about adding following line in Line 309. that would call a function i then create. But the "." after fragmentCameraBinding.changeFocusButton is always red and won't go away.
fragmentCameraBinding.changeFocusButton.setOnClickListener { chnageFocuslen() }
What do I need to change / where do I need to add the OnClickListener?
Thank you very much in advance!

I had to download the sample because I couldn't see what the issue was from the snippets in your post.
The issue is that CameraFragment has two layout files, one in the default folder layout, and one in layout-land for landscape orientation. So you need to add your change_focus_button XML to both layout files to make it not nullable when you access it via fragmentCameraBinding in CameraFragment.

Related

How to fix the IDs renaming problem while using View Binding in android studio?

The problem is that when I rename the id of any view like a button in the xml file, the name of the view doesn't get updated in the activity while using view binding.
When I rename the id, I use (refactor) or (shift+f6).
This is a big problem because when ever I rename an id in the XML file, it will break the code in the activity in case I'm using View Binding.
Example:-
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
when I change Button1 to Button2, the code breaks in the activity in case I'm using View binding.
Can anyone give me any way to properly rename the IDs so that the names in the activity get updated automatically while using View Binding.
Please, remember I do use (refactor) not f2.
If you agree with me that this is a very urging problem and it breaks our code, please vote this question up, so it will be suggested to more android developers. Maybe some already found a solution.

Android - Exoplayer : How to set different controller layout for portrait mode and fullscreen mode?

I am trying to achieve this design with Exo-Player2. Progressbar and some other views position is different in portrait mode and fullscreen mode.
As per my research we can pass only one layout design through player view by this line of code: app:controller_layout_id="#layout/bplayer_controller_view"
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/playerView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_gravity="top"
android:background="#color/black"
android:foregroundGravity="center"
app:controller_layout_id="#layout/bplayer_controller_view"
app:resize_mode="fill" />
I can't find any way to change this layout in runtime. Did I miss anything? Can you please help me on this topic. Thanks in advance.
You cannot change the app:controller_layout_id programmatically. You can also see the same mentioned here. There is no such method at this moment which allows you to directly set a custom controller on playerView and for now you have to add your controller to playerview in xml from where it gets inflated.
Now coming to your problem of having a different custom controller for Landscape and Potrait orientation. There is no direct way of it. Here are some work around options:
Have two different ExoPlayer in your XML for orientation and potrait and hide/show them accordingly as per orientation change.
The other option will be to have a single FrameLayout in which you can then inflate two different types of ExoPlayer XML Layouts according to the orientation change.
Please follow the steps below:
Res folder -> Right Click -> New -> Android Resource Directory.
Choose layout as Resource Type at the popup that will be displayed(second dropdown).
Then, at the left side choose orientation press >> and OK. A layout_port folder will be created.
Finally, create a layout with the same name as in the main folder(bplayer_controller_view).
Android chooses the correct file depending on requested orientation at runtime!

Creating id for Android Views

This question is quite basic. Let me go with an example.
I have two activities
activity_a.xml
ActivityA.java
activity_b.xml
ActivityB.java
Both the XML files contain only a TextView to display a simple text. As usual, the TextViews are going to be referenced in the corresponding .java files using their View id
My question is, if it is right to reference the TextView in both the XML files with same id? (like using the below code with exactly same id for activity_a.xml and activity_b.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I was practising this procedure without any problems. When trying to reach the corresponding xml code for the TextView using Ctrl + Click (on Windows), I am provided with two options (to display the TextView's xml code from activity_a.xml or from activity_b.xml).
Also, what is the recommended way to name a View in Android? This will be helpful, when your Android project contains multiple layout files.
Yes , if you have same name of view or view group in different layout then it tells from which layout file it belongs to, ask for select required layout file.
so for that you have to follow proper naming conventions to avoid this type of confusion
https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md
or you can give name like
activity_home_tvUserName if username textview from home activity
and
activity_profile_tvUserName if username textview from profile activity.
if it is right to reference the TextView in both the XML files with same id?
It is totally fine, the compiler will only look at the ID under a single view hierarchy.
e.g.: findViewById(R.id.textview) inside ActivityA.java will only search for textview ID inside activity_a.xml (assuming you have setContentView(R.layout.activity_a); beforehand.
what is the recommended way to name a View in Android?
In my opinion, you just need to be consistent in naming your view throughout the app. The main goal is to avoid misinterpretation and confusion.
Hope it helps!
They are in different activities, so you should have no problem using the same id. you could event declare them both as #+id/textview. However, why not just use the same XML file for both activities? No reason you can't.
You can also create an ids.xml file under the values folder and declare all your ids under it, so you don't have to declare them in your layouts, but this is not a very common approach.
if it is right to reference the TextView in both the XML files with same id?
My answer is Yes, It is right.
Whenever we set the setContentView(R.layout.activity_a), then it'll search for the given id within the above activity. Local attribute having the same id will take the more preference over the other attributes with the same id.
But having unique id's is Best practice.

Questions related to android:onClick="selfDestruct" xml method

New to android and programming in general. This question might sound silly but I'd appreciate the answer. The question description and reasoning is at the beginning and the question is at the end of it all.
I want to apply a listener to a button in android. The way I understood from android.googlesource.com is that there is two way to do it:
applying an OnClickListener to the button in the activity.java
or
assign a method to my button in the xml layout using this
{#link android.R.attr#onClick android:onClick}
they gave the following xml layout example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="#string/self_destruct" />
plus the the code in activity.java
public void selfDestruct(View view) {
// Kabloey
}
android.googlesource.com
Questions:
According to this: {#link android.R.attr#onClick android:onClick} android.R.attr in the example are the following:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
?
Does this:
android:text="#string/self_destruct"
android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
If I want to add more than one button listener in xml form how do I write it in the java document?
Thank you in advance
Really appreciate it.
Does this:
android:text="#string/self_destruct" android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
The button isn't "called" anything, it just has the text of the value for #string/self_destruct defined in the strings.xml file.
But, yes, the public void selfDestruct(View view) method is the method that will be called for the listener that is setup by the XML.
If I want to add more than one button listener in xml form how do I write it in the java document?
You can only set one click listener for a View.
Yes, layout_height, layout_width and text are attributes. You
can learn more about android attributes
here.
But keep in mind, that different views can use the same attributes
in different way.
It does not. The android:text attribute in this sample just refer to a string-resource called self_destruct to decide which text should be shown within the button. To distinguish views you can use android:id.
As already mentioned by other people, you are able to specify only one onClickListener using XML.

Android activity loads wrong layout

I copied a .java class and .xml layout. I thought I renamed everything in order to create a new class, but the new class is loading the old layout when I start a new Intent. Am I missing someplace where I need to enter the name of the new layout file I have created from a copy of another class' layout?
I think I missed this line:
setContentView(R.layout.xxx)
This should do it!
If you have not set the launcher activity in manifest then you have to specify which Activity starts first
If you have set old XMl in setContentView(R.layout.xxx) then this loads old layout so check for proper xml.
After trying the other answers, try checking the following attribute on your layout element:
tools:context=".SignUpActivity">
That was it for me.

Categories

Resources