[Q] Set BackgroundColor but keep onTouch - Java for Android App Development

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

Related

Adding custom Background Wallpaper

Guys i am trying to add Background Wallpaper on Home screen! No luck atm..
Anyone want's to suggest anything?????
My idea is very basic and easy though, it's like adding a Desktop wallpaper on you Windows XP/Vista/7 via the registry method!
which basically is string -> c:\documents\blablabla\customimage.jpg
On windows phone more probably would be like this
I got the tweak base that you make the black background to silver and added this path but nothing!
[HKEY_LOCAL_MACHINE\ControlPanel\Themes\1]
"ChromeColor" value= documents\blablabla\customimage.jpg
What is the value though???? String?? value?? dword maybe???
I will try to create one on PC and deploy it to my phone and see...hmmm!!!
Tell me your thoughts!
*snip* MY REGISTRY EDITOR DOESN'T OPEN ANYMORE....LESSON..DON'T MESS WITH THE REGISTRY! LOL! WILL TRY TO INSTALL IT AGAIN WHEN I GO HOME!
MOD EDIT: Profanity removed...please mind the language
Anyone for a response??? I am trying to add a Background Wallpaper here!!!! Gimme some thoughts you guys!
If you go to themes you will find your answer here:http://forum.xda-developers.com/showthread.php?t=919742
FlowerPotmen said:
If you go to themes you will find your answer here:http://forum.xda-developers.com/showthread.php?t=919742
Click to expand...
Click to collapse
Those are Boot animations...I want to make Background Wallpapers for the HOME SCREEN!!!!
arkatis said:
Guys i am trying to add Background Wallpaper on Home screen! No luck atm..
Anyone want's to suggest anything?????
My idea is very basic and easy though, it's like adding a Desktop wallpaper on you Windows XP/Vista/7 via the registry method!
which basically is string -> c:\documents\blablabla\customimage.jpg
On windows phone more probably would be like this
I got the tweak base that you make the black background to silver and added this path but nothing!
[HKEY_LOCAL_MACHINE\ControlPanel\Themes\1]
"ChromeColor" value= documents\blablabla\customimage.jpg
What is the value though???? String?? value?? dword maybe???
I will try to create one on PC and deploy it to my phone and see...hmmm!!!
Tell me your thoughts!
*snip* MY REGISTRY EDITOR DOESN'T OPEN ANYMORE....LESSON..DON'T MESS WITH THE REGISTRY! LOL! WILL TRY TO INSTALL IT AGAIN WHEN I GO HOME!
MOD EDIT: Profanity removed...please mind the language
Click to expand...
Click to collapse
Reason why it doesn't work is cause it's most likely relocked itself. Unlock it again and it should work....
It's covered in tiles, what would be the point?
sulphuricaciduk said:
It's covered in tiles, what would be the point?
Click to expand...
Click to collapse
The point is tha MS disables Background wallpapers and i want to enable them because i want to and it's nice i guess!
".....We’re not so sure on this one. Some of the text labels are different from what we saw at the MWC unveiling, and Microsoft seemed to indicate that background wallpapers, as seen here, would probably not be allowed...."
http://www.techautos.com/tag/hd2/
{
"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"
}
it would be nice, any suggestion?

[Q] Transparent ImageButton on AppWidget

