List of Strings in multiple buttons - android

I have a list of Strings (Video tags) and I wanna show each of them in a button (same as when you want to send a text for multiple users) and when I click on each an action happens for that box. I took a look at:
https://github.com/kpbird/chips-edittext-library
It generate each string in a box but finally I don't have access to specific item onclick.
Does anyone has any suggestion what to use or how to make it?!

Read out the array of strings and
programmatically add a button with the text of each
string to for example a LinearLayout.
pseudo code:
ArrayList<string> list = getResources.array("id");
LinearLayout l = (Linearlayout) findViewById(R.id.linearlayout);
foreach(String s in list)
{
l.addView(new Button(s));
}
Something like that.

Related

How to get user inputs from multiple fragments added to single activity

As shown in image, I have added the same fragment 5 times in activity by performing on click operation on ADD FRAGMENT button:
Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?
(Both buttons is in Main Activity)
First we need to traverse to the dynamic edit text linear layout and then only we can count how many edit texts are avaliable then read the values of corresponding edit text :)
//step:1
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearDynamicEditTextID);// change as your //dynamic EditTexts' parent Linear Layout id.
//step:2
for(int i=0;i<layout.getChildCount();i++)// Count how many children avaliable
{
View v = (View)layout.getChildAt(i);// get the child view
if (v instanceof EditText)// verifying whether the child is EditText for secure.
{
EditText editText = (EditText)layout.getChildAt(i);//thats it.
Log.w("Edit Text "+i+" Value" , editText.getText());
}
}
Now I want to get all user entered data from those 5 edittext on click
of GET DATA button. Is this possible?
Answer: Yes, this is possible.
How?
Solution: You can make public variables in the activity and you can access those variables from the fragment which you are adding. On click of GET DATA button, you can read those variables set by the different fragment and show them in the same Activity or in different Fragment.

Android get text from button

I have 6 Buttons on my screen
I need to check the text on each Button and if the result is a capital "X" then I need to make the Button invisible
I can get the text when a Button is clicked using this
Button b = (Button)view;
String text = b.getText().toString();
but how do you get it without clicking a Button?
Any help appreciated
mark
There are different options. If that is the only type of View then you can iterate through the layout's children with something like
for (int i=0; i<myLL.getChildCount(); i++) // assuming you have a LinearLayout named myLL
{
Button btn = (Button) myLL.getChildAt(i); // cast the View to a Button
String text = btn.getText().toString();
// check the text here and do what you need
}
Or you could put them in an ArrayList at the beginning then iterate through that and check the text with something like
ArrayList<Button> myBtns = new ArrayList<Button>();
myBtns.add(R.id.someBtnId);
// iterate through them when needed.
There may be easier ways with listeners and such but without more information this is the best I can give you.
In the onCreate of your activity (or the onCreateView of your fragment) you need to use findViewById to get the views on your app.
Once you have the views you can query their contents and set up listeners to see when they change.

ANDROID - textview find lineindex with string ; search in textview by linenumber

i know highlighting in text-view is possible
Highlight Textview Using EditText
and scrolling text-view is also possible
(got the scroll code from here and is successfully scrolling too)
textView scroll at first line
now the question is, i am searching and i want to highlight that text and navigate to it, when someone presses the search button, the highlighting part is perfect, now i can get the index of the word in the string, but not line number of the string in the text-view,
point is if i want to find a position of certain text in text-view, i.e. which line number is that on, how to do it ?
i found an answer for this, but later i realized its for iOS
Search occurrences of a string in a TextView
Correct if I am wrong, you want to know the position where a particular text is in a String? If so then you can do it by using the following
String text = "0123hello9012hello8901hello7890";
String word = "hello";
Log.d("startOfWordPosition",text.indexOf(word)); // prints "4"
Log.d("endOfWordPosition",text.lastIndexOf(word)); // prints "22"
As you see that it will tell you the position as to where the word is located but you have to think about case where a word may come more than once. If you are sure that word will occur only once then above code is perfect for you. If not, then you will have to somehow tackle the problem.
This is working for loading a file into a textview so that the user's last selection or cursor position is at the top of the screen.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
page.setSelection(pos, pos2);
Layout layout = page.getLayout();
scroll.scrollTo(0, layout.getLineTop(layout.getLineForOffset(pos2))); }
}, 500);
page is a TextView, pos and pos2 are ints and the two ends of the last selection by the user (they are the same int if it's just the cursor), scroll is a scrollview containing the textview. It is all in a Handler because of delay issues internal to Android's loading all these objects. Th filename, pos and pos2 are saved as settings on exit.

how to display text in edit text in the list view

My team and I are making an android chat application. We want to display the messages sent by the users in a ListView. How can we display the text entered in an EditText in the list view. Or is there any other way to achieve our goal?
It all depends on what type of adapter you're using to back the view. If you're using an ArrayAdapter, just add the text to the array each time you have a new message you want to display (for instance, every time a button was clicked). If you're using a CursorAdapter, you'd then have to add the text to the ContentProvider that the Cursor is viewing. Beyond that, I can't tell you much else. We'd need more details about what you're doing in order to answer any further.
I've done this with some different by your needs. I just get the editText values to my database and printed into EditText. Just see this code -
EditText b = (EditText)findViewById(R.id.editText1);
b.setVisibility(1);
output = (TextView) this.findViewById(R.id.editText1);
output1 = (TextView) this.findViewById(R.id.editText2);
String s1= output1.getText().toString();
this.dh = new DataHelper(this);
this.dh.insert(s1);
List<String> names = this.dh.selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n\n");
for (String name : names)
{
sb.append("\t - \t" + (name) + "\n");
}
this.output.setText(sb.toString());
Just change this with your needs. Try out using this
You can do this using the custom list view. In the custom list view use the custom adapter in which you can take an edit TextView. You can see this how to implement custom list view here. In this list view a TextView is used but you can replace this TextView with EditText view.

Android Edittext listviews?

I have been scouring the Internet for a way to use an input box at the top of the page, and have an add & remove button.
I want the EditText to write to the ListView, which in turn writes the list to a .txt-file.
I have tried breaking this down into multiple small sections of just simple things like
getting the input from the EditText to post to a TextView.
Organizing a ListView
writing to a file and
reading from a file.
If you have any advice about how to get the ListView to go from the EditText greatly helpful.
The best way to do this is to have an ArrayList<String> to store your Strings from the TextView, and in turn have that ArrayList<String> feed into the ListView. For example, for your Add button handler:
// Declare our variables
ArrayList<String> myStringList = new ArrayList<String>();
// OnClickListener stuff for Add button
..
myStringList.add(myEditText.getText().toString());
myListView.setListAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, myStringList));
..
// Close OnClickListener stuff
Make sure you refresh your list adapter afterwards to show the new values. Likewise if you're deleting, use myStringList.remove(index) to remove that string from the list and refresh the array adapter. As for writing to a text file, have a look at this thread.

Categories

Resources