Update Text View as User Click on it - android

I have 8 TextView in my Linearlayout and What I am trying to achieve is as soon as user clicks on any TextView it should get updated with the next Textview below it ...
I am trying to make simple game where Image will be randomly displayed and the user has to find that Image name from the List displayed as different 8 TextView...
eg : If there is 8 TextView with List Dog, Cat , Bike , Horse, Cow etc.. and say Image of Cat has shown up then I want cat to disapper now every textview should get updated one row above so I can add more views at the end ...
I hope am not confusing because I am confused here ...

If you place all of the TextViews in an Array or a List, then it becomes simple array manipulation:
public void onClick(View pressed){
boolean tvFound = false; //have we found the clicked tv yet?
for(int i=0;i<mTextViews.length;i++){ //loop through all tvs
if(pressed.getId() == mTextViews[i]){ //if this is the tv that was clicked on
tvFound = true;
}
if(tvFound){ // if we have found the tv that was clicked on
if(i<mTextViews.length-1){ //if we arent on the last tv
mTextViews[i].setText(mTextviews[i+1].getText()); //then replace txt with next tvs text
}else{ //if we ARE on the last tv
mTextViews[i].setText(""); //replace text with nothing
}
}
}
}
I also want to note that if you don't want to loop through all of the TextViews to find the one that was clicked on, you can map the ResId of each TextView to its index in the array, then you can manipulate just the necessary TextViews.

Related

Set Id to dynamically created edittext

I'm dynamically creating Edittext on a button click and saving this edittext to an ArrayList of type Edittext and then getting values of all edittext in an ArrayList of type String. While creating new Edittext Iam also creating a BUTTON with cross image which delete that edittext when user click on it. and then Iam passing these edittexts to Options of CheckBox question which is also Iam creating dynamically.
Issue is when I create multiple edittext and randomly delete someone and click ok arraylist remove the last index of it .Iam also setting ids to Edittext but couldnt able to understand how to get the Id of the same Edittext whose value I want to delete and then remove him from ArrayList too.
Here I'am creating the Edittext and adding them in "moreOptList" with there ids.
EditText checkBoxMoreEdt = new EditText(this);
checkBoxMoreEdt.setId(i);
moreOptList.add(i, checkBoxMoreEdt);
Here Iam deleting the view on Button clickListener,but instead of deleting the the exact value from arrayList also its only delete the view and the last index of array from the list.For example if I have enter 4 options
a
b
c
d
and then deleted the option 2 which is b and click ok,it will show me
a
b
c
what its doing is removing the last save value from arrayList now How should I get the Id of exact edittext I want to delete and delete it from arrayList also.
final Button button = new Button(this);
button.setId(b);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chkbParentLayout.removeView(ll);
moreOptList.remove(i);
}
});
Most probably the problem is in ArrayList. When you remove element by index all elements are shifted, hence indexes do not match your views:
list.add(0, 0) //[0]
list.add(1, 1) //[0, 1]
list.remove(0) //[1]
list.remove(1) //throws IndexOutOfBoundsException
One options is to replace ArrayList with HashMap:
map = HashMap<Integer, View>()
map.put(i, view)
...
group.remove(map.remove(i))
Another useful thing is to use setTag/getTag methods of View, you can store your indexes there, so you might not need to have external indexes storage:
view.setTag(index)
index = view.getTag()

spinner value on textView automatically android

In my task, I have two spinners and one textView. I choose a city from the first spinner than, I choose a person from the second spinner. On textView, I want to display some information according to the selected person in the spinner. In my program, person and textView parts are populating from JSON. My problem is that on my textView I can see only information about the first item of the spinner. When I select another person, textView does not update.
textView = (TextView)findViewById(R.id.textView23);
cenazeSpinner.setAdapter(new ArrayAdapter<String>(Celenk1.this,
android.R.layout.simple_spinner_dropdown_item,
cenazeList));
cenazeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> adapterView,View view, int position,long l){
textView.setText("Cenaze Töreni Tarihi: "+celenkList.get(position).getCenazeToreniTarihi()+ "\n"+"Cenaze Töreni Vakti: "+celenkList.get(position).getCenazeToreniVakti());
}
I tried to write update function under the class, however, it does not work.
From what I can understand your comments, spinner actually works fine, but you have problems with setting the textView. Below are some possible debugging attempts and possible explanations:
Are you sure textView is the TextView you want? What happens when you put textView.setText("" + position); inside the spinner does the textView change?
If the textview doesn't change, either the textView you are looking is not the same one you are referencing, or another piece of code is changing textView constantly.
If it does, then most probably the problem is with the celenklist. If you print the information in the celenklist to logs, are the logs different when different people are selected? Are we sure we are getting different data (compared to the default one)? Maybe we are selecting different items at celenklist but because the data is same, we cannot see it.

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.

List of Strings in multiple buttons

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.

numbering the textviews

I am new in android programming.
I am creating a app where I supposed to use multiple textviews. Number of TextViews go on changing according to number of records fetched from database.
Here, i was trying to number textviews as textView1, textView2, textView3,...
but as i am unaware about the number of records, i can not define them statically
is there any way to do so dynamically
like we do in PHP
e.g
$count = 1;
if(condition)
{
textView.$count;
$count++;
}
Thanx in advance.
You should create a List of TextViews and populate it dynamically
List<TextView> list = new LinkedList<TextView>();
Find the total number of record at runtime and create the TextView in a loop for each textView and finally ad it to the current layout.
For example:
for(int i=0;i<data.size;i++){
TextView tv=new TextView(this);
tv.setText(data.get(i));
currentLayput.addView(tv);
}
where data is some vector or Arraylist in which you can store the data from database.

Categories

Resources