[Q] how to show focus on clickable image? - Android Software Development

I have created a custon UI menu, and for backwards compatabilty to non-touchscreen devices (such as google TV) I want the user to be able to visually see that the image is selected (I have tried with my desires trackball and it works but just looks the same)
How would i get my app to show the different image on focus and switch back when off focus?
So far i have tried:
Code:
@Override
public void onFocusChange(View arg0, boolean arg1) {
ImageView imageView = (ImageView) findViewById(R.id.tl);
Drawable newFocusImage;
newFocusImage = getResources().getDrawable(R.drawable.menu_top_left_trans_ic_fcs);
imageView.setImageDrawable(newFocusImage);
}
However this somehow changes it to a completely different image than the new drawable specified, and does not revert on un-focus.
All help would be greatly appreciated
Thanks

Rather than draw a new image, why not say change the alpha channel values to make it appear lighter or darker? Or overlay it with a solid color image with a low alpha value?

The alpha change is a good idea. As a photographer i would say adding a transparent vignette would look great. If you don't know what a vignette is, its basically darkening the edges of an image / a circular gradient (dark edges, light middle)
From something awesome

ye that sounds cool
however my main problem still lies in the coding of the problem, do you know how to code a vignette? is it possible to change a image like that in java?

Instead of using getDrawable and setImageDrawable use setImageResource
Create two images:
menu_top_left_trans_ic_fcs_normal - to be shown when not focused
menu_top_left_trans_ic_fcs_focused - to be shown when focused
Then add to res/drawable a new file named menu_top_left_trans_ic_fcs.xml with the following content:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/menu_top_left_trans_ic_fcs_focused" android:state_focused="true" />
<item android:drawable="@drawable/menu_top_left_trans_ic_fcs_normal" />
</selector>
Note: You can also set android:state_pressed
Use in your code
Code:
imageView.setImageResource(R.drawable.menu_top_left_trans_ic_fcs);
I never tested this exact code but it should work.

thanks it worked !!

Related

[Q] ScrollView, SetText and scrollTo

Hi,
Im just getting into Android development and picking up pace slowly. Getting confused how people are getting settings style menus with the appropriate colour Summary Line while adding icons for Stars and Checkboxes etc to highlight and tick.
Anyhoo, I have a TextViews inside a LinearLayout inside a ScrollView, seems to work OK. I use SetText to set the Text on the TextViews and I create everything at run time instead of through the XML files.
But the application I have is a polling type application and I need to repopulate the TextViews and redraw the ScrollView in the same position that the user scrolled to previously.
Im accutely aware of problems using ScrollTo after you've used SetText, so I found the following code:
Code:
sView.post(new Runnable() {
public void run() {
sView.scrollTo(0, iTop);
}
});
It works, but there's a screen flicker where it first draws the screen in its normal position then sets the scrollTo.
What is the best way of achieving this, or am I doing everything completely backwards and most normal people wouldn't have this kind of problem?
Thanks
Simon
It sounds more like you might want to consider using a listview instead. It was designed to contain several child views and scrolls and updates seamlessly.
Hi,
Thanks for the reply. Perhaps I am a bit too much of a beginner.
I seem to be favouring TableLayout so that I can display tabular data and have columns resize correctly and have the data presented OK.
I think that's why I went with updating everything at design time because I have a variable number of rows that can be added. IIUC, variable rows (in an XML file) I think can only be created using ListView, but of course a TableRow needs a TableLayout as a parent.
Here is the idea...
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:paddingRight="5px" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
The code I have for TableRows is not dissimilar to, http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout
But of course using setText means I cannot then scrollTo
Duh.
First learning curve mastered.
You only need to add views to containers etc the first time you display the screen.
You can update the text in the views later on without affecting the screen position.
I like this.

[Q] Simple math, what am I missing?

