Currency Conversion - - Android Software Development

Im trying to create an currency conversion app but nothing happens when i click on the convert button. Here is the code i followed the tutorial.
package com.currencyconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
public class currencyconverter extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
Button convert;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
dollars = (TextView)this.findViewById(R.id.dollars);
euros = (TextView)this.findViewById(R.id.euros);
dtoe = (RadioButton)this.findViewById(R.id.dtoe);
dtoe.setChecked(true);
etod = (RadioButton)this.findViewById(R.id.etod);
convert = (Button)this.findViewById(R.id.Convert);
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
if (dtoe.isChecked()) {
convertDollarsToEuros();
}
if (etod.isChecked()) {
convertEurosToDollars();
}
}
// TODO Auto-generated method stub
protected void convertEurosToDollars(){
double val = Double.parseDouble(euros.getText().toString());
dollars.setText(Double.toString(val/0.67));
}
protected void convertDollarsToEuros(){
double val = Double.parseDouble(dollars.getText().toString());
euros.setText(Double.toString(val*0.67));
}
}

You dont handle clicks
Sent from my Acer Liquid using XDA App

In which part in my program I add /change or remove handle clicks. Thanks.
Sent from my GT-I9000 using XDA Premium App

I cant see it that good on my phone. But try to use the first overridden
Method instead of creating a new one
Sent from my Acer Liquid using XDA App

Which overriden referring to.
Sent from my GT-I9000 using XDA Premium App

The area that has the first "//TODO" is what gets executed when you click. Put the code there. Remove the second onClick stuff.

Try this code
Code:
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (dtoe.isChecked()) {
convertDollarsToEuros();
} else if (etod.isChecked()) {
convertEurosToDollars();
}
}
});

I'll try it after work. I'll let you know if it works. Thanks.
Sent from my GT-I9000 using XDA Premium App

Ok. That worked. But now two problems. One, if I enter nothing and press convert it force closes. Two, when it calculates the number keep screwing up. Example, pending on conversion, in one of the box fields I can see partial digits.
Sent from my GT-I9000 using XDA Premium App

You should implement a Check if something was entered of course
Sent from my Liquid using XDA App

Related

ImageView Scrolls But Disappears

Okay so after working hard I finally have scrolling somewhat working using the information from the accelerometer. I am using scrollBy() to make it move. But it scrolls I guess what you could say "in spot". It disappears when it goes outside its bounds. Here is my code:
Code:
package com.bobhoil.tdodger;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.ImageView;
import android.widget.TextView;
public class TestActivity extends Activity implements SensorEventListener {
private int accelXValue;
private int accelYValue;
private TextView accelZValue;
private ImageView mobil1;
private SensorManager sensorManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.test);
accelZValue = (TextView) findViewById(R.id.accel_z_value);
mobil1 = (ImageView) findViewById(R.id.mobil);
accelZValue.setText("0.00");
}
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelXValue = (int) sensorEvent.values[0];
accelYValue = (int) sensorEvent.values[1];
accelZValue.setText(Float.toString(sensorEvent.values[2]));
}
mobil1.scrollBy(accelXValue,accelYValue);
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
sensorManager.unregisterListener(this);
super.onStop();
}
}
Can I get some assistance please?
Okay so I tried using a few other ways of moving the imageview but it is still doing the same thing. Is it that I am using a absolute view??
I'd definitely recommend against using an AbsoluteLayout, but I'm guessing that you intend for the ImageView to move within another layout and instead you're telling the ImageView to move within itself. Basically, the scroll methods scroll the content of a view, so you would want to scroll a parent view if you're intending to move the ImageView.
So right now it isn't scrolling the ImageView? It is scrolling within the ImageView?
So now I need to figure out how to move a view in a view?
Just making sure I understood this.
Can I get some more help? I really need this for my game..

[NEWBIE] ProgressDialog Not Working properly

