[HELP] Creating a simple app - Android Software Development

Hi guys!
I want to create a simple app, i have tried to, but i fail every time (in the very start!)
It shall be built in this way:
When opened a list will appear. When an element is chosen another list will appear.
In the second list, when an item is selected text will appear!
Can some very nice chap help me?

What have you tried so far? Are you developing in eclipse and java? Maybe try the Google App Inventor first.
You are basically wanting an app with a bunch of listviews (class extends listview activity) and finally a standard activity with a textview.
That should be pretty simple but then you have to populate the lists with some kind of data.
The best way to start learning is to just start, and keep at it. Google is your friend, every question you have, google it. There is a ton of resources on the net for android already. Chances are your question has been asked by someone else.
If you are wanting someone to do it all for you...good luck. Unless you pay someone to do it.

I have builded my own j2me midlet with microemulator, looks good, but i can't enter any element in any listview. Guess it is because one could not use the center key in the midlet but had to use the to other keys. You know, just below the screen on good old phones.

You're putting a lot into a "simple" app in my opinion; especially if you're new to programming.
Restart a new project. Create an array of data. Create array list and loop through your data array and add the items to the array list. Create an array adapter that will get populated with your list. Setup main.xml with <ListView> tag.
If you get that far, your list should be displayed. Then work on your onClicks'. It's going to get confusing if you're going to have a clickable listview come up that's clickable and does something else. Why not just one list?

Sounds very hard :S how about creating a kind of book with chapters, so one can start up the app and choose a chapter and be sent directly to it? Alternative for the lists.

Well, you have to deal with "onClicks" no matter whether they are buttons or items in a listview.
Here's a little code to help you out with a listview:
Code:
// Create some data
String myArray[] = {"Chapter 1", "Chapter 2", "Chapter 3"};
// Create a list to hold our data
ArrayList<String> myList = new ArrayList<String>();
// Load the list; usually done from an array in a loop
for(int i=0; i<myArray.length(); i++) {
myList.add(myArray[i]);
}
// Setup Array Adapter with myList
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.test_list_item, myList);
// Get id of ListView in xml file
lv = (ListView) findViewById(R.id.myListView);
// And display it
lv.setAdapter(aa);
Typed that off top of my head so I might not be 100%; compiler will let you know LOL Once you get a list displayed, Google something like "Android ListView onClick" for further help.
This code is pretty basic; if you don't understand it, you may want to look into some tutorials on Android programming.
Good luck

Rootstonian said:
Well, you have to deal with "onClicks" no matter whether they are buttons or items in a listview.
Here's a little code to help you out with a listview:
Code:
// Create some data
String myArray[] = {"Chapter 1", "Chapter 2", "Chapter 3"};
// Create a list to hold our data
ArrayList<String> myList = new ArrayList<String>();
// Load the list; usually done from an array in a loop
for(int i=0; i<myArray.length(); i++) {
myList.add(myArray[i]);
}
// Setup Array Adapter with myList
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.test_list_item, myList);
// Get id of ListView in xml file
lv = (ListView) findViewById(R.id.myListView);
// And display it
lv.setAdapter(aa);
Typed that off top of my head so I might not be 100%; compiler will let you know LOL Once you get a list displayed, Google something like "Android ListView onClick" for further help.
This code is pretty basic; if you don't understand it, you may want to look into some tutorials on Android programming.
Good luck
Click to expand...
Click to collapse
Dont forget your setcontentview()

I said "off the top of my head" LOL
I usually don't provide code, but I was in a good mood. There is still plenty of work to be done around that snippet I posted...as you well know

#¤&*@ 1 buck for the man who will write the 'skeleton' of this app!

Ihaveatattoo said:
#¤&*@ 1 buck for the man who will write the 'skeleton' of this app!
Click to expand...
Click to collapse
why are you asking for a how to if you are not willing to put any effort in it ?
i dont get it ...

It's about two months ago i asked for help. I the meantime i have tried to make it. But thanks, i just remembered i had some code to work from!
The problem is, i can almost no java. And i have another study and work.

At least post the code you have so far. You are asking for help with an app you have barely described with code we have never seen.

