how to make localtime.now a continuous function that updates automatically? - android

As the title says, How could I make that LocalTime.now update continuously, essentially becoming somewhat into a clock?
var current = LocalTime.now()
Current.text = current.toString()
//Current is the textView id in xml
// this currently prints out what the local time is at app launch.

Use TextClock
This shows the time and updates it automatically you don't have to do anything, please refer to see in detail.
<TextClock
android:id="#+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="hh:mm:ss a" />
https://developer.android.com/reference/android/widget/TextClock.html

Related

In Android: How to Force refresh (update) of a TextView that is INSIDE a Dialog Window?

This is probably a basic procedure, and in fact I've been extensively searching for a suitable answer, but I haven't found anything usable or that actually works. Now, the case:
A Dialog window is placed inside a method:
public void method_with_Dialog_code() {
Dialog simpleDialog = new Dialog(this, R.style.FilterDialogTheme);
simpleDialog.setContentView(R.layout.dialog_xml_layout);
simpleDialog.setCancelable(true);
TextView insideTextView = (TextView) simpleDialog.findViewById( R.id.insidetextview );
insideTextView.setText("This text should change when the WiFi is offline");
simpleDialog.show();
}
The respective dialog_xml_layout.xml file is simply:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/insidetextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
If the device is for example disconnected from the Internet, it generates a message that should be shown in the insideTextView. But notice that simpleDialog and insideTextView are inside the method, so they are local objects, so the first issue is how to execute:
insideTextView.setText("This device is now offline");
from another part of the code, that is, outside of the method?
If I decide to make simpleDialog and insideTextView as Global variables, I can with no problem, from another part of the program, set the line:
insideTextView.setText("This device is now offline");
But the instruction doesn't work. The TextView is never updated with the new message.
So, any ideas? Maybe with TextView.addTextChangedListener, so insideTextView could be updated when the TextView.setText is executed externally?
Gracias.
make insideTextView a member variable of the class and use it later.

Algolia Android initial search is really slow