I am a newbie in android development. I was learning some of these android Dialog API stuff. But i got stuck with progressDialogs. When I run the app in the sdk.. The Dialog never ends. Where as, it should terminate after 5 seconds. I am unable to understand the problem. Please help me out. Thanks in advance...
SOURCE CODE:
Code:
package com.example.dolaog;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener, Runnable {
Button b, b2;
View a;
ProgressDialog d;
Thread t = new Thread();
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b.setOnClickListener(this);
b2.setOnClickListener(this);
}
[user=439709]@override[/user]
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
showDialog(0);
break;
case R.id.button2:
d = ProgressDialog.show(this, "Initialising", " Please wait...", true);
t.start();
break;
}
}
public void run() {
// TODO Auto-generated method stub
try {
t.sleep(5000);
d.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
[user=439709]@override[/user]
protected Dialog onCreateDialog(int g) {
switch (g) {
case 0:
Builder b = new AlertDialog.Builder(this);
b.setIcon(R.drawable.ic_launcher);
b.setTitle("From Crazyandroidgalaxy, please hit me !!");
b.setPositiveButton("asd", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
"I am Tanmay aka Crazyandroidgalaxy admin!",
Toast.LENGTH_LONG).show();
}
});
return b.create();
}
return null;
}
}
If you don't get a force close then it sounds like your thread never starts.
Code:
Thread t = new Thread();
Change that to just:
Code:
Thread t;
and in your OnCreate method:
Code:
t = new Thread();
The problem is that the run() method needs to be a method of the Thread. Use this:
Code:
Thread t = new Thread() {
public void run() {
// TODO Auto-generated method stub
try {
t.sleep(5000);
d.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
However, this WILL NOT work due to the fact that you can just change the UI from the UI Thread.
So change it to this:
Code:
package com.example.dolaog;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
[COLOR="Red"]import android.os.Handler;[/COLOR]
public class MainActivity extends Activity implements OnClickListener, Runnable {
Button b, b2;
View a;
ProgressDialog d;
[COLOR="Red"]Handler handler;
Thread t = new Thread() {
public void run() {
// TODO Auto-generated method stub
try {
sleep(5000);
handler.post(new Runnable() {
public void run() {
d.dismiss();
}
};
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};[/COLOR]
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
[COLOR="Red"] handler = new Handler();[/COLOR]
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b.setOnClickListener(this);
b2.setOnClickListener(this);
}
[user=439709]@override[/user]
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
showDialog(0);
break;
case R.id.button2:
d = ProgressDialog.show(this, "Initialising", " Please wait...", true);
t.start();
break;
}
}
[user=439709]@override[/user]
protected Dialog onCreateDialog(int g) {
switch (g) {
case 0:
Builder b = new AlertDialog.Builder(this);
b.setIcon(R.drawable.ic_launcher);
b.setTitle("From Crazyandroidgalaxy, please hit me !!");
b.setPositiveButton("asd", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
"I am Tanmay aka Crazyandroidgalaxy admin!",
Toast.LENGTH_LONG).show();
}
});
return b.create();
}
return null;
}
}
That should work.
Thank you so much!
Sent from my GT-S6102 using xda app-developers app
hiphop12ism said:
Thank you so much!
Sent from my GT-S6102 using xda app-developers app
Click to expand...
Click to collapse
You are welcome.
nikwen said:
You are welcome.
Click to expand...
Click to collapse
But can you, explain me the code modification. I am unfamiliar with the Handler class and post method...
Thanks :laugh:
hiphop12ism said:
But can you, explain me the code modification. I am unfamiliar with the Handler class and post method...
Thanks :laugh:
Click to expand...
Click to collapse
Yeah, the UI is created in one thread. That is what is called the "UI thread". The UI can just be modified from that UI thread.
If you want to modify the UI from another thread, you use handlers. If you pass a Runnable object to their run() method, that runnable will be executed in the UI thread. So you can modify the UI from that thread.
Better code would be using an AsyncTask. Basically, it is a wrapper class for Thread and Handler. (Additionally, there are some performance enhancement due to a thread pool.)
nikwen said:
Yeah, the UI is created in one thread. That is what is called the "UI thread". The UI can just be modified from that UI thread.
If you want to modify the UI from another thread, you use handlers. If you pass a Runnable object to their run() method, that runnable will be executed in the UI thread. So you can modify the UI from that thread.
Better code would be using an AsyncTask. Basically, it is a wrapper class for Thread and Handler. (Additionally, there are some performance enhancement due to a thread pool.)
Click to expand...
Click to collapse
Thank you so much.
But, the problem is... Whenever the 2nd button (b2) is clicked for the second time..
The app force closes..
hiphop12ism said:
Thank you so much.
But, the problem is... Whenever the 2nd button (b2) is clicked for the second time..
The app force closes..
Click to expand...
Click to collapse
That happens because the thread has already been started.
Check whether it is running and if it is not, start it. If it is, do nothing.
Use the isAlive() method for that.
Gotcha... Thanks!!
Sent from my GT-S6102 using xda app-developers app

Need help with my app

