I need to go to the same page on a PDF document after screen change. I have found how to navigate to page here but I failed to get the current page number.
I have found some codes regarding this in MuPDF library but couldn't call it from main activity. In PageView class (MuPDFPageView extends PageView):
public int getPage() {
return mPageNumber;
}
The problem is I don't know how to call this from main activity. I tried something like this:
final MuPDFPageView pageView = new MuPDFPageView(getApplicationContext(), filePickerSupport, core, parentSize, sharedHqBm);
int i = pageView.getPage();
But failed to fill the parameters in. Anybody has a better or easier way to get the current page number?
Within MuPDFActivity, you can call:
mDocView.getDisplayedViewIndex()
and that will return you the current page number.
Related
How do I get getString in fragment, I have tried to solve it with snippets of code on this but it did not work to tap
chronology
-ok so in the uploaded image there are some 6 items in the recyclerview that line up the grid, I try to make it to the String of the name as I have included the code but the code I press [commant + tap] displays the message {public statuc int} , but it can't be tapped to go to the next page, but if I try to use the usual string as I have included the code, it works on the next page
get an error public static int
dataProduct.add(Products(R.drawable.ic_electricity, getString(R.string.electricity), true))
I've looked for a solution in this forum but it still hasn't worked, but if I use the code below it works
dataProduct.add(Products(R.drawable.ic_electricity, "Electricity", true))
maybe I'll send a picture, below, in this case I will take the string but can't be tapped like the image below
image getString
getString() is a method that comes from a context. In a fragment, this context is commonly provided by the activity hosting the fragment. To use the activity to get a resource, just call getActivity() (which in Kotlin can be written just as activity):
activity.getString(…)
I am using Appium for automating my Android app. I have a fragment to enter email and after verifying that second fragment with passwordfield will be loaded. I am able to find all elements in the first fragment; but elements in the second fragment can't be found using any of the methods like By, PageFactory + #AndroidFindBy. Can someone provide a help to sort this issue?
Use ExplicitWait method.
public static void ExplicitWait(MobileElement element){
(new WebDriverWait(driver,30)).until(ExpectedConditions
.elementToBeClickable(element));
}
then before you use element of second fragment call ExplictWait
ExplictWait(passwordField);
passwordField.sendKeys("your password");
I have an activity which can take a few seconds to load its content (mainly pictures) from the cloud. Let's say this content is pictures & description from a person. The user can go to pictures & description from another person by clicking on the next button. I'd like to avoid the loading time When this button is clicked.
To do this I have two activities : firstPersonActivity for the first person content, and secondPersonActivity for the second person content.
What I try to do is to load the content of the secondPersonActivity when the user is in the firstPersonActivity, so that the secondPersonActivity can be displayed "directly" (= without needing to load content from the cloud when the next button is clicked in the firstPersonActivity).
But I do not succeed in doing this, I don't know how to modify the views of the secondPersonActivity layout from the firstPersonActivity class.
I tested the following code in my firstPersonActivity but it doesn't work (there is no connexion to the cloud, it's just a test to better understand how it works). R.id.first_image_second_person is the id of my imageview in the secondPersonLayout (= the layout used in the secondPersonActivity).
ImageView firstImageSecondPerson = (ImageView) findViewById(R.id.first_image_second_person);
firstImageSecondPerson.setImageResource(R.drawable.mypicture);
When I click on the next button to go from the firstPersonActivity to the secondPersonActivity, my firstImageSecondPerson imageview is not filled.
Do you see what's wrong ?
Is there a better way to avoid the loading time when the user click on the next button ?
It is not possible to change activity if activity instance is not created. In my opinion to do what You need I would go to single activity with hidden content of second person ( VIEW.INVISIBLE ) and show/hide it when it is needed.
But if second activity must be there, then create some structure for saving bitmaps in memory. In Your code sample You get picture from drawable so we are not talking about some delays, but if images are downloaded from some server then You can create some class which will have those bitmaps created from url and instance of this class can be used on any activity.
So for example Your class for caching pictures would have some method for creating bitmaps like -How to load an ImageView by URL in Android?. And those bitmaps should be saved in some Array or HashMap to have access to it, for example ( pseudo code ):
//some structure for cashing pictures in app
class PictureCache {
static PictureCache instance;
final HashMap<Integer, Bitmap> bitmaps;
//singleton
static PictureCache getInstance(){
if (instance==null)
instance = new PictureCache();
return instance;
}
public PictureCache(){
bitmaps = new HashMap<>;
}
private Bitmap loadBitmap(String url);//#1
public addPicture(Integer personId, String href){
//add to hashMap
bitmaps.put(personId, loadBitmap(href));
}
public Bitmap getPicture(Integer personId){
return bitmaps.get(personId);
}
}
#1 - method from How to load an ImageView by URL in Android?
Using it in first activity:
PictureCache.getInstance().addPicture(12,"http://url.to.bitmap");
Using it in second activity:
Bitmap pic = PictureCache.getInstance().getPicture(12);
Important note - above code was written here and was not tested, it shows solution concept.
Important second note - using such approach with bitmaps in memory can cause to much memory usage
You cannot access the views of SecondActivity before its creation. If you called this activity once only then you are able to access its views by making them static.
One more Solution for this is..
Access the whole data at once and save it in static arraylist with the help of getter-setters. Then on SecondActivity set data from that arraylist.
Hope this will work.
I don't think you can access the view before creating the activity.
You can try to use Glide for caching your images and minimizing loading time.
Try this
Picasso.with(getContext()).load(R.drawable.generatedId).into(imageView);
or
imageView.setImageDrawable(ActivityCompat.getDrawable(getContext(),
R.drawable.generatedID));`
In my app I have a header with icon hidden, I have a adapter with a listview when I click the listview I go to a login screen using listener, when the login is successful is should come back to listview(adapter) and icon should get visible on header.
In the login activity I have the following code:
public void onClick(View v) {
String password = etPassword.getText().toString();
if(password.equals("guest")){
SearchAdapter.setImgVisibility();
} else {
//-----
}
finish();
}
In my adapter I am calling the setImgVisibility() as follows, but it is not working
public static void setImgVisibility() {
img.setVisibility(View.VISIBLE);
}
I am getting a Nullpointerexception near the line img.setVisibility(View.VISIBLE);
I am stuck here and don't know what I am doing wrong. Any suggestions or help is appreciated
I would imagine that img is null. You need to look at where this value is set and make sure happens before you call the method setImgVisibility.
Show more of your complete code for people to help further.
Additionally, i've just noticed you've used a static reference to your search adapter, you should be really careful using statics, especially where any referencing of images is concerned as images can be bound to the context, as such unless you nullify the static you will end up with a memory leak. (this used to be an old problem, not sure its still valid, but i would still avoid using a static reference).
Without more code we're not likely to be able to properly help you. For example are you switching activities when logging in? If you are, this won't work at all.
[given the comment below] If you switch activities then your activity containing the list view is going to be destroyed and then rebuilt then you navigate back to it. or it will at least go through the activity lifecycle. This means you can set the icon during the instantiation of the header img.
You could store your logged in state as a property of the Application or a preference. Grab this value when you set the header image and set the image accordingly.
your img object is null. Is your img object is same as View v then you can pass v in setImgVisibility() and then set v.setVisibility(View.VISIBLE)
I am a total newbie at android (as well as this is my first post on StackOverflow) and I was wondering if there is a way of passing control from a web view displaying HTML to the actual android code. To make it a little more clear:
Suppose I have a HTML code that I am displaying in the web view in android, now I click the submit button (which is in the HTML page), is there a way to use that click to call a method in the android code?
A very high level view can be something like
if(Submit is clicked){ //submit would be the submit button in the html page
call xyz(); //xyz would be the android method
}
I would really appreciate it if you guys can help.
It is possible, refer to this.
There is a simple example there that should kick you off.
Javascript is the word of the day.
Use addJavaScriptInterface() to put a reference in the Javascript environment to some Java object.
Here is a sample application demonstrating this, to allow a page in a WebView to retrieve a location from the activity hosting the WebView.
You can use WebView addJavascriptInterface to export your method to the HTML page, here is a relevant snippet:
WebViewVariable.addJavascriptInterface(new MyJavaScriptInterface(context), "Android");
Here is the code for MyJavaScriptInterface :
class MyJavaScriptInterface {
Context localContext;
public MyJavaScriptInterface(Context ctx) {
localContext = ctx;
}
public void pageReady() {
// do your work here
}
}
You can now call the above class's method pageReady() from your HTML page like this Android.pageReady(). Remember though that you can only pass primitive data types (If I remember correctly).
Hope it helps