Hi people!
I'm trying to learn how to develop android apps. I'm not trying to become a developer but for fun and learn this. So I go step by step.In the past I just have very basic visual basic and fortran experience...I don't know java lang. But while searching I found out that android is based on java. so I grabbed netbeans and jdk. and it was easy and understandable to do some basic math(with tutorials) with it, like + - * /
So I thought I'd give a try to appinventor. Ok easy enough.Done basic math. But I want to learn the code itself.
So I'm dealing with eclipse right know. I learned some basic stuff which you can not learn with appinventor.
But when it comes to basic math, I'm stuck!
Let me explain:
with appinventor it's easy to make this (pics are at the end of the post)
when I try to make this with eclicpe...I'm going nuts. I couldn't find any tutor.
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Lorem Ipsum</string>
<string name="input"></string>
<string name="butcel">Button</string>
<string name="result">result</string>
<string name="ClickHandler">ClickHandler</string>
</resources>
Click to expand...
Click to collapse
Forum does not let me enter my main.xml and main.java codes. so I atteched the pics and txt files below.
this gives me a "app has stopped unexpectedly" error
what I'm trying to do is
1)user inputs a number
2)clicks on a button (there will be several buttons in my app,this is just a test app)
3)application does some math with that number
ie. ((number*5)/2)
4)textView1 shows the result
Should it be simple like in appinventor or am I missing sth in my codes?
And I'm not sure but do I need to add a java math function or sth like that in my app to do simple math like this?
Any advice is appreciated.
Thanks in advance.
The first thing to check is your AndroidManifest.xml file. The Eclipse ADT plugin should create this file and include the main activity for your app automatically if it has been installed. However, an app will throw the error you are receiving if an activity is run that isn't listed in the manifest and it's very common(at least for me) to forget at first to add a newly created activity to the manifest. Below is an example of an AndroidManifest.xml file from the Hello, Android tutorial.
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Hi there desisamay,
to be honest i did not really look and your code to try and find the error, but i did write you an example app you can expand or use to learn .
Attached to my post is the example app and my whole eclipse project directory of it.
The app has an edittext to input a number, a button to start the calculation x*2/5 and a textview to show the result.
I hope you enjoy it and if anything is still unclear, just ask!
when the activity starts you try to get the text of the edittext and convert it to string which is null since the edittext is empty at the beginning. You should get the input when you click the button (and maybe even check if the edittext is empty)
Dark3n said:
Hi there desisamay,
to be honest i did not really look and your code to try and find the error, but i did write you an example app you can expand or use to learn .
Attached to my post is the example app and my whole eclipse project directory of it.
The app has an edittext to input a number, a button to start the calculation x*2/5 and a textview to show the result.
I hope you enjoy it and if anything is still unclear, just ask!
Click to expand...
Click to collapse
Thank you very much! This was exactly what I was trying to do! I was trying to do this with onClickListener and defining values... But your lines are clear and understandable and now I know how to make definitions and basic math functions. I think there'd be more lines to write with onClickListener. But this way is simple and works like a charm. Thank you again.
Also I've added
android:inputType="numberDecimal|numberSigned"
Click to expand...
Click to collapse
to EditText in main.xml just to be sure the user inputs numbers only. maybe just "number" will do the same.
But one more question:
we write the result to output like this
output.setText("The result of " + mInput + " times two, divided by 5 is " + mOutput);
do we have to use some text in it? if I try just
output.setText(mOutput);
eclipse warns me
The method setText(CharSequence) in the type TextView is not applicable for the arguments (double)
Click to expand...
Click to collapse
and underlines setText with red. but we 've already defined mOutput before like double mOutput = (mInput*2/5);
@MongooseHelix
My AndroidManifest.xml was the same as you posted. Since I only have one action. Thanks.
@nemoc 23
I was trying to that. get the input when button clicked. Convert to string of course and do some math. But I couldn't.(now I can ) But you're right for checking the edittext field after clicking the button. I'm pretty sure if you click the button with an empty edittext area, app will give an error.(i tried and it did)
When writing my little app I'm planning to add this and warn the user with a toast text.
output.setText( mOutput.toString() );

[Q] Making an alternatice UI menu (like for example: the camera menu)