Hi I'm working on a custom Android launcher I'm running into a little problem I have an image button that indicates show all applications but the problem is I don't know the code syntax to show all installed applications when that button is clicked!!
Please help I tried googling for the last four hours and getting very frustrated
Thanks in advance
Rapsong11
Sent from my Nexus 4 using xda app-developers app
Maybe you should use PackageManager to query the installed applications for you.
For example, when you want the list of launchable activities. You can do like this:
Code:
// create a intent for the query below
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
// add the intent category you want, if need
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// get the instance of the package manager
PackageManager packageManager = context.getPackageManager();
// these are the activies you want
List<ResolveInfo> activities = packageManager.queryIntentActivities(mainIntent, 0);
Hope can help you
Code:
Here is my code:
package com.d4a.SchoolLauncher;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.content.Context;
import com.d4a.SchoolLauncher.R;
public class LauncherActivity extends Activity{
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//scroller
final HorizontalScrollView scroller= (HorizontalScrollView) findViewById(R.id.scroller);
scroller.setSmoothScrollingEnabled(true);
//apps
final ImageButton apps= (ImageButton) findViewById(R.id.all_apps); //my all apps button
apps.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mainIntent, 0);
}
});
final ImageButton wbrowser= (ImageButton) findViewById(R.id.wbrowser);
wbrowser.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
final ImageButton osettings= (ImageButton) findViewById(R.id.osettings);
osettings.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_SETTINGS));
}
});
//remove from home screen button
//apps
final ImageButton delapps= (ImageButton) findViewById(R.id.delapps);
delapps.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
apps.setVisibility(View.GONE);
delapps.setVisibility(View.GONE);
}
});
//web browser
final ImageButton delwbrowser= (ImageButton) findViewById(R.id.delwbrowser);
delwbrowser.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
wbrowser.setVisibility(View.GONE);
delwbrowser.setVisibility(View.GONE);
}
});
//web browser
final ImageButton delosettings= (ImageButton) findViewById(R.id.dellosettings);
delosettings.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
osettings.setVisibility(View.GONE);
delosettings.setVisibility(View.GONE);
}
});
//add button
final ImageButton addshortcut= (ImageButton) findViewById(R.id.addshortcut);
addshortcut.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
startActivity(new Intent("com.PhysicsPhantom.Launcher"));
}
});
//done button
final ImageButton done= (ImageButton) findViewById(R.id.done);
done.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
delwbrowser.setVisibility(View.GONE);
delosettings.setVisibility(View.GONE);
delapps.setVisibility(View.GONE);
done.setVisibility(View.GONE);
addshortcut.setVisibility(View.GONE);
}
});
//edit button
final ImageButton edit= (ImageButton) findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
if (apps.getVisibility()==View.VISIBLE){
delapps.setVisibility(View.VISIBLE);
}
if (wbrowser.getVisibility()==View.VISIBLE){
delwbrowser.setVisibility(View.VISIBLE);
}
if (osettings.getVisibility()==View.VISIBLE){
delosettings.setVisibility(View.VISIBLE);
}
done.setVisibility(View.VISIBLE);
addshortcut.setVisibility(View.VISIBLE);
}
});
//code to open editor for all delete buttons
//apps
apps.setOnLongClickListener(new View.OnLongClickListener() {
[user=439709]@override[/user]
public boolean onLongClick(View v) {
if (delapps.getVisibility()==View.GONE){
delapps.setVisibility(View.VISIBLE);
}
else{
delapps.setVisibility(View.GONE);
}
return true;
}
});
//web browser
wbrowser.setOnLongClickListener(new View.OnLongClickListener() {
[user=439709]@override[/user]
public boolean onLongClick(View v) {
if (delwbrowser.getVisibility()==View.GONE){
delwbrowser.setVisibility(View.VISIBLE);
}
else{
delwbrowser.setVisibility(View.GONE);
}
return true;
}
});
//settings
osettings.setOnLongClickListener(new View.OnLongClickListener() {
[user=439709]@override[/user]
public boolean onLongClick(View v) {
if (delosettings.getVisibility()==View.GONE){
delosettings.setVisibility(View.VISIBLE);
scroller.fullScroll(ScrollView.FOCUS_RIGHT);
}
else{
delosettings.setVisibility(View.GONE);
}
return true;
}
});
}
}
I get a error at: PackageManager packageManager = context.getPackageManager();
The error is as follows: context cannot be resolved
please help!!
[ code] tags would be helpful
out of ideas said:
[ code] tags would be helpful
Click to expand...
Click to collapse
just added code tags
sorry about that lol
You dont have startActivity in your launcher button
Can you please give me a code example fairly new to this
Sent from my Nexus 4 using xda app-developers app
Well, if you just want a activity list and launches when clicked, you can simply create a LauncherActivity.
Can you please show me using my code example where I would place that at? Thank you so much
Rapsong11
Sent from my Nexus 4 using xda app-developers app
rapsong11 said:
Can you please show me using my code example where I would place that at? Thank you so much
Rapsong11
Sent from my Nexus 4 using xda app-developers app
Click to expand...
Click to collapse
1. create new class MyLauncherActivity
2. override the method "getTargetIntent()" in LauncherActivity
Code:
...
import android.app.LauncherActivity;
public class MyLauncherActivity extends LauncherActivity {
 [user=439709]@override[/user]
protected Intent getTargetIntent () {
// just a example, you should replace the method yourself
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
}
3. replace the code inside your onClick method to the R.id.all_apps button :
Code:
Intent intent = new Intent(this, MyLauncherActivity.class);
startActivity(intent);
4. don't forget to declare MyLauncherActivity in your AndroidMenifest.xml
5. try to do it yourself with others' experience, but not just ask for the code, that's not good for a coder. Wish you can understand this.

ProgressBar not working

Hey, I was working on progress Bars. But I am having a little trouble. On the following code, The bar seems to work perfectly, but just when its done loading. The app crashes. Please help me out. Thanks in advance
Code:
package com.example.progresses;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
ProgressBar b;
int progressStatus = 0;
static int progress = 0;
Thread t;
Handler hand = new Handler();
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (ProgressBar) findViewById(R.id.progressBar1);
b.setMax(200);
new Thread(new Runnable() {
public void run() {
while (progressStatus <= 200) {
progressStatus = doWork();
hand.post(new Runnable() {
public void run() {
b.setProgress(progressStatus);
}
});
}
b.setVisibility(View.GONE);
}
}).start();
}
private int doWork() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ++progress;
}
}
LOGCAT
You have to do this via the handler, too:
Code:
b.setVisibility(View.GONE);
Every action regarding the UI from another thread has to be done via an handler.
nikwen said:
You have to do this via the handler, too:
Code:
b.setVisibility(View.GONE);
Every action regarding the UI from another thread has to be done via an handler.
Click to expand...
Click to collapse
Thank you!
hiphop12ism said:
Thanks you!
Click to expand...
Click to collapse
Welcome.

