Webview app - Android Software Development

anyone got some decent code for webview app? I want to put my site into an app the site already has a mobile view so just need to put it in a web view app. I did play with some code but just can't get it quite right. I would like it to not have a loading bar and be able to use back button to move back through web pages rather than hitting back and being tacken out of app, would be greatfull for any help.
Sent from my HTC Magic using XDA App

Would be great if anyone could help with this.
Sent from my HTC Magic using XDA App

Hey,
the official documentation has a Hello WebView section which basically
does all the things you want except for hiding the progressbar:
developer.android.com/guide/tutorials/views/hello-webview.html
(sry cant post links as a new user)
You may want to read and work through this.
The progressbar can be hidden by calling
Activity.setProgressBarVisibility(false)
for example inside onCreate().
Greets

Thanks I did have a go using the codes there but kept messing it up or getting confussed when trying to put all the bits tog just wondered if the code was out there all tog ready for me to use. Lazy I know but hey you don't ask you don't get.
Sent from my HTC Magic using XDA App

Alright, I dont do this usually, but since I wanted to open a blog for Android since a while (mainly for self-documentation how to do things in code), and needed a first post, I wrote a small template and a description how to use it.
Check it out at
alexsbraindump.blogspot.com/2010/10/simple-webview-app-template.html
You can also download the app code there.
Greets

thanks for that although i can not get the progress bar to show.

Hey,
mh yeah I see. You're right. It works on my HTC Desire w/ sense,
but it doesnt work on the emulator with various Android versions.
I couldn't figure out yet why, but I'll look into that a bit more later.
Greets

Thanks
Sent from my HTC Magic using XDA App

If you are still looking for another tutorial you may want to take a look here:
Building Android Apps with HTML, CSS, and JavaScrip
It starts with the very, very basic HTML and works its way on building a webapp to even converting it into an .apk

Web App
PhoneGap is an open source development framework for building cross-platform mobile apps.

