Update textview on android - android

the problem is that my layout is to type on and I need a TextView move from one party to an 'other screen, to do this I make sure that the layout view and then remove all face appear again the same TextView in such a way as not to have that the TextView be "dragged" across the screen.
Here's my code:
Textview textview = new TextView(context); //context was been defined
poiView.removeAllViews();
for (int j = 0; j < poiP.length; j++) {
//code
texview.setText("iojforj");
poiView.addView(textview, params); //params was been defined
textview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
function (v.getId());
}
});
Now the problem is that the TextView seems, it works, moves and so on but will not let me access the method Mr clicks because the TextView is removed and put in the time.

Instead of removing and re-adding the textView, you should just mess with it's visibility.
So instead of removing them and adding them later on, you should just do
textView.setVisibility(View.INVISIBLE); //to make it disappear
textView.setVisibility(View.VISIBLE); //to make it reappear
that way they will keep their assigned OnClickListeners
however if this is running from a different thread, you need to put this in a runOnUiThread() call.
EDIT:
lets try this
Textview textview = new TextView(context); //context has been defined
poiView.removeAllViews();
OnClickListener buttonListener = new OnClickListener() { //listener here }
for (int j = 0; j < poiP.length; j++)
{
//code
texview.setText("iojforj");
poiView.addView(textview, params); //params was been defined
textview.setOnClickListener(buttonListener);
}

Related

Find child of a LinearLayout inside a LinearLayout

I have a LinearLayout ("ll") that is already created in xml and the app dynamically creates another LinearLayout inside of it and creates an EditText and a Button inside of that view. The button makes the whole LinearLayout destroy itself along with the EditText and Button inside it (the whole system is a player name entering activity). Anyway, I am trying to find a way to get the text from all of the EditTexts. I have tried using a for loop on "ll" and using ll.getChildAt() but I can't seem to use .getChildAt() on whatever ll.getChildAt() generates because getChildAt() generates a "View" not a "LinearLayout." I basically just need a way to search two children in, rather than just one. Also, if there is just a better way I should be doing this, let me know. I'm open to suggestions.
Here's my code if it will help:
NewGameCreate.java
public class NewGameCreate extends Activity {
int numOfPlayers = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game_create);
}
public void newPlayer(View view) {
numOfPlayers++;
final LinearLayout ll = findViewById(R.id.playerView);
final LinearLayout llNew = new LinearLayout(getApplicationContext());
llNew.setOrientation(LinearLayout.HORIZONTAL);
llNew.setId(numOfPlayers);
ll.addView(llNew);
EditText newName = new EditText(this);
newName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newName.setHint("Enter Player Name");
newName.setId(numOfPlayers);
newName.setWidth(0);
llNew.addView(newName);
final Button delete = new Button(this);
delete.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
delete.setText("Delete");
delete.setId(numOfPlayers);
delete.setWidth(0);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = delete.getId();
ll.removeViewInLayout(findViewById(id));
Drawable back = ll.getBackground();
ll.setBackgroundColor(00000000);
ll.setBackground(back);
ll.invalidate();
}
});
llNew.addView(delete);
}
public void startGame(View view){
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for(int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = ll.getChildAt(loop);
}
}
}
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!

Android layout doesn't refresh after calling removeAllViews()

I have a Linearlayout called "resultView". I then dynamically added many textViews in it after clicking a button.
I want to remove all the textviews that I just created when I click the button again.
btn_search.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View view) {
resultView.removeAllViews();
String strFileName = et_fileName.getText().toString();
searchFiles(strFileName);
}
});
public void searchFiles(String strFileName){
....
for (int i = 0; i < fileList.size(); i++) {
TextView textView = new TextView(this);
textView.setText(fileList.get(i).getName());
textView.setPadding(5, 5, 5, 5);
if (fileList.get(i).isFile())
resultView.addView(textView);
}
}
App Screenshot, All the listed results are dynamically created textViews.
My XML, the id "view" is my resultView.
But resultView.removeAllViews(); doesn't work. The results are still appened.
Calling resultView.invalidate(); after that doesn't work either.
What should I do to make the layout refresh?
Use .invalidate() to upddate UI.

disable button click for certain rows of buttons

