Translate chat in a chat application using Yandex key - android

Am developing chat application in android using Firebase with a feature that user can translate its chat to desired language.I put translation selection menu in my Chat class.Now my code is working fine to translate one text using Yandex Key but my question is how can i translate whole chat using the same scenario? Or how to get all messages in one String to Translate them.
My code for translating one message is as below:
case R.id.german:
TranslatorBackgroundTask tbt = new TranslatorBackgroundTask(getApplication());
String translationResult = String.valueOf(tbt.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, message, "en-de"));
textView.setText(TranslatorBackgroundTask.result);

Why not just use List object for storing all texts and execute translation function repeatedly? Some thing like this
List<String> texts = new ArrayList<>();
// pass texts
List<String> translatedTexts = translate(texts);
public void translate(List<String>) {
List<String> resultTexts = new ArrayList<>();
//apply your logic for translating each text with the for each method
return resultTexts;
}

Related

Retrieve data from firebase and store it in variable

I'm working on a project in which I want a feature, message us on WhatsApp and for that, I used WhatsApp API using intent.
in 'message us on WhatsApp' feature I'm using 2-3 different numbers and storing it in the list and using random() method to retrieve that number from the list, as I want whenever a user uses that feature he/she has to connect with a different number every time.
but now I want to change those numbers which are stored inside the list.
So how can I change those numbers without changing the actual code every time?
I know it can be achieved using firebase, but I don't have too much idea about firebase.
I can only upload simple text and images on firebase and retrieve them. but how can I upload all these numbers on firebase? and how I can change them every time within a minute
the code I did is like this
public class DashBoard extends AppCompatActivity implements View.OnClickListener {
public CardView wpicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
Util.blackIconStatusBar(DashBoard.this, R.color.white);
wpicon = findViewById(R.id.cardview1);
wpicon.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent i;
String num;
String text;
num = ";
text = "I'm interseted in your service";
ArrayList<String> list = new ArrayList<>();
list.add("+91");
list.add("+91");
Random random = new Random();
String rando = list.get(random.nextInt(list.size()));
switch (v.getId()) {
case R.id.cardview1:
Intent intent = new Intent(Intent.ACTION_VIEW);
i = intent.setData(Uri.parse("http://api.whatsapp.com/send?phone=" + rando + "&text=" + text));
startActivity(i);
break;
One way of doing this with Firebase is by using Firebase Remote Config. The idea is generally simple. The remote config represents a list of keys and values. You have to list your data from the Firebase console and publish it. It will be immediately available to your users.

Android - Automatically fill text fields of another app

I am implementing an Android app that is responsible for some data exchange with other services such as credentials. I then want to use that information to automatically fill in the input fields of other applications on the device such as Spotify.
Is there any way to fill the input fields of another app, like the username and password to remove the chore for the user to manually input it?
Also I noticed that at least on iOS, Spotify recognizes 1Password to be installed and displays a small icon next to the input fields with which I can fill the fields from the data stored in 1Password - how is this done as it seems to be another solution to my problem?
Thanks in advance
You might want to implement Autofill Service https://developer.android.com/guide/topics/text/autofill-services.html
There is a ready to use sample app which will get you started https://github.com/googlesamples/android-AutofillFramework
Android will invoke onFillRequest() method giving your service a chance to show autofill suggestions. Here is a sample code from above link:
#Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal, FillCallback callback) {
// Get the structure from the request
List<FillContext> context = request.getFillContexts();
AssistStructure structure = context.get(context.size() - 1).getStructure();
// Traverse the structure looking for nodes to fill out.
ParsedStructure parsedStructure = parseStructure(structure);
// Fetch user data that matches the fields.
UserData userData = fetchUserData(parsedStructure);
// Build the presentation of the datasets
RemoteViews usernamePresentation = new RemoteViews(getPackageName(), android.R.layout.simple_list_item_1);
usernamePresentation.setTextViewText(android.R.id.text1, "my_username");
RemoteViews passwordPresentation = new RemoteViews(getPackageName(), android.R.layout.simple_list_item_1);
passwordPresentation.setTextViewText(android.R.id.text1, "Password for my_username");
// Add a dataset to the response
FillResponse fillResponse = new FillResponse.Builder()
.addDataset(new Dataset.Builder()
.setValue(parsedStructure.usernameId,
AutofillValue.forText(userData.username), usernamePresentation)
.setValue(parsedStructure.passwordId,
AutofillValue.forText(userData.password), passwordPresentation)
.build())
.build();
// If there are no errors, call onSuccess() and pass the response
callback.onSuccess(fillResponse);
}
class ParsedStructure {
AutofillId usernameId;
AutofillId passwordId;
}
class UserData {
String username;
String password;
}

Filtering RecyclerView item (City) after getting json API

So, my app has successfully
1) Identified user's location (GPS Coordinates)
2) Passed these coordinates to an API and retrieved location specific data with retrofit.
3) Populated data to Recyclerview.
4) Implemented search functionality for user to filter the recyclerview as desired. For example, user can search for "pizza" and shrink their results from the initial 300 to the 20 that contain the String "pizza".
Lastly, I want to give the user the ability to filter by City name. I want to provide the user with the list of Cities that are generated from the recycleview list. Of course, the list would change each time based on users initial search criteria. How can I get the City list, which is one of the item fields retrieved from the API?
The "city" filter can be done before, after, or in place of the other filter. in other words, if I can only do one, that's OK, then I would just do the "city" filter and not offer the wildcard filter.
Any help is appreciated, even if it is just a high level view of what needs to be done. I guess first and foremost, I need to know how to get that list. My limited knowledge of the nuts and bolts of how recyclerview actually works tells me this might not be available.
#Override
public void onResponse(Call<Api> call, Response<Api> response) {
Api api = response.body();
data = new ArrayList<>(Arrays.asList(api.getDetails()));
//get city string array
String[] sa_cities = new String[data.size()];
for (int i = 0; i < sa_cities.length; i++) {
sa_cities[i] = data.get(i).getLocation();
}
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
#Override
public void onFailure(Call<Api> call, Throwable t) {
Log.d("Error",t.getMessage());
}

Adding items to Array List with specified objects

I am working in a translator kind of app and i need some help.
I have a class with getters and setters for my Array List objects. Each object has a phrase, a meaning, and usage.
so i have this to create my list:
ArrayList<PhraseCollection> IdiomsList = new ArrayList<PhraseCollection>();
now how do i add these objects to the list, each object containing the phrase, its meaning, and a use in a sentence?
For Example: The Layout would be something like this
Phrase
Kick the bucket
Meaning
When someone dies
Usage
My grandfather kicked the bucket
Thanks a lot
this is what i came up with that worked for me
private void loadIdioms() {
//creating new items in the list
Idiom i1 = new Idiom();
i1.setPhrase("Kick the bucket");
i1.setMeaning("When someone dies");
i1.setUsage("My old dog kicked the bucket");
idiomsList.add(i1);
}
ArrayList has a method call add() or add(ELEMENT,INDEX);
In order to add your objects you must first create them
PhraseCollection collection=new PhraseCollection();
then create the ArrayList by
ArrayList<PhraseCollection> list=new ArrayList<PhraseCollection>();
add them by :
list.add(collection);
Last if you want to render that in your ListView item, you must override the toString() in your PhraseCollection.
I suppose you would use the add(E) method (http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E)).
Here is an example using your example provided.
public class Phrase {
public final String phrase, meaning, usage;
//TODO: implement getters?
public Phrase(String phrase, meaning, usage) {
this.phrase = phrase;
this.meaning = meaning;
this.usage = usage;
}
}
and use it like this:
// create the list
ArrayList<Phrase> idiomsList = new ArrayList<Phrase>();
// create the phrase to add
Phrase kick = new Phrase("kick the bucket", "When someone dies", "My grandfather kicked the bucket");
// add the phrase to the list
idiomsList.add(kick);

