I recently started Android coding and wanted to create a little program for changing the screen brightness...
Well.. i know there are already some questions about it, but i tried everything suggested here and really dunno how i can solve my problem :)
I understood that you have to "refresh" the screen after setting brightness. And at this point my problem starts... I've created some kind of dummy activity and also have an intent in my main activity, but it seems like the intent dont sart the dummy activity... Heres the relevant part of my main activity:
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 255);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class); //it is working...
startActivity(in); //it is working...
}
and the dummy code:
public class DummyBrightnessActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.finish();
}
}
the manifest.xml:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Test"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.Test.DummyBrightnessActivity"
android:taskAffinity="com.Test.Dummy"
android:excludeFromRecents="true"
android:theme="#style/EmptyActivity"></activity>
</application>
maybe relevant, the styles.xml:
<resources>
<style name="EmptyActivity" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Toast</item>
<item name="android:background">#00000000</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#000</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
oh, btw... I dont get any errors... The dummy just wont start (I think so, because when i run it without android:excludeFromRecents="true" then it wont appear in the recent apps list.
I hope someone can help me...
Have a nice day
//EDIT: Well... it looks like the code is working properly xD
Just relooked and put some text instead of the "finish()" and the activity run properly... but i thought that the finish have to be there :/
Maybe you have any suggestions how to "reset" the screen instead? Looks like i understood one of the tutorials wrong...
//EDIT2:
Well... I cant post an answer to my own question in the first 8hours:D
So i post it in here:
Thank you all for the help and tips, but now i found the solution for myself :D
this one: Refreshing the display from a widget?
the part "kicking off an empty activity and executing the WindowManager refresh" is working for me.
i came across this before asking here, but back then i just couldnt get it to work :D
So, anyways, thank you very much ;)
This was just an example of hard it can be to code "a little, fast-coded beginner app" ;)
You may also want to consider calling invalidate() on your View instead of launching and closing a new one.
I am not sure if it will work for this scenario, as I have not tried changing the screen brightness. However, as per http://developer.android.com/reference/android/view/View.html#invalidate(), calling invalidate() on a view will cause onDraw() to be called, which sounds like what you want.
I think this should work. I use this when I am moving from one page to another. It simply identifies the class and starts the activity, running whatever your new activity is going to do.
public void onClick(View v) {
// (IN THIS EXAMPLE) IDENTIFIES WHAT IS BEING PRESSED
if(v.getId() == R.id.activity) {
// STARTS THE OTHER ACTIVITY
Intent i = new Intent(this,Activity2.class);
this.startActivity(i);
}
Also, remove finish(); because your activity initializes and closes at the same time.
Make sure that you set the content view (setContentView(R.layout.main);) so you can tell whether or not the activity you started actually brings you to the page you want.
EDIT: You say the button press isn't detected. Try implementing the OnClickListener directly to your activity android import it like this.
public class MyApplication extends Activity implements OnClickListener {
Also, make sure your button is properly initialized. Try removing this: button1.setOnClickListener(new OnClickListener(), and then add this to your code:
button1 = (Button) findViewById(R.id.activity);
button1.setOnClickListener(this);
Related
This question already has answers here:
Android - Prevent white screen at startup
(18 answers)
Closed 4 years ago.
I created a splash screen for my application using an empty activity that stays visible for 3 seconds with a background image.
Usually, the application starts with a white screen before the background image becomes visible, however, some applications are already started with the "real" splash screen image.
How to implement this?
you can use splash screen in this way.
Add style for splash screen activity in styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/yourImageName</item>
</style>
Add that style as theme to your SplashScreenActivity in manifest file
<activity android:name=".SplashScreenActivity"
android:theme="#style/SplashTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Remove setContentView() in SplashScreenActivity's onCreate() Method and use it as a java file which extends AppCompactActivity
The problem is because system process draws initial blank screen when launching the app, from documentation:
A common way to implement a themed launch screen is to use the windowDisablePreview theme attribute to turn off the initial blank
screen that the system process draws when launching the app. However,
this approach can result in a longer startup time than apps that don’t
suppress the preview window. Also, it forces the user to wait with no
feedback while the activity launches, making them wonder if the app is
functioning properly.
You can disable it with windowDisablePreview attribute, something like this:
<application
...
android:windowDisablePreview="true">
...
</application>
Splash Activity usually starts with the background.
Try this code, it might help you.
Code for your splash activity page.
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread myThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}}
In layout file, you need to include just Image which you want to see on the splash page.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/capture"
/>
</RelativeLayout>
And make sure you add the Splashscreen page before Main activity in Manifest.
<activity android:name=".SplashScreen">
I am learning android, I am trying to develop game, I have two classes "Starter" and "Board". Starter class contains menu(http://postimg.org/image/dnyvoey2l/). Its Exit and Help buttons are working properly, but when I press "Two Player" option instead of showing board it shows an error (Unfortunately, (Application Name) has stopped). I am sharing code snippet , please suggest solution.
twop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent tow= new Intent(Starter.this, Selector.class);
startActivity(tow);
}
});
You can't show View using startActivity() method
Intent tow= new Intent(Starter.this, Board.class);
startActivity(tow);
Board should be extended from Activity not from View class. Create an BoardActivity.java and extend it from Activity.
You should then add Board View either from XML or programmatically using setContentView(); in your onCreate() method.
Edit
Don't forget to add new Activity in your Manifest.xml file. Like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.application.package.name">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Starter" android:label="#string/app_name"></activity>
<activity android:name=".Selector"></activity>
</application>
</manifest>
Below is my idea.
The code of OnClick means starting a new activity.
Intent tow= new Intent(Starter.this, Board.class);
startActivity(tow);
However, the Board is a View, not an activity.
So, you should make Board inherit from Activity (or create other activity to hold the Board).
When creating any activity, make sure to register in manifest.
I've created my first app, step by step, as described on
http://developer.android.com/training/basics/firstapp/starting-activity.html
According to this page, after clicking the button, there should appear a default "Hello world" layout, respectively the message typed in the text field.
Unfortunately, none of them appears at all. Absolutely nothing happens. Why?
Thanks in advance...
I suggest using setOnclicklistener. It seems you are new in coding. Google provide a good beginner guide. But still confusing a lot. take a look at this code.
first You need to locate your curret Button.
Button b1=(Button) findViewById(R.id.send);
assuming that your button hase a xml name"Send"
Now starting activity by clicking button
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
Intent in=new Intent(MainActivity.this,secondActivity.class);
startActivity(in);
}
});
}
Assuming that "secondactivity" is your target activity which you want to open by clicking button. Another Putextra and getextra method will be same as described in tutorial. Note this is an alternative method which i have described but you can still use that one.
I suggest that if you are a beginner you should start with youtube or lynda.com video tutorial. Rest is upon you
Have you edited the AndroidManifest.xml file.You should make sure that the following code is in your manifest file:
<activity
android:name="your package name.your main activity name"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
I wanna create a custom PopupWindow similar to this :
http://android-er.blogspot.kr/2012/03/example-of-using-popupwindow.html
In this example, PopupWindow is created by a button click event from an activity, but i want to create PopupWindow via intent from another application.
Is it possible? Any comments will be very appreciated!
The idea is to declare a standard Activity to make it appear like a Popup window of sorts.
Use this code (standard boiler plate Intent code to trigger the Activity)
SOME_WIDGET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), THE_POPUP_ACTIVITY.class);
startActivity(myIntent);
}
});
If, for example, you name the popup Activity as Popup, then replace the THE_POPUP_ACTIVITY.class with Popup.class
Now, for this Activity, in the Manifest, declare a theme. For example:
And this is the corresponding style declaration:
<style name="DialogNoTitleBar" parent="android:style/Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Also, in the onCreate() of the Popup Activity, you might want to add this statement right after the setContentView(R.layout.THE_LAYOUT_XML); I say might because how you want it to appear may vary from how I program my popup Activity.
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Your requirement isn't really clear. Intents are usually used when you want to switch to another activity or maybe send the intent to a service of some sorts. If you want to open a dialog for an action, you don't really need intents.
Create an activity with your custom design, while registering the activity in the manifest file just add this android:theme="#android:style/Theme.Dialog"
and call the activity with your intent. Hope you got it. :)
I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.
Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.