Impossible to update the text of a textview in a RelativeLayout - java

I have a simple screen created in xml, the parent layout is a "relativelayout" and i have a child layout (who is also a "RelativeLayout" containing 3 textview inside).
the only thing I have to do is press a button and change the values ​​of textviews.
to change the text is obviously this:
MyTextView.SetText("Text");
the code runs perfectly, but does not refresh the text of the textviews in the layout.
but when the screen is rotated, the screen refreshes, and the label gets the correct value.
Why does this happen? Why when pressing the button I can not update the text?
I tried using "AsyncTask" and the text is not updated either.
Did something simple can be so problematic. ?
greetings.

Your code
MyTextView.SetText("Text");
Should be executed on UI thread to give effect say suppose if you want it to be updated after button click then this code should be inside your onClickButton listener of your button
Visit This Link for more details

Related

How to expand a cardView to show hidden elements in android with a dropping animation?

I am beginner in android animations and therefore, am wondering exactly how can I implement something like expand a cardView to show hidden options or elements.
For example: a post in social networking, we have a cardView holding the post info. it also has likes and comments buttons/options. When user taps on comments button the view should expand a little say certain height and show the comments and shrink back when the same button is tapped again.
I am listing my posts using RecyclerView in a Fragment using a CardView. I am looking for a soft animation along with expanding and shrinking back to original place or size.
Below is a screenshot that might help you understand what exactly I want:
How to do this? Will you please provide me a small snippet of code?
use wrap content as height of cardview and use textview inside it below title, so on click make the textview visible and invisible.
And use android:animateLayoutChanges="true" in parent layout.

how to make a checkbox editable when clicked in android studio

im trying to make a to do list in android studio with checkboxes, the idea is to be able to add checkboxes to a linear layout every time i press a button. this is pretty simple but i want to be able to change the checkbox text when i click on it after ive created it.
i tried to make the text box with no text and just place a plain text beside it but i havent found a way to place both of them on the same line of the linear layout. the plain text just appears on the line underneath. i would like to know if theres any way to make a custom component that would make the checkbox text editable when clicked on the text and check the box when clicked on the check box itself that i could place it in the linear layout.
this is the code ive done to add another checkbox to the linear layout:
public void click(View v)
{
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
CheckBox cb=new CheckBox(this);
cb.setLayoutParams(lparams);
cb.setText(t.getText());
this.ll.addView(cb);
}
Check the Checkbox what is it: Checkbox source
Now you know you have a "CompoundButton" , which is a Button if you look at the
source
which is a TextView if you check the source source
The EditText, which is used for editing texts it is a TextView if you check the source
So the parent it is a TextView and in your custom component you must combine the EditText added functions and values to your CheckBox. If you have time and you aren't lazy you can do it for sure, just extend the CheckBox and add the event and handling.
Solution ofered by BAHMAN is fast and simple ( but not enough elegant for me), if you want a quick result you can do that solution or based on that one:
Declare a View named "container" in .xml layout. At your code ( runtime ) first add the Checkbox. On the Click event remove the Checkbox ( if you arenn't added at .xml, but in code you can do it ) and add an Edit text, where you listen the finishing editting event. Then you remove the Edit text and put back the Checkbox. In this case you have not doubled the components and listeners.
Since it is a TODO list, can have many Checkboxes, I would choose the last solution offered by me. If you do an expensive, commercial product, than the first solution offered by me.
Instead of adding a check box to your layout you can add a horizontal linear layout or a custom view extending LinearLayout containing a CheckBox and an EditText at first set visibility of CheckBox to gone when user enters some text in EditText and presses Enter set CheckBox label equal to text of EditText then set visibility of EditText to gone and set Visibility of CheckBox to Visible. Again when a check box is clicked do similar job.
It is better for you to create a custom View (a class extending LinearLayout) and do above job in that class. and every time you need a new check box you can add this custom view to your layout instead of adding check box to your layout.

How to create this Setting Layout in android?

I want to create a settting UI like this in android:
Questions:
How to create the layout? I believe it's a Vertical LinearLayout containing some of TextView with Divider set, is that right?
How to create sub-textview? (see the image above)
How to create a checkbox which aligned at right? (again, see the image above)
Look at the image above, the "Keypress popup" can be clicked and it will show up a dialog. How to create a clickable TextView? I have tried giving android:onClick on the TextView, but nothing happened when I clicked the TextView.
PreferenceActivity will help you to implement all these very easily. Go through this and this tutorials.
PreferenceFragment should be used post Honeycomb.

SWT button position after resizing

I'm working with SWT. I created a button, and it supposed to change its text after being pressed.
The second text is wider than the first one, and the button does not being re-sized appropriately.
so I used button.pack(), and setSize() functions. The real issue is actually with its alignment in the shell after this size changing.
it's being expanded to the right instead to the left.
I guess it's actually the normal behavior, but I'm not sure where to change it.
I tried to change things by creating a gridData but it only did worse.
The button parent is tabFolder, and the last consists of a gridlayout with two uneven columns, the spesific button is on the second column.
I tried to change things with the margins and few others, but without success.
currently all the configurations are default.
This is my button:
This is how I would like it to behave:
any help would be highly appreciated.
many thanks!
i would need some code to be sure, but it looks like you need to call tabFolder.layout(true) after changing the button text to compute the new position of your button

Android Button background Image changes back to state in XML

I have a simple soundboard which uses Buttons to represent the sounds. When a sound button is clicked. The background image is changed using the view.
Button butt = (Button)view;
butt.setBackgroundResource(buttons_on[media]);
buttons_on is a int[] representing the drawables of the buttons.
This all works perfect, but I also use a FragmentActivity to create a paged App. The App has 4 different pages which you can swipe through. When I change a button using the above code and swipe two pages to the right and than swipe back. The image of the button has changed back to it's default defined in the page.xml.
How can I prevent this behavior?
change the Buttons to ImageButtons and setting the src?
somehow prevent the page from reloading
Instead of doing that you should look at how to use a StateListDrawable. It will simplify your approach.

Categories