Android: Keep screen on [duplicate] - android

This question already has answers here:
Correct method for setKeepScreenOn / FLAG_KEEP_SCREEN_ON
(5 answers)
Closed 9 years ago.
I try to set the screen to always on however I cant figure out how to do it within a fragment. I have tried to get access to the ViewPager but it returns with a null value. I want the screen to be on for the whole application but it should be able to be changed by the user within the settings of the app.
Here is my code from within my fragment:
private void setIsAlwaysOn(boolean b)
{
ViewPager pager = (ViewPager)getView().findViewById(R.id.pager);
pager.setKeepScreenOn(b);
sharePropertiesEditor.putBoolean(sp_alwaysOn, b);
sharePropertiesEditor.commit();
}
I get a nullpointerexception at line 4 of the visible code above.

Yeah. Can't you use this?
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
For Kotlin, use:
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Related

when i pass a variable from the main method, my app crash [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
im using android studio and im making an app but i have this problem when i pass the variable from my main to the class, my app crash.
its show me a massage that the app has stopped
this is how i pass the variable
bravo b = new bravo();
b.updatetext(Correctcounter);
and this is my method in the class
public void updatetext(int x) {
TextView scoreView = (TextView)findViewById(R.id.score);
scoreView.setText(""+x);
}
You can't access views from classes that are not UI.
You should never access a view from a custom class.
Views must be accessed from Activity or Fragment!
Change your logic to match this requirement.

how to disable preloading nextpage in Viewpager? [duplicate]

This question already has answers here:
ViewPager.setOffscreenPageLimit(0) doesn't work as expected
(13 answers)
Closed 7 years ago.
How do I disable page pre loading in Viewpager??
I tried
viewPager.setOffscreenPageLimit(0)
but it's not working.
use this viewPager.setOffscreenPageLimit(1) instead of viewPager.setOffscreenPageLimit(0) . because the minimum value of setOffscreenPageLimit() is 1.
Have a look at this.
According to Ali Imran you can try this:
First create a count variable in the adapter class
second in the ViewPager Adapter make a public function say setCount()
and in the override getCount() function of Adapter return the count variable
now override the on pageScroll() function of the ViewPager when the
user flips the page you can call the setCount() to increase the
count of the count variable
also check that if that the current
index is last and page is scrolled to left

What is (button) in android? [duplicate]

This question already has answers here:
How does Java Object casting work behind the scene? [duplicate]
(5 answers)
Closed 7 years ago.
(I am new to android programming) I've seen this piece of code which assigns a predefined button to variable b:
Button b = (Button) findViewById(R.id.button1);
It's all clear for me (to me?), but I just don't get one thing: what is that (Button)?!
(Button) is a typecast. Every widget that comes back from findViewById is a View. To treat it as a button, you must explicitly tell the compiler that it is a Button.
More information on findViewById here, in the Android documentation:
http://developer.android.com/reference/android/app/Activity.html
when you are calling findViewByID(...) it returns a View type. Button is a child class of View and by saying (Button) you are type casting it which will allow you to use the methods/functions in the Button class.

Android ArrayList Adapter does not change pictures [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have an ArrayList adapter wich used to load my listView.
My problem is that I want to change a pictures on the items in the list;
Until now I used a simple String array and didnt have any problem, but I had to change it to ArrayList wich loaded by an ORMlite selection and now my adapter doesent change the pictures...
I toast the text of the item wich is like ..."Asd"...
and in the adapter I use this:
if(textView.getText()=="Asd"){imageView.setImageResource(R.drawable.ic_asd);}
I also tried this:
if(values.get(position).getName()=="Asd")
{
imageView.setImageResource(R.drawable.ic_asd);
}
What is the problem? :S
Well, it's hard to say exactly what the issue is, but the main thing that's sticking out is that you're checking String equality wrong.
To check string equality, you should use Object.equals(Object obj); For example, textView.getText()=="Asd" should be textView.getText().toString().equals("Asd");
yes your problem is that you should use textview.getText().equals("Asd")
Use
if(textView.getText().toString().equals("Asd"))
I'm sure this is the problem ;)

Viewpager OffscreenPageLimit issue

I've multiple choice (radio buttons) questions on pages. I'm getting user answers with hashmap. So I set previous question answer on start of next question. As a result I start using set hashmap on start 2.page.
BUT problem is: I think viewpager loads first 2 pages at the same time ; so "set hashmap" on start 2.page goes null ; because 1. page isnt loaded / or just loaded at the same time
How can I get rid of this problem ?
page: list answers (1)
page: get user choice(1), clear list(1), set list answers (2)
page: get user choice(2), clear list(2), set list answers (3) ...
maybe setoffscreenpagelimit 0 could be solution but lowest value is 1
This older answer can probably help you.
Basically you just want to know when another fragment is visible, and update it from there.

Categories

Resources