create On/off button with slide - android

I want to create on/off button with slide .
I am using this Technic:
I put the buttons in the same position and one is visible and the other is hidden.
and when I click on one the other button is appeared and the clicked is disappeared.
Now How can I make that button slide-able.
Here is the buttons:
How can i do this ?

try using Switch but, if you are ok with minSdkVersion>=14.
hope following code helps you out.
In Layout Xml create a switch like:
<Switch
android:id="#+id/on_off_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"/>
Then in your activity get a switch from layout and set listener as follows,
Switch onOffSwitch = (Switch) findViewById(R.id.on_off_switch);
onOffSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"isChecked"+isChecked, Toast.LENGTH_LONG).show();
}
});

Related

Have BOTH an onLongPress menu and select-copy text on aTextview item?

My App contains a RecyclerView with TextView items. On each TextView item I have defined a few functionalities, like sharing the text to another App.
How can I combine both onLongPress or onCreateContextMenu with (enabling) select-and-copy text? It is OK to enable the select-and-copy text from the context menu.
Of course I could use the for selecting text. But that conflicts with the long press (context) menu on the textview item.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/pwTextView"
android:enabled="true"
android:textIsSelectable="true" <== mandatory
android:focusable="true" <== optionally
android:longClickable="true" /> <== optionally
Of course I would like to have the context menu back after select-copying the text.
Is this functionality realistic? Yes. For the app this is essential. I have seen it in other apps as well ;-)
The below solution helps me for most situations.
I want text to be either selectable (for copy and paste) or want other gestures to work.
Initially setting the gestures on the text field:
Set textIsSelectable to false, either in the layout file or programmatically.
Set an onTouchListener on the textview with your Gestures.
Allow one of the gestures to switch to textSelection mode. See below.
How to set textSelection programmatically?
Set the textIsSelectable, focusable, longPressable to true
Set the onTouchListener to null.
Install a clickListener on the textview to allow you switch back to the original TouchListener.
1) Install your GestureHandler:
// Create your Touch Listener
onTouchListener = new OnSwipeTouchListener(mCtx, this);
view.setOnTouchListener( onTouchListener);
2) Switch to textselction modus:
// Create your popup with an menu option to switch to textselection modus:
PopupMenu popup = new PopupMenu(mCtx, view);
popup.inflate(R.menu.text_options_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case ...
case R.id.text_textisselectable:
view.setOnTouchListener(null);
((TextView)view).setTextIsSelectable( true);
((TextView)view).setFocusable( true);
((TextView)view).setLongClickable( true);
// Install a click listener to switch back to the previous Touch Listener
((TextView)view).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupx = new PopupMenu(mCtx, view);
popupx.inflate(R.menu.selecttext_back_menu);
popupx.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
((TextView)view).setTextIsSelectable( false);
((TextView)view).setFocusable( false);
((TextView)view).setLongClickable( false);
view.setOnTouchListener(onTouchListener);
return true;
}});
popupx.show();
}
});
break;

Custom switch button?

I am using a switch button in an android studio activity.What I want to do is that when I slide from left to right,in the right side to appear a timer,and in the thumb part of switch button to have an image.Too a need it to not appear the track part before starting sliding the thumb!How it is possible to make that?
as Anjal Saneen said in the comments, you can set the visibility programmatically. You can call your switch and then set visibility for your components like so:
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myTimer.setVisibility(View.VISIBLE);
myImage.setVisibility(View.VISIBLE);
}
});

How to get onClick event on radioButton in android