I just added an ImageButton to my AppWidget and there seems to be a problem. i made them as png's with transparent backgrounds and made a selector xml file like this:
ic_call_contact.xml
Code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/ic_call_contact_pressed"/>
<item android:drawable="@drawable/ic_call_contact_unpressed"/>
</selector>
and then added it to my widget like this:
Code:
<ImageButton android:id="@+id/call_contact"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_call_contact"/>
the images are as follows:
{
"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"
}
and
but when it renders on the desktop there is a grey box surounding the imagebutton where there should be transparency of the png. any thoughts?
I checked both images under gimp with a fine tooth comb and could not find any grey borders, only transparency. Perhaps could be the ImageButton rather than the use of ImageView... you could add a setOnTouchListener for an ImageView. Another thought is to check and see if you are overriding the theme. Outside of that, I'm a little green with Android widgets.
spr0k3t said:
I checked both images under gimp with a fine tooth comb and could not find any grey borders, only transparency. Perhaps could be the ImageButton rather than the use of ImageView... you could add a setOnTouchListener for an ImageView. Another thought is to check and see if you are overriding the theme. Outside of that, I'm a little green with Android widgets.
Click to expand...
Click to collapse
thats not a bad idea to just use an ImageView... it'll just be more work to get the button to change when pressed. ill give it a go and check back.
thanks
Found the ideal solution. You have to define your own background (android:background="") or set the transparency of the button. That way you don't have to use the ImageView. Verified it on this end.
spr0k3t said:
Found the ideal solution. You have to define your own background (android:background="") or set the transparency of the button. That way you don't have to use the ImageView. Verified it on this end.
Click to expand...
Click to collapse
the problem with the android:background is that this is going to be on the homescreen where any sort of wallpaper can be up. so i wanted to just have the button float with a bit of transparency around it. and you cant have the android:background be set to ""
i did however mess with some something in the adjacent TextView to the ImageButton and it got rid of the grey box.... but when resizing the widget to a larger size the grey box expands to a random size. not the entire gap from the ImageButton to the edge of the widget but just a bit.
ill have to get around to trying the ImageView in the morn
----
it definitly is an issue with ImageButton and AppWidgets. i just replaced the ImageButton with an ImageView with the same src images and it displays just fine
----
ill be able to make the ImageView act like an ImageButton with setOnTouchListener() and then defining the ACTION_UP and ACTION_DOWN for the OnTouchListener's MotionEvent. this seems a bit much but if it works and displays properly then ill be happy
Yeah, I'm finding widgets are fickle like that. May have to make a layout scheme for various portrait and landscape sizing to handle the awkward changes.
spr0k3t said:
Yeah, I'm finding widgets are fickle like that. May have to make a layout scheme for various portrait and landscape sizing to handle the awkward changes.
Click to expand...
Click to collapse
i always forget this when i work on widgets but i cant access the Views directly like a normal Activity. it is all done through RemoteViews and is thus limited to setting values of already existing Views. So basically i cant get a reference to the ImageView to implement the setOnTouchListener()... bleh.
but you did give me an idea. i have to abandon the whole nested linear layout i am using and use a relative layout. that way hopefully things wont get messed around when the content changes and the widget is resized

[Q] Auto-dismissing text popup

As stated in the subject, I'd like to implement in my project that little auto-dismissing text popup that sometimes appears when applying an action in certain apps.
It's a simple floating text with a dark rectangle as background positioned in the bottom part of the app that remains between activities/application switches and goes away automatically after X seconds.
Does anybody know how it's called?
It was called Toast.
You can easily custom a toast to have a interface like whatever you want, look Titanium Backup for example.
Default usage like below:
Code:
Toast.makeText(mContext, "Hello", Toast.LENGTH_LONG).show();
As a side note, the App-Msg(Crouton) library provides a good alternative to toasts : in that it is context-centric, the message will only be displayed on the activity/fragment it was generated from, whereas if the user quits your app while a toast is displayed, for example, the toast will be displayed on his launcher or on he app he switched to, which could be confusing.
Its usage is also very simple :
Code:
AppMsg.makeText(/*Activity*/, /*CharSequence*/, /*AppMsg.Style*/).show();
Maybe this library can be helpful on my app. I have a custom toast, which still displayed after I choose to leave the activity via a button. So with this library the toast dissapear when the activity goes to background? And also can customize the look of toast?
dancer_69 said:
Maybe this library can be helpful on my app. I have a custom toast, which still displayed after I choose to leave the activity via a button. So with this library the toast dissapear when the activity goes to background? And also can customize the look of toast?
Click to expand...
Click to collapse
Yeah exactly.
And yes, you can modify the appearance of the toast to a certain extent (background color, gravity and display length afaik) using the static methods already setup in the library, but nothing prevents you from forking the library and modifying it to suit your needs.
You can also choose to have a toast which will only be dismissed when the user clicks it.
There are 3 types of preset styles for the croutons (from left to right in the image below) :
AppMsg.STYLE_ALERT (red) AppMsg.STYLE_CONFIRM (yellow) AppMsg.STYLE_INFO (green)
{
"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"
}
But you can create your own using something like this :
Changing length & color :
Code:
AppMsg.Style style = new AppMsg.Style(AppMsg.LENGTH_SHORT, R.color.holo_blue); [COLOR="SeaGreen"]// the default length is [I]AppMsg.LENGTH_LONG[/COLOR][/I]
colors.xml :
Code:
<resources>
<color name="holo_blue">#33B5E5</color>
</resources>
Changing gravity :
Code:
appMsg.setLayoutGravity(Gravity.BOTTOM); [COLOR="SeaGreen"] // the crouton is displayed at the top by default[/COLOR]
Result in the thumbnail
Thanks to Evgeny Shishkin for his work on this library by the way.

