How to dynamically add radio buttons with two extra buttons android - android

I want to create the following view:
For above picture, I declared a vertical radio group in xml, and rows were added at run time. In a single radio button , i want to add two other buttons,as shown in picture,
This code is to add address (radio buttons) to radiogroup.
private void add_to_address_list() {
for (int i = 0; i < addresses_list.size(); i++) {
RadioButton rdbtn = new RadioButton(this);
Log.d("mytag", "id : " + i + "name :" + i);
addresses_list.get(i).getUser_line2());
String line1 = addresses_list.get(i).getUser_line1();
String line2 = addresses_list.get(i).getUser_line2();
String line3 = addresses_list.get(i).getGoogle_line1();
rdbtn.setId(i);
rdbtn.setText(Html.fromHtml("<font color=black size=3em>" +line1+"<br>"+line2+"<br></font>" +"<font color=grey size=3em>"+ line3+"</font><br>"));
((ViewGroup) findViewById(R.id.address_radio_group)).addView(rdbtn);
}
}
How can i add two other buttons, one is for delete the address and another is for edit address, is it possible ? or can i go with simple list view?
if i use listview to display it, is it possible to select one only. i think it may possible with check boxes. suggest any correct method. pleas help me.Thanks.

Related

Android - Go to next found word from EditText in TextView

I have a TextView and I can search some words, so that they're marked.
The TextView is named textView and it shows the text, I have an EditText Etxt to get the searched word and a Button to start the search. The code above is the code of the search. The app marks all found words big and italic. And I have a TextView text_total which shows the number of found words.
The problem: But if there is a searched word in the text below the shown screen, you must scroll and find the marked word:
int total = 0;
String word_search = Etxt.getText().toString().trim().toLowerCase();
String fullTxt = textView.getText().toString();
String[] array = fullTxt.split("\n");
String word;
StringBuilder st = new StringBuilder();
for (int i = 0; i < array.length; i++) {
word = array[i];
if (word.toLowerCase().contains(word_search)) {
String abc = word.trim();
st.append("<b><i>" + abc + "</i></b>");
total++;
} else {
st.append(word);
}
st.append("<br>");
}
textView.setText(Html.fromHtml("" + st));
text_total.setText("Ergebnisse: " + total);
Now, I have a problem because the text is too long to see all search results. I want that I have a 'back' and a 'next' button and the view goes to the next result if I click the next button and that the the found word goes automatically to the shown screen.
Does anyone know how to code this?
That's very important. Thanks for help!
Try this.
Put all the finded words in a list
List words= new ArrayList();
Add the words to the list
words.add(word_search);
Finally in next and previous buttons, you need to check the positions of the words in the list
words.get(position)
See how in this link

How to Set Id in EditText programmatically in Android

I have this button on GridLayout called addnewTask. When you create this button, it will create an EditText.
private GridLayout gridLayout;
int rowIndex = 3;
int colIndex = 1;
int i=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_task);
gridLayout = (GridLayout) findViewById(R.id.taskLayout);
}
This function to create EditText when the button is clicked -->
public void addView(View view) {
i++;
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.MATCH_PARENT;
param.rowSpec = GridLayout.spec(rowIndex);
param.columnSpec = GridLayout.spec(colIndex);
editText.setLayoutParams(param);
if (rowIndex > 3) {
editText.setTag(tname);
}
gridLayout.addView(editText);
rowIndex++;
}
My problem is that i want to set the android:id of EditText i created.
like this: When the button is clicked, EditText is created, in row 3, column 1 and id name task1.
When the button is clicked again, another EditText is created, in row 4, column 1 and id name task2.
When the button is clicked again, another EditText is created, in row 5, column 1 and id name task3.
ANS SO ON.....
Ids in android aren't strings - they are always numbers. Even if you write in xml #+id/textId, a number is generated for this text. You can see that in your R file.
What you can do is assign id to your edit texts by using editText.setId(int) method. If you want to be able to easily refer to the edit texts, you can either:
assign the ids sequentially: 1, then 2, 3 etc. Then id of the item would be (row-1) * <columnsCount> + column) (so if you have 3 columns, then second item in fifth row would have id 4 * 3 + 2)
create a map field of type Map<String, Integer>, and again assigns ids sequentially, and save them in.
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editText.setId(i);
idsMap.put(tname, i);
You then get edittext's id by calling idsMap.get("task3")
Third option is to just keep reference to your EditText in a map: you'd then have a Map<String, EditText> map, and then call
String tname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editTextsMap.put(tname, editText);
You can keep references of these edit text in an array representing cells of your grid.
declare arraylist like this:
ArrayList<EditText> etArray = new ArrayList<>();
and keep reference to your EditText in array list at the end of your addView method like this:
etArray.add(i,edittext);
now refer these view like this:
etArray.get(i);
this way you will be able to refer them for accessing text.
assigning ids dynamically can cause problems as id is an integer and your assigned ids may cause conflict with system assigned ids to other components.
You can't set id as a String. You can only assign integer as Id. But if you want to use String as id for the ease of use then - in res/values/ids.xml file
<item name="edit_text_hello" type="id"/>
And then use it as:
edText.setId(R.id.edit_text_hello);
So you can do what you need.