Ok can someone tell what what the heck i am doing wrong, i ma just getting force close all the time.
Here is my code:
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.luvdroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".luvdroid"
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>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Java
package com.luvdroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class luvdroid extends Activity
{
final Activity activity = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://developer.android.com");
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<webView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Anybody know why I get force close?
Sent from my Desire HD using XDA App

Related

Developing Game

I have decided to recreate a game I was making on the PC for Google Android but I have run into some snags. Currently I have the menu partially created but am not exactly sure how the views and such work. So I have uploaded my source and was hoping someone could look at my source and help me. I have menu created through xml and have ninepatchdrawable button with a background set for the menu. What I am trying to figure out is clicking start and going to another view.
http://www.bobhoil.com/wp-content/uploads/2010/08/T-Dodger.zip
You can also visit my blog here: http://bobhoil.com/
Thanks! I hope I can get some assistance
I am also having some trouble installing it to my att Samsung Captivate. I tried using a program I have been using to install from the pc but it says wrong SDk version even though it is the same one I have been using.
Please Help?
look at this: http://dl.dropbox.com/u/2817943/T Dodger.rar
i have added an additional layout and an acitivity-class to your project. and i called that activity by clicking you start button!
i hope that helps you
Thanks that helps a ton! That was the main issue I was having. Now I can start connecting parts of my game together. I appreciate it a ton.
Also just a tip, root your captivated and turn on USB debugging. That will allow you to run your program right to your phone, rather than the slow emulator.
Sent from my HTC HD2 using XDA App
Okay that sounds good. One thing though that voids your warrenty and I bought an extended warrenty from At&t. So would it void my warrenty?
Also I am making some progress. I have the code laid out for detecting the accelerometer! Next I just need to use that data and apply it to the image that I will be moving around the screen.
It will not void warrenty as it is software based. Sounds good. Hope to see a beta soon.
Okay sounds great then. I will start working on that then.
I have run into another little problem. Now I have rooted my phone which makes testing a LOT easier! (Thanks Metastable!)
The following code seems to cause it to crash:
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.TextView;
public class TestActivity extends Activity implements SensorEventListener {
private TextView accelXValue;
private TextView accelYValue;
private TextView accelZValue;
private SensorManager sensorManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.test);
accelXValue = (TextView) findViewById(R.id.accel_x_value);
accelYValue = (TextView) findViewById(R.id.accel_y_value);
accelZValue = (TextView) findViewById(R.id.accel_z_value);
accelXValue.setText("0.00");
accelYValue.setText("0.00");
accelZValue.setText("0.00");
}
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelXValue.setText(Float.toString(sensorEvent.values[0]));
accelYValue.setText(Float.toString(sensorEvent.values[1]));
accelZValue.setText(Float.toString(sensorEvent.values[2])); }
}
}
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();
}
}
Click to expand...
Click to collapse
This is supposed to capture movement through the accelerometer.
please zip the eclipse project and tell us the link to it.
i'm too lazy to build my own xml with 3 EditText's
OKay here is the link: http://www.bobhoil.com/wp-content/uploads/2010/09/T-Dodger2.zip
I have been busy working and hadn't had a chance to upload it.
Don't mean to be persistent I am just trying to figure out why it is crashing. I changed the code in the above file to some ibm examples just to test if that worked and it too seems to crash. Am I starting the activity wrong?
there are some attributes missing in your test.xml (android:layout_width... and so on)
here is the correct test.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mobil"/>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accelerometer_label"
android:layout_column="1"
android:text="Accelerometer"
android:textSize="9pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_x_label"
android:layout_column="1"
android:text="X:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_x_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_y_label"
android:layout_column="1"
android:text="Y:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_y_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/accel_z_label"
android:layout_column="1"
android:text="Z:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_z_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip" android:layout_width="fill_parent"
android:background="#FF909090" />
</LinearLayout>
by replacing the test.xml with the correct one, the activity opens like it should (you can check that by adding a breakpoint in the onCreate method).
But there is another error too that will cause the activity to close immediately. At the moment i have no idea what's the problem, but i try to figure it out!
Okay thanks. I have been trying to solve the problem for about a week now. I am having a hard time figuring out the problem.
EDIT:
Okay after doing some research maybe it is getting information from the accelerometer to quickly?
no, the problem is the test.xml again... something is wrong with your table-rows.
while executing your application, logcat shows some exceptions like "null-child" in a table row...
Code:
09-05 07:45:40.114: ERROR/AndroidRuntime(223): java.lang.NullPointerException
09-05 07:45:40.114: ERROR/AndroidRuntime(223): at android.widget.TableRow.measureNullChild(TableRow.java:182)
I'm afraid I have too little experience with <TableRow> in Android. I suggest to use simple <TextView> elements to display the sensor data. You only need one TextView, and in your java class file you can put all the sensor values in this TextView.
for example:
accelValue.setText("Accel X: " + values[0]+"\n" + "Accel Y: " + values[] +.....)
The TestActivity is for testing purposes only... so don't care about the layout
Okay I will change over to using TextView and see then if I can get something up and running.
Thanks for the help! I have finally got it fully working!
OKay one last question. Is there any way to change android:layout_y outside of the xml? From the activity?
Have you set up your activities in manifest.xml file??
Sent from my ADR6300 using XDA App

[Q] spinner help

I'm new to most of this and I'm trying to figure out how to have my app react to a spinner selection. I've seen tutorials using setOnItemSelectedListener but i get errors with it.
Heres my code:
hellospinner.java
package com.shanewoodard.hellospinner;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
public class HelloSpinner extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors , android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hubSpinner.setAdapter(adapter);
}
}
Click to expand...
Click to collapse
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/myspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>
</LinearLayout>
Click to expand...
Click to collapse
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="colors">
<item>Red</item>
<item>Blue</item>
<item>White</item>
<item>Yellow</item>
<item>Black</item>
<item>Green</item>
<item>Purple</item>
<item>Orange</item>
<item>Grey</item>
</string-array>
</resources>
Click to expand...
Click to collapse
And this is the code i tried using:
Spinner s = (Spinner) findViewById(R.id.myspinner);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
//do something here
}
@Override
public void onNothingSelected(AdapterView arg0) {
//do something else
}
});
Click to expand...
Click to collapse
can someone give me the exact code i need? if i don't see the code i can't quite figure it out yet, i'm better at reverse engineering it you could say.

[Q] Need help with discount calculator

