I am developing my first Android app. I have a table layout in my app, which will add rows dynamically. But, I want to convert the table rows into string. I tried "row.toString()" but I am not getting the expected result. Please find the following code snippet:
View view = null;
for(i = 0, j = tblayout.getChildCount(); i < j; i++)
{
view = tblayout.getChildAt(i);
TableRow rw = (TableRow)tblayout.getChildAt(i);
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
int colorId = viewColor.getColor();
if(colorId == Color.parseColor("#E143ED23")) {
num ++;
//temCnt = view.toString();
temCnt = rw.toString();
}
}
Toast.makeText(getApplicationContext(),temCnt, Toast.LENGTH_LONG).show();
But it is not showing the actual table row data. It is showing some ids' or garbage value.
My requirement is:
I have 2 text boxes and 2 buttons. When the first button "Ok" is pressed the value inside the text boxes will be populated into the Table layout along with a checkbox. By default the row will be checked. But user can uncheck each items. After that, when the second button "Save All" is pressed, then it will search for all the table rows which are checked and it should insert the table rows into the my MS SQL table.
Is there any way for converting the table row into a string in Java? I really trapped on this. It would be very helpful if someone can help me on this.
Please help...
toString() is a very basic method in Java, C# and other programming languages. Java's Object class has a basic implementation for it and you can override this method in any class that inherits from Object (that is, every class whatsoever :)). I don't know if those that developed the TableRow class of Android have overridden this method. It could be that what you're getting is some unreadable, meaningless representation of a TableRow object.
Let's say you have a TextView called txtName within the TableRow, followed by a second TextView called txtNumber. You can get what you want by doing the following:
...
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
int colorId = viewColor.getColor();
if(colorId == Color.parseColor("#E143ED23")) {
num ++;
//temCnt = view.toString();
TextView nameField = (TextView) rw.findViewById(R.id.txtName);
TextView numberField = (TextView) rw.findViewById(R.id.txtNumber);
temCnt = nameField.getText().toString() + ": " + numberField.getText().toString();
}
...
Related
I need to add 5 Input fields (EditText) dynamically one by one on button click and want to take values from them and store them into database using Room Persistence with MVVM.
Here I'm adding the view dynamically
private void addEditTextView() {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
Any suggestion would be very helpful.
Thank you in advance.
Add view based on child count
private void addEditTextView() {
if (binding.layoutList.getChildCount() <= 5) {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
}
"When I clicked Add button it is adding input field one by one, this code is working but I just want to limit for 5 fields not more not less and take values from them."
If you want to add exactly 5 fields on button click I recommend designing a fragment with the 5 fields in place, then when the button is clicked, inflate the fragment into your parent view. Then code the fragment appropriately with the data you're working with.
Then if you wanted, you could deflate the fragment on button click to clear the view or add some other way to clear the fragment when you want. Much easier than what you're doing currently in my own opinion.
You might as well include a submit button in your fragment assuming this is some kind of form.
You can simply define an integer and increase it every time you add the EditText but you should check if your integer is less than 5 everytime the method is called.
Example
private void addEditTextView() {
int count = 0;
if (count < 5){
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
count++;
}
}
I was wondering if it was possible to ouput xml elements such as editText and textView based off user input. For example I'm making a simple game app and in the app you get taken to a new activity where it asks the amount of players. Based off the input (e.g. 5) I woulld like to display editText's and textViews so the players can be given names. Is this possible with Java and if so, how?
Sure is, though without having to go through XML.
Pass the number of players to the next activity as an argument. Then make sure you have a ViewGroup (LinearLayout for example) in the second activity's layout.
Then loop through and create the Views dynamically:
ViewGroup container = findViewById(R.id.linearLayout);
for( int i = 0; i < numberOfPlayers; i++ ){
EditText et = new EditText(this);
et.setHint(R.string.new_player_hint);// String providing a hint to the user
container.addView(et);
}
You can put the EditTexts in a List or if that's all you'll have you can directly cycle over the ViewGroup's children to get their values later on:
for( int i = 0; i < container.getChildCount(); i++ ){
String playerName = ((EditText) container.getChildAt(i)).getText().toString();
}
If you're going to have many possible players you should wrap the LinearLayout in a ScrollView.
Because you'll likely use this in onCreate remember that you can't rely on the EditText to save its own data in case the Activity is destroyed, so save them yourself and feed them back in when re-creating.
thanks in advance.
I have multiple TableRow objects in an Android app, each of which contains exactly two EditTexts. I want to have the contents and quantity of editTexts saved and restored when I open/close the app and so I need to have a way to set the text of the EditTexts, but this is where the problem is.
Android Studio is saying "Cannot Resolve Symbol 'setText'":
//will loop through each of the TableRows in a tableRowHolder(no problem yet):
for (int i = 0; i < tableRowHolder.getChildCount() && tableRowHolder.getChildAt(i) instanceof android.widget.TableRow; ++i) {
//set tableRow to be the i-th child in tableRowHolder (no problem yet)
TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);
//where the problem is("setText" is red), I don't think Java recognises that "tableRow.getChildAt(1)" is an EditText, even though it always will be.
tableRow.getChildAt(1).setText();
//this however, is perfectly fine:
EditText et = new EditText(
et.setText("");
}
To recap, I have:
A tableRow object always containing exactly two EditTexts
and my problem is that:
Java seems to not recognise that I am asking for .setText() on a EditText
Thanks so much in advance.
Just like you're casting your TableRow out of the TableRowHolder, you need to cast the View child to an EditText before you can call its methods.
TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);
((EditText) tableRow.getChildAt(1)).setText("Some Text");
You could optionally wrap your calls inside an instanceof if-block to avoid any ClassCastExceptions if there's any chance that the View may not be an EditText always.
View child = tableRow.getChildAt(1);
if (child instanceof EditText) {
EditText et = (EditText) child;
et.setText("Some Text");
}
This question is related to one I posted earlier. Views removed using removeView() are there when I next open the Activity (Android)
Background: When a user logs into my app they are taken from the login activity to the mainpage activity. The mainpage has a TableLayout that contains dynamically generated buttons. However if the user logs out and back in again, all of these buttons are repeated so I am trying to find out how best to remove these buttons after they are generated. In my previous post it was suggested I remove the buttons at the very start of the main page activity, before the new ones are drawn, so this is what I am trying to implement.
However when I call getChildCount() on this layout it does not always return the correct answer.
So far, here is the code that is run at the start of the main page activity:
TableLayout tableLayout = (TableLayout)findViewById(R.id.MainPageTableTitle);
//removeSectionButtons(tableLayout); this is where i am trying to remove the buttons
System.out.println("there are oncreate " + tableLayout.getChildCount());
drawButtons(tableLayout);
System.out.println("there are ondraw " + tableLayout.getChildCount());
The first print line returns 0 and the second print line always returns the correct answer (number of buttons drawn including all of the repeated ones). But I am not sure why getChildCount() returns the wrong answer the first time. If anyone can explain I would be incredibly grateful
My drawButtons() method is as follows (it draws two buttons per row):
public void drawButtons(TableLayout tableLayout){
//get the number of buttons
int noOfButtons = mySectionTableHandler.getSectionDetails().size();
//calculate the number of rows needed (there are 2 columns)
//set flag to say if buttons are odd as it affects how many are drawn
int noOfRows;
boolean evenNoOfButtons;
if(noOfButtons % 2 == 0){
//even no of buttons
noOfRows = noOfButtons/2;
evenNoOfButtons = true;
} else {
//odd no of buttons
noOfRows = (noOfButtons+1)/2;
evenNoOfButtons = false;
}
//counter to give each button a unique id
int counter = 1;
for(int i = 0; i<noOfRows;i++){
TableRow newRow = new TableRow(this);
Button a = new Button(MainPageActivity.this);
a.setId(counter);
sectionButtons.put(counter, a);
counter++;
newRow.addView(a);
//if there are even buttons OR if there are an odd no
//of buttons but this isn't the last row then add
//second button to row
if(evenNoOfButtons || (!evenNoOfButtons && (noOfRows-1!=i))){
Button b = new Button(MainPageActivity.this);
b.setId(counter);
sectionButtons.put(counter, b);
counter++;
newRow.addView(b);
}
tableLayout.addView(newRow);
}
}
This was my bad, turns out I was re-adding data to the sqlite database each time the user logged in without wiping previous details.
So my code was generating a button for every field in the database as it should have been.
I have an Activity with two layouts, both implemented in R.layout.main. The first one is a Relative Layout with the app's main screen, and the other is a Table Layout, holding a kind of Preferences Screen. Normally, the first one is set to visible, and the second one to gone. By clicking a button I make the Relative Layout gone, and the Table Layout visible.
And here starts my problem, I wanted to set a OnClickListener to that Table Layout (which is actually an array of buttons).
I tried something like:
final TableLayout table = (TableLayout)findViewById(R.id.tab);
table.setOnClickListener(new OnClickListener(){
public void onClick(View arg){
Button clickedButton = (Button)arg;
String t = (String) clickedButton.getTag();
Toast toast = Toast.makeText(getApplicationContext(),t,Toast.LENGTH_SHORT);
toast.show();
}
});
Obviously, it doesn't work.
I'm quite new to Android programming, and I've been looking for a suitable solution for the whole day without any results.
It couldn't work because you are first trying to cast a TableLayout to a button...
if your TableLayout is only containing buttons you could do something like:
TableLayout yourRootLayout = findView....
int count = yourRootLayout.getChildCount();
for(int i = 0; i < count; i++){
View v = yourRootLayout.getChildAt(i);
if(v instanceof TableRow){
TableRow row = (TableRow)v;
int rowCount = row.getChildCount();
for (int r = 0; r < rowCount; r++){
View v2 = row.getChildAt(r);
if (v2 instanceof Button){
Button b = (Button)v2;
b.setOnClickListener(this);
}
}
}
}
and let your activity implement OnClickListener. Just copy your Existing onClick into Activity itself...