I am trying to finding a way to get onClick event on radio button. I know that there is way to get a selected radio button value like this:
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
But I have a requirement, that when user click/select radio button by touch on a screen then do some logic. So while using this way when i choose a radio button which user previous selected then radioButton.setOnCheckedChangeListener is calling. It is little bit difficult to distinguish that who is calling onCheckedChanged event from code(when show previous selection) or user himself click on screen.
Can someone tell me how i can find onClick on touch event on radioButton(not radioGroup)
Just set onClick in XML file like this :
android:onClick="Button1"
After this, in your java file :
OnClickListener yourRadiolistener = new OnClickListener (){
public void onClick(View v) {
//Do whatever you want to do
}
};
RadioButton Button1 = (RadioButton) findViewById(R.id.yourFirstRadioButton);
Button1.setOnClickListener(yourRadiolistener);
try this to get click listener to your radio button
RadioButton radioButton = (RadioButton) findViewById(R.id.yourRadioButton);
radioButton.setOnClickListener(radio_listener);// set listner to your radio button
than create a new radio_listener using below code
OnClickListener radio_listener = new OnClickListener (){
public void onClick(View v) {
//perform your action here
if(v==radioButton){
//perform your action here
}
}
};
you can get the id of each view that has trigered the checkchange event so get the id inside the
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.getId()==firstButton)
compare the id and apply further logic.
Alternatively, you can set the click handler directly in XML:
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickHandler" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickHandler" />
</RadioGroup>
Your activity must then implement the onClick handler:
public class MainActivity extends AppCompatActivity {
...
public void onClickHandler(View view) {
public void onClick(View view) {
switch (view.getId()) {
case R.id.radioButton1:
...
break;
case R.id.radioButton2:
...
break;
}
}
}
}
Here are the official docs: https://developer.android.com/guide/topics/ui/controls/radiobutton.html

Android Switch button inside RecyclerView's every item

I am working with RecyclerView and using Switch component in every item of it.
<Switch
android:id="#+id/list_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
android:layout_toLeftOf="#+id/list_item_price"
android:textOff="#string/switch_save"
android:textOn="#string/switch_buy"
android:layout_centerVertical="true"
/>
I am using setOnCheckedChangeListener event to listen to state changes
holder.mSaveBuySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isPressed()) {
moveItemDialog(pos, isChecked);
}
}
});
Use of buttonView.isPressed() is to identify whether the setChecked on Switch is called by User or it is called programmatically by code.
Now it is working perfect When we click on Switch.
Problem - How can I make it work when we slide the Switch? in Android 4.0+ Switch component present nice ready made toggle button but I need to know when user slides the switch so that I can execute the same code which is executed when it is pressed. That is,
if (buttonView.isPressed()) {
moveItemDialog(pos, isChecked);
}
Thank You.
I know this question is too old. But I've try with android.support.v7.widget.SwitchCompat it's also works with sliding.
This my sample code and some debug log.
<android.support.v7.widget.SwitchCompat
android:id="#+id/saklar_switch"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Debug Code
Log.d("SAKLAR","Pos: "+position+" buttonView.isPressed()"+buttonView.isPressed());
Log.d("SAKLAR","Pos: "+position+" buttonView = isCheccked? "+isChecked);
Debug Result
Pos: 2 buttonView.isPressed()true
Pos: 2 buttonView = isCheccked? true
isPressed() debug also return true while I'm sliding the switch button.

How to identify mute/unmute click - SOLVE

my problem is following:
I want indentify (the keyevent) when the user click in the mute/unmute button and so change the satus of toggle button.
One image to understand better: https://lh6.googleusercontent.com/-yHjyvtCiyac/Ul6uQz_Ga2I/AAAAAAAAAiY/Ug06zyq7_Vg/w619-h81-no/volumebar.jpg
How do this?
Thanks.
Sorry for my english. I use Google Translator for do this >.<
Update: The mute/unmute button in XML file :
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/audiobtn"
android:textOn=""
android:textOff="" />
Last Update: The problem was solved using the BroadcastReceiver class. Thanks for the help!
For Toggle Button
add this line in toggle button xml
android:onClick="onToggleClicked"
Now in your activity class set this listener as
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// set image for on state
} else {
// set image for off state
}
}
More info for Toggle button can be find here
For simple button
Step 1 Create object on your button
Button muteBtn=(Button)findViewById(R.id.mute_btn);
Step 2 Create the click listener of this button and change it to pressed/non pressed state using a boolean which stores its state like this.
boolean muteBtnSelection;
muteBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (muteBtnSelection) {
muteBtnSelection=false;
// set yout image for mutebtn_pressed
muteBtn.setBackground(R.drawable.mutebtn_pressed);
} else {
muteBtnSelection=true;
// set yout image for mutebtn_not_pressed
muteBtn.setBackground(R.drawable.mutebtn_not_pressed);
}
}
});
in this way you can toggle between pressed and non pressed state.

Categories

Resources