Added my code snippet to empty project, added listview to main.xml. Took me longer to remember my photobucket password! LOL
Now, if you're not a programmer, there isn't much we can do to help besides writing it for you. And we like to get paid for that
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}

I know how to make that.. ;-) Problem now is to make the items in menu "click able ". I'LLC post my code soon.
Thanks for help chaps!

Related

[Help] How launch an Activity or dialog from background

Hello, I'm starting with Android and I'm trying to program a timer which launch an application or an AlertDialog, even if the application is in background. I know it is very invasive for the user but is necesary cause it show an important alarm that must be handled instantly.
Thats the code I used, but only works if MainActivity is the current activity =(
Code:
Timer time=new Timer();
teme.schedule(new TimerTask() {
public void run() {
Intent dialog = new Intent(MainActivity.this., Alarm.class);
dialog.setFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(dialog);
}
}, delay);
Can someone help me please?
Going to have to run it as a Service
You have MainActivity.this is why MainActivity has to be running.
When you start an activity, update the reference, and then call:
[whateverActivityIsRunning].this
Aditionally, you may try launching another thread under your process, this however will not work if your app was force closed by system or user.
Code:
private void dodelayed(int delay, Intent someintent){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override public void run(){
//If that dialog is custom class then someintent.setclass(this,classname.class);
startActivity(someintent);
}
},delay);
}
Thanks guys, starting the activity from a service works nice for my target.
Now i'm trying a new thing:
I start the service in my app, then i open another app, Angry Birds for example xD. The startActivity tiggers and my app comes to front with a Activity that shows a message and a button.
I want a button click hide the Activity and move Andry Birds back to front.
How could I do it? Thanks!
finish();
With finish() I go to the last Activity of my app before it went to background =(
Do you know what that last activity is? You could set a variable in it to call from your other activity telling it to also finish.
You could also try something like this:
http://developer.android.com/guide/topics/manifest/activity-element.html#reparent
The "Love & Hate" of Android OS.
finish(); will only pop the last Activity on it's OWN thread and not Angry Birds as it is running on it's own thread (well VM actually).
The OS was designed this way; they didn't want any old application dumping something else that was running...makes sense to me.
I don't know what you're doing, but it sounds like it's working OK to me. Say I'm in the middle of creating a text, your timer goes off (for whatever reason I don't know), I respond to it and go about my texting. I would be more than upset if your timed event blew me out of my texting!!! Your app would be in the trash bin pretty quickly.
Might need a little more info on what you're trying to do...
My app wont be comercial. It reads from a bluetooth device checking for alarms, if any, gives the user some time for cancell it, else its sent to the main server.
I want do something like whatsapp application. where, if you activate the emergent notifications. when you receive an message a alertdialog is shown. But you can cancell it and continue with your stuff
See that screenshot:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
for such notifications, first create an activity with normal textview and buttons and remember to use layout_width and height as wrap_content, then in your application manifest use the following
Code:
<activity android:name="activity"
android:label="Title"
android:theme="@android:style/Theme.Dialog">
now when you call this activity from a service, a dialog box such as in the picture above depending on your layout xml will be shown. and when you call finish() through a button on this dialog, the focus will be passed to angry birds since this activity was launched from a parent with no UI.
Exactly what i was looking for. Thanks!
dont waste posts, hit the thanks button!
Sorry, you are limited to five thanks per day
have to wait =)
Dont worry I gave him one for ya

[Q]Showing preference screen activity

Hey guys,
I'm still learning to make android apps and now I'm writing small apps...Today I wrote a small app to test the scrolable tabs+ swipe created with blank activity.I need to show show contents of a PreferenceScreen xml.So I created a xml and added a new class:
Code:
public class About extends PreferenceActivity{
[user=1299008]@supp[/user]ressWarnings("deprecation")
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.info);
addPreferencesFromResource(R.layout.info);
}
}
And then found that the below code is responsible for the screens and so done this:
Code:
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.example.testy","com.example.testy.About"));
startActivity(intent);
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
Now the thing is that,I need the app to show the contents of xml directly on section1 instead of opening an activity above.Also noted that,when I go to section2,The activity is opened once and when back to section1,twice.
I need to show completely different contents on section1 and section2 also that the contents should be directly on sections instead of opening a new activity over the main one.Sorry for confusing you.All the xml I use are in preferences.Thank you
Regards,
Vijai
vijai2011 said:
Hey guys,
I'm still learning to make android apps and now I'm writing small apps...Today I wrote a small app to test the scrolable tabs+ swipe created with blank activity.I need to show show contents of a PreferenceScreen xml.So I created a xml and added a new class:
Code:
public class About extends PreferenceActivity{
[user=1299008]@supp[/user]ressWarnings("deprecation")
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.info);
addPreferencesFromResource(R.layout.info);
}
}
And then found that the below code is responsible for the screens and so done this:
Code:
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.example.testy","com.example.testy.About"));
startActivity(intent);
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
Now the thing is that,I need the app to show the contents of xml directly on section1 instead of opening an activity above.Also noted that,when I go to section2,The activity is opened once and when back to section1,twice.
I need to show completely different contents on section1 and section2 also that the contents should be directly on sections instead of opening a new activity over the main one.Sorry for confusing you.All the xml I use are in preferences.Thank you
Regards,
Vijai
Click to expand...
Click to collapse
Change your activity into a FragmentActivity. Then you will be able to add a PreferenceFragment. However, you will need to show the content of your screen in a Fragment instead of the "normal" way of doing it in the onCreate method. Add a Fragment there and if you want to show the preferences, add a new one on the top. It will not open a new Activity but just a new Fragment in the Activity.
nikwen said:
Change your activity into a FragmentActivity. Then you will be able to add a PreferenceFragment. However, you will need to show the content of your screen in a Fragment instead of the "normal" way of doing it in the onCreate method. Add a Fragment there and if you want to show the preferences, add a new one on the top. It will not open a new Activity but just a new Fragment in the Activity.
Click to expand...
Click to collapse
Wait...I dont understand this:
However, you will need to show the content of your screen in a Fragment instead of the "normal" way of doing it in the onCreate method. Add a Fragment there and if you want to show the preferences, add a new one on the top. It will not open a new Activity but just a new Fragment in the Activity.
Click to expand...
Click to collapse
.However I changed my activity to fragment activity from the link you gave
vijai2011 said:
Wait...I dont understand this:.However I changed my activity to fragment activity from the link you gave
Click to expand...
Click to collapse
Yes, change it to a FragmentActivity. It will act like the container of your Fragments. Then you will set the content by adding new Fragments on the top of each other.
Check this: http://developer.android.com/guide/components/fragments.html
Here´s a full example showing how to implement Tabbed Preferences Screen (with no swipe for now). I hope it helps you!!
Entire ICS source code attached!
Screenshots:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}

