How can I hide or show my phone number during a call without going into the settings?
you can implement this by using a check box :
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_Show" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
/>
_
CheckBox cbShow = (CheckBox) findViewById(R.id.cb_Show);
TextView text=findViewById(R.id.text);
text.setText("some text to hide");
cbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
text.setVisibility(VIEW.INVISIBLE);
else
text.setVisibility(VIEW.VISIBLE);
}
});
or you can use a toggleButton instead example
good luck
Related
I have crated 4 radiobutton in recyclerview but when i check one it checked but when i checked another one radiobutton then first one can not be unchecked
holder.cb_votes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (int i = 0; i < pollItems.size(); i++) {
if (isChecked) {
holder.cb_votes.setChecked(true);
} else {
holder.cb_votes.setChecked(false);
}
}
}
});
this is my xml
<RadioGroup
android:id="#+id/rg_cb_votes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<RadioButton
android:id="#+id/cb_votes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="Yes"
android:textSize="12dp" />
</RadioGroup>
Radiobutton in wikipedia says:
A radio button or option button is a type of graphical user interface element that allows the user to choose only one of a predefined set of options.
You would have to just use checkboxes instead of radio button for allowing multiple options to be selected,else you are doing something which is inherently wrong in a UI be it mobile/web.
:-)
This is supposed to be something pretty straight forward but for some reason, the listener for the Checkbox does not work.
Here's my layout :
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginLeft="#dimen/dimen_5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:textStyle="bold"
android:text="#string/my_text"
/>
<CheckBox
android:id="#+id/check_status"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content" />
</RelativeLayout>
And here's the implementation in my fragment:
CheckBox checkBox = mView.findViewById(R.id.check_status);
checkBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
LOGD(TAG, "IsButton checked ? "+ isChecked);
Toast.makeText(getActivity(), "Check",
Toast.LENGTH_SHORT).show();
}
});
Like I said, this is something supposed to be pretty straight forward, but the listener.....well it doesn't listen anything. I'm testing on Nexus 5x Oreo and a small Motorola Marshmallow.
At some point I even tried the setOnClickListener() method:
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LOGD(TAG, "IsButton checked ? "+ ((CheckBox)
v).isChecked());
}
});
The result is the same, I don't see anything in the logcat
Does anyone have a clue as to why the Checkbox's listener is not working properly ?
If you use DataBinding, set the attribute android:checked="#={viewModel.someState}" and later call this listener via ViewBinding like
binding.checkBox.setOnCheckedChangeListener { _, _ ->
//
}
setOnCheckedChangeListener won't be called.
You can set app:checkListener and use BindingAdapter:
#BindingAdapter("app:checkListener")
fun CheckBox.setCheckedChangeListener(listener: CompoundButton.OnCheckedChangeListener) {
setOnCheckedChangeListener(listener)
}
Or observe changes in someState:
viewModel.someState.observe(viewLifecycleOwner) {
viewModel.changeCheckBox()
// Other actions.
}
There are few reason to not works.
In your xml you have to at-lease define checked type as per your requirement.
android:checked="false"
In java file you have to required used setOnCheckedChangeListener().
Hope its work for all.
Most likely, setOnCheckedChangeListener() is not being called :)
I am new in android app development.
I am searching an example like bellow:
There is a checkbox and two buttons named btn1 and btn2.
Logic should be when user will checked on the checkbox then btn2 will visible and btn1 will invisible.
In my XML file:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/input_chk_provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:duplicateParentState="false"
android:text="#string/user_type_provider" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_sing_up"
android:background="#color/ic_launcher_background"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/btn_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_provider_dtl"
android:background="#color/ic_launcher_background"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
Without having the code...
You'll need a OnCheckedChangeListener for the Checkbox so you can adjust the button visibility when it's checked/unchecked.
Something like:
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.VISIBLE);
} else {
btn1.setVisibility(View.VISIBLE);
btn2.setVisibility(View.GONE);
}
});
In your XML use android:visibility="invisible" to set the visibility of the buttons.
In the code, get your views with findViewById()
CheckBox checkBox = (CheckBox) findViewById(R.id.check1);
Button yourButton = (Button) findViewById(R.id.btn1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if(isChecked){
yourButton.setVisibility(View.VISIBLE);
} else {
yourButton.setVisibility(View.INVISIBLE);
}
}
});
Use the method .setVisibility(View.VISIBLE) to change the visibility.
There are 3 states:
View.GONE
View.VISIBLE
View.INVISIBLE
Checkbox has OnCheckedChangeListener() method which will give you the state of the checkbox. Based on the state of the Checkbox you can use Button1.setVisibility(true/false); to show/hide the button.
try this..
<CheckBox
android:id="#+id/input_chk_provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:state_checked="true"
android:checked="false"
android:onClick="itemClicked"
android:duplicateParentState="false" />
public void itemClicked(View v) {
//code to check if this checkbox is checked!
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()==true){
bt1.setVisibility(View.VISIBLE);
bt2.setVisibility(View.INVISIBLE);
}else if(checkBox.isChecked()==false){
bt2.setVisibility(View.VISIBLE);
bt1.setVisibility(View.INVISIBLE);
}
}
i want code en eclipse android when i clicked checkbox i want to show hidden button
i'm beginner in android so please help me :(
this is my xml code :
<CheckBox
android:id="#+id/CB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:fontFamily="Segoe UI"
android:text="I Want To Select A Favorite Station And Receive Notification."
android:textStyle="bold" />
CheckBox checkBox = (CheckBox)findViewById(R.id.CB1);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
btn.setVisibility(View.GONE);//TO HIDE THE BUTTON
btn.setVisibility(View.VISIBLE);//TO SHOW THE BUTTON
}
});
where btn is your button.
Reference the checkbox in your Activity and set an OnCheckedChangeListener:
CheckBox checkBox = (CheckBox)findViewById(R.id.CB1);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do whatever
}
});
This is my code:
<CheckBox
android:id="#+id/sprint_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sprint_game" />
<CheckBox
android:id="#+id/marathon_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/marathon" />
<CheckBox
android:id="#+id/never_ending_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_ending" />
What i want to do is "detect" when one of these is checked and then set the other two to "disable", so the user can select only one at time.
I tried to use the ".setOnCheckedChangeListener", but i can't do that, can someone help me with some code?
Thanks a lot guys!
This is the way you are notified about checked changes:
CheckBox check = findViewById(R.id.sprint_checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});
You can also let your activity implements the OnCheckedChangeListener interface and then:
CheckBox check1 = findViewById(R.id.sprint_checkbox);
CheckBox check2 = findViewById(R.id.marathon_checkbox);
CheckBox check3 = findViewById(R.id.never_ending_checkbox);
check1.setOnCheckedChangeListener(this);
check2.setOnCheckedChangeListener(this);
check3.setOnCheckedChangeListener(this);
Overriding the interface method:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.sprint_checkbox:
//do stuff
break;
case R.id.marathon_checkbox:
//do stuff
break;
case R.id.never_ending_checkbox:
//do stuff
break;
}
}
I believe a RadioButton would be more suitable for your aims.
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radio_group1">
<RadioButton
android:id="#+id/sprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option1"
android:checked="true"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/marathon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option2"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/neverending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option3"
android:onClick="onRadioButtonClick"
/>
</RadioGroup>
Then in your code:
public void onRadioButtonClick(View v) {
RadioButton button = (RadioButton) v;
// DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED
}
In this case, you won't have to deal with disabling other options. The user will have only one option to pick by default.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
switch(id){
case R.id.sprint:
//add to database
...
}
}
else{
switch(id){
case R.id.sprint:
//remove from database
...
}
}
}
This should allow you to with ID's. Hope it works.