Dynamically name objects based on EditText input?

I am creating a finance Android app that will open up and ask the user to add an account. (This will always be static on the page.) On the Main activity, it will have an EditText box next to a Button ("Add Account"). When the Button is pressed, I want a new Object created and it will then be stored into an ArrayList. The List of accounts (as they are added) will then be looped below (with corresponding dynamic buttons to edit the account). This is my practice/unfinished code. It is very raw at this point!
String accountName = (Whatever is in EditText Box)
ArrayList<Accounts> accountList = new ArrayList<Accounts>();
int accountListSize = accountList.size();
(Button on Click) {
Account{accountName} = new Account(); // Not sure how to dynamically name
accountList.add({accountName}) // Not sure how to dynamically name
}
// iterate through finance loop
for(int i = 0; i < accountList .size(); i++)
{
// do stuff - Create Dynamicly Edit and Clear Buttons for each account
}
One of the big issues I am trying to overcome is how to name an Object Dynamically?
Am I over-thinking this process overall and making it harder than it should be? I am going to create a class to handle the account specifics. I eventually have to keep the data stored--so maybe should I scrap the Object orientated style and use SQLite? Shared-preferences?
Any code samples would be great, but I am mostly hoping to find the recommend method I should take.
I would recommend to create an Account object that takes the name in the constructor. For example:
public class Account {
private String name;
public Account( String name ) {
this.name = name;
}
// ... other account related methods here...
public String getName() {
return name;
}
}
Then in your code above:
List<Account> accountList = new ArrayList<Account>();
(Button on Click) {
Account anAccount = new Account( accountName ); // accountName taken from text box.
accountList.add( anAccount );
}
Then to loop through the account list:
for( Account account : accountList ) {
String name = account.getName();
// .. do whatever you need to for each account...
}
Once you have this list of Account objects, you can do anything you need to do with them, such as storing in SQLite DB for later, etc...
Hope this helps...

Categories

Resources