Android: Switch with text and custom view - android

I need to develop a button like in this picture:
How can I customize Switch? Or if it is not possible, how to implement this custom view?

Try this
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toogle_switch"
android:text=""
android:textOn=""
android:textOff="" />
toogle_switch.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/on" android:state_checked="true"></item>
<item android:drawable="#drawable/off" android:state_checked="false"></item>
</selector>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:text="Switch1:"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
</RelativeLayout>
RelativeLayout layout = (RelativeLayout)findViewById(R.id.r_layout);
Switch sb = new Switch(this);
sb.setTextOff("OFF");
sb.setTextOn("ON");
sb.setChecked(true);
layout.addView(sb);
Switch sw = (Switch) findViewById(R.id.switch1);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});

Related

How to turn on one of the toggle buttons

I want the other toggle button to turn off when one turns on.
<item android:state_checked="true"
android:drawable="#drawable/press"></item>
<item android:drawable="#drawable/nomal"></item>
This is my togglebutton's xml code This code is made in 'drawable'.
You can use below selector as a background of the ToggleButton which picks the appropriate drawable when the button changes its check status
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_checked" android:state_checked="true" />
<item android:drawable="#drawable/ic_unchecked" android:state_checked="false" />
</selector>
and wrap both buttons in a RadioGroup; then traverse this group in a loop whenever a button is checked/clicked, and then negates those buttons with setChecked() method.
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<RadioGroup
android:id="#+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:orientation="horizontal">
<ToggleButton
android:id="#+id/toggle_btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_btn_selector"
android:onClick="onToggle"
android:textOff=""
android:textOn="" />
<ToggleButton
android:id="#+id/toggle_btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_btn_selector"
android:onClick="onToggle"
android:textOff=""
android:textOn="" />
</RadioGroup>
</LinearLayout>
Behavior
public class MainActivity extends AppCompatActivity {
RadioGroup mGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGroup = findViewById(R.id.toggleGroup);
}
public void onToggle(View view) {
ToggleButton checkedBtn = ((ToggleButton) view);
for (int j = 0; j < mGroup.getChildCount(); j++) {
ToggleButton toggleButton = (ToggleButton) mGroup.getChildAt(j);
int id = toggleButton.getId();
if (view.getId() == id)
continue;
toggleButton.setChecked(!checkedBtn.isChecked());
}
}
}
You need to create RadioGroup, and add to it 2 RadioButtons with you drawable. Checkout second link, it will describes how to do it properly.
Also you need to change your drawable xml press to checked

Button in navigation drawer menuitem not working

I just try to make one Navigation Drawer Menu item to specific view. I added two textView respectively left and right side. And SwitchCompat. But SwitchCompat is not working. I just try to out one message for test.
I searched many resources but couldn't find anything like that. Is this bad practice? or not good to use?
Here my codes:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Pay" />
<android.support.v7.widget.SwitchCompat
android:id="#+id/paymentMethod"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:theme="#style/SelectionSwitch" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="4"
android:text="Card" />
</LinearLayout>
On my Activity:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
toolbarMenu.findItem(R.id.menu_item_search).setVisible(id == R.id.nav_camera);
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
View view = item.getActionView();
SwitchCompat payMethod = view.findViewById(R.id.paymentMethod);
payMethod.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(ActivityHome.this, "TEST", Toast.LENGTH_SHORT).show();
}
Actually after clicking more than 10 times it works but can't understand why.
create a layout then add to menu xml :
<item
android:id="#+id/nav_datasaver"
app:actionLayout="#layout/menu_item_datasaver" />
menu_item_datasaver is the linear layout which is created in layout floder and using to menu xml
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_download"
android:icon="#drawable/ic_download"
android:title="SAVED OFFLINE" />
<item
android:id="#+id/nav_referral"
app:actionLayout="#layout/menu_item_refer" />
<item
android:id="#+id/nav_contest"
app:actionLayout="#layout/menu_item_contest" />
<!-- <item
android:id="#+id/nav_login_button"
android:icon="#drawable/ic_download"
android:title="LOGOUT"/>-->
</group>

How to Make LinearLayout Clickable