[Library] Simple GridLayout

Often we find ourselves messed up with simple things such as layouts.
So I made this library modifying AOSP code to help put items in a Grid Layout.
You can add this as a library or put it separately into your package since it consists of just a single Java file.
Its pretty simple and extremely easy to use. Just put the items in xml between the following tags:
Code:
<tk.eatheat.gridlayout.GridLayout>
....
</tk.eatheat.gridlayout.GridLayout>
By default, it creates a 3x3 grid. To change it just set the number of rows and columns in these lines:
Code:
static private final int COLUMNS = 3;
static private final int ROWS = 3;
Here's a sample app made with the library to show the result.
{
"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"
}
Click to expand...
Click to collapse
Sample app can be found here.
Library can be found here on my Github here.
-------------------------------
Featured on Portal:
http://www.xda-developers.com/android/simple-gridlayout/
Hey, cool library.
Thanks. :good:
I recently started hobby android app development and this was the thing that always bothered me the most !
Thanks a ton ! this saves me a lot of effort and simplifies everything
sheennick123456 said:
I recently started hobby android app development and this was the thing that always bothered me the most !
Thanks a ton ! this saves me a lot of effort and simplifies everything
Click to expand...
Click to collapse
You are welcome. Glad to be of help.
EatHeat said:
You are welcome. Glad to be of help.
Click to expand...
Click to collapse
Could you add activity starter to buttons?
Sent from my GT-S5570I using xda app-developers app
@m4RinKo2
Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(newComponentName("com.package.app","com.package.app.ExampleActivity"));
startActivity(intent);
Set intent on the buttons with the correct package and activity names.
Thanks for your hard-work.
This library will be surely somewhere used by me.
Sent from my GT-I9300 using XDA Premium 4 mobile app
Nice lib brother I have a question before importing it to my project how can we add Item click listener so that if i have a list of images in grid i can select one image from that silly question plz help @EatHeat

How to show a map in an activity?

How would I show a map in an activity with a customized marker that shows the current location of the device?
If you're planning to use Google Maps, it's pretty easy: https://google-developers.appspot.com/maps/documentation/android/
And for the device location: http://developer.android.com/training/location/retrieve-current.html
Ianlmt said:
How would I show a map in an activity with a customized marker that shows the current location of the device?
Click to expand...
Click to collapse
Look into using a MapFragment.
Stick one in your layout;
Code:
<fragment
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/myMap"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then in the onCreate of your activity, get a handle to the fragment and then get the map as a GoogleMap object from there;
Code:
final MapFragment mapFragment = ((MapFragment)getFragmentManager().findFragmentById(R.id.myMap));
final GoogleMap googleMap = mapFragment.getMap();
When you have the GoogleMap instance, it's easy to set positions and markers;
Code:
final LatLng position = new LatLng(100, 100);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoomposition, 10));
map.addMarker(new MarkerOptions().title("Some place of interest").position(position));
Hope this helps.
Thank you so much! Going to play with it later today and see what I can produce!
Ianlmt said:
Thank you so much! Going to play with it later today and see what I can produce!
Click to expand...
Click to collapse
Cool, remember that you need to get you API key's sorted to get it to work.
bornander said:
Cool, remember that you need to get you API key's sorted to get it to work.
Click to expand...
Click to collapse
How do I do that (sorry so new to this whole thing)... Do you mean I need to apply for an API key?
Ok I have attached what I worked out in a zipped project file below this post, what it produces is this:
{
"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"
}
Close, but no actual map is displayed... where did I go wrong?
Thanks for all the help
never-mind it appears I have fixed it ... what I did;
was missing a bunch of code, and the keystone had to match the one that was given to google api, I also had to enable the api from google develper control panel.
Seems to be working now! I am going to keep on tinkering with it, but this is the first map activity I have created... yaaaay

Categories

Resources