[Q]need help to make an xposed module

I am trying to make an xposed module(private use) to implement clear all button(ImageView) in recents.
Here is my code
Code:
package com.mycompany.myapp3;
import android.graphics.*;
import android.view.*;
import android.widget.*;
import de.robv.android.xposed.*;
import de.*;
import de.robv.android.xposed.callbacks.*;
import de.robv.android.xposed.callbacks.XC_LoadPackage.*;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class MainActivity implements IXposedHookLoadPackage
{ImageView m;
String rc="com.android.systemui.recent.RecentsPanelView";
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable
{
findAndHookMethod(rc, lpparam.classLoader, "updateClock", new XC_MethodHook() {
@Override
protected void afterHookedMethod(final MethodHookParam param) throws Throwable
{
m.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
ViewGroup mRecentsContainer = (ViewGroup) XposedHelpers.getObjectField(
param.thisObject, "mRecentsContainer");
// passing null parameter in this case is our action flag to remove all views
mRecentsContainer.removeViewInLayout(null);
}
});
}
});
}}
Now the problem is that i have already that ImageView in recent_panel.xml with no action assigned.i know its id and want to implement onclick action to that ImageView only by using its id.so what code should i add so that code recognises that ImageView and assigns it the above onclick action
Sent from my GT-S5570 using XDA Premium 4 mobile app
It's only an idea and maybe it doesn't solve your problem: Have you tried to hook onCreate and use the this-Object to call findViewById?
And you are making a mistake: You have to look, whether the loaded package is your specific package you want to hook. You try to hook every app.
Regards
EmptinessFiller said:
It's only an idea and maybe it doesn't solve your problem: Have you tried to hook onCreate and use the this-Object to call findViewById?
And you are making a mistake: You have to look, whether the loaded package is your specific package you want to hook. You try to hook every app.
Regards
Click to expand...
Click to collapse
1. this-object.?Can you give an example?
2.yes you are right i will fix it
Sent from my GT-S5570 using XDA Premium 4 mobile app
Sth. like m = (ImageView) ((Activity) param.thisObject).findViewById(id);
(Not tested)
EmptinessFiller said:
Sth. like m = (ImageView) ((Activity) param.thisObject).findViewById(id);
(Not tested)
Click to expand...
Click to collapse
I have got the solution already.thanks for your help
Sent from my GT-S5570 using XDA Premium 4 mobile app

Categories

Resources