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();
}
Related
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
The problem is next, when I typed in ImageView mIV_WishItem "heart", caused onClick background ImageView mIV_WishItem
The code this buttons:
holder.mIV_WishItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setFavorites(holder, sectionsItem, true);
}
});
holder.mIV_pictureGoodsItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SectionsItem sectionsItem = sectionsItems.get(position);
sectionItemId = sectionsItem.getId();
getDataForItemDetails(mContext);
}
});
And xml for this buttons
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/_200sdp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
>
<ImageView
android:id="#+id/iV_Wish"
android:layout_width="#dimen/bottom_navigation_icon"
android:layout_height="#dimen/bottom_navigation_icon"
/>
</LinearLayout>
<ImageView
android:id="#+id/iV_pictureGoods_Item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
Please help me find solution
You need to add attribute in your ImageView(mIV_WishItem) :
android:focusable="true"
android:focusableInTouchMode="true"
Viewholder item itself focusable, so we need to set focus on imageview. After that we can enable click on imageview.
I have a Layout that has a button that shows another view, but only for 1 time, I mean, you click the button, it dissapear and the other view is shown, the second time you should click the parent view of that button and the other view (the one that was shown) should dissapear and the button should appear again. I am trying with clickable:false and focusable:false but it is not working. How can I achieve that?
Relevant Code
XML
<LinearLayout
android:id="#+id/item_tournament_header"
android:background="#drawable/bg_card_tournament"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15">
<RelativeLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.CircularImageView
android:id="#+id/item_friend_img_profile_pic"
android:layout_height="48dp"
android:layout_width="48dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_profile"
app:siBorderColor="#color/white"/>
</RelativeLayout>
<LinearLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="10"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/tournament_name"
android:textSize="#dimen/text_h3"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tournament_client"
android:textSize="#dimen/text_p"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="2"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/btn_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_plus_tournaments"/>
</RelativeLayout>
</LinearLayout>
Java
btn_plus = (ImageView) findViewById(R.id.btn_plus);
TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.GONE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.VISIBLE)
{
// It's not entering here!!!
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
btn_plus = (ImageView) findViewById(R.id.btn_plus);
TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.GONE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.GONE)
{
// It's not entering here!!!
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
Maybe i miss understood the problem, but i think that your problem is the logic of if statement : if(btn_plus.getVisibility()==View.VISIBLE)
that should be :
if(TournamentContent.getVisibility()==View.VISIBLE) or if(btn_plus.getVisibility()==View.GONE)
Try to modify your code like this:
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.INVISIBLE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.INVISIBLE)
{
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
Textview should be made visible on checking the checkbox. However, it's not happening. When i tap on the checkbox, it becomes checked but the texview doesn't appear. i've set the visibility of the textview to be GONE in the XML. However,i'm setting it to VISIBLE when the checkbox is checked.
View footer = inflater.inflate(R.layout.footer, viewGroup,
false);
final CheckBox chb = (CheckBox) footer.findViewById(R.id.notify_confirm);
final TextView notify_tv = (TextView) footer.findViewById(R.id.notify_tv);
chb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (chb.isChecked())
{
//new CheckBoxThread().execute();
notify_tv.setVisibility(View.VISIBLE);
}
else
{
}
}
});
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:orientation="vertical"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
>
<CheckBox
android:id="#+id/notify_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Notify on Confirmation"
android:textColor="#4d4d4d"
android:paddingLeft="5dp"
android:visibility="visible"
/>
<TextView
android:id = "#+id/notify_tv"
android:layout_width="fill_parent"
android:textColor = "#color/myPrimaryColor"
android:gravity="center"
android:visibility="gone"
android:layout_height="wrap_content"
android:text="You will be notified on confirmation"></TextView>
</LinearLayout>
</LinearLayout>
It might be a bit naive of me to ask this question but i've tried everything and it just doesn't seem to work.
Instead of using OnClickListener , use OnCheckedChangeListener for checkboxes. Replace your code as below.
chb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
notify_tv.setVisibility(View.VISIBLE);
}
}
});
Add this property to the checkbox widget android:onClick="onCheckboxClicked"/>
Within the Activity that hosts this layout, the following method handles the click event for both checkboxes:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.notify_confirm:
if (checked)
//visible the textview here
else
//
break;
According to your way with onClickListener:
chkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
Toast.makeText(MyAndroidAppActivity.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
else{
//do else work
}
}
});
Other and Better way is to use:
CompoundButton.OnCheckedChangeListener:
As described by Mr. Lal.
Thanks
I checked your code it was fine, but just a little bit orientation (Layout Orientation) problem was there.
and Don't use TextView:visibility:"gone" (as gone will update your view and your view will scattered) rather use "invisible", it will not effect your view.
just put android:orientation="vertical" on your second LinearLayout.
here is your modified Layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:paddingLeft="6dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="vertical">
<CheckBox
android:id="#+id/notify_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingLeft="5dp"
android:text="Notify on Confirmation"
android:textColor="#4d4d4d"
android:visibility="visible" />
<TextView
android:id="#+id/notify_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="You will be notified on confirmation"
android:textColor="#000000"
android:visibility="invisible" >
</TextView>
</LinearLayout>
</LinearLayout>
and here is your java file:
final CheckBox chb = (CheckBox) findViewById(R.id.notify_confirm);
final TextView notify_tv = (TextView)findViewById(R.id.notify_tv);
chb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (chb.isChecked())
{
//new CheckBoxThread().execute();
notify_tv.setVisibility(View.VISIBLE);
}
else
{
notify_tv.setVisibility(View.INVISIBLE);
}
}
});
this will work... :)
I have a textview in dialog and I want when I click this textview to do something.
I want it when clicking on the textview to do something.
I have added clickable and onClick in my layout
Here are my codes
public class Activitytwo extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//WORKING CODES
//WORKING CODES
//WORKING CODES
//WORKING CODES
}
// This one to show the dialog
//This one works fine!
public void share(View view){
sharedialog = new Dialog(Activitytwo.this);
sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sharedialog.setContentView(R.layout.postdialog);
sharedialog.setCanceledOnTouchOutside(true);
sharedialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
sharedialog.show();
}
public void facebookp(View view){
//This one for the TextView (onClick)
//This one doesn't work..
}
}
This is my layout (postdialog.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/postdialogl"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="150dp"
android:background="#drawable/postdialog" >
<TextView
android:id="#+id/postdtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/post"
android:textColor="#a7b8d6"/>
<ImageView
android:id="#+id/poststrokea"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#202e4c"
android:layout_below="#+id/postdtitle"
android:layout_marginTop="5dp"/>
<ImageView
android:id="#+id/poststrokeb"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#803e5482"
android:layout_below="#+id/poststrokea"/>
<TextView
android:id="#+id/facebookpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="60dp"
android:layout_marginLeft="30dp"
android:drawablePadding="5dp"
android:text="#string/facebook"
android:textColor="#a7b8d6"
android:textSize="12sp"
android:drawableTop="#drawable/facebook"
android:clickable="true"
android:onClick="facebookp"/>
</RelativeLayout>
One solution is to declare your textview inside your dialog.
public void share(View view){
//your code.
sharedialog = new Dialog(Activitytwo.this);
sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
sharedialog.setContentView(R.layout.postdialog);
sharedialog.setCanceledOnTouchOutside(true);
sharedialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
final TextView tviPost = (TextView) sharedialog.findViewById(R.id.sharedialog);
tviPost.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//Do something
}
});
sharedialog.show();
}
and delete the next lines from your layout.
android:clickable="true"
android:onClick="facebookp"