I am a newbie in android dev.
I want to develop a very small and simple discount rate calculator.
in which there will be drop down menu with options like:
calculate 10%
calculate 20%
calculate 30%
Based on the selection of option from the drop down menu the discount rate should change.
After selection, there will be a textbox which should ask user to enter price.
and on the click even of "Calculate" button, the resultant amount (discounted rate) should be displayed.
I guess it is very simple for you guys. If anyone can help me with the code, i will deeply appreciate.
thank you
ok this is what I have done till now:
I created an Android Project names as :MyDC
MyDC.java
Code:
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
}
}
This is main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price"
/>
<EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
<Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
</LinearLayout>
This is string.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi, MyDC!</string>
<string name="app_name">My, DC</string>
<string name="spinner1">Choose discount type</string>
</resources>
This is what I am able to get:
{
"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 am able to calculate discount on the button click, BUT i have hardcoded the discount rate.
I want to select discount rate based on the dropdown menu (I don't know how to set dropdown values with different discount rate in this problem)
In the dropdown menu I would like to have values like this:
Type 1 discount
Type 2 discount
Type 3 discount
Here is the respective discount rate for the above drop-down values:
Type 1 discount - 10%
Type 2 discount - 15%
Type 3 discount - 18%
PLZ HELP ME
Ok somehow I was able to add spinner tool on my application.
Now the question is how to select discount rate based on selected value from spinner.
PLZ HELP ME WITH THE CODE
Here is my current code:
MyDC.java
Code:
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/planet_prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price"
/>
<EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
<Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
</LinearLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi, MyDC!</string>
<string name="app_name">My, DC</string>
<string name="spinner1">Choose a planet</string>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
</resources>
Anyone ? Please Help ?
Im sorry to not be of very much help I studied java as a highschool unfortunately it was senior year and I am good at slacking. I would like to add suggestions though. first off i would try to find someway of implementing a way to get closer increments such as 5 or 1 %. such as a text box. let me sift through your code and see what i can see. are you having specific errors or what. we need some info.
Ok i looked at your code and im having a hard time reading it just because it isnt the norm that I look at. first off ya need to name your variables more recognizable things then x and y. that will make coding immensely more easy. also I am not seeing where you gathered the information from the drop down or spinner. at some point in the code you have to collect the information from the drop down or spinner and use it for the percent. also i didn't see any point where you gave the drop down any values. if you have any questions or anything feel free to pm I have xda app on my phone so even if not im not online ill get a notification on my phone.
{SBR}_L3GION said:
Ok i looked at your code and im having a hard time reading it just because it isnt the norm that I look at. first off ya need to name your variables more recognizable things then x and y. that will make coding immensely more easy. also I am not seeing where you gathered the information from the drop down or spinner. at some point in the code you have to collect the information from the drop down or spinner and use it for the percent. also i didn't see any point where you gave the drop down any values. if you have any questions or anything feel free to pm I have xda app on my phone so even if not im not online ill get a notification on my phone.
Click to expand...
Click to collapse
Hi,
Yes, I know I haven't defined values to drop down because that is the thing I need help with.
I also posted above this:
"Now the question is how to select discount rate based on selected value from spinner.
PLZ HELP ME WITH THE CODE"
I need help - How to define discount rate based on selection of drop down values.
Please help me with the code....
I also tried doing this quite awhile with an app I was writing and then discovered I had not enough experience to with the android system to write the program I should look it up and fix both of our programs but I've had a migraine for days and I'm just getting close to sleeping but when I get a chance I will look up giving values to the drop down and collecting them unless you want to google it first.
Sent from my Synergized xda Premium Evo
I have tried to find the solution on google but didn't get anything but somehow from google I was able to do (whatever I did above)
thanks, I think that would be great, if you can test my program on your side by that we both will be on the same page.
That's fine, you can test it whenever you get some time.
I appreciate your help!
I would setup radio buttons for the discount percentages
0 10% 0 20% etc.
Name each radio button and use a case statement to set a float to it's value:
switch(radioGroupName) {
case 10Percent:
int discountValue = .10
break;
...
Then do something like:
int discountedAmount = usersAmount - (usersAmount * discountValue);
Try this code.
myDC.java
Code:
package com.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class myDC extends Activity {
double initial_price;
double discount;
double final_price;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount=10;
}
else if (pos==1){
discount=15;
}
else if (pos==2){
discount=18;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount1 = (EditText) findViewById(R.id.amount1);
if (amount1.length()>0 ) {
initial_price=Double.parseDouble(amount1.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price);
final_price=initial_price-(initial_price*discount/100);
final_price_text.setText("Final Price : "+final_price);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount1"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price"></TextView>
</LinearLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, main!</string>
<string name="app_name">myDC</string>
<string-array name="discount">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
</resources>
Re: Need Help
Hello nemoc 23 ,
Firstly thank you very much for replying back with a solution.
It did work and solved my problem upto some extend.
Now what I only want 2 things to be done:
1) Right now, if we enter price in decimal or anything, if the final result is in decimal, it is being displayed as EXACT something like 99.9999999
How to limit the RESULT upto 2 decimal points only ?
2) What I am trying to do is to have this calculator on 3 different pages (in each page I will just modify the discounted rate). So I want a homepage (which will work as MENU) with 3 or 4 buttons and each button should be connected with specific page that will have ITS OWN above discount calculator.
Could you please show me how to create a homepage screen (which will work as MENU) and 3 or 4 buttons on it which are linked with different pages (in each page you can copy the same above calculator and I will modify the rate by myself)
thank you
Why 3 pages? Just do this:
Enter discount amount: ___________
Enter discount rate:______
Discounted Amount: ___________
Easy peasy.
Rootstonian said:
Why 3 pages? Just do this:
Enter discount amount: ___________
Enter discount rate:______
Discounted Amount: ___________
Easy peasy.
Click to expand...
Click to collapse
I know it is easy but it is not what I am looking for. My requirement is what I posted above. Thanks
Help
Hello nemoc 23 ,
Just giving you an update. I did try the tutorial you asked me to try. I am having some problem in it which you can see here: http://forum.xda-developers.com/showthread.php?p=15612542
Coming back my question (this calculator topic) - I created a screen layout to give you an idea what I want to do and need your help with.
This is what I want to do:
In this each button click will open separate page (or I guess it is called activity). I have mentioned on button what each button will open.
Please help me. thanks
Intent i = new Intent(this, calculator1.class);
startActivity(i);
You are still better off creating just ONE new Activity and passing to it which calculator you want to run. It is poor programming to code 3 activities when one will suffice. You'll need "extras" to do this.
Need Help
Ok somehow I was able to code for this problem but I am not getting desired output.
I have 3 buttons on my welcome screen "calculator", "calculator2", "exit" - My buttons are not working, I don't know why.
**Below is my complete code / programs:**
**welcome.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class welcome extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome1);
//////// MENU //////////
Button playBtn = (Button) findViewById(R.id.playBtn);
playBtn.setOnClickListener(this);
Button playBtn2 = (Button) findViewById(R.id.playBtn);
playBtn2.setOnClickListener(this);
Button exitBtn = (Button) findViewById(R.id.exitBtn);
exitBtn.setOnClickListener(this);
}
/**
* Listener for game menu
*/
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.playBtn :
i = new Intent(this, testcalculator.class);
startActivity(i);
break;
case R.id.playBtn2 :
i = new Intent(this, testcalculator2.class);
startActivity(i);
break;
case R.id.exitBtn :
finish();
break;
}
}
}
**welcome1.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id ="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
/>
<Button
android:id ="@+id/playBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator2"
/>
<Button
android:id ="@+id/exitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
/>
</LinearLayout>
**testcalculator.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class testcalculator extends Activity {
double initial_price;
double discount;
double final_price;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount=10;
}
else if (pos==1){
discount=15;
}
else if (pos==2){
discount=18;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount1 = (EditText) findViewById(R.id.amount1);
if (amount1.length()>0 ) {
initial_price=Double.parseDouble(amount1.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price);
final_price=initial_price-(initial_price*discount/100);
final_price_text.setText("Final Price : "+final_price);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
**testcalculator.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount1"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price"></TextView>
</LinearLayout>
**testcalculator2.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class testcalculator2 extends Activity {
double initial_price;
double discount2;
double final_price2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount2=20;
}
else if (pos==1){
discount2=25;
}
else if (pos==2){
discount2=28;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate2 = (Button) findViewById(R.id.calculate2);
calculate2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount2 = (EditText) findViewById(R.id.amount2);
if (amount2.length()>0 ) {
initial_price=Double.parseDouble(amount2.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price2);
final_price2=initial_price-(initial_price*discount2/100);
final_price_text.setText("Final Price : "+final_price2);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
**testcalculator2.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner2"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount2"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price2"></TextView>
</LinearLayout>
**AndroidManifest.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testcalculator"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".welcome"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".testcalculator" />
<activity android:name=".testcalculator2" />
</application>
</manifest>
**strings.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, testcalculator!</string>
<string name="app_name">testcalculator</string>
<string-array name="discount">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
<string-array name="discount2">
<item>20% discount A</item>
<item>25% discount B</item>
<item>28% discount C</item>
</string-array>
</resources>
**main.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
When I click Button1 "Calculator" - I get error saying ""The application testcalculator (process com.testcalculator) has stopped unexpectedly. Please try again.""
Button2 "Calculator2" doesn't do anything and same with 3rd button "exit:
**Please help me!!!**
You got a couple options. First instead of the startActivity() throw in a Toast so you can see if your onClick() is ok. You can also throw some inline clicklisteners (anonymous inner class) inside the setOnClickListener() instead of using 'this'. I find that way is a bit easier to organize.
Annd might I suggest you follow the Java guidelines for capitalisation of class names. Its confusing when reading your code and seeing testcalculator.class when usually instances of objects are lower case while the class name is capitalized. Like TestCalculator.class
From something awesome

[Q] Choose a string in spinner

Hey guys!
I am working on my first app, cut me some slack for asking please .
So I'm stuck on a little problem:
I have a reader activity and I put a spinner on top of the screen to choose the string where the text comes from:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="@+id/kapitelspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/kapitel_prompt" />
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/kapitel1"
/>
</ScrollView>
</LinearLayout>
The TextView in the ScrollView is supposed to be the selected text from the spinner.
Code:
package com.asm.reader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class BookActivity extends Activity {
static final private int CHOOSE_KAPITEL = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
}
Code:
package com.asm.reader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I'm trying to make the spinner choose the string of the textview but i have no idea how to do it. (Already did the toast part)
If anyone could help me it would be greatly appreciated!
Thanks !
bump. the last problem im stuck on
The way I understand what you're trying to do is select an item from the spinner, and set the TextView according to the selected spinner item.
If that is the case you will, for starters, need to set an id attribute for the TextView in the layout xml file.
Then within your onItemSelected() method you need to create a reference to the TextView the same way you did it with the spinner; using the findViewById() method.
Then using that reference you can set the text of the TextView using the same string you got when you created the Toast notification.
Hope that helps!

[Q] Help needed display fragment with listadapter properly...

Hi guys, I am new to android development and I'm having a little trouble working with a list in fragment.
This is what I'm trying to achieve:
------------------
frag 1
------------------
frag 2 <-- list is here
-----------------
frag 3
-----------------
The list displays 3 strings in one row. This is my fragment code: ( the CustomListViewAdapter is implemented in another file called CustomListViewAdapter.java)
Code:
public class ListActivity extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
if (fm.findFragmentById(android.R.id.content) == null) {
DataListFragment list = new DataListFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class DataListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<List<Model>> {
CustomListViewAdapter mAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("DataListFragment.onActivityCreated");
// Initially there is no data
setEmptyText("No Data Here");
// Create an empty adapter we will use to display the loaded data.
mAdapter = new CustomListViewAdapter(getActivity());
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
}
@Override
public Loader<List<Model>> onCreateLoader(int arg0, Bundle arg1) {
return new DataListLoader(getActivity());
}
}
public static class DataListLoader extends AsyncTaskLoader<List<Model>> {
List<Model> mModels;
public DataListLoader(Context context) {
super(context);
}
@Override
public List<Model> loadInBackground() {
System.out.println("DataListLoader.loadInBackground");
List<Model> entries = new ArrayList<Model>(5);
entries.add(new Model("1", "a","3:48"));
entries.add(new Model("2", "b","4:02"));
entries.add(new Model("3", "c","3:48"));
entries.add(new Model("4", "d","3:52"));
entries.add(new Model("5", "e","3:44"));
return entries;
}
@Override public void deliverResult(List<Model> listOfData) {
...
}
@Override protected void onStartLoading() {
...
}
protected void onReleaseResources(List<Model> apps) {}
}
The 3 fragments are added programmatically in the main activity. MainActivity.java
The problem I am facing is that once the code runs, this fragment 2 overlaps the fragment 1 (both are still visible) that was supposed to be above it. How do I resolve this issue? Please help me figure this out! I believe I'm doing something wrong while adding this fragment.
This is the mainactivity.java
Code:
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.info_fragment, new InfoActivity());//frag 1
fragmentTransaction.add(R.id.list_fragment, new listActivity()); // frag 2
fragmentTransaction.add(R.id.controls_fragment, new ControlsActivity()); //frag 3
fragmentTransaction.commit();
}
}
code_name said:
Hi guys, I am new to android development and I'm having a little trouble working with a list in fragment.
Click to expand...
Click to collapse
The layout and views along with the layoutParams control how it looks, the fragments just nest inside the views, that you reference with your resource idents, so however it looks is probably based of the layout or layout parameters that are set in the xml layout
deanwray said:
The layout and views along with the layoutParams control how it looks, the fragments just nest inside the views, that you reference with your resource idents, so however it looks is probably based of the layout or layout parameters that are set in the xml layout
Click to expand...
Click to collapse
I doubt there is any problem with the xml layout. Here is the main layout file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http//schemas.android.com/apk/res/android"
android:id="@+id/linlayname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/info_fragment"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="0dp" >
</FrameLayout>
<FrameLayout
android:id="@+id/list_fragment"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" >
</FrameLayout>
<FrameLayout
android:id="@+id/controls_fragment"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp" >
</FrameLayout>
</LinearLayout>
And this is the list xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http//schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
code_name said:
I doubt there is any problem with the xml layout. Here is the main layout file:
Click to expand...
Click to collapse
Are you sure you are using android:layout_weight properly ?
It would first occur to me that not all the views (3 that contain the fragments) should/would be variable ... but it suggests they are ? weight is of the unused remainder space, so I would maybe suggest a weight of 0,1,0 meaning the 1st and last you set to wrap_content and they are variable based on content... the middle using a weight of 1 uses any space remaining
so
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http//schemas.android.com/apk/res/android"
android:id="@+id/linlayname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/info_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<FrameLayout
android:id="@+id/list_fragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" >
</FrameLayout>
<FrameLayout
android:id="@+id/controls_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>
deanwray said:
Are you sure you are using android:layout_weight properly ?
It would first occur to me that not all the views (3 that contain the fragments) should/would be variable ... but it suggests they are ? weight is of the unused remainder space, so I would maybe suggest a weight of 0,1,0 meaning the 1st and last you set to wrap_content and they are variable based on content... the middle using a weight of 1 uses any space remaining
so...
Click to expand...
Click to collapse
Yes that is the way I want it laid out, it looks better now, but the main problem still remains. The listActivity (the middle fragment) starts from the top of the screen and overlaps the first fragment (first one is visible under the list).I think I am adding the fragment at the wrong place?
code_name said:
Yes that is the way I want it laid out, it looks better now, but the main problem still remains. The listActivity (the middle fragment) starts from the top of the screen and overlaps the first fragment (first one is visible under the list).I think I am adding the fragment at the wrong place?
Click to expand...
Click to collapse
BTW I think I have flu atm (or a man cold, either way it would kill a woman) so double check whatever I say
Erm, not sure what your doing with ListActivity onCreate() your doing fragment transactions in there ? But will it not always be null when loading anything ? onCreateView() is where it would return the new view to the fragmentManager for attaching... so when it does the transaction in ListActivity your also using android.R.id.content which is a base id for the area that an app runs in (also happens to be a frameLayout) which is what you problem is... personally I would think and design out of this problem a little different ... if you want a chat hit me up on hangouts, happy to help!

Categories

Resources