Button Selected State Android? - 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;
}
}
});'

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

Highlight selected item on a gridview

I am trying to highlight a selected item on a gridview (dinamically populated with an adapter), but it is not working.
I did research and i even tried to copy exactly the selector of other people and even the way that they put it on the gridview but i am not being able to put it working.
It just doesn't do anything. The background of each item is white (like i wanted), but when i press it (it it is on top of a textview or a imageview (part of the gridview item) it doesn't do anything. If i press out of the imageView or textview, it will do what i want.
EDIT : I have listeners for the images and the textviews, so it might be interfering with this selector ? How could i solve this problem?
Here is the code of the activity where i create the gridview :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highway_appselection_activity);
gridView= (GridView) findViewById(R.id.HighwayGridView);
gridView.setSelector(new ColorDrawable(Color.BLACK));
Here is the xml of each item of this gridview : (where i define the background as the selector)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/HighwayGridViewItem"
android:orientation="vertical"
android:background="#drawable/highway_appselection_selector"
android:padding="5dp">
<cm.aptoide.lite.HighwayCustomImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:id="#+id/highwayGridViewItemIcon"
android:background="#FFFFFF"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:padding="5dp"
android:clickable="true"/>
<!-- does this need to be my custom image view anymore? CHeck on that-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/highwayGridViewItemName"
android:textColor="#000000"
android:text="texto de teste"
android:textSize="10sp"
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_weight="2"
android:textStyle="bold"
android:paddingRight="5dp"
android:layout_marginLeft="5dp"
android:clickable="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info_icon"
android:padding="5dp"
android:clickable="true"
android:id="#+id/highwayGridViewItemInfoButton"/>
And here is my selector :
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_selected="true" android:drawable="#color/green_main_color" />
<item android:drawable="#android:color/white" />
I might be missing something, I am new to Android, sorry if there is any rookie mistake.
Create file selector.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green_main_color" android:state_pressed="true"/>
<item android:drawable="#color/green_main_color" android:state_selected="true"/>
<item android:drawable="#color/white"/>
</selector>
Put your selector file in drawable folder as drawable/selector.xml and then in your gridView:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:listSelector="#drawable/list_selector"
android:scrollbars="none" />
try this :
int nPrevSelGridItem = -1;
gridview.setOnItemClickListener(new OnItemClickListener() {
View viewPrev;
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
if (nPrevSelGridItem != -1) {
viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
viewPrev.setBackgroundColor(Color.WHITE);
}
nPrevSelGridItem = position;
if (nPrevSelGridItem == position) {
//View viewPrev = (View) gridview.getChildAt(nPrevSelGridItem);
view.setBackgroundColor(getResources().getColor(R.color.orange));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});

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.

Change the background image of a button

I am using two buttons, one for next news and one for previous news. In my activity when I am on the first news my button should set the image with the grey image and when I move forward my previous news button should change from the grey image to the colored one. Similarly when I reach at the last news my next button should change to grey image.
This is my xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
>
<TextView
android:id="#+id/tv_submitDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textColor="#000000"
android:padding="5dp"/>
<TextView
android:id="#+id/tv_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_submitDate"
android:text="Medium Text"
android:textColor="#000000"
android:textStyle="bold"
android:padding="5dp"/>
<View
android:id="#+id/viewSeperator"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_below="#+id/tv_headline"
android:background="#FF909090" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_relative_layout"
android:layout_below="#+id/viewSeperator" android:padding="10dp">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_detailNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#000000" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/btn_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#DCDCDC"
android:padding="10sp" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/arrow_left"
android:clickable="true" />
<Button
android:id="#+id/btn_nextNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_marginLeft="5sp"
android:layout_weight="0.5"
android:background="#drawable/arrow_right"
android:clickable="true" />
</LinearLayout>
</RelativeLayout>
This is where I am trying to change the image of my button:
public void onClick(View v) {
if (v == btnPrevNews && newsId > 0) {
if (newsId > 0) {
newsId--;
putDetailNewsinView(newsId);
} else if (newsId == 0) {
btnPrevNews
.setBackgroundResource(R.drawable.disable_arrow_left);
btnPrevNews.setEnabled(false);
}
// btnPrevNews.setVisibility(View.INVISIBLE);
} else if (v == btnNextNews && newsId < newsListDetail.size() - 1) {
if (newsId < newsListDetail.size() - 1) {
newsId++;
putDetailNewsinView(newsId);
if(newsId == 0)
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
} else if (newsId == newsListDetail.size()) {
// btnNextNews.setVisibility(View.INVISIBLE);
btnNextNews.setBackgroundDrawable(getResources().getDrawable(
R.drawable.disable_arrow_right));
// btnNextNews.setBackgroundResource(R.drawable.disable_arrow_right);
btnNextNews.setEnabled(false);
}
}
}
Please help me out in this.
Thanks in advance.
I assume you have Methods which will be executed if the Button is pressed. Just use these and add Logic to change the Background of these Buttons to grey:
Give both Buttons an ID, you can access these Buttons with:
Button mybutton = (Button) findViewById(R.id.yourid);
Now you can do all kinds of things with your button, like changing the background.
edit:
The Problem could lay in the Pictures, but if i were you, i would set some breakpoints and debug step-by-step.
I hacked some code of mine to test this functionality, it works fine for me. Here is it:
package my.namespac;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SomeTestProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.btn_prevNews);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.koala));
}
});
}
}
And the Layout-Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_prevNews"
android:layout_width="120sp"
android:layout_height="40sp"
android:layout_weight="0.5"
android:layout_marginRight="5sp"
android:background="#drawable/ic_launcher"
android:clickable="true" />
</LinearLayout>

Categories

Resources