Link on a Android textView

I am trying to put a link on a textview, but when I click this link the application breaks!! This is my code:
final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
msg.setText(Html.fromHtml(dbStructure.getMsg()));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());
Where dbStructure.getMsg() returns a String. This String could be something like:
< a href="/reference/android/widget/RelativeLayout.html">RelativeLayout< /a>
lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
It seems nice, but the app stops when I press it.
EDIT
The error thrown ActivityNotFoundException.
the link you are trying to open is broken
/reference/android/widget/RelativeLayout.html
there is nothing corresponding to the above link.
replace it with the proper url like this
http://developer.android.com/reference/android/widget/RelativeLayout.html
Thank you very much for every one... the problem is (as #Antonio #danidee #TheRedFox and #Arslan say) the format of the url... it doesn´t start with http.
With permission from you all, I am going to answer my own question:
final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
String msg_text = dbStructure.getMsg();
if(msg_text.contains("href=\"")) {
String[] msg_aux = msg_text.split("<a href=\"");
if (!msg_aux[1].toLowerCase().startsWith("http"))
msg_aux[1] = "href=\"http://" + msg_aux[1];
else
msg_aux[1] = "href=\"" + msg_aux[1];
msg_text = msg_aux[0] + msg_aux[1];
}
msg.setText(Html.fromHtml(msg_text));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());
Thank you.
EDIT on the code, these lines:
else
msg_aux[1] = "href=\"" + msg_aux[1];

RadioButton not unchecking when another in the same RadioGroup is checked

This is a duplicate of: Android dynamically generated radio buttons not unchecking once setChecked programmatically
However in that question the answers didn't solve the question because all the code I added to try to solve the problem muddied the issue. In addition I have also applied several potential solutions below since then.
The problem is that if I check a RadioButton programmatically, it stays checked when I select another RadioButton in the same RadioGroup.
I have learned several potential solutions from Googling that I have applied but have not worked, such as:
Using RadioGroup.Check instead of RadioButton.setCheck
specifying each RadioButton's Id explicitly
Adding the RadioButton to the RadioGroup before checking it
Adding all the RadioButtons to the RadioGroup before checking one of them
despite all this, the RadioButton still stays checked when you click on another.
private void PopulateAccessPoints(List<clsAccessPoint> accessPoints){
try{
rg = new RadioGroup(this);
rlaAccessPoints.addView(rg);
int i = 0;
for (clsAccessPoint acp : accessPoints) {
i++;
RadioButton rd = new RadioButton(this);
rg.addView(rd);
rd.setId(i);
rd.setText(acp.ID + ": " + acp.Name);
}
rg.check(rg.getChildAt(1).getId());
} catch (Exception ex) {
ex.printStackTrace();
}
Note: The RadioButton that should be checked will be determined dynamically, but I have hardcoded it as the above exhibits the same symptoms and is simpler.
I've spent several hours on this now so will appreciate any help for this apparently very simple problem!
RadioButton rd = new RadioButton(this);
rg.addView(rd);
rd.setId(i);
rd.setText(acp.ID + ": " + acp.Name);
try moving this code around to be like this:
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(acp.ID + ": " + acp.Name);
rg.addView(rd);
setting the ids before you add it to the group?

Removing newline from string

Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:
Episode 1
Episode 2
Episode 3
etc.
I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1
Basically the only reason I have a custom adapter is so I can do something like this:
textView.setText("Episode " + cursor.getString("column_for_episode_number");
The problem is, I get a list that looks like this:
Episode
1
Episode
2
Episode
3
When I try something like this(which worked in a different portion of my code):
String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");
I get the exact same list output :(
Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/
text.replaceAll(System.getProperty("line.separator"), "");
There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Check if there is new line at the beginning before you replace and do the same test again:
for(int i=0; cursor.getString("blah").length()-1; i++)
{
if(cursor.getString("blah").charAt(i)=='\\n') <-- use the constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.
This could help you:
response = response.replaceAll("\\s+","");
It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?
this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}
have you tried
cursor.getString("blah").trim()

Categories

Resources