I dynamically create Buttons by entering a word. If I write "met", it appears on the screen - one Button per letter. The same thing happens for the next word I enter, and it appears below the previous word --- as shown in the image above.
When I click on a Button it turns green. My question is, what is the best way to disable the clicking of a row of Buttons. Meaning, if the user clicks on the 'm' in "met" I want the user to only be able to click on the Buttons in "met" and to not be able to click on any of the Buttons in "had", "goes", or "ran"
Here is my code:
EDIT
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++){
final Button dynamicButtons = new Button(view.getContext());
dynamicButtons.setLayoutParams(rlp);
dynamicButtons.getLayoutParams().width = 130;
dynamicButtons.getLayoutParams().height = 130;
dynamicButtons.setTag("0");
dynamicButtons.setId(1);
dynamicButtons.setText(edit_text_array[i]);
dynamicButtons.setBackgroundResource(R.drawable.button);
button_list.add(dynamicButtons);
linearLayout2.addView(dynamicButtons, rlp);
dynamicButtons.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
int i=0;
LinearLayout ll = (LinearLayout) dynamicButtons.getParent();
for(i=0; i<list_of_ll.size();i++){
if (ll == list_of_ll.get(i)){
list_of_ll.get(i).setId(i);
break;
}
}
if(list_of_ll.get(i).getId()==i)
ButtonOnClick(view);
}
});
}
linearLayout2.setId(0);
linearLayout2.setTag("0");
list_of_ll.add(linearLayout2);
EDIT
I created a List of the LinearLayouts for each row of Buttons. The Buttons turn green if the id of the LinearLayout is set to 1. When I click on a Button I want that LinearLayout to stay at 1 and have all other rows/LinearLayouts set to 0 so they become unclickable.
Currently, every Button I click turns green even if it's in a different row. Can someone please help me solve this issue?
Why you don't set Id in the for loop so that you are able to refer and set the onlicklistener to null like jpcrow already mentioned.
Set Id like:
YourCreatedBtn.setId(i+1);
//Id's setted programmatically don't.
have to be unique... But they should be
a positive number (referring to the
android documentation)
And in your on click method simply set onclicklistener for specified Id's to null. Just a hint, hope it helps
Update regarding Thread-openers Comment
I found two simple ways but i would prefer the one which is not commented out in the buttonIsClicked:
LinearLayout llrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llrow = (LinearLayout) findViewById(R.id.test_layout);
//Adding 5 Buttons
for(int i = 0; i<5; i++) {
Button mybtn = new Button(this);
//set LayoutParams here
mybtn.setId(5);
mybtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonIsClicked(v);
}
});
llrow.addView(mybtn);
}
}
private void buttonIsClicked(View v) {
/*ArrayList<View> myButtons = llrow.getTouchables();
for(int i = 0; i < llrow.getChildCount(); i++){
myButtons.get(i).setOnClickListener(null);
}*/
for(int i = 0; i<llrow.getChildCount(); i++){
llrow.getChildAt(i).setOnClickListener(null);
}
}
It's just a simplified Version of your code, but i'm sure you will get the Content..
What if found out is, that you don't have to set the ID in both cases.. You can easily get all the child over
YourRowLinearLayout.getChildAt(starting from 0 to n-1-Views you added)...
I didn't found a way around the for-loop... But this small-little loop will not break your neck regarding to Performance..
The outcommented-code is the second Approach, finding all the Child over getTouchables which logically leads to an ArrayList and that's exactly the reason why i don't like it. You have to initialize an arraylist...... However, this also won't break your neck regarding to Performance but a penny saved is a penny got! ;) Hope it helps and everything is clear. Both of them work! Please mark as accepted answere if it fits your Needs...
You have to distinguish between the two rows, either add them to different ViewGroups or you can use View.setTag(int key, Object tag)

setOnClickListener not working

I've tried to follow the several other times this question has been asked but all other reports of what's working seem to be the same as mine, except mine doesn't work. It compiles and runs and doesn't crash, but it simply does not do what it's supposed to do. In the code, the "Alpha" error log DOES NOT occur, so it is not recognizing that it's clicked.
Here's the relevant snippet of code. Any ideas?
for(int i=0; i<[big long statement]; ++i)
{
final TextView resourceText= new TextView(ctx);
resourceText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
resourceText.setText([big long statement]);
resourceText.setTextSize(18);
resourceText.setClickable(true);
resourceText.setFocusable(false);
resourceText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Alpha", "Alpha");
resourceText.setTextColor(Color.RED);
}
});
scrollLinearLayout.addView(resourceText);
}
This is where scrollLinearLayout is created
//Add Linear Layout for the scrollview
scrollLinearLayout = new LinearLayout(ctx);
scrollLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
scrollLinearLayout.setOrientation(LinearLayout.VERTICAL);
//scrollLinearLayout.setId(MyR.Ids.ROOMDIALOGFRAGMENTLL_ID);
resourceScrollView.addView(scrollLinearLayout);
Thanks
Maybe you should move the listener away from loop :
for(int i=0; i<[big long statement]; ++i)
{
final TextView resourceText= new TextView(ctx);
resourceText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
resourceText.setText([big long statement]);
resourceText.setTextSize(18);
resourceText.setClickable(true);
resourceText.setFocusable(false);
resourceText.setOnClickListener(onClickListener);
scrollLinearLayout.addView(resourceText);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Alpha", "Alpha");
((Button)v).setTextColor(Color.RED);
}
};
it is because your linearlayout encompasses the textview, resourceText. If you allow the LinearLayout to consume the clicks, then the textview will never see a click. You need to Try adding android:descendantFocusability="afterDescendents" to the LinearLayout

Refresh tableLayout

Hi I have a tableLayout and am populating the layout with ImageView[][] using a nested for loop. I'm currently trying to click on an ImageView and rearrange the ImageView[][] data then refresh the screen to reflect the rearrangement. Is there a way to do this? Apparently I can't call the setContentView more than once.
Edit: you can see this is the code for the onclick event. I setContentView of the original TableLayout ('tl') in the onCreate event and can't call it again here.
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case 1:
image_array = switchTile(image_array, 2,1,0,0);
Toast.makeText(this, "1 clicked!", Toast.LENGTH_LONG).show();
}
tl.removeAllViewsInLayout();
for(int i = 0; i < level; i++){
TableRow new_tr = new TableRow(this);
new_tr.setLayoutParams(layout_image);
for(int j = 0; j< level; j++){
new_tr.addView(image_array[i][j]);
}
tl.addView(new_tr);
}
tl.invalidate();
}
try this: tableindex.removeAllViews();
If all you want to do is move the images around, you can just do setImageBitmap () operations on the existing ImageViews. You can use the same bitmaps and display them on whichever ImageViews you please.

Categories

Resources