I'm an Android noob and doing my best to understand it. I'm trying to make an apk just for fun, to understand things better and also, have a problem which I haven't yet resolved even after searching google a lot.
So, the problem is that I have an EditText which allows only numbers, and by inserting a number in there, it will get into a string and, from there will be intruduced in setProgress as 'the percentage'. At least, that's what I want it to do; because, after putting that value into string and try to get it to be read as a percentage in setProgress, eclipse won't let me:
The method setProgress(int) in the type ProgressBar is not applicable for the arguments (String)
Here is the code:
*don't bother with anything else, it's just for testing...
package com.example.singur;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements OnClickListener
{
Button button;
ToggleButton toggle;
TextView rateText;
TextView textProgress;
Switch legit;
EditText edit;
RatingBar rating;
ProgressBar progress;
int count = 0;
// int value = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
toggle = (ToggleButton) findViewById(R.id.toggle);
rateText = (TextView) findViewById(R.id.rateText);
textProgress = (TextView) findViewById(R.id.textProgress);
legit = (Switch) findViewById(R.id.legit);
edit = (EditText) findViewById(R.id.edit);
rating = (RatingBar) findViewById(R.id.rating);
progress = (ProgressBar) findViewById(R.id.progress);
legit.setEnabled(false);
edit.setEnabled(false);
button.setOnClickListener(this);
toggle.setOnClickListener(this);
edit.setOnClickListener(this);
//rating.setOnRatingBarChangeListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button:
textProgress.setText("button clicked" + count + progress.getProgress());
count++;
progress.setProgress(count);
break;
case R.id.rating:
break;
case R.id.toggle:
if(toggle.isChecked() == true)
{
edit.setEnabled(true);
edit.setText("" + count);
//count++;
//progress.setProgress(count);
textProgress.setText("button clicked" + count + progress.getProgress());
}else
{
edit.setEnabled(false);
edit.setText("disabled but count = " + count);
//count++;
//progress.setProgress(count);
textProgress.setText("button clicked" + count + progress.getProgress());
}
count++;
textProgress.setText("toggle:"+count);
break;
case R.id.edit:
if(edit.isEnabled() == true)
{
edit.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
// do your stuff here
String value = edit.getText().toString();
progress.setProgress(value.valueOf(value));
}
return false;
}
});
}
}
}
}
You need to convert your String to an int. As the error indicates, setProgress() only accepts ints, whereas the value you get from the EditText is a String (even if the String can only contain numbers).
The easiest way to do this is with Integer.parseInt(String).
You code will look something like this:
String value = edit.getText().toString();
progress.setProgress(Integer.parseInt(value));
Also note that Integer.parseInt() will throw a NumberFormatException if the String can't be parsed as an Integer. Be sure to handle that Exception appropriately.
Related
I use an Edittext in android to fill a cell of xls.file by using aspose-cell for android API. But when i put some text in the edittext, the displayed value in the xls. file is always the same : 2131492965 !! and not the text of the edittext.
Can you help me ?
Here is my code :
MainActivity.java
package com.example.lionel.cells;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.aspose.cells.Cell;
import com.aspose.cells.Cells;
import com.aspose.cells.*;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import java.io.File;
import static com.aspose.cells.b.b.zp.v;
import static com.example.lionel.cells.R.id.button1;
public class MainActivity extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public EditText mEditText;
public String text;
// public String text = textview.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LibsLoadHelper.loadLibs(getApplicationContext());
mEditText = (EditText) findViewById(R.id.Edittext1);
// mEditText.setOnKeyListener(new EditText.OnKeyListener()
// {
// #Override
// public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
//
// if(KeyEvent.KEYCODE_ENTER == keyCode)
// {
// if(KeyEvent.ACTION_UP == keyEvent.getAction())
// {
// EditText editText = (EditText)view;
// mCellValue = editText.getText().toString();
// Toast.makeText(MainActivity.this, mCellValue, Toast.LENGTH_SHORT).show();
// }
// return true;
// }
// return false;
// }
// });
text = mEditText.getText().toString();
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new TestTask().execute();
}
});
}
}
TestTask.java :
package com.example.lionel.cells;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;
import com.aspose.cells.Cell;
import com.aspose.cells.Cells;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import java.io.File;
import java.io.IOException;
import static com.example.lionel.cells.R.id.text;
/**
* Created by Lionel on 25/09/2016.
*/
public class TestTask extends AsyncTask<Void, String, Boolean> {
// File sdDir = Environment.getExternalStorageDirectory();
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String sdPath = sdDir.getAbsolutePath();
#Override
protected Boolean doInBackground(Void... params) {
Boolean result = false;
Workbook book = new Workbook();
Worksheet sheet = book.getWorksheets().get(0);
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
Cell cell1 = cells.get("A2");
Cell cell2 = cells.get("A3");
cell.putValue(text);
cell1.setValue(true);
cell2.setValue(123);
try {
book.save(sdPath + "/output.xlsx");
//
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
#Krukiou,
A similar inquiry of yours have already been replied in Aspose.Cells support forum. Please check the following piece of code as well as this thread for details.
Regarding the following snippet, it uses the mCellValue variable to load the contents of the EditText field on Enter key. You can use the same variable to insert the contents of the EditText field to the spreadsheet cell using the UI button mButton. Moreover, as by default, the EditText can span over multiple lines therefore the Enter key on the real device (or soft keyboard) will act as carriage return, therefore the code sets the EditText.SingleLine as true in order to change the behaviour of the soft Enter key.
private TextView mResultTextView;
private TextView mProgressTextView;
private Button mButton;
private EditText mEditText;
private String mCellValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LibsLoadHelper.loadLibs(getApplicationContext());
mResultTextView = (TextView)findViewById(R.id.result_textview);
mProgressTextView = (TextView)findViewById(R.id.progress_textview);
mButton = (Button)findViewById(R.id.button);
mEditText = (EditText)findViewById(R.id.edit_text);
mEditText.setSingleLine(true);
mEditText.setOnKeyListener(new EditText.OnKeyListener()
{
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if(KeyEvent.KEYCODE_ENTER == keyCode)
{
if(KeyEvent.ACTION_UP == keyEvent.getAction())
{
EditText editText = (EditText)view;
mCellValue = editText.getText().toString();
Toast.makeText(MainActivity.this, mCellValue, Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
});
mButton.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v) {
new TestTask().execute();
}
});
Note: I work with Aspose as Developer Evangelist.
Hi I want to connect bluetooth and laptop
What I want to do is, send numeric value from android application to Labview program installed in Laptop.
The android program returns value which changes according to the button click.
(When I press up button, value +1).
And I want to send this value to the laptop via bluetooth!.
I was looking for google, stackoverflow and other many communities., I couldn't find any hints or solutions.
package com.u2ring.control;
import com.u2ring.control.R;
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;
import android.widget.TextView;
import android.webkit.*;
public class MainActivity extends Activity implements OnClickListener
{
Button Plus, Minus;
TextView Value;
TextView url;
int score = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Plus = (Button) findViewById(R.id.up);
Minus = (Button) findViewById(R.id.down);
Value = (TextView) findViewById(R.id.number);
String host = getString(R.string.host);
Plus.setOnClickListener(this);
Minus.setOnClickListener(this);
Button bt1 = (Button) findViewById(R.id.button2);
bt1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent in = new Intent(MainActivity.this,Secondpage.class);
startActivity(in);
}
});};
public void onClick(View v)
{
boolean showText = false;
int id = v.getId();
if (id == R.id.up) {
score++;
showText = true;
} else if (id == R.id.down) {
score--;
showText = true;
} else if (id == R.id.number) {
showText = true;
}
if(showText)
Value.setText(String.valueOf(score));
WebView wv= (WebView) findViewById(R.id.web);
wv.loadUrl("http://10.16.27.184:8080/admin/speed/"+Integer.toString(score));
}
}
There is a nice Bluetooth example with code for LabVIEW and Android on LabVIEW Hacker.
I am new to Android and Java and don't know why the Text value is null after I assign Text to it? This is a project for University They didn't teach us Java only gave us Eclipse and Android Sdk and Java need some help please!
package com.example.glossaryapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Syntax extends Activity {
static String Text;
public static TextItem[] Syntax = new TextItem[50];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.syntax);
final TextView show = (TextView) Syntax.this
.findViewById(R.id.textViewSyn);
// Linking the text View widget
int Count1 = 0;
// Using the TextView's shows method set text to display the appropriate
// arrays and indexes
if (Syntax[0] != null) {
Text = Syntax[0].displayText();
show.setText(Text);
}
// The Add button for if a user wants to add a new TextItem to the Array
Button but1 = (Button) findViewById(R.id.butAddSyntax);
but1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Create.strSender = "Syntax1";
startActivity(new Intent(Syntax.this, Create.class));
}// end of on click
});// butAddSyntax
// Home Button
Button but2 = (Button) findViewById(R.id.buttHomeSyntax);
but2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}// end of on click
});// buttonHomeSyntax
}
public void onResume() {
super.onResume();
TextView show = (TextView) Syntax.this.findViewById(R.id.textViewSyn);
if (Syntax[0] != null) {
Text = Syntax[0].displayText().toString();
show.setText(Text);
}
}
}
Using Android's LogCat will help you to solve this kind of problems.
Here you have a nice article:
http://www.vogella.com/tutorials/AndroidLogging/article.html
I think the problem is in here:
if( Syntax[0] != null ){
Text = Syntax[0].displayText();
show.setText(Text);
}
you could begin using LogCat trying this:
if( Syntax[0] != null ){
Log.d("MyApp", "Sintax (0) = " + Syntax[0].displayText());
Text = Syntax[0].displayText();
show.setText(Text);
} else Log.d("MyApp", "Nothing ");
And watch the result in the Logcat pane.
This one is destroying my brain, when you enter the activity you can click the paypal button and you go into the paypal activity fine. If you cancel the paypal activity you are brought back to the original activity. From here if you click the button again nothing happens. I tried printing a basic message to console in the OnClick method and it didn't even show, it doesn't seem to be registering the clicks. I'm Assuming the problem has nothing to do with the layout xml file or the manifest file. I would post an image but i cant because im new, all you need to know is that its a checkout screen with a paypal button really.
Thanks in advance,
Hugh.
package com.cit.datastructuretesting;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalPayment;
public class StoreItemInterface extends Activity implements OnClickListener, OnKeyListener
{
Double subtotal, shipping, total;
EditText etItemQuantity;
TextView tvItemSubtotal, tvItemTotal;
DecimalFormat decFormat = new DecimalFormat("#.##");
PayPal ppObj;
CheckoutButton launchPayPalButton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.storeiteminterface);
ImageView ivItemImage = (ImageView) findViewById(R.id.ivItemImage2);
TextView tvItemTitle = (TextView) findViewById(R.id.tvItemTitle2);
TextView tvItemDescription = (TextView) findViewById(R.id.tvItemDescription2);
etItemQuantity = (EditText) findViewById(R.id.etItemQuantity);
tvItemSubtotal = (TextView) findViewById(R.id.tvItemSubtotal);
TextView tvItemShipping = (TextView) findViewById(R.id.tvItemShipping);
tvItemTotal = (TextView) findViewById(R.id.tvItemTotal);
subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
shipping = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getShippingPrice();
total = subtotal + shipping;
ivItemImage.setImageBitmap(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getImageBitmap());
tvItemTitle.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getTitle());
tvItemDescription.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getDescription());
tvItemSubtotal.setText("€" + decFormat.format(subtotal));
tvItemShipping.setText("€" + decFormat.format(shipping));
tvItemTotal.setText("€" + decFormat.format(total));
etItemQuantity.setOnKeyListener(this);
//setup paypal stuff
ppObj = PayPal.initWithAppID(this.getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);
launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.bottomMargin = 10;
launchPayPalButton.setLayoutParams(params);
((RelativeLayout)findViewById(R.id.paypalLayout)).addView(launchPayPalButton);
launchPayPalButton.setOnClickListener(this);
}
#Override
public void onClick(View arg0)
{
if(arg0.getId() == launchPayPalButton.getId())
{
System.out.println("TEST");
if(!tvItemTotal.getText().toString().equals("---"))
{
PayPalPayment newPayment = new PayPalPayment();
newPayment.setSubtotal(BigDecimal.valueOf(total));
newPayment.setCurrencyType("EUR");
newPayment.setRecipient("Cape_1328492032_biz#mycit.ie");
newPayment.setMerchantName("Cape Clear App");
Intent paypalIntent = PayPal.getInstance().checkout(newPayment, StoreItemInterface.this);
StoreItemInterface.this.startActivityForResult(paypalIntent, 1);
}
else
{
Toast.makeText(StoreItemInterface.this, "Invalid Quantity!", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2)
{
if(etItemQuantity.getText().toString() != null && !etItemQuantity.getText().toString().trim().equals("") && !etItemQuantity.getText().toString().trim().equals("0"))
{
subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
total = subtotal + shipping;
tvItemSubtotal.setText("€" + subtotal);
tvItemTotal.setText("€" + decFormat.format(total));
}
else
{
tvItemSubtotal.setText("---");
tvItemTotal.setText("---");
}
return false;
}
}
Try launchPayPalButton.updateButton() after the stuff you do when onClick()
https://www.x.com/developers/paypal/forums/paypal-mobile-xspace/android-interrupting-paypal-onclick-event#answer-203333
In case you are still looking for an answer, I have one
If you have a look at getCheckoutButton method, it takes Context as parameter, so when the Activity is for e.g. say Paused which happens when you start another Activity, the instance of the CheckoutButton is lost somehow.
I fixed is by using updateButton method in onResume of the Activity
#Override
protected void onResume() {
/**
* The CheckoutButton has to be updated each time the Activity is
* resumed, otherwise the onClickListener of CheckoutButton will not work
**/
if (mCheckOutBtn != null && (mCheckOutBtn instanceof CheckoutButton))
mCheckOutBtn.updateButton();
super.onResume();
}
This works considering you initialized PayPal library and CheckoutButton in onCreate of Activity.
I'm new to android and this is my first application, it seems fine to me but every time I pressed the calculate button it seems to stop unexpectedly and force close.
package com.test.simplecalc;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button myButton = (Button) findViewById(R.id.myButton);
final EditText firstNum = (EditText)findViewById(R.id.firstNum);
final EditText secondNum = (EditText)findViewById(R.id.secondNum);
final EditText finalNum = (EditText)findViewById(R.id.finalNum);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int num1 = 0;
try {
num1 = Integer.parseInt(firstNum.getText().toString());
} catch(NumberFormatException nfe) {
Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
}
int num2 = 0;
try {
num2 = Integer.parseInt(secondNum.getText().toString());
} catch(NumberFormatException nfe) {
Toast.makeText(MainActivity.this, "Could not parse" + nfe, Toast.LENGTH_SHORT).show();
}
int num3 = num1 + num2;
finalNum.setText(num3);
}
});
}
}
It's likely that one of the two blocks is throwing something other than NumberFormatException. (My guess would be a NullPointerException in the call to toString().)
Try changing the caught exception in each case to Exception and see if this reveals the problem.
just try
num3.toString()
while printing the answer, and better to use textview for showing answer