Toggle button android - android

I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was trying
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isEnabled()==true)
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);

Your code has to be called each time you click on your button. so use :
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setTextSize(13.0f);
} else {
toggleButton.setTextSize(8.0f);
}
}
});
You can set OnClickListner with a easy method. In your .xml put the option
android:onClick="nameOfMethod"
And then in your code:
public void nameOfMethod(View v){
}
Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )

I put the solution here:
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isChecked())
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Use isChecked() instead of isEnabled()

You need to do some debugging.
Firstly:
if(mf.isEnabled()==true)
can be
if (mf.isEnabled())
Does mf.setTextSize(13) on it's own work as expected?
Add some logging:
if (mf.isEnabled())
{
// Add some logging, is this line reached correctly?
mf.setTextSize(13);
}
else
// Add some logging, is this line reached correctly?
mf.setTextSize(8);
Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

show/hide the content by clicking button

Hi I want to show/hide the content by clicking button. I am having a problem to hide the content by clicking button.
Here is my code for hiding the content
private boolean visible;
protected Button SearchButton;
private void Toggle(){
if(visible=false){
DishButton.setVisibility(View.INVISIBLE);
SpoonButton.setVisibility(View.INVISIBLE);
cupButton.setVisibility(View.INVISIBLE);
FridgeButton.setVisibility(View.INVISIBLE);
}
else {
DishButton.setVisibility(View.VISIBLE);
SpoonButton.setVisibility(View.VISIBLE);
cupButton.setVisibility(View.VISIBLE);
FridgeButton.setVisibility(View.VISIBLE);
visible=true;
}
}
if(visible=false)
is not gonna work!
Use if(visible==false).
Note that you can use View.GONE to hide the content and free the empty space.
From your comments and question it seems like
You have not put any listener to your button.
You have written = in place of ==.
You have user View.INVISILE which will permanently hide the element it will not come back. SO use View.GONE
You have some logic flaw in case of handling visible/invisible.
You have not initialized visible boolean with true because as first time you are showing all the buttons so it should be true.
So possible solution is
In your onCreate() method add
visible=true;
SearchButton.setOnclickListener(new OnClickListener()
{
public void onClick(View v)
{
Toggle();
}
});
And make the Toggle method look like
private void Toggle(){
if(visible==true){
DishButton.setVisibility(View.GONE);
SpoonButton.setVisibility(View.GONE);
cupButton.setVisibility(View.GONE);
FridgeButton.setVisibility(View.GONE);
visible=false;
}
else {
DishButton.setVisibility(View.VISIBLE);
SpoonButton.setVisibility(View.VISIBLE);
cupButton.setVisibility(View.VISIBLE);
FridgeButton.setVisibility(View.VISIBLE);
visible=true;
}
}

Programmatically hide views if it is shown. Show views if it is hidden

My button use code that shows and hides the views:
public void onClick (View v){
if (What code you need to enter here to determine hidden views or shown)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
}
What code I need to add in the "if()", so that clicking on my button was checked condition. If the activity is hidden, it should be shown, and Vice versa. If the views is shown, hide it.
I'm guessing since you are using setVisibility, that you want to check the visibility of a View , not an Activity.
In that case you just use getVisibility()
(I used != cause the visibility could be IINVISIBLE as well, change per your needs) :
public void onClick (View v){
if (testActivity.getVisibility() != View.VISIBLE)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
} });
Don't understand why, but only that removed the answer of a man who has solved my problem. Here is his response, and this code works:
public void onClick (View v){
if ((testActivity.getVisibility() == View.VISIBLE))
{
testActivity.setVisibility(View.GONE);
}
else
{
testActivity.setVisibility(View.VISIBLE);
}

Change EditText value from text to password and vice versa on ToggleButton checked

I am studying programming in Android and I'm hitting an issue. I have this toggle button and when I input a text and the button is on, the text is starred (passworded, call it whatever you want) and when the button is off, the text is a text. However, I am hitting an issue and I don't see the problem.
The block of code looks like:
toggleCommand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(toggleCommand.isChecked()){
input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
I don't see the problem. Can you tell me what did I do wrong and explain?
When I turn on the application.. I type something in and it's passworded. I press the button to uncheck it and the passworded text turns into a text. I press the button again and instead of getting passworded again, the text stays normal.
Here is the working code:
toggleCommand.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if (toggleCommand.isChecked())
{
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
else
{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
More info: http://thenewboston.org/watch.php?cat=6&number=27
Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa
edit: Further explanation on setInputType here: http://developer.android.com/reference/android/text/InputType.html
Try this:
toggleCommand = (ToggleButton)findViewById(R.id.the_id_of_your_togglebutton) ;
toggleCommand.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked)
{
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else
{
input.setTransformationMethod(null);
}
}
});
You can read more about it on the documentation.
UPDATE
To make this kind of manipulation, you can use TransformationMethod.
So, to set the text to a hidden password, you must use PasswordTransformationMethod, and to turn it to text again, you just set the TransformationMethod to null, this way, no transformation will be applied to the text.
Hope this helps you.

Trying to get if click on one radion button the other is unclicked

I am trying to find how if you click on one radio button the other is unclicked using java not XML. I have both of my radioButtons in a tableRow. When I put them in a radioGroup is messes up the rest of my XML layout. Basically,if one is clicked make sure the other in not clicked.
public void drivable(View v) {
if (notDrivable.isChecked()) {
notDrivable.toggle();
}
}
public void notDrivable(View v) {
if (drivable.isPressed()) {
drivable.toggle();
}
}
I figured it out. I needed to change it from toggle() to notDrivable.setChecked(false); Here is the code that works.
public void drivable(View v) {
if (notDrivable.isChecked()) {
notDrivable.setChecked(false);
}
}
public void notDrivable(View v) {
if (drivable.isChecked()) {
drivable.setChecked(false);
}
}
as a suggestion you can give it a try.... get the id's for your radio buttons and in their onclick's method make another one unchecked.
I think you can put an OnClick Listener to each button and with a boolean and if clause determine when a button is clicked if it is selected or not and act accordingly. I hope I helped you.

Categories

Resources