I have a linearlayout like this
<LinearLayout
android:id="#+id/optionlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/divider"
android:orientation="vertical" >
</LinearLayout>
I want to add another xml view into this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--
<View
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2fd275"
/>
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="#+id/option_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/logout" />
<TextView
android:id="#+id/option_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:visibility="gone" />
<TextView
android:id="#+id/option_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:text="Signout"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#616161" />
</LinearLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:background="#d3d3d3" />
</LinearLayout>
via the code
for (int i = 0; i < opt.length; i++) {
View view = inflater.inflate(R.layout.option_item, optionlist,
false);
view.setBackgroundResource(R.drawable.optionlist);
view.setTag(opt_image[i]);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
int res = (Integer) v.getTag();
switch (res) {
case R.drawable.logout:
signout();
break;
}
}
});
final TextView tv = (TextView) view.findViewById(R.id.option_name);
final ImageView iv = (ImageView) view
.findViewById(R.id.option_image);
tv.setText(opt[i]);
iv.setImageResource(opt_image[i]);
optionlist.addView(view);
}
Now on click of the layout i want to load this xml file as below...but i am not able to get the backgroud loaded on the click event. Please give suggestion what am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/holo_green_dark" android:state_focused="true"/>
<item android:drawable="#color/holo_green_dark" android:state_enabled="false" android:state_pressed="true"/>
<item android:drawable="#color/white"/>
</selector>
First get id of LinearLayout from xml and then perform onClickListener on it instead of whole View
View view = inflater.inflate(R.layout.option_item, optionlist, false);
LinearLayout layout = view.findViewByIt(R.id.optionlist);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
int res=(Integer) v.getTag();
switch(res)
{
case R.drawable.logout: signout();
break;
}
}
});
try this.. hope it works..
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#ff0000" />//ur onclick desired color
<item android:state_focused="false"
android:drawable="#android:color/transparent" />focused color
</selector>
just create new xml and add this code in drawable and set ur linear layoyut backgrout as its name like android:background="#drawable/createdxmlname"
<LinearLayout
android:id="#+id/optionlist"
android:layout_below="#id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/createdxmlname"
android:clickable="true">
android:clickable="false"
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:clickable="false"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="Cinema"
android:textSize="13sp"
android:layout_marginRight="10dp" />
<Button
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Check in"
android:textSize="13sp" />
</LinearLayout>
Create in drawable an pressed_layout.xml with the following content
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
And in your layout add it as background resource:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/pressed_layout"
android:clickable="true">
</LinearLayout>
layout.setclickble="true" in XML file and
layout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
}
});
We can also use onClick in XML of layout,and use that method in java file,like if onClick="next",then in java file we have to use the method .
public void next()
{
// do something
}

How to make a Relativelayout clickable?

I have a layout like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:clickable="true"
android:duplicateParentState="true"
android:focusable="true"
android:paddingBottom="10dip" >
<ImageView
android:id="#+id/imagView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"
android:background="#drawable/imageView_bg"
android:focusableInTouchMode="false" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="18dip"
android:background="#drawable/button_bg"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left|center_vertical"
android:paddingLeft="25dip"
android:textColor="#ffffff"
android:textSize="20dip" />
</RelativeLayout>
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed"
android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal"/>
</selector>
imageView_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img_pressed"
android:state_pressed="true"/>
<item android:drawable="#drawable/img_normal"/>
</selector>
The image view and button have pressed images.when the user clicks on the view ,wants to press both imageview and button(Showing pressed images).But it does not show pressed images. How to achieve this? Where is the problem?
Thanks in advance
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainLayout);
rl.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {}
});
I think it solves the problem
((RelativeLayout)findViewById(R.id.mainLayout)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup .getChildCount(); i++) {
View viewChild = viewGroup .getChildAt(i);
viewChild.setPressed(true);
}
}
});
Simple.Place RelativeLayout inside the FrameLayout.

Button Selected State Android?

I am developing an application which consists of button.
Here Is My problem,how to set the selected state and unselected state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ox_mnu_dashboard_over" android:state_selected="true"/>
<item android:drawable="#drawable/ox_mnu_dashboard_normal"/>
</selector>
I am using this code but not working for me.
Here is the xml file:
<FrameLayout
android:id="#+id/tabDashboard"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_weight="1"
android:clickable="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="#string/dashboard"
android:textColor="#color/white"
android:textSize="12sp" />
<ImageButton
android:id="#+id/btnDashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tab_dashboard"
android:clickable="true" />
</FrameLayout>
You can set image when click on that imageButton like below code.
int isClicked = 0;
image = (ImageButton) findViewById(R.id.image);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isClicked1 == 0) {
image.setImageResource(R.drawable.second_image);
isClicked1 = 1;
} else {
image.setImageResource(R.drawable.original_image);
isClicked1 = 0;
}
}
});'

Categories

Resources