creating android library from fragment [duplicate] - java

This question already has answers here:
how to get a fragment added in an XML layout
(5 answers)
Closed last year.
i'm having a fragment that i want to use it as a library in other projects.
i created a library module and copy-paste all my fragment code and resources to library mode and then to use that library module i'm writing the following code in activity's xml
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="com.dhirunand.meter.MeterFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
and in java file
MeterFragment meterFragment = new MeterFragment();
String selectedNumber = meterFragment.getSelectedNumber();
visually library mudule is working fine but i'm not able to fetch data from the library module
project github url https://github.com/dhirunand/meter--number-picker

when you are using new MeterFragment() line then you are creating NEW fragment, not using one attached to FragmentContainerView. this NEW one isn't visible on screen, its just created in memory, not shown (as you haven't posted attaching code), so getSelectedNumber() will return default value instead this set in MeterFragment attached to FragmentContainerView
you should obtain this "real" existing and visible instance from XML by
FragmentContainerView fcv = (FragmentContainerView) findViewById(R.id.fragmentContainerView);
MeterFragment meterFragment = (MeterFragment) fcv.getFragment();

Related

How do I set videopath for UDP stream using vitamio?

I'm trying to get a UDP stream from a GoPro camera.
https://github.com/KonradIT/GoProStream
I'm redoing this python code in android studio using vitamio.
But for some reason, as soon as I set the path, it crashes
_myVideoView.setVideoPath("udp://#10.5.5.100:8554");
But if I remove it, the app can launch but of course there is no video.
If someone encountered the same issue and found a solution, that would help me.
_myVideoView.setVideoPath("udp://#10.5.5.100:8554");
_myVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
_myVideoView.setBufferSize(2048);
_myVideoView.requestFocus();
_myVideoView.setMediaController(new MediaController(this));
_myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
_myVideoView.start();
I have done that for onCreate :
if(!io.vov.vitamio.LibsChecker.checkVitamioLibs(this)){
return;
}
Vitamio.initialize(this);
the videoview :
<io.vov.vitamio.widget.VideoView
android:id="#+id/VideoView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="50dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageButtonLeft"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/WebView"
tools:visibility="visible" />
and the build graddle :
implementation project(path: ':vitamio')
ps : I've tried to put the compile line in graddle, but it can't find the method compile and the implement line seem to work just fine.
ps2 : I just tried but even http link from YouTube doesn't work, I've definitely made a mistake but dunno where
Found the solution...
I was just importing the project but I didn't build it beforehand
So there was no way it could work.
But I gave up later for other reasons and I would recommend lib vlc instead of vitamio for displaying video from udp streams.

Get admob Id remotely

I have an application I made using android studio. As of the moment, the admob ids are defined in the strings.xml file. However, I want to make the app get the admob ids from either my server or from firebase as I want to be able to change them remotely should any problems occur. How would I go about doing this?
Either you can load Ads From Firebase Using Remote Config
or you Can Use the Firebase database. Refer this Sample Project from my Github
Load Ads From Firebse
You can do it programmatically.
Layout
From your layout file, add AdView:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
Retrieving Ad Unit Id
You need to do network calls either on your splash screen or main activity to retrieve your Ad Unit Id/s
You can use Retrofit for doing such http request calls from your web server
If you choose Firebase, you can use their
Firebase Realtime Database
Setting Ad Unit Id
In Java
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Set ad unit id
In Kotlin
val adView = AdView(this)
adView.adSize = AdSize.BANNER
adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // Set ad unit id
Additional
Depending on your persistent storage implementation, here's a one liner way of saving the Ad Unit Id using SharedPreferences
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MY_AD_UNIT_ID", "YOUR_AD_UNIT_VALUE").apply();
Here's how you will retrieve it
PreferenceManager.getDefaultSharedPreferences(context).getString("MY_AD_UNIT_ID", "DEFAULT_STRING_IF_NOTHING_WAS_FOUND");
Where context is your Context.
Read more:
https://developers.google.com/admob/android/banner
There is a new API that allows you to manage Firebase projects and get configuration data from them. This would be the most logical place to look up such configuration data programmatically.
I checked the projects.get and projects.getAdminConfig, but haven't been able to find the ID you're looking for yet.
If that ID is indeed missing, it might be worth it to file a feature request.

