I've been trying to create an auto-paste feature in my downloader app. My app has an editText and when a user opens the app the app will automatically check for a copied link that matches certain parameters. The main page has two fragments, one of them has this feature.
Problem is when I use bottomNavigationView with NavController using Navigation UI, that feature doesn't work. It shows null as a result when checking for clipboard data.
But when I use an adapter extending FragmentStateAdapter instead of navigation UI then it works properly.
if (!this.clipboardManager.hasPrimaryClip()) {
Log.d("ContentValues", "PasteText");
} else if (this.clipboardManager.getPrimaryClipDescription().hasMimeType("text/plain")) {
ClipData.Item itemAt = this.clipboardManager.getPrimaryClip().getItemAt(0);
if (itemAt.getText().toString().contains(TikTok)) {
inputLink.setText(itemAt.getText().toString());
resetButton.setVisibility(View.VISIBLE);
pasteButton.setVisibility(View.GONE);
if (Util.getAuto(requireContext())) {
String string = inputLink.getText().toString();
getDownloadLink(string);
Util.showProgress(requireContext());
}
}
} else if (this.clipboardManager.getPrimaryClip().getItemAt(0).getText().toString().contains(TikTok)) {
inputLink.setText(this.clipboardManager.getPrimaryClip().getItemAt(0).getText().toString());
resetButton.setVisibility(View.VISIBLE);
pasteButton.setVisibility(View.GONE);
if (Util.getAuto(requireContext())) {
String string = inputLink.getText().toString();
getDownloadLink(string);
Util.showProgress(requireContext());
}
}
I don't know what is the issue here. It works in fragment when I use Adapter but not with Navigation UI. Any explanation is appreciated. Thanks.
Related
I'm trying to navigate from a RecyclerView adapter to a detail fragment, passing an id value.
I can navigate from an item selected in the RecyclerView to open the GameDetailFragment using the following:
this.cardGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NavDirections action = ScoreboardFragmentDirections.actionScoreboardFragmentToGameDetailFragment();
Navigation.findNavController(v).navigate(action);
}
});
However, I can't seem to find how to pass data when clicking on the CardView item to the GameDetailFragment to query the details.
I can find many examples in Kotlin, but I'm just learning Java and Android and would rather not start with Kotlin.
Using the example from the documentation:
#Override
public void onClick(View view) {
EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
int amount = Integer.parseInt(amountTv.getText().toString());
ConfirmationAction action = SpecifyAmountFragmentDirections.confirmationAction();
action.setAmount(amount);
Navigation.findNavController(view).navigate(action);
}
It seems it's inaccurate as the documentation indicates that the word "action" is added to the END of the class name.
I decided to look at the actual generated class file, without modifying it, of course. The word "action" is added to the BEGINNING of the class file name or combination of the originating and receiving destinations.
The result:
this.cardGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String strTemp = "todo";
ScoreboardFragmentDirections.ActionScoreboardFragmentToGameDetailFragment action = ScoreboardFragmentDirections.actionScoreboardFragmentToGameDetailFragment();
action.setMessage(strTemp);
Navigation.findNavController(v).navigate(action);
}
});
P.S. Have you ever even tried to follow along with the documentation? There is very little, if any, flow to their descriptions.
One paragraph they're talking about cats & dogs, then the next paragraph they talk about apples and oranges. As a noobie/beginner, it's incredibly frustrating because somehow you have to figure out how they all interconnect/interrelate to one another.
Then, when we ask, we get snooty responses referring right back to the same documentation. Like really...
But, what do I know, I'm only a beginner.
I am new to Xamarin Android Development and I am using MvvmCross for binding data.I have SerachView on action-bar.I want to search data from list which is in ViewModel.How can I implement that ? I have searched for this issue on internet but all have used adapter and i want to search list-item without using adapter from ViewModel.I am not getting any idea how to do that.So anyone can suggest me an easy way?
Any suggestion or advice will be appreciated.
It is pretty simple.
Your SearchView is bound to a string property which you are using for filtering. Here I assume it is called SearchQuery.
It is not clear what criteria you want to use for filtering, I will assume that the ViewModel has a Name property, where the SearchQuery will be contained in that name.
So your ViewModel would look something like:
public class SearchViewModel : MvxViewModel
{
public string SearchQuery
{
get { return _searchQuery; }
set {
_searchQuery = value;
RaisePropertyChanged(() => SearchQuery);
RaisePropertyChanged(() => FilteredResults);
}
}
public List<ListItemViewModel> UnfilteredResults
{
get { return _unfilteredResults; }
set {
_unfilteredResults = value;
RaisePropertyChanged(() => UnfilteredResults);
RaisePropertyChanged(() => FilteredResults);
}
}
public List<ListItemViewModel> FilteredResults
{
get
{
if (string.IsNullOrEmpty(SearchQuery))
return UnfilteredResults;
return UnfilteredResults
.Where(r => r.Name.Contains(SearchQuery)).ToList();
}
}
}
So what happens is, whenever you enter a new value into the search box, it will trigger the PropertyChanged event on FilteredResults and use a simple LINQ query to filter the results.
If you don't want to swap out the entire list every time, you can do this with an ObservableCollection and add and remove items in that instead.
EDIT:
So as stated above you just bind the MvxListView to the new items source. Assuming you are using a AXML layout for your view:
<MvxListView
..
local:MvxBind="ItemsSource FilteredResults; ItemClick ResultClickedCommand" />
As for the SearchView, I just looked, there does not seem to be any code in MvvmCross to easily bind to that, and it does not inherit from EditText, so you need to do something like described here: https://stackoverflow.com/a/22501906/368379
Easiest way is probably simply to implement the SearchView.IOnQueryTextListener interface and in there set the new string you receive in the implementation on your ViewModel.
I have one Activity having 5 buttons: button1, button2, button3, button4, button5.
button1 clicks----open fragment1
button2 clicks----openf ragment2
button3 clicks----open fragment3
button4 clicks----open fragment4
button5 clicks----open fragment5
In fragment1 I am downloading data and displaying in a customized listview. In fragment2 I am downloading data and displaying in edittexts, textviews..etc.
But if I click the button1 again data is downloading again. I want to show the same view where the user comes back from fragment1 to fragment2 by clicking buttons.
How can I reach this logic? Please help me in this. If you want any information I will provide.
Thank you in advance!
EDIT : I need google chrome tab functionality in android fragments.here tabs are fragments.if you open one website in google search and open onother page in anothe tab.if you can navigate to first tab you can see the opened one only.in my case i am starting from the scratch of the fragment.how to reach chrome tab functionality in android fragments.
There are many ways to go around it. whatever data you have download you can store it either temporarily or permanently. if you want to store it temporarily you can:
use an ArrayList<HashMap<String,String>> object to store it.
or permanently using either
SQLiteDatabase or
SharedPrefrences
depending on the data type.
Now whenever you open your fragment you can check if the data already exists if not you hit the Service and get the data otherwise you can read directly from the source you have chosen.
I would use the Model-view-controller architecture on this app.
This means create a class to store this data. Use the singleton pattern when designing the class. First time the user press Button 1, upon creation of the fragment, call a method from the Model class to check if the data is downloaded or not. If it's not downloaded then fetch it, store it in the class and display it. If data is downloaded then use an method from the Model class to get the data and display it.
If you have small a amount of data to display you can use bundles or intents to store it.
UPDATE:
Below is a simple example of a singleton class that can be used to store your data and find out if your class contains initialized data or not. In my example I used as data an int value but you are free to use whatever type you want, even a class.
public class SingletonExample {
private static SingletonExample mSingleton = null;
private int mMyData;
private boolean mDataInitialized;
private SingletonExample() {
mDataInitialized = false;
}
public static SingletonExample getInstance() {
if (mSingleton == null) {
mSingleton = new SingletonExample();
}
}
return mSingleton;
}
public boolean isDataInitialized() {
return mDataInitialized;
}
public void setMyData(int myData) {
mMyData = myData;
mDataInitialized = true;
}
public int getMyData() {
return mMyData;
}
}
The singleton call you call like this:
SingletonExample mDataBank = SingletonExample.getInstance( );
mDataBank.setMyData(0);
I have a HomeActivity which extends Activity that contains Actionbar items. The HomeActivity has 1 fragment (StatusFragment which extends Fragment). In the Fragment there is a ListView which uses a custom ArrayAdapter and a method call to supply the data.
private ParseUser[] GetUsers(){
final ParseQuery<ParseUser> query = ParseUser.getQuery();
ParseUser[] usersArray;
try {
List<ParseUser> users = query.find();
usersArray = users.toArray(new ParseUser[users.size()]);
} catch (ParseException e) {
usersArray = null;
e.printStackTrace();
}
return usersArray;
}
I'm having trouble getting the ListView to update from the OnOptionsItemSelected callback.
case R.id.home_ab_refresh:
StatusFragment pFrag = (StatusFragment) getFragmentManager().findFragmentByTag("mFragment");
pFrag.users = pFrag.GetUsers();
pFrag.mAdapter.notifyDataSetChanged();
return true;
1) Is this an appropriate way to access the Fragment from the Actionbar items (HomeActivity)?
2) Is there a better way to design this code?
Thanks much!
Re 1) I probably wouldn't do a findFragmentByTag() every time, and instead just stick the fragment into a member variable of the activity during the activity's onCreate().
The main issue with the code is something else:
pFrag.users = pFrag.GetUsers();
pFrag.mAdapter.notifyDataSetChanged();
Here you violate the object-oriented design principle of loose coupling. The HomeActivity is too intimately bound to the implementation details of the StatusFragment. What you should do instead is move that code into the fragment and expose it as a single, public method that is named for the intent (goal, purpose) of the action, not its implementation.
// In HomeActivity
pFrag.reloadData();
// In the fragment
public void reloadData() {
this.users = pFrag.GetUsers();
this.mAdapter.notifyDataSetChanged();
}
This way, it's easier to reuse the status fragment elsewhere. More importantly, it's easier to evolve that fragment, you can now completely change the internals without having to change the host activity. This is cleaner from a design perspective.
Re 2) Aside from the issue I already mentioned, you should consider returning an empty array rather than null when an exception occurs. It's generally a better idea to return empty array/collection from finder methods, because people tend to immediately use the result for an iterator or an addAll() or something like that, without null-checking it first.
First of all, you dont make a nullpointer check, since you cant be certain that the FragmentManager will actually return a validFragment.
you can however just catch the onOptionsMenuSelected event in the fragment itself, which will result in a much more capsulated code.
Besides that, when do you refresh the ListView? Wouldnt it make sense to update the listview automatically once the new data has arrived?
Ok - preface with I am new to android and new to java as well. But I did code in a previous lifetime.....
I am working on an application and now trying to pull some methods out and place into a utility class. In particular, I have a method which updates text views that I wanted to move out of an activity.
When in the activity, I had two versions of the method the only difference being that one would accept a view in the parameter list (I used this to populate some fields in a custom dialog). They all worked fine.
Once placed in the external utility package/class, the method no longer works - no errors, and it appears to have all it needs - I've done some logging and the view claims to be visible and the textview ids appear to be correct. Yet nothing changes on the screen.
I'm guessing this is something completely obvious and stupid but I can't seem to sort it out.
package xxx.xxx.Utility;
(some imports)
public class Utility {
public static void updateTextView(int id, String opt_data, View v) {
String TAG = "updateTextView: ";
if (v.getVisibility() == View.VISIBLE) Log.i(TAG," visible");
TextView tvTarget = (TextView) v.findViewById(id);
if (tvTarget == null) {
Log.i(TAG, "Error: updateTextView target is null");
}
if (opt_data != null) {
if (tvTarget != null) {
tvTarget.setText(opt_data);
}
} else {
if (tvTarget != null) {
tvTarget.setText(" ");
}
}
}
}
EDIT w/ Additional Info:
In the inital description I mentioned that this method was also being used to populate some fields of a pop-up dialog with data. In fact, I can request any number of dialogs in that manner and they all display properly and with the correct (and different) data. So it seems to fail only when trying to update the tv data of the main activity (the initial) view.
I'm guessing this is something completely obvious and stupid but I
can't seem to sort it out.
It helps to get the root(?) parent (?) view properly. IE,
currentView = this.findViewById(android.R.id.content).getRootView();
and now all is well.