How to create EditText numeric (signed and unsigned decimal) from java code - android

this is a loop that creates a table of EditText and I wish they were numerical
for (int i = 0; i < 3; i++) {
tableRow = new TableRow(this); //oggetto di tipo tableRow
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < 3 ; j++) {
values[i][j] = new EditText(this);
values[i][j].setText(" " + array[count]);
values[i][j].setPadding(10, 10, 10, 10);//spaziatura di ogni cella per i quattro lati
tableRow.addView(values[i][j]);
count++;
}
tableLayout.addView(tableRow);
}

values[i][j].setInputType(InputType.TYPE_CLASS_NUMBER);
you can use the above line of code
Update try the below and thier combination
I've tried combining flags (in desperation to see if it would work):
values[i][j].setInputType(InputType.TYPE_CLASS_NUMBER);
values[i][j].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
values[i][j].setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
refer this link for more inputtypes

Related

how to set properties and actionEvent on EditText on androud

I passed a EditText view to a table row addView() method. like this:
myTablerow.addView(new EditText (this))
Q1: How do I set properties like size, colour for the EditText etc
Q2: How do I get the input value from the EditText?
Full code from the author: (P.s. should update in the question instead of my answer)
(It looks like your post is mostly code; please add some more details.)
if (cursor.getCount() > 0) {
// looping through all rows and adding to list
for (int i = 0; i < numberOfRows; i++)
{
TableRow tr = new TableRow(this);
TableRow trheader = new TableRow(this);
if (i == 0)
trheader.setBackgroundColor(Color.parseColor("#344C58"));
else
tr.setBackgroundColor(Color.parseColor("#203F50"));
for (int j = 0; j < numberOfColumns; j++)
{
txtGeneric = new TextView(this);
TextView txtGenericHeader = new TextView(this);
//txtGenericHeader.setGravity(Gravity.LEFT);
if (i == 0) {
txtGeneric.setTextColor(Color.parseColor("#ffffff"));
for (int z = 0; z < numberOfColumns; z++) {
txtGenericHeader.setTextSize(12);
txtGenericHeader.setAllCaps(false);
//txtGeneric.setBackgroundColor(Color.parseColor("#768F9E"));
txtGenericHeader.setTextColor(Color.parseColor("#768F9E"));
txtGenericHeader.setText(cursor.getColumnName(j));
txtGenericHeader.setGravity(Gravity.CENTER_HORIZONTAL);
}
trheader.addView(txtGenericHeader);
}
txtGeneric.setTextSize(12);
txtGeneric.setTextColor(Color.parseColor("#768F9E"));
txtGeneric.setText(mArrayList.get(counter++));
txtGeneric.setGravity(Gravity.CENTER_HORIZONTAL);
tr.addView(txtGeneric);
}
tr.addView(new EditText(this));
tr.addView(new EditText(this));
table.addView(trheader);
table.addView(tr);
cursor.moveToNext();
}
db.close();
sv.addView(table);
lay.addView(sv);
}//EOF PARENT IF
You can do this for setting the properties.
EditText edtText = new EditText(this);
edtText.setTextColor(someColor);
edtText.setTextSize(someSize);
myTablerow.addView(edtText);
And for getting the value from EditText you can use.
String edtTextValue = edtText.getText().toString();
you may refer to this
Q1: #setTextColor() , #setTextSize()
Q2: #getText()

How can I fill views using cycles?

How can I fill views using cycles. For example, I have a three element:
TextView tv_1, tv_2, tv_3
Can I do something like this?
for(int i=1; i<=3; i++){
tv_{i}.setText(i);
}
Just create an List of textViews and run cycles/loop on it.
List<TextView> textViews = new ArrayList<>();
textViews.add(textView1);
textViews.add(textView2);
textViews.add(textView3);
after that just iterate through it :
for(int i=0 ;i <textViews.size(); i++)
{
textViews.get(i).setText("text");
}
Here, try this.
TextView[] tvs = new TextView[3];
tvs[0] = findViewById(R.id.tv1);
tvs[1] = findViewById(R.id.tv2);
tvs[2] = findViewById(R.id.tv3);
for(int i=0; i<3; i++){
tvs[i].setText(i);
}

How to identify a disabled switch in android

I am developing an android project where I am struck in a case, where I have to identify (ie.. to get some information about the 'disabled switch') so that I can use that particular disabled switch's information to change the database through PHP query. Please let me know your valuable suggestions.
Here is my code where I declared the switch -
mySwitch = new Switch[len];
flag = new int[len];
pnr = new String[len];
id = 0;
for( j = 0; j < len; j++ ) {
newlist = passenger[j].split("#");
pnr[j] = newlist[0];
hrow = new TableRow(this); //create row for each passenger
hrow.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
hrow.setPadding(0, 0, 0, 3);
Thanks
What your looking for is isClickable() (according to your comment)
http://developer.android.com/reference/android/view/View.html#isClickable()

Android shuffle Questions and Answers (string arrays)

I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}

Android - How to set value of ArrayList to TextView

i have a problem here.
I've ArrayList from the result of my parser class. then i wanna put that value (value from ArrayList) to TextView.
here is the work i've done till now.
I create TextView on my main.xml
<TextView
android:id="#+id/deskripsi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
and here at my onCreate Method, i initialized the Text View
desk = (TextView)findViewById(R.id.deskripsi);
and then i tried to parser the KML document from google map and at the Node Placemark i put its value to ArrayList, here my code
ArrayList<String> pathName = new ArrayList<String>();
Object[] objPlace;
//Parser Node Placemark
NodeList nlPlace = doc.getElementsByTagName("Placemark");
for(int p = 0; p < nlPlace.getLength(); p++){
Node rootPlace = nlPlace.item(p);
NodeList itemPlace = rootPlace.getChildNodes();
for(int y = 0; y < itemPlace.getLength(); y++){
Node placeStringNode = itemPlace.item(y);
NodeList place = placeStringNode.getChildNodes();
//valueName = nameList.item(0).getNodeValue().toString() + "+";
pathName.add(place.item(0).getNodeValue());
}
}
objPlace = pathName.toArray();
desk.setText("");
for (int j = 0; j < objPlace.length; j++){
desk.append("Deskripsi:\n" + objPlace[j].toString() + "\n");
}
but when itried to run its to my emulator and real device, i get an error. here's my LogCat
please help me, and sorry for my english >_<
You can do this instead; use pathName directly in the for loop...
desk.setText("");
for (int j = 0; j < pathName.size(); j++){
desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}
you can put the line
objPlace[] = new Object[pathName.size()]
before
objPlace = pathName.toArray();
and check output otherwise do this way
desk.setText("");
for (int j = 0; j < pathName.size(); j++){
desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}
You never initialize your array... objPlace[] is a null array.
You need to do something like:
objPlace[] = new Object[arraysizenumber]
Ii is caused by NullPointerException, that means some needs parameters and you have passed a null value to it.
Based on your code it might be around desk.setText("");. Try inserting a space or some value into it and run.
Thanks my fren. im still not really well on using Object[]. then i used this code for solving my problem
String strPlace[] = (String[])pathName.toArray(new String[pathName.size()]);
desk.setText("");
for (int j = 0; j < strPlace.length; j++){
desk.append("Deskripsi:\n" + strPlace[j] + "\n");
}
Thanks for userSeven7s, flameaddict and khan for trying to help me.
nice to know u all :)

Categories

Resources