[GUIDE] Create Transparent Demo pages on Android

Have you seen those apps with the really cool hand drawn arrows that show the users how the app works the first time they run it? Yeah I have too and I wanted to figure out how to do that. So I did. Now I'm going to show you also.
This is going to be pretty easy and we'll do it for Activities and Fragments so all your apps will be covered.
The first thing you'll need to do is draw the pictures that we'll place over the Activity/Fragment. I draw mine on Gimp but you can use whatever drawing tool you want. I recommend you draw them 480 X 760 px. You'll want to set your background as Transparent. Here is a sample of what I've done.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
That's easy enough. I recommend you draw these as you go, I've found that I needed to edit the images to make sure they are pointing to the exact right place on the screen.
Next up we'll create an XML layout for each image that you want to use in your app. We'll create a LinearLayout with an embedded ImageView. The ImageView will display our beautiful art from above. Make sure to give the LinearLayout an ID, set the background to @null. As for the ImageView you'll need to give it an ID also and set the source to the image file you drew previously. Be sure to put the image in the res/drawable folder, make sure the image file name is all lower case and it is a .png file type. Again you will need to create as many of these XML layouts as you did images above.
overlay_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llOverlay_activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background= @null"
androidrientation="vertical" >
<ImageView
android:id="@+id/ivOverlayEntertask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/activity_overlay" />
</LinearLayout>
We are going to control when this is displayed to the user with SharedPreferences. Utilizing SharePreferences gives us the ability to show the tutorial on the first run of the app and also allows the user to see it again by selecting it in the apps Settings menu.
We'll need to create a folder under /res/ named /xml, so you'll end up with /res/xml. Now let's create a new XML layout for our preferences. In Eclipse you'll choose File New > Android XML file. Change the Resource Type to Preference, then name the file exactly this prefs.xml and click Finish.
Next we'll add a CheckBoxPreference with the following attributes:
android:defaultValue="true"
android:key="tutorial"
android:summary="Enable tutorial on device"
android:title="Tutorial"
So your final prefs.xml file will look exactly like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="tutorial"
android:summary="Enable tutorial on device"
android:title="Tutorial" />
</PreferenceScreen>
Now let's look at how to show this to the user from our Activity or Fragment. First we'll determine if our tutorial CheckBoxPreferene is set to true and if so we'll display the drawing on top of the current Activity/Fragment.
Create and instance of SharedPreferences and a boolean to store the value:
SharedPreferences setNoti;
boolean showTut;
Then in our OnCreate we'll get the value for our tutorial CheckBoxPreference and if ti's true we'll overlay the image onto our Activity/Fragment:
setNoti = PreferenceManager.getDefaultSharedPreferences(this);
// SharedPref tutorial
showTut = setNoti.getBoolean("tutorial", true);
if (showTut == true) {
showActivityOverlay();
}
Now for the last part we'll create the method to display the overlay. We will create a Dialog and apply a translucent theme with no title bar, this provides a dialog that covers our activity but doesn't hide our notification bar.
private void showActivityOverlay() {
final Dialog dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_activity);
LinearLayout layout = (LinearLayout) dialog
.findViewById(R.id.llOverlay_activity);
layout.setBackgroundColor(Color.TRANSPARENT);
layout.setOnClickListener(new OnClickListener() {
@override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
We'll use a method like this whereever we want to display the overlay to our users. When we get to the last overlay it is best to change the value in our SharedPreferences so we don't continue showing the overlays to the user. We do that with this variation on our above method where in the onClick we update the SharePreferences to set our tutorial to false.
private void showActivityOverlay() {
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_activity);
LinearLayout layout = (LinearLayout) dialog
.findViewById(R.id.llOverlay_activity);
layout.setBackgroundColor(Color.TRANSPARENT);
layout.setOnClickListener(new OnClickListener() {
@override
public void onClick(View arg0) {
// Get SharedPrefs
PreferenceManager.setDefaultValues(getActivity(), R.xml.prefs, true);
SharedPreferences setNoti = PreferenceManager
.getDefaultSharedPreferences(getActivity());
setNoti.edit().putBoolean("tutorial", false).commit();
showTut = false;
dialog.dismiss();
}
});
dialog.show();
}
And there you have it! As I mentioned at the begging of this post, this can be applied to an Activity or a Fragment. You'll notice in the last method instead of using the context of "this" I'm using the context of "getActivity()", that is the only change you have to make for this to work with a Fragment.
I am publishing the full code on my GitHub. Please feel free to leave comments, questions or your own pointers below.
https://github.com/marty331/overlaytutorial
Thanks for reading!
Cool guide.
I suggest adding [GUIDE] or [HOW-TO] to the thread title to make it easier to find. I thought it was a question when I clicked on it.
marty331 said:
I recommend you draw them 480 X 760 px.
[...]
overlay_activity.xml
Code:
[/QUOTE]
Where's your code? :(
Personally, I'd use Inkscape to create them as a *.svg and convert them to a *.png file later. That way they will not look pixelated.
Click to expand...
Click to collapse
nikwen said:
Where's your code?
Personally, I'd use Inkscape to create them as a *.svg and convert them to a *.png file later. That way they will not look pixelated.
Click to expand...
Click to collapse
Somehow my post was corrupted, I edited it and re-pasted everything.
I haven't tried Inkscape, but am up for giving that a go.
marty331 said:
Somehow my post was corrupted, I edited it and re-pasted everything.
I haven't tried Inkscape, but am up for giving that a go.
Click to expand...
Click to collapse
Great. Thank you.
The only thing which would be even better now would be using
Code:
tags. ;)
I didn't do much with it. However, scaling SVGs looks much better because they consist of single lines and shapes which can be rendered for all densities.
The disadvantage is that it is more difficult to create good-looking graphic effects in SVG.
I just found the Showcaseview library. It looks very promising.
Homepage: http://espiandev.github.io/ShowcaseView/
Github code: https://github.com/Espiandev/ShowcaseView
nikwen said:
I just found the Showcaseview library. It looks very promising.
Homepage: http://espiandev.github.io/ShowcaseView/
Github code: https://github.com/Espiandev/ShowcaseView
Click to expand...
Click to collapse
nikwen,
ShowcaseView is really awesome, I agree. However, it does not work with Fragments yet. Super bummer!
marty331 said:
nikwen,
ShowcaseView is really awesome, I agree. However, it does not work with Fragments yet. Super bummer!
Click to expand...
Click to collapse
Why do you think so?
Shouldn't this work? Correct me if I am wrong.
Code:
ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
co.hideOnClickOutside = true;
sv = ShowcaseView.insertShowcaseView(R.id.buttonBlocked, getActivity(), R.string.showcase_main_title, R.string.showcase_main_message, co);
(Code based on this: https://github.com/Espiandev/Showca...andev/showcaseview/sample/SampleActivity.java)
I agree that it should, however it does not. I've emailed the developer and he said he will work on it. If you want to use ShowcaseView in an activity it works very well.
marty331 said:
I agree that it should, however it does not. I've emailed the developer and he said he will work on it. If you want to use ShowcaseView in an activity it works very well.
Click to expand...
Click to collapse
Ok, that's strange.
Thanks exactly what I was looking for!
Sent from my Galaxy Nexus using Tapatalk 4 Beta
marty331 said:
I agree that it should, however it does not. I've emailed the developer and he said he will work on it. If you want to use ShowcaseView in an activity it works very well.
Click to expand...
Click to collapse
I got it to work.
I will push my code to Github later, but I will improve it before.
Then I will write a tutorial for that.
Here is my pull request: https://github.com/Espiandev/ShowcaseView/pull/68
Guide for doing it using the ShowcaseView library: http://forum.xda-developers.com/showthread.php?t=2419939
This guide is great. Thanks again. Please understand that by posting this I don't want to criticise, offend or denigrate you.
You got mentioned in my guide.
marty331 said:
Somehow my post was corrupted, I edited it and re-pasted everything.
I haven't tried Inkscape, but am up for giving that a go.
Click to expand...
Click to collapse
I think you should also disable smiles in the OP!
androidrientation=„blahblahblah” is android(smile)rientation!
great tut.
thanks
cascio97 said:
I think you should also disable smiles in the OP!
androidrientation=„blahblahblah” is android(smile)rientation!
Click to expand...
Click to collapse
I always use another trick:
Code:
android:[B[I][/I]][/B[I][/I]]orientation
The result:
Code:
android:[B][/B]orientation
That allows you to use smileys in other parts of the post.
nikwen said:
I always use another trick:
Code:
android:[B[I][/I]][/B[I][/I]]orientation
The result:
Code:
android:[B][/B]orientation
That allows you to use smileys in other parts of the post.
Click to expand...
Click to collapse
Thank you! didn't know about this one!
Sent from my Galaxy Nexus using XDA Premium 4 mobile app

Need help with really small app

I have the Android SDK and Eclipse downloaded and I can get it work properly. However, there doesn't seem to be any commands anywhere, like, am I supposed to know all of the JAVA commands by memory to use this tool???
I only want a very simple app that does this:
- When screen is off, turn WIFI off
- When screen is on, Turn WIFI on
That's it. I don't need any fancy menu or anything, it can run in the background for what I care, it's just that Stamina mode doesn't work with these roms and bootloaders and whatever, so I need this app to save me some power.
I reckon it'd be pretty easy for anyone who knows java, so maybe I could get someone to post a code for me?
Thanks in advance
You don't need to know everything by memory but you do need to know the basics.
For example, you need to know the difference between an Activity and a Service and determine which one you would want to use.
For your app, you would need a Service and a BroadcastReceiver.
Your BroadcastReceiver would capture the intents of the screen turning on and off:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
You would then call your service from your broadcastreceiver to do the work of turning off your wifi:
http://stackoverflow.com/questions/8863509/how-to-programmatically-turn-off-wifi-on-android-device
Be sure to add the proper permissions in your manifest file that allows you to read and change wifi state.
Ok, so now I know how the code should be, but there's just one more thing.
How in the world can anyone program a user interface with text only??? This program is certainly not easy to use, I don't even know where to put the code with all of those different folders.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Do I even need all of those commands/folders? I tried deleting all that stuff and putting in the code but I got lots of errors obviously.
So if you would please tell me:
- Where to put the code
- How to make an interface like this one with radio buttons or something, a switch might be easier
I don't want anything fancy, just a super basic app
I suppose the code would look like this:
Code:
package selecm.wwf; //your package name
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
WifiManager wm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public class ScreenReceiver extends BroadcastReceiver {
// THANKS JASON
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wm.setWifiEnabled(false)
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wm.setWifiEnabled(true)
wasScreenOn = true;
}
}
}
And then make these in the manifest.xml file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Am I right?
Your user interface is located in the "res/layout" folder. It is most likely called activity_main.xml.
Also, I would recommend keeping your BroadcastReceiver separate from your activity. It makes things cleaner to debug and easier to build on.
After doing some playing and spending more time on this then I planned, using a service does not work reliable with the broadcast receiver. I am not sure how logical google was with the thought process of requiring some intents that can only be registered in code. With that limitation, your activity would need to be running to receive the intent from the screen being turned off or on.