Fresco Library Not Loading Any Images

So due to memory issue I thought about using Facebook's Fresco library.
So this is what I did,
First I added the following dependencies,
compile 'com.facebook.fresco:fresco:1.3.0'
compile 'com.facebook.fresco:animated-gif:1.3.0'
Then I initialized it in application class,
Fresco.initialize(getApplicationContext());
Then I added SimpleDraweeView to layout file,
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/imageMessage"
android:layout_width="180dp"
android:layout_height="180dp"
/>
Then I added the code in my activity,
SimpleDraweeView gif = (SimpleDraweeView) row.findViewById(R.id.imageMessage);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource (Uri.parse(url.get(position)))
.setProgressiveRenderingEnabled (true)
.build ();
DraweeController controller = Fresco.newDraweeControllerBuilder ()
.setImageRequest (request)
.setAutoPlayAnimations (true)
.build ();
gif.setController (controller);
But it's not loading images, I also tried different urls but same result, So what I'm missing...
i was facing same issue but not with gif. below is the code which load images, don't forget to initialize fresco.
Fresco.initialize(context);
SimpleDraweeView iv_image = (SimpleDraweeView)itemView.findViewById(R.id.iv_image);
iv_image.setImageURI(Uri.parse("your image url here"));
`
and here is xml
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/iv_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
fresco:actualImageScaleType="centerCrop"
fresco:placeholderImage="#drawable/iv_car"
fresco:viewAspectRatio="1.22"/>

Programmatically get hint text value in android Edittext [duplicate]

This question already has answers here:
EditText getHint() returns null when using design support library
(4 answers)
Closed 6 years ago.
I have to change theTypeface of EditText dynamically and also process the hint text if the Typeface changed. I simply do as following:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_textBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/textBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_date" />
</android.support.design.widget.TextInputLayout>
And my code snippet is as follow:
EditText textBox = (EditText) findViewById(R.id.textBox);
Typeface tf = Typeface.createFromAsset(this, "myfont.ttf");
textBox.setTypeface(tf);
String hintText = textBox.getHint().toString(); //Got NullPointerException here....
String newText = processText(hintText); //This will process the hint text;
textBox.setHint(newText);
When I runt the program, I got the following exception:
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
Please help me to solve this problem.
We need to look at your_layout.xml first to determine that, but you're probably using the design support lib.
If so, you can get your hints as follows:
((TextInputLayout)textBox.getParent()).getHint();
Note
This has already been answered here. (Although we needed to make sure that you were using the design support lin to mark your question as duplicate)

Customizing Android BluetoothLeGatt example and send data via BLE

I have to say at first that i am new to android programming. I am trying to send simple data (a short string) to a BLE peripheral device via Android. Therefore i customized the BluetoothLeGatt example from
http://developer.android.com/samples/BluetoothLeGatt/project.html
I replaced the Gatt Services List in the gatt_services_characteristics.xml with a text field for incoming data and below a EditText and a "send" button. The code is the following:
<TextView android:id="#+id/gatt_services_list"
android:layout_width="match_parent"
android:layout_height="250dp" />
<EditText android:id="#+id/sendText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_below="#id/gatt_services_list"/>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="SEND"
android:onClick="sendData"/>
I also commented out the function displayGattServices(List gattServices) in DeviceControlActivity.java and wrote a function for the button click as below:
public void sendData(View v) {
value = textField.getText().toString();
byte [] strBytes = value.getBytes();
mGattServicesList.setText(text + "\n>" + value);
text = text + "\n>" + value;
clearUI();
}
I now want to implement a simple way to send the data typed in the EditText. The data just shall be shown in a terminal.
I searched for a solution, but i didn't fully understand the concept of the Gatt Characteristic which i am afraid i have to use for this. I am programming with Android Studio 1.4.1 and using API 18 and higher.
How can i send the data in an easy way?
Thanks in advance
Android BluetoothLeGatt example already customized to Send and Receive via UART. See
https://github.com/danasf/hm10-android-arduino

Categories

Resources