I recently implemented Algolia on my app successfully just like the examples.
But the initial search takes about 5 to 7 seconds and I couldn't find a way to make it faster after checking the whole library code and documentation. After the initial search, search becomes very fast.
There is nothing unusual in my implementation but maybe you can see something that I don't. The following code is from the activity where I initialize Algolia:
Searcher searcher = new Searcher(ALGOLIA_APP_ID, ALGOLIA_SEARCH_API_KEY, ALGOLIA_INDEX_NAME);
searcher.setQuery(new Query("word").setExactOnSingleWordQuery(Query.ExactOnSingleWordQuery.ATTRIBUTE));
searcher.addNumericRefinement(new NumericRefinement("CountryId", NumericRefinement.OPERATOR_EQ, 1));
InstantSearch helper = new InstantSearch(this, searcher);
helper.setSearchOnEmptyString(false);
helper.search();
And this is the related xml layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="6dp"
android:paddingRight="10dp"
android:paddingLeft="1dp"
android:paddingTop="6dp">
<com.algolia.instantsearch.ui.views.SearchBox
android:id="#+id/searchBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:queryHint="#string/search_text_hint"
algolia:searchIcon="#drawable/icn_search_big"
algolia:closeIcon="#drawable/icn_clear_filled_big"
android:queryBackground="#drawable/sarch_query_shape"
android:background="#drawable/search_shape"
algolia:autofocus="true"
algolia:submitButtonEnabled="false" />
</FrameLayout>`
<com.algolia.instantsearch.ui.views.Hits
android:id="#+id/hits"
android:layout_width="match_parent"
android:layout_height="match_parent"
algolia:autoHideKeyboard="true"
algolia:hitsPerPage="6"
android:layout_below="#+id/searchBarParentLayout"
algolia:infiniteScroll="false"
algolia:itemLayout="#layout/search_item_algolia_row"/>
Do you have any idea what can be the issue here?
I'm glad that the issue disappeared when you switched to another wifi.
For future readers that may encounter network issues with InstantSearch Android:
First of all, build and run one of our demo applications
If you see no problem running the example application, you can try using a proxy like Charles to investigate what's happening between your app and the network
If your problem persists when running the examples, or if you are following the documentation, send an email to support#algolia.com describing the issue with a sample of your code!

Google +1 button in Android app does not change count

I followed the standard procedure to add a Google plus button:
<com.google.android.gms.plus.PlusOneButton
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="#+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
plus:annotation="bubble"
plus:size="standard(none)" />
It is linked to the app's Google Play page. It is initialized in onResume as recommended:
_PlusOneButton.initialize("https://play.google.com/store/apps/details?id=net.mydomain.myapp", 0);
The button gets the count correctly initially, but the count never changes. The Google Play page shows the count keeps changing (increasing) as expected. I have been curious about activityRequestCode in initialize (String url, int activityRequestCode). I do not know what it does.
Could anyone shed some light on this?

Google Play Games achievement unlocked popup not showing

I'm unlocking achievement using this simple method from developers docs:
Games.Achievements.unlock(getApiClient(), "my_achievement_id");
Achievement unlocks, but no popup shows up. There is also no popup when logged in to Google Play Games - which is connected.
Just add a view to the layouts you want to display achievements on like this:
<FrameLayout
android:id="#+id/gps_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
When you have your layout ready you neet to execute this inside your Activity or Fragment:
Games.setViewForPopups(getApiClient(), findViewById(R.id.gps_popup));
You have to be sure, that your GoogleApiClient is connected though, otherwise your app will crash.
<FrameLayout
android:id="#+id/gps_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
This is the same in Jacek KwiecieĊ„ answer
GamesClient gamesClient = Games.getGamesClient(MainActivity.this, GoogleSignIn.getLastSignedInAccount(context));
gamesClient.setViewForPopups(findViewById(R.id.gps_popup));
This changed because setViewForPopups with 2 parameters is deprecated.
Jacek and user3782779 answers didn't work for me, I had to do the following:
GamesClient gamesClient = Games.getGamesClient(this, GoogleSignIn.getLastSignedInAccount(this));
gamesClient.setViewForPopups(findViewById(android.R.id.content));
gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
Had the same problem. I have solved it by adding icon to the achievement. I am not kidding, it is really strange but it started to work after that. Please note that I am talking about not published project, I was just testing my app and wondering what is going on.
The ONLY view that worked for my case of having multiple activities was:
activity.window.decorView.findViewById(android.R.id.content)
I had the same problem with the achievement popup. The "Welcome Back" popup was showing correctly by just using my own View, but once I started opening other screens where I wanted to show the achievement unlocked popup, it was not working. The only thing that ended up working was using the content view from the decorView
val gamesClient = Games.getGamesClient(activity, googleSignInAccount)
gamesClient.setGravityForPopups(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
gamesClient.setViewForPopups(activity.window.decorView.findViewById(android.R.id.content))
You can call this code from any new activity you open and the pop-up will show up there as soon as you unlock your achievements.

Insert drop caps text in android

I am learning Android and the following is part of an assignment.
I need to write some text in an Android layout with the first letter in drop caps, like the following text:
I looked up the web and did not find many answers. Is there a style option or some property that I could use?
At the moment, I am thinking of the following options. Please suggest what is the best way to do such a thing
Use an image for the first letter
Write the first letter separately in a big font.
Any suggestions would be helpful.
You can use a RelativeSizeSpan.
final String someText = "A long time ago in a galaxy far, far away";
final SpannableString ss = new SpannableString(someText);
ss.setSpan(new RelativeSizeSpan(5f), 0, 1, 0);
There's a library written by Novoda in which you can add a DropCap https://github.com/novoda/spikes/tree/master/drop-cap
Here's an image from the demo app:
Please follow GitHub sample app
https://github.com/datanapps/CapTextView
<datanapps.captextview.CapTextView
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:padding="5dp"
app:layout_constraintTop_toBottomOf="#+id/text1"
app:capsDropNumber="2"
app:capFont="#font/eb_garamond_regular"
app:capTextColor="#color/purple_700"
app:capTextSize="#dimen/cap_text_size"
app:bodyTextColor="#color/purple_700"
app:bodyTextSize="#dimen/body_text_size"
app:bodyTextFont="#font/eb_garamond_regular"
/>
Hope it will help

Categories

Resources