Android default Switch have text and then Switch on/off. I'm Expecting Switch on/off and then text. Don't use another widgets like TextView. How can i set Switch right side text.
Wrap it in another layout with no text value and add a textview. see my code:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="wrap_content">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/reminder" />
<TextView
android:layout_width="wrap_content"
android:text="Meeting Reminders"
android:layout_height="wrap_content" />
</LinearLayout>
Use
android:layoutDirection="rtl"
Then
app:switchPadding="8dp"
Add this code on Xml file:
<Switch
android:id="#+id/simpleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:checked="false"
android:textColor="#color/black"
android:textOff="#string/no"
android:textOn="#string/yes"
android:theme="#style/SCBSwitch" />
<TextView
android:id="#+id/switchBtn_txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/simpleSwitch"
android:gravity="center_horizontal" />
In activity Or Fragment :
Switch simpleSwitchBtn = (Switch) findViewById(R.id.simpleSwitch);
final TextView switchBtn_txtView = (TextView) findViewById(R.id.switchBtn_txtView);
simpleSwitchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switchBtn_txtView.setText("Yes");
}
else {
switchBtn_txtView.setText("No");
}
}
});
I think Its helpfull to you....
Thank you
I was able to achieve the look I wanted with a combination of:
width (120dp)
padding (75dp)
and switchpadding (-90dp)
Related
i'm new to Android and i'm working on a University project, an app to take notes. I can add and delete note, but i would like to implement a classic way to delete an item: long press on it and delete it with dialogs or whatever.
This is what i have: Main Activity
I tried to click for a few seconds on the cards/notes (they are in a recycle view nested in a coordinator layout) and i saw that a background visual effect is displayed, so i think that something is already implemented.
Let me know if you need XML layout implementation or something else to answer. Thanks! :)
EDIT
requested code
CardView cardView = findViewById(R.id.cardId);
cardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Toast.makeText(MainActivity.this, "long click!", Toast.LENGTH_SHORT).show();
return true;
}
});
XML Declaration of the cardview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/touch_layout"
android:background="?attr/selectableItemBackground">
<androidx.cardview.widget.CardView
android:id="#+id/cardId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/cardStyle"
android:elevation="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
tools:text="title"
android:textStyle="bold"
android:textSize="20sp"
android:layout_toStartOf="#id/showText"/>
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/showText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_leftarrow" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:layout_alignParentEnd="true"
android:textSize="12sp"
android:layout_marginTop="4dp"
tools:text="#string/date" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_below="#+id/title"
tools:text="message..." />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
You can use long click listener easy on recylerview
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Log.v("LongClick: "+"LongClick");
return true;// returning true instead of false, works for me
}
});
What you see in the background is the nativen effect applied on some ItemViews by Android. For registering clicks, follow these steps:
Create the layout in the XML and assign an ID to it.
<TextView
android:id="#+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_marginRight="16dp"
android:layout_alignParentEnd="true"/>
Declare and define the view (TextView in this case) by using the assigned ID:
TextView messageText;
messageText = itemView.findViewById(R.id.timestamp);
Attach Listener to register clicks:
messageText.setOnLongClickListener(new View.OnClickListener() {
#Override
public void onLongClick(View view){
// Do what you want here.
}
});
I am trying to create a ripple when my button is clicked by using the RippleEffect project, however every time I run the code the app crashes. How do I fix this? Alternatively, is there an easier way to get a ripple when a button is clicked? Please help if possible. Thank you!
<RelativeLayout
android:id="#+id/topPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.andexert.library.RippleView
android:layout_width="match_parent"
android:layout_height="95dp">
// Button 1
<Button
android:id="#+id/name1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="95dp"
android:gravity="center"
android:background="#drawable/button_border"
android:textSize="17dp"
android:textAllCaps="false"
android:textStyle="normal|bold" />
// TIME 1
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/name1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="4dp"
android:textSize="18dp"
android:textColor="#android:color/darker_gray"
android:elevation="2dp"
android:id="#+id/editText1" />
// NAME 1
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text=" "
android:elevation="2dp"
android:layout_alignBaseline="#+id/name1"
android:layout_alignBottom="#+id/name1"
android:layout_centerHorizontal="true"
android:cursorVisible="false" />
</com.andexert.library.RippleView>
</RelativeLayout> // End Left Side
Just to check the sample codes
View that imitates Ripple Effect on click which was introduced in Android L.
For a working implementation, Have a look at the Sample Project - RippleViewExample
Include the library as local library project.
Include the RippleView widget in your layout.
In your onCreate method refer to the View and add 'OnClickListener' for the same.
mButton = (RippleView) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code
}
});
Xml Code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/trip2"
android:orientation="vertical">
<com.cengalabs.flatui.views.FlatTextView
android:id="#+id/Rpickdtetx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Return Pick Up Date"
/>
<com.cengalabs.flatui.views.FlatEditText
android:id="#+id/Rpickdte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:maxLines="1"
android:textSize="15dp" />
</LinearLayout>
</RelativeLayout>
<!--Return Pick up time-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.cengalabs.flatui.views.FlatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Return Pick Up Time"
android:textSize="15dp"
flatui:fl_theme="#array/grass"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<com.cengalabs.flatui.views.FlatEditText
android:id="#+id/Rpicktme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:maxLines="1"
android:gravity="center_vertical"
android:hint="Pick-Up Time"
android:includeFontPadding="true"
android:drawableLeft="#drawable/time"
flatui:fl_fieldStyle="fl_box"
/>
</LinearLayout>
</RelativeLayout>
.java code
Triptype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.triptype1:
view.findViewById(R.id.trip2).setVisibility(View.INVISIBLE);
view.findViewById(R.id.trip21).setVisibility(View.INVISIBLE);
/* view.findViewById(R.id.Rpickdte).setVisibility(View.INVISIBLE);
view.findViewById(R.id.Rpickdtetx).setVisibility(View.INVISIBLE);*/
break;
case R.id.triptype2:
ReturnTrip(view);
break;
}
}
});
I am newbie to android,I am using radio button on toggling,have to add some text boxes(above xml code of text boxes),My query making visible/invisible text box even though i wrap_content, can see a empty space.Can anyone help me how to resolve that.
view.findViewById(R.id.trip2).setVisibility(View.GONE);
view.findViewById(R.id.trip21).setVisibility(View.GONE);
/* view.findViewById(R.id.Rpickdte).setVisibility(View.GONE);
view.findViewById(R.id.Rpickdtetx).setVisibility(View.GONE);*/
break;
set them to "Gone" instead of "invisible" will help to retain space
Try to use View.GONE instead of View.INVISIBLE.
The documentation says:
View.GONE: This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
If you want to know more, you can look at this question.
I have a program which computes perimeter, circumference, an area of different shapes like, square, circle, rectangle and soon. Any help with enabling the radiobutton to display an edittext to write the measurements.
check below code don't use as it is, its just reference
inyourxml.xml
<RadioGroup
android:id="#+id/groupRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_big_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:gravity="center"
android:minHeight="30dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/circumferenceRadio"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/tab_selector_background"
android:button="#null"
android:checked="true"
android:gravity="center"
android:text="#string/choice_yes"
android:textColor="#drawable/tab_selector_text" />
<RadioButton
android:id="#+id/perimeterRadio"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/tab_selector_background"
android:button="#null"
android:checked="false"
android:gravity="center"
android:text="#string/choice_no"
android:textColor="#drawable/tab_selector_text" />
</RadioGroup>
<EditText
android:id="#+id/perimeterEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_normal_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:background="#drawable/action_bar_background"
android:inputType="textPassword" />
<EditText
android:id="#+id/circumferenceEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_normal_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:background="#drawable/action_bar_background"
android:inputType="textEmailAddress" />
inyourjava.java
RadioGroup groupRadio=(RadioGroup)rootView.findViewById(R.id.groupRadio);
EditText perimeterEditText=(EditText)rootView.findViewById(R.id.perimeterEditText);
EditText circumferenceEditText=(EditText)rootView.findViewById(R.id.circumferenceEditText);
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.circumferenceRadio)
{
circumferenceEditText.setVisibility(View.VISIBLE);
perimeterEditText.setVisibility(View.INVISIBLE);
}
else if(checkedId==R.id.perimeterRadio)
{
perimeterEditText.setVisibility(View.VISIBLE);
circumferenceEditText.setVisibility(View.INVISIBLE);
}
}
});
Hide a EditText & make it visible by clicking a menu
in case of menu ... you can declare setvisibility in xml and make it hide... then programatically make it visible...hope this helps
Given the following code:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textPersonalInfoEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:hint="Edit to set your personal info"
android:textSize="16sp"
android:visibility="gone" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:hint="Name"
android:visibility="gone"
android:textSize="16sp" />
<Button
android:id="#+id/insertGameId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editName"
android:layout_marginTop="20dp"
android:text="Insert" />
</RelativeLayout>
The TextView and the EditText are visible depending the user interaction and both will never be visible at the same time. One per time only!
The problem is the third component(Button) need to change the attribute android:layout_below depending on which component is visible.
I need to this programmatically.
Any help?
Use Relativelayout for your textview and edittext. And relativelayout's height:wrap-content
Then give an id to your relativelayout
and then add this line your xml file for your button
<Button
android:layout_below="#id/RealtiveLayoutid" />
Thats all
First, use a ToggleButton instead of Button. It can have all the attributes that you assigned to it.
Next add this to the ToggleButton:
<ToggleButton
....
android:textOn="#string/ifTextView"
android:textOff="#string/ifTextBox"
android:onClick="onToggleClicked"
/>
Then this for the activity
public void onToggleClicked(View v) {
boolean on = ((ToggleButton) v).isChecked();
if(on) {
theTextView.setVisibility(View.VISIBLE);
theTextBox.setVisibility(View.INVISIBLE);
} else {
theTextBox.setVisibility(View.VISIBLE);
theTextView.setVisibility(View.INVISIBLE);
}
}
Remember to import the required classes.