hello i have a toggle button like this :
<RadioGroup android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ToggleButton android:id="#+id/detailed_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/detailed_view"
android:textOff="#string/detailed_view"
android:textOn="#string/detailed_view"
android:onClick="viewChange" />
<ToggleButton android:id="#+id/main_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_view"
android:textOff="#string/main_view"
android:textOn="#string/main_view"
android:checked="true"
android:onClick="viewChange"
/>
</RadioGroup>
and i have the function viewChange like this:
public void viewChange(View v){
int id=v.getId();
if(id==R.id.main_view){
//do some thing with main view
}else if(id==R.id.detailed_view){
do something with detailed view
}
}
Now i need to call this function ViewChange(View v) and set the parameter v programatically, i means that i want to click this toggle button programatically and specify which view i want to click.
You can programmatically change the state of the ToggleButton by using the setChecked method:
toggleButton.setChecked(true or false)
More information on the ToggleButton may be found in the API reference: http://developer.android.com/guide/topics/ui/controls/togglebutton.html
make use of
.setChecked()
property.
Ref
http://developer.android.com/reference/android/widget/ToggleButton.html#setChecked%28boolean%29
Related
My goal is to have a CardView that when is clicked, it goes from Start -> Stop and vice versa when clicked again. Inside the CardView I have two TextView which I would like to alter when it's clicked. The functionality behind this, would be to start a foreground service and stop it accordingly.
What I have in mind for the layout xml:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/cardStartStop">
<TextView
android:id="#+id/txtCardStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/card_start"
app:drawableTopCompat="#drawable/ic_start_black_24dp" />
<TextView
android:id="#+id/txtCardStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="#string/card_stop"
app:drawableTopCompat="#drawable/ic_stop_circle_black_24dp" />
</androidx.cardview.widget.CardView>
</GridLayout>
Then I thought to programmatically change the visibility of each TextView and persist its state using SharedPreferences when closing and re-opening app. My question is more at a design level, is there a better way to achieve this or I'm pointing in the right direction?
Add one TextView and setOnClickListener
XML File
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Start"
android:id="#+id/text_start_stop"
/>
</androidx.cardview.widget.CardView>
Class
private TextView tStartStop;
Boolean flag = true;
ClickEvent
tStartStop = findViewById(R.id.text_start_stop);
tStartStop.setOnClickListener(view -> {
if (flag) {
flag = false;
tStartStop.setText("Start");
} else {
flag = true;
tStartStop.setText("Stop");
}
});
I have a RadioGroup consisting of 2 choices and a button, After I press that button I want to get the value and based on that value I want to perform different actions:
<RadioGroup
android:id="#+id/rg_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tv_title">
<RadioButton
android:id="#+id/rb_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="#drawable/ic_mo_512dp"
android:drawablePadding="20dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:text="Visual Advance" />
<RadioButton
android:id="#+id/rb_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="20dp"
android:text="Visual basic" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
onSingleClick="#{() -> onClickListener.getRadioButton()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rg_select">
fun getRadioButton() {
val result = rg_select.checkedRadioButtonId
when (genderId) {
R.id.rb_a -> {
Toast.makeText(context, "KKSKSK", Toast.LENGTH_LONG).show()
}
R.id.rb_b -> {
Toast.makeText(context, "KKSKSK", Toast.LENGTH_LONG).show()
}
}
}
But my above code is not suitable for databinding so can anyone help me to solve this problem?
Can't see any obstacle for it. Just handle two actions separately, when radio button is checked "android:checked="#{viewModel.selectedRG.equals(RG.A)}", when button pressed onSingleClick="#{viewModel.performSomeAction()}". So in this case when performSomeAction() will be invoked RG type can be taken from "selectedRG".
If smth missed, let me know.
I have a couple of buttons I want to make a computation on, so I want to get the ID of the particular button clicked so I can perform an operation on.
I have configured the id for all my buttons in my XML file.
I hope i can get help here.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="15dp"
android:textSize="45dp"
android:gravity="center_horizontal"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/displayCalc"
android:text="0"></TextView>
<Button
android:id="#+id/button_one"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="1"
android:onClick="one"/>
<Button
android:id="#+id/button_two"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="2"
android:onClick="two"/>
<Button
android:id="#+id/button_three"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="3"
android:onClick="three"/>
<Button
android:id="#+id/button_add"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="+"
android:textColor="#color/design_default_color_primary_dark"
android:textSize="18sp"
android:onClick="calcAdd"/>
</LinearLayout>
Your question is a little strange. If I got it correctly you want to know which button is clicked in Java/Kotlin and perform some operation based on buuton id.
If that is what you want:
Implement View.OnClickListener in your activity or fragment related to this xml.
Implement onCLick(View) method.
Now you can use when (kotlin) or switch (java) to find the clicked button id.
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button_one.setOnClickListener(this)
button_two.setOnClickListener(this)
}
override fun onClick(v: View) {
when (v.id) {
R.id.button_one -> {
//TODO (do something for button with id button_one)
}
R.id.button_two -> {
//TODO (do something for button with id button_two)
}
}
}
}
You have defined in the XML file for the Button with id button_one with this attribute:
android:onClick="one"
that the listener will be a method named one, so all you have to do is create that method inside your activity like this:
public void one(View view) {
<your code goes here>
}
If you want to refer to the Button inside that method you can do it like this:
Button button = (Button) view;
and if you want its id:
int id = button.getId();
So create 4 methods (like the one above) inside your Activity class for all 4 buttons: one(), two(), three() and calcAdd().
say you're doing something like that
Button buttonOne = findViewById(R.id.button_one); //now you have access to the button that is in your layout
buttonOne.setOnClickLister(new View.OnClickListener(){ // i hope you understand anonymous inner class
#Override
public void onClick(View v){ //now when your button is clicked this method is called
//and you passed v in argument which is nothing but your button
//you're passed a View object and not a out of the box button because the
//View.OnClickListener can be implemented on any view if you want to call button
//specific method on it you cast it to a button , because you know it is a button
//inside
v.getId() //gives you the id of your button which you wanted
}
}
Tell me there is still some confusion .
Change a layout in same Activity when click on play button then layout show pause Button at the same place in Android Studio music player app.
How it is possible?
I'm a beginner so I don't know how to use method about it?
I cannot get an answer.
I understand from your question that when your user taps pause button, you should hide pause button and show play button and vice-versa. This is relatively simple to achieve. Just create a layout with a linear layout with two buttons like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:padding="5dp">
<Button
android:id="#+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:text="Play"
android:padding="5dp"/>
<Button
android:id="#+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Pause"/>
</LinearLayout>
Now set on click listner to both of the buttons and handle button visibility within that like this :
Button btnPlay = barcodeDialog.findViewById(R.id.btnPlay);
Button btnPause = barcodeDialog.findViewById(R.id.btnPause);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.VISIBLE);
btnPause.setVisibility(View.GONE);
}
});
This way you can handle the visibility of your Play and Pause buttons. Reply if you need more information.
I am trying to develop a Log in page, where I have Username, password and a button. When I click the button for the first time, nothing happens, but when I click the button for the second time, then it works properly. I am confused, why it is happening ?
activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_bg"
tools:context=".LoginActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="30dp" >
<EditText
style="#style/EditText1"
android:id="#+id/userEditText"
android:layout_marginBottom="50dp"
android:inputType="textNoSuggestions"
android:singleLine="true"
android:hint="username" />
<EditText
style="#style/EditText1"
android:id="#+id/passEditText"
android:inputType="textPassword"
android:layout_marginBottom="50dp"
android:hint="password" />
<Spinner
android:id="#+id/locationSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="#ffffff"
style="#style/EditText1"
android:layout_marginBottom="50dp" />
<Button
style="#style/Button1"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="onLoginClick"
android:text="continue"
/>
loginActivity.java
#SuppressLint("DefaultLocale")
public void onLoginClick(View view) {
String username = mUserEditText.getText().toString();
String password = mPassEditText.getText().toString();
String location = mLocationData.get(
mLocationSpinner.getSelectedItemPosition()).toLowerCase();
if (username.isEmpty() || password.isEmpty()) {
CreatorMessenger
.getInstance()
.showMessage(this, "Error!!",
"You need to enter username and password both to continue!!");
return;
}
User user;
user = new User(username);/*
* }
*/
user.setLocation(location);
AppManager.getInstance().setLoggedInUser(user);
APICaller.getInstance().login(username, password, location);
}
You are setting android:focusableInTouchMode="true"
Hence on 1st click it receives focus.
Refer Android button - How to set focusable to true and still accept onClick listener on first click?
Set this -
android:focusableInTouchMode="true"
to this -
android:focusableInTouchMode="false"
on button.
Explanation - If you'll make the button focusable then on first click the focus is passed to the button, then the click is passed on second touch. EditText is a focusable View which gains focus first and therefore other views must first regain the focus from EditText, unless they do not need focus to work, just like buttons. If you just need the OnClick function, then you don't need focus, so you can spre one extra click.
PS: Although it shouldn't require, but setting android:focusable to false will help too, if the first one doesn't work.
Yes Yes I got the answer, after a lot of RND, I got the solution, I just need to implement setOnClickListener(), and setOnFocusChangeListener(). So I am putting here the solution.
ActivityLogin.java
buttonLogin= (Button) findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("hello", "hellow");
String username = mUserEditText.getText().toString();
String password = mPassEditText.getText().toString();
String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase();
if(username.isEmpty()||password.isEmpty()){
popbox();
return;
}
User user;
user = new User(username);
user.setLocation(location);
AppManager.getInstance().setLoggedInUser(user);
APICaller.getInstance().login(username, password, location);
}
});
buttonLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.performClick();
}
}
});
}
activity_login.xml
<Button
android:id="#+id/buttonLogin"
style="#style/Button1"
android:text="continue"
/>
if in your xml in button, or img, or liner layout has:
android:focusableInTouchMode="true"
To resolve, just
remove this.
I suggested to use request focus in the "onCreate()" method.
When you using android:focusableInTouchMode="true" you may don't want the focus catch by an 'EditText' field.
For example:
1. An activity has a view contain -> android:focusableInTouchMode
2. You open a Fragment Dialog, but the back button need click two times to fire it.
3. You should call requestFocus() after the Fragment Dialog onCreateView()