In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ?
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:textColor="#ffffff"
/>
public void selectit(View v)
{
Log.d("tv0","ok");
if(v.getId()==tv1.getId())
{Log.d("tv1","ok");
selectoption(1);
Log.d("tv1","ok");
}
if(v.getId()==tv2.getId())
{Log.d("tv2","ok");
selectoption(2);
}
if(v.getId()==tv3.getId())
{Log.d("tv3","ok");
selectoption(3);
}
}
Add this into your xml
android:clickable="true"
You probably need to have this code in your xml
android:clickable="true"
Or Set the Clicklistenner to TextView via code by
TextView btn=(TextView) findViewById(R.id.tv1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectit(v);
}
});
It will automatically make it clickable so no need of android:clickable="true"
You can set the click handler in xml with these attribute:
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:clickable="true"
android:textColor="#ffffff"
/>
i hope it will help u all the best
Try like below:
android:onClick="onClick"
android:clickable="true"
My textview:
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
Main Activity:
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
Related
I have the following Layout:
<LinearLayout
android:id="#+id/group_button_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/group_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Week"/>
<Button
android:id="#+id/group_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:padding="0dp"
android:text="Month"/>
<Button
android:id="#+id/group_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Quarter"/>
<Button
android:id="#+id/group_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Half Year"/>
</LinearLayout>
I created the onClickListener on layout like that:
private void initViews() {
mGroupedButtonLayout = (LinearLayout) findViewById(R.id.group_button_layout);
}
private void initListeners() {
mGroupedButtonLayout.setOnClickListener(mButtonGroupListener);
}
And I thought that after click on a button onClick event will be triggered:
private View.OnClickListener mButtonGroupListener = new View.OnClickListener() {
public void onClick(View v) {
Logger.d("Clicked");
Logger.d(String.valueOf(v.getId()));
// do something when the button is clicked
}
};
Is it possible to catch onClick event on every button without the need to create a listener for each button?
You could set the android:onClick attribute for the buttons, and handle your logic in the method you point it to.
XML
<Button
android:id="#+id/group_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Week"
android:onClick="doButtonClick"/>
Java
public void doButtonClick(View v) {
switch(v.getId()) {
case R.id.group_button_1:
//do whatever for this button
break;
default:
break;
}
Hy.... I'm making a simple project on eclipse. I'm using relative layout as shown below :
<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:gravity="center"
tools:context=".ProfileActivity"
>
<Button
android:id="#+id/Prof_edit_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="invisible"
android:clickable="false"
/>
</LinearLayout>
</RelativeLayout>
In the main activity, I've made a simple onClicklistener for Edit and Save button as shown below :
mEdit = (Button)findViewById(R.id.Prof_edit_btn);
mEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mBatal.setVisibility(View.VISIBLE);
mBatal.setClickable(true);
mSave.setVisibility(View.VISIBLE);
mSave.setClickable(true);
mEdit.setVisibility(View.INVISIBLE);
}
});
mSave = (Button)findViewById(R.id.Prof_save_btn);
mSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mEdit.setVisibility(View.VISIBLE);
mBatal.setVisibility(View.INVISIBLE);
mSave.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Data Anda berhasil disimpan", Toast.LENGTH_SHORT).show();
}
});
Well... As we can see from my layout xml, only EDIT button will be visible on the start, then SAVE and BATAL button will be visible when EDIT is clicked by user according to the code program within setonclick listener of EDIT button. And as soon as EDIT button is clicked it will be invisible.... The problems here are WHen I clicked EDIT button, only SAVE button will be visible and BATAL button will be visible when SAVE button is clicked, another problem that I'm facing now is EDIT button is not invisible at all when I click it.... So.. anyone can help me with this problem please...??? :'(
Thanks in advance....
Naaah... I found my own solution... Just set the visibility of SAVE btn and BATAL btn to GONE... And it's all done.... :-)
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="gone"<-----------=
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="gone"<-----------=
android:clickable="false"
/>
I have Following layout Scenario
Want to implement the Click even of TextView ..but the TextView Click not working
Here is code Sample
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:clickable="false">
<customview
android:id="#+id/custom"
android:layout_width="200dp"
android:layout_height="200dp"
android:clickable="false"/>
<LinearLayout
android:id="#+id/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/zero_done"
android:padding="5dp"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
My TextView ClickEvent not working ...please help me
Class Code
TextView view= (TextView) findViewById(R.id.btn);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show();
}
});
You didn't call show method of Toast,so you have no idea whether TextView click event did happen or not.
Try this:
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show(); // don't miss show method.
i have this layout, but the button is not clickable.
i tried solutions from previous posts, such as adding android:focusable="false" to the button or listview or the linear layout but it didnt work. i tried adding : button.setFocusable(false) after the button OnClickListener but also didnt work. i even tried adding: android:descendantFocusability="blocksDescendants" to the main linear layout but the problem still exists.
Thanks.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
<EditText
android:id="#+id/search_field"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:ems="10"
android:hint="Search"
android:textColor="#1F6260" />
<Button
android:id="#+id/refresh_button"
android:layout_width="34dp"
android:layout_height="32dp"
android:background="#drawable/refresh3" />
</LinearLayout>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector">
</ListView>
</LinearLayout>
Java code:
refresh= (Button)myFragmentView.findViewById(R.id.refresh_button);
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((MainTabs)getActivity()).refresh();
((MainTabs)getActivity()).getUsersList();
}
});
Added the toast in the onclick method and it showed, so my code isn't working and nothing is wrong with the button. Sorry for this inconvenience.
Thanks.
Two ways to achieve this
1.
Change your button like this
<Button
android:id="#+id/refresh_button"
android:layout_width="34dp"
android:layout_height="32dp"
android:background="#drawable/refresh3"
android:clickable="true"
android:onClick="buttonClick" />
then create a method inside your activity which have to pass your View as parameter like this.
public void buttonClick(View v) {}
or you can follow the second way
2.
Button button;
button = (Button) findViewById(R.id.refresh_button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Hope this will help you.
I want to change the background image and TextView Color on click of a layout .The background color is chnaging properly but my textview color is not changing.Here is my XML code :
<RelativeLayout
android:id="#+id/flight_relative"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="#+id/imgLogo"
android:layout_marginTop="5dp"
android:background="#drawable/button_effect"
android:gravity="center_vertical" >
<TextView
android:id="#+id/flight_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flight_list_image"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/flight_list_image"
android:text="#string/flight_tittle"
android:textColor="#152b72"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="#+id/content_flight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/flight_content"
android:layout_toRightOf="#+id/flight_list_image"
android:text="#string/flight_content"
android:textColor="#2f2f2f"
android:textSize="10sp" />
<ImageView
android:id="#+id/flight_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/flight_content"
android:layout_alignParentRight="true"
android:src="#drawable/arrow" />
<ImageView
android:id="#+id/flight_list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dip"
android:src="#drawable/flight_icon" />
</RelativeLayout>
Code to chnage the textview color is :
flightRelative = (RelativeLayout )findViewById(R.id.flight_relative);
flightRelative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
flight = (TextView)findViewById(R.id.flight_content);
flight.setTextColor(Color.WHITE);
}
});
What wrong i am doing please suggest me .For the first time it is not working on second time it is working
You should add this definitely working.I am also check it out...
flightRelative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
TextView flight = (TextView)findViewById(R.id.flight_content);
flight.setTextColor(Color.parseColor("#FFFFFF"));
}
});
you should use
#Override
public void onClick(View arg0) {
flight = (TextView)flightRelative.findViewById(R.id.flight_content);
flight.setTextColor(Color.WHITE);
}
I believe it should work now.