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... :)
Related
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();
}
I have a Bottomsheet xml which contains two elements, Follow and Copy link.. when I click on Follow, it should be changed to Unfollow dynamically. I tried using setText, not worked.
Please guide me how to change text of bottomsheet dialogues.
Here is my Bottomsheet xml file
<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/bottom_sheet_follow"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_mode_follow_grey_24dp"
/>
<TextView
android:id="#+id/followTView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Follow"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_sheet_copy"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_copy_grey_24dp"
/>
<TextView
android:id="#+id/copyLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Copy link"/>
</LinearLayout>
Bottomsheet Java code
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
But, the Follow Textview (I mean followTView) is not changing to Unfollow.
Please tell me where I have gone wrong...
Thanks in advance...
You can do it with boolean i have done it with my imageview.
Might help you
//Initialize boolean as true by default
public boolean showingFirst = true;
holder.ivFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (showingFirst){
Toast.makeText(mActivity, "Item added to favorite", Toast.LENGTH_SHORT).show();
holder.ivFavorite.setImageResource(R.drawable.filled_heart);
showingFirst = false;
}else {
holder.ivFavorite.setImageResource(R.drawable.ic_favorite);
Toast.makeText(mActivity, "Item removed from favorite", Toast.LENGTH_SHORT).show();
showingFirst = true;
}
}
});
Just a guess. Did you try to re-arrange the code? maybe the the click listener should load first before calling "mDialog.show()".
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();
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 am looking to slide/up down a nested layout on its parent layout click.
Ie the parent layout will have a hidden child. On click I would like the parents height to animate down (slide down) to fit the child layout. On click again I would like the child to animate up (slide up). Basically just animating the parents height to show/hide the child.
I have found this which looks to work but seems like a lot of code:
http://gmariotti.blogspot.com/2013/09/expand-and-collapse-animation.html
I have seen a lot of things using 'animateLayoutChanges' to animate things however I cannot get that to work.
I have tried this:
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout android:id="#+id/child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:animateLayoutChanges="true">
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text to show/hide"/>
</LinearLayout>
</LinearLayout>
Then in code:
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout child = (LinearLayout)findViewById(R.id.child);
child.setVisibility(child.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
That sets the visibility of the child view correctly but there is absolutely no animation.
Well, first of all android:animateLayoutChanges effects the child elements. So, obviously, if you are changing the properties of the element itself, it will not be animated.
I think you can accomplish what you are trying to in API 16 by enabling the LayoutTransition.CHANGING animation.
<LinearLayout android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Some text to show/hide"/>
</LinearLayout>
LinearLayout parent = (LinearLayout)findViewById(R.id.parent);
parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
View text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View message = findViewById(R.id.message);
ViewGroup.LayoutParams params = message.getLayoutParams();
params.height = params.height == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
message.setLayoutParams(params);
}
});
Try to use SlidingDrawer
use this code
xml layout sideupdown.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bopen"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="open" />
<Button
android:id="#+id/btogg"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="change" />
<Button
android:id="#+id/bclose"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="#+id/chkbok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="On/Off" />
</LinearLayout>
</SlidingDrawer>
</FrameLayout>
java class SlideUpDown.java
public class SlideUpDown extends Activity implements OnClickListener,
OnCheckedChangeListener, OnDrawerOpenListener, OnDrawerCloseListener {
Button opn, cloz, chng, hndl;
CheckBox chkbox;
SlidingDrawer sd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sideupdown);
refference();
opn.setOnClickListener(this);
cloz.setOnClickListener(this);
chng.setOnClickListener(this);
chkbox.setOnCheckedChangeListener(this);
sd.setOnDrawerOpenListener(this);
sd.setOnDrawerCloseListener(this);
}
private void refference() {
opn = (Button) findViewById(R.id.bopen);
cloz = (Button) findViewById(R.id.bclose);
chng = (Button) findViewById(R.id.btogg);
chkbox = (CheckBox) findViewById(R.id.chkbok);
sd = (SlidingDrawer) findViewById(R.id.slidingDrawer);
hndl = (Button) findViewById(R.id.handle);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bopen:
sd.open();
break;
case R.id.bclose:
sd.close();
break;
case R.id.btogg:
sd.toggle();
break;
}
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkbox.isChecked()) {
sd.lock();
hndl.setEnabled(false);
} else {
sd.unlock();
hndl.setEnabled(true);
}
}
#Override
public void onDrawerOpened() {
}
#Override
public void onDrawerClosed() {
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
In one of our applications we hide and show a header and footer. We do this by changing the height and visibility of the view.
It looks something like this
ValueAnimator va;
if (show)
va = ValueAnimator.ofInt(0, px);
else
va = ValueAnimator.ofInt(px, 0);
va.setDuration(400);
va.setInterpolator(new DecelerateInterpolator(2));
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
topicNavBar.getLayoutParams().height = value;
topicNavBar.requestLayout();
if (value == 0) {
if (show)
topicNavBar.setVisibility(View.VISIBLE);
else
topicNavBar.setVisibility(View.GONE);
}
if (show && value == px) {
animationInProgress = false;
}
if (!show && value == 0) {
animationInProgress = false;
}
}
});
va.start();
Hope this helps
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"