[Q] Set BackgroundColor but keep onTouch

Hello,
I want to set the BackgroundColor of ListView items, but I want to keep the color which appears when I click the items.
How can I do that?
Auroratic said:
Hello,
I want to set the BackgroundColor of ListView items, but I want to keep the color which appears when I click the items.
How can I do that?
Click to expand...
Click to collapse
Rather than replace a "stateListDrawable" with a color or static drawable, replace it with another state list that has the "states" you require
deanwray said:
Rather than replace a "stateListDrawable" with a color or static drawable, replace it with another state list that has the "states" you require
Click to expand...
Click to collapse
I've got it, but I have one problem: The colour on state_pressed should be the default system colour (this one: android.R.drawable.list_selector_background)
How can I do that?
Auroratic said:
I've got it, but I have one problem: The colour on state_pressed should be the default system colour (this one: android.R.drawable.list_selector_background)
How can I do that?
Click to expand...
Click to collapse
I'm not sure what your asking, if you have read about stateListDrawabes and have understood it, also maybe even looked at examples, then you should know ? Open that resource, or even clone it and edit it how you want.
deanwray said:
I'm not sure what your asking, if you have read about stateListDrawabes and have understood it, also maybe even looked at examples, then you should know ? Open that resource, or even clone it and edit it how you want.
Click to expand...
Click to collapse
I have the following:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, new ColorDrawable(android.R.drawable.btn_default));
stateList.addState(new int[0], new ColorDrawable(Color.YELLOW));
textView.setBackgroundDrawable(stateList);
The background is yellow, but when I press on the textView, the background turns into transparent (it should be like this when i press:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
)
Auroratic said:
I have the following:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, new ColorDrawable(android.R.drawable.btn_default));
stateList.addState(new int[0], new ColorDrawable(Color.YELLOW));
textView.setBackgroundDrawable(stateList);
The background is yellow, but when I press on the textView, the background turns into transparent (it should be like this when i press:
)
Click to expand...
Click to collapse
well fyi you should look at the view type "states" as not sure you want android.R.attr.state_pressed ... neways you probably want state_focused and state_enabled for an editText view, but really google should provide you with simple absolute answers
hope that clears it up neways
deanwray said:
well fyi you should look at the view type "states" as not sure you want android.R.attr.state_pressed ... neways you probably want state_focused and state_enabled for an editText view, but really google should provide you with simple absolute answers
hope that clears it up neways
Click to expand...
Click to collapse
I have that now:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, context.getResources().getDrawable(android.R.drawable.list_selector_background));
but when I press on the TextView, the background goes orange
Auroratic said:
I have that now:
Code:
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
android.R.attr.state_pressed
}, context.getResources().getDrawable(android.R.drawable.list_selector_background));
but when I press on the TextView, the background goes orange
Click to expand...
Click to collapse
now your setting a state with a state "list" so not sure that will ever work.. needs to be a simple drawable

Categories

Resources