I am trying to create a more appealing menu for my application, however I am unsure as how to re-root the input of the menu button so that it makes certain images in the corners visible, (they are all in the same xml file, however just set to "invisible" by default. If also possible I'd like to activate this menu by just touching the screen, does anyone have any advice on how to do this?
If you need any more information please just ask
Thank you
You'll need to capture the menu button click. This might help:
http://stackoverflow.com/questions/4239880/detecting-physical-menu-key-press-in-android
For touching the screen you'll need an onTouch event for the view.
Sent from my HTC Desire using XDA App
thanks
but i am still unsure as how to identify the xml object and then change it's visibility in java
image:
Code:
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menu_bottom_left"
android:id="@+id/bl"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:visibility="invisible"></ImageView>
using the physical keycode:
Code:
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_MENU)
{
}
return true;
}
sorry but i am not that familiar with java
OK.
// Create an ImageView and assign it to your bl ImageView
Imageview bl= (ImageView)findViewById(R.id.bl);
// Change the visibility (VISIBLE = Visible, INVISIBLE = Invisible but takes up the same space in layout
// GONE = Invisible and does not take up space in layout)
bl.setVisibility(ImageView.VISIBLE);
Hope this helps

Show spinner on left when ActionMode is active

I have a gridview supporting multiple selection, when user selects one item the action bar shows the classic AcitonMode and the menu items as defined in layout/menu files, every works fine.
Now I want to add a spinner and show it on the left side (like Gallery does) but defining it on the menu.xml it is positioned near other menu items (on right), so how can I create a spinner on left side and menu items on right side like on Gallery app??
I've searched on Gallery source code but it is really complicated
My current menu definition is show below
Code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_selection_spinner"
android:actionViewClass="android.widget.Spinner"
android:showAsAction="always"/>
<item
android:id="@+id/action_edit"
android:showAsAction="always"
android:title="@string/edit_title"/>
</menu>
My onPrepareActionMode method
Code:
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Spinner selectionSpinner = (Spinner) menu.findItem(
R.id.item_selection_spinner).getActionView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar()
.getThemedContext(), R.layout.path_spinner_dropdown_item);
selectionSpinner.setAdapter(adapter);
adapter.addAll("One item selected", "Select All");
return true;
}
I'm not sure, but I think you have to use a custom layout instead of your menu-xml file.
Regards
EmptinessFiller said:
I'm not sure, but I think you have to use a custom layout instead of your menu-xml file.
Regards
Click to expand...
Click to collapse
Yes, using setCustomView works fine, thanks

Drawable Help..

Hey all -
I'm getting into an area I hadn't gone before - drawable XML files, and am stumped, since what I think should work is giving me an exception.
Some background -
I have multiple images, used as a background. Then there are a couple other images used for foreground symbols. (These are for markers on a map.) So, for example, I have 5 different colors, and 5 symbols. Instead of creating 30 different markers (5 symbols + blank) I figured I could include the resources, and then use a list-layer to show the symbol on top of the marker. Would save some room when the APK's created.
So, I made a simple test, like this. XML file is simply named "marker":
Code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="34dp"
android:drawable="@drawable/marker_green_blank"
android:left="0dp"
android:right="20dp"
android:top="0dp"/>
<item
android:bottom="20dp"
android:drawable="@drawable/marker_symbol_int"
android:left="3dp"
android:right="19dp"
android:top="4dp"/>
</layer-list>
Oddly, when I go to use this, I get an exception when it's tried to be used:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Any thoughts? I obviously missed something - probably something idiotic, since I do that a lot.
Or, is there another way to combine two images/res's together?
--Mike
Edit:
I forgot to add - this is how I'm creating the 'icon' to add it to the map. The second line is map-specific code and may not really apply here. I'm starting to think that the first line - BitmapDescriptor - is causing the issue:
Code:
BitmapDescriptor marker_red_blank = BitmapDescriptorFactory
.fromResource(R.drawable.marker);
<!-- Code that sets other options using a MarkerOptions object ... -->
opt.icon(marker_red_blank);
mark.associatedMapMarker = MapViewFragment.this.mMap.addMarker(opt);
coyttl said:
Hey all -
I'm getting into an area I hadn't gone before - drawable XML files, and am stumped, since what I think should work is giving me an exception.
Some background -
I have multiple images, used as a background. Then there are a couple other images used for foreground symbols. (These are for markers on a map.) So, for example, I have 5 different colors, and 5 symbols. Instead of creating 30 different markers (5 symbols + blank) I figured I could include the resources, and then use a list-layer to show the symbol on top of the marker. Would save some room when the APK's created.
So, I made a simple test, like this. XML file is simply named "marker":
Code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="34dp"
android:drawable="@drawable/marker_green_blank"
android:left="0dp"
android:right="20dp"
android:top="0dp"/>
<item
android:bottom="20dp"
android:drawable="@drawable/marker_symbol_int"
android:left="3dp"
android:right="19dp"
android:top="4dp"/>
</layer-list>
Oddly, when I go to use this, I get an exception when it's tried to be used:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Any thoughts? I obviously missed something - probably something idiotic, since I do that a lot.
Or, is there another way to combine two images/res's together?
--Mike
Edit:
I forgot to add - this is how I'm creating the 'icon' to add it to the map. The second line is map-specific code and may not really apply here. I'm starting to think that the first line - BitmapDescriptor - is causing the issue:
Code:
BitmapDescriptor marker_red_blank = BitmapDescriptorFactory
.fromResource(R.drawable.marker);
<!-- Code that sets other options using a MarkerOptions object ... -->
opt.icon(marker_red_blank);
mark.associatedMapMarker = MapViewFragment.this.mMap.addMarker(opt);
Click to expand...
Click to collapse
Although I haven't used BitmapDescriptors, I do use layer-list a fair bit to overlay shapes, backgrounds etc.
Looking at the documentation for fromResource...
'Creates a BitmapDescriptor using the resource id of an image.'
It seems that you're not passing it an image, you're passing it a drawable (layer-list in this case).
PicomatStudios said:
Although I haven't used BitmapDescriptors, I do use layer-list a fair bit to overlay shapes, backgrounds etc.
Looking at the documentation for fromResource...
'Creates a BitmapDescriptor using the resource id of an image.'
It seems that you're not passing it an image, you're passing it a drawable (layer-list in this case).
Click to expand...
Click to collapse
Thanks, I'll look into that. I had thought I tried to create a bitmap out of it, but it still gave the error - but since my memory is flaky, I'll do it and see what I get.
Cheers-
Mike
Drawable Button XML resource selector
This technique could be used for animation of XML drawables.
Keep separate drawables in your res/layout folders as usual.
switchable_drawables.xml ( put this in res/layout too)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/active_button"
/>
<!-- Button Focused Pressed-->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/active_button"
/>
<!-- Button Pressed-->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/active_button"
/>
<!-- Button Default Image-->
<item android:drawable="@drawable/inactive_button"/>
</selector>
coyttl said:
Thanks, I'll look into that. I had thought I tried to create a bitmap out of it, but it still gave the error - but since my memory is flaky, I'll do it and see what I get.
Cheers-
Mike
Click to expand...
Click to collapse
Hey guys -
Thanks for the info. Unfortunately, same error. The code now looks like:
Code:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferQualityOverSpeed = true;
opts.outHeight = 34;
opts.outWidth = 20;
Bitmap icon = BitmapFactory.decodeResource(MapViewFragment.this.getResources(),
R.drawable.marker, opts);
BitmapDescriptor marker_green_blank = BitmapDescriptorFactory.fromBitmap(icon);
Creating options to generate the bitmap, setting desired height and width, and then using the decodeRecource function, creating a Bitmap, then using the .fromBitmap to turn it into a Descriptor. When the GMaps tries to access it, same null error. I may post this over on StackOverflow, since looking at the docuemtation and other examples, this should work.

Categories

Resources