Button not clickable when added to listview - android

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.

Related

Problems with setvisibility for button on RelativeLayout Eclipse

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"
/>

Replace the content of Linear layout at onclick button android

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
Like this:
<?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:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};

TextView onclick event not working

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) {
...
}
}

How can I enable jumping to linked page if user clicks TextView?

XML Layout
<LinearLayout style="#style/behindMenuScrollContent"
android:paddingTop="25dp" >
<TextView
style="#style/behindMenuItemTitle"
android:text="People" />
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<jp.fureco.IconView
android:id="#+id/iconViewItem4"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="20dp"/>
<TextView
android:layout_marginLeft="10dp"
style="#style/behindMenuItemLabel"
android:text="Visitor" />
</LinearLayout>
</LinearLayout>
Then I have myWebView just like this
WebView myWebView = (WebView)findViewById(R.id.webView1);
When a user clicks on iconViewLink4, I want it jump to the url "http://example.com/messsage"
How can I code that?
Yes you can. Just use android:onClick="methodName" in <LinearLayout> or add it programatically by using setOnClickListener like
LinearLayout linLay = (LinearLayout)findViewById(R.id.iconViewLink4);
linLay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// the method or code that will take control to your URL
}
}
Make it clickable and give it an OnClickListener (you can do this on any View)
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
findViewById(R.id.iconViewLink4).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something here
}
});

Button onClick not detected with a ListView and ListActivity

I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Height"
android:layout_height="wrap_content"
android:id="#+id/buttonHeight"
android:layout_width="wrap_content"
android:layout_weight="15"
android:onClick="OnClickHeight">
</Button>
<Button
android:text="Width"
android:layout_height="wrap_content"
android:id="#+id/buttonWidth"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/buttonHeight"
android:layout_weight="1"
android:onClick="onClickWidth">
</Button>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:
public void onClickWidth(View v)
{
// does stuff
}
However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!
I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?
Can you tell us what are you try to do? And why do you need ListActivity?
Sorry for edditing:P
You could take a look here: How to handle ListView click in Android
put OnClicklistener within adapter itself .
Rather then using the onClick stuff in the layout, have you tried something like this:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
Then do the same for buttonHeight.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_land);
Button btn = (Button) findViewbyid(R.id.buttonWidth);
btn.setOnClickListener(this);
}
public void onClickWidth(View v)
{
if(v == btn){
//your code...
}
}
set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>

Categories

Resources