How to make Layout clickable which has disabled EditText and Button - android

I have simple layout with EditText and a button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true">
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:focusable="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
I don't want EdiText to be editable and want to handle click on complete layout
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.linearLayout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
}
});
}
}
but it is not working I mean There is no Toast message when I am clicking.

as #Liem Vo said in the comments: you should add android:clickable="false" to the button, and make sure not to add a click listener in java.
becuse the event is handled by the child view.
for the 'EditText' you can call the parents click listener manually:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View parent = (View) v.getParent();
parent.performClick();
}
});
please refer to this question: onClick not triggered on LinearLayout with child

Related

How to make your parent clickable but children not?

I have LinearLayout which has 2 children which are used only as a preview. LinearLayout functionality here is same as Button. I want to disable any interaction with its children but make LinearLayoutclickable by using onClickListener. Currently if I click on Spinner it will not trigger onClickListener of LinearLayout. I also called spinner.isEnabled = false in code.
Layout:
<LinearLayout
android:id="#+id/clickableLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical">
<Spinner
android:id="#+id/picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:enabled="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
</Spinner>
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textColor="#color/colorBlack"
android:textSize="14dp"
android:enabled="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:duplicateParentState="true"
android:maxLength="9"/>
</LinearLayout>
Make on Click Listener for All of the views and then call your function, so it does not matter on which view you click. It will always trigger that function.
LinearLayout linearLayout;
TextView textView;
Spinner spinner;
linearLayout = findViewById(R.id.clickableLayout);
spinner = findViewById(R.id.picker);
textView = findViewById(R.id.text1);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myFunction();
}
});
}
private void myFunction() {
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
}

Android Custom Alert with XML - Button Click Action

I have 2 layout files, one for main layout showing (activity_main.xml) and another is for custom dialog showing (dialog_custom.xml). The 2 files are as follows-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/btn_1_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with 1 Button"
android:padding="10dip"
android:layout_marginTop="40dip">
</Button>
<Button android:id="#+id/btn_2_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert With 2 Buttons"
android:padding="10dip">
</Button>
<Button android:id="#+id/btn_3_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with 3 Buttons"
android:padding="10dip">
</Button>
<Button android:id="#+id/btn_custom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert with Custom Layout - From XML"
android:padding="10dip">
</Button>
</LinearLayout>
dialog_custom.xml
<?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="wrap_content">
<TextView
android:id="#+id/dialog_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/dialog_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#id/dialog_info">
<Button
android:id="#+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#3333"
android:text="Cancel"/>
<Button
android:id="#+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:background="#888888"
android:text="Agree"/>
</LinearLayout>
</RelativeLayout>
And what I have done in the Main class is -
public class Activity_Main extends Activity
{
Context context = Activity_Main.this;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_main);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
}
So, I get a dialog like this-
Now I want to add on click option for this CANCEL and AGREE button. But if I want, I am getting forced close. Can anyone please help me, how can I do it?
Thanks for helping.
Do it as follows :
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.layout.dialog_main);
Button btCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
Button btOk = (Button) dialog.findViewById(R.id.dialog_ok);
btCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Cancel Click
}
});
btOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//OK CLICK
}
});
dialog.show();
If you want to add Buttons or any Widget (inside of the Dialog) make sure that you are finding the view with dialog.findViewById(R.id.ID_WIDGET) otherwise it will crash.
I couldn't find your click code cancel and agree, but try this:
Button cancel=(Button)dialog.findViewById(R.id.dialog_cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do your work
}
});
Thanks if problem persists then post your error and click code.
You may need to add View.OnClickListener to buttons of dialog.
Button btnCancel = (Button) dialog.findViewById(R.id.dialog_cancel);
Button btnOk = (Button) dialog.findViewById(R.id.dialog_ok);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Called on click of Cancel button
}
});
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Called on click of Cancel button
}
});
dialog.show();

1 listview and 3 buttons. 1 button is not clickable, why?

I have a class wich extends ListActivity. Inside there are 3 buttons and a listview (clickable items), list items and 2 of the buttons work fine but one doesn't respond to clicks (it is created exactly the same way as the others). I think it is something related with the focusability of the button, i have already tried android:focusable="true" and "false" but nothing changes.
this is my xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_below="#+id/checkall"
android:layout_width="match_parent"
android:layout_height="400dp" >
</ListView>
<Button
android:id="#+id/checkall"
android:layout_height="38dp"
android:layout_width="wrap_content"
android:layout_marginLeft="19dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="Seleccionar todos"
android:background="#D1D1D1" />
<Button
android:id="#+id/uncheckall"
android:layout_height="38dp"
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/checkall"
android:layout_alignParentTop="true"
android:text="Borrar selección"
android:background="#D1D1D1"
android:layout_alignParentRight="true" />
//this is the one not accepting clicks -->
<Button
android:id="#+id/mostrar"
android:layout_height="49dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:text="Mostrar en mapa"
android:background="#D1D1D1" />
</RelativeLayout>
There is no problem in your layout and it works fine with this code-behind:
public class MyActivity extends ListActivity {
Button checkall, uncheckall, mostrar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
checkall = (Button) findViewById(R.id.checkall);
uncheckall = (Button) findViewById(R.id.uncheckall);
mostrar = (Button) findViewById(R.id.mostrar);
checkall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "checkall is clicked", Toast.LENGTH_SHORT).show();
}
});
uncheckall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "uncheckall is clicked", Toast.LENGTH_SHORT).show();
}
});
mostrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this, "mostrar is clicked", Toast.LENGTH_SHORT).show();
}
});
}
Try to 'clean' and 'Rebuild' your project or post your code here

Insert views in HorizontalScrollView programmatically

I'm making a chat app which looks like this(dummy view):
Use the xmls & activity exactly as they are
<?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">
<HorizontalScrollView
android:id="#+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/send"
android:clickable="true" >
<LinearLayout
android:id="#+id/LL_inside_HSV"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
<ImageButton
android:id="#+id/send"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I want to add a view to HorizontalScrollView(HSV). I've read that HSV can have only 1 child (mostly LinearLayout) & views must be added to it. This is the view I want to add:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:orientation="vertical"
android:background="#454545" >
<ImageView
android:id="#+id/profilePicture"
android:layout_width="60dp"
android:layout_height="60dp" />
<AutoCompleteTextView
android:layout_width="75dp"
android:layout_height="wrap_content" />
</LinearLayout>
This is my activity:
public class MainActivity extends Activity {
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
LinearLayout lLinsideHSV;
View child;
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = inflater.inflate(R.layout.word_element, null, false);
sendIB = (ImageButton) findViewById(R.id.send);
chatMessageHSV = (HorizontalScrollView) findViewById(R.id.chat_message_HSV);
lLinsideHSV = (LinearLayout) findViewById(R.id.LL_inside_HSV);
chatMessageHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT)
.show();
}
});
lLinsideHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "LL", Toast.LENGTH_SHORT)
.show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
lLinsideHSV.addView(child);
}
});
}// end of onCreate
}
NOTE: I want to add views onClick of HSV.
Q-1 : I am unable to trigger the onClick of HSV or the LinearLayout inside it. I tried doing onTouchEvent but that event is triggered 4-5 times on every touch.
Q-2 I wrote the code to insert view inside onClick of button(just to experiment). I am able to insert the view only once. After that it throws an exception saying:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
So how do I insert the views again & again??
Q2: You have to create new child
child = inflater.inflate(R.layout.word_element, null, false);
on every click event. Something like:
#Override
onClick() {
View child = inflater.inflate(...);
lLinsideHSV.addView(child);
}

One of the ImageButton not clicking & make HorizontalScrollView clickable

This seems to be a very silly problem but its just not working for me.
I have 2 ImageButtons & a HorizontalScrollView(HSV) on a page. Strangely one of the Image Button(1st one) is not clicking. Also I wanted to make the HSV clickable so I used:
android:clickable="true", which does not work
You can use the xml & activity exactly as I have posted.
This is the xml:
<?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"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/topRL"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/free_msgs_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="free msgs left" />
<ImageButton
android:id="#+id/buy_imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher"
/>
</RelativeLayout>
<ListView
android:id="#+id/chat_page_listView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<HorizontalScrollView
android:id="#+id/chat_message_HSV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#+id/send_image_button"
android:clickable="true" />
<ImageButton
android:id="#+id/send_image_button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
This is the activity file:
public class MainActivity extends Activity {
ImageButton buyIB;
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sendIB = (ImageButton) findViewById(
R.id.send_image_button);
chatMessageHSV = (HorizontalScrollView) findViewById(
R.id.chat_message_HSV);
buyIB = (ImageButton) findViewById(R.id.buy_imageButton);
buyIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "buy", Toast.LENGTH_SHORT)
.show();
}
});
chatMessageHSV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT).show();
}
});
sendIB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "send", Toast.LENGTH_SHORT).show();
}
});
}// end of onCreate
}
And I cant use button click like
public void onClick(View view)
{}
because the actual problem is containing a drawer layout where I'm using fragments & there this method doesn't work.
The problem in the ImageButton doesn't respond for you click because the ListView will respond first for the click because it above the ImageButton try to re order it , when i remove it the ImageButton respond for the click
about the HorizontallScrollView i found the solution to use the Touch Event instead of Click try this code
chatMessageHSV.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(ImageButtonActivity.this, "hsv",
Toast.LENGTH_SHORT).show();
}else if (event.getAction() == MotionEvent.ACTION_UP){
// here code if the touch up
}
return false;
}
});
feed me back
The ImageButton not clicking problem can be solved by adding to ListView these 2 lines:
android:layout_below="#+id/topRL"
android:layout_above="#+id/bottomRL"

Categories

Resources