I have a problem adding programmatically buttons to linearLayout, the first button gets added but the others aren't visible.
nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//show or load next
Button curButton = new Button(myClass.this);
curButton.setText("" + text);
curButton.setBackgroundResource(R.drawable.tran_mini);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
curButton.setLayoutParams(params);
curButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
//do smg
}
});
paginglibrary.addView(curButton);
}
});
this is the xml file where the buttons are being added:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<LinearLayout
android:id="#+id/pagingView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
the first time I click on nextButton the button gets added and is show but the second time I add another button, it doesn't show.
I am sure they are getting added as I checked through the following code:
ViewGroup layout = (ViewGroup)findViewById(R.id.pagingView1);
System.out.println("layout count: "+ layout.getChildCount());
the count shows that the button is getting added but it's not showing.
Any idea what could be the cause?
I tried setting the abscissa of the button to make sure it's not being outside of the view but still the same scenario occurs.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new button"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<LinearLayout
android:id="#+id/pagingView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
The only difference is on the HorizontalScrollView width.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//show or load next
Button curButton = new Button(MainActivity.this);
curButton.setText("generated button");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
curButton.setLayoutParams(params);
curButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0)
{
//do smg
}
});
ll.addView(curButton);
}
});
Related
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
I have one AlertDialog which is working fine.I have set some background images to it with following code:
Button buttonPositive = (Button)dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button buttonNegative = (Button)dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonPositive.setBackgroundResource(R.drawable.custom_button);
buttonPositive.setTextColor(Color.WHITE);
buttonNegative.setBackgroundResource(R.drawable.custom_button);
buttonNegative.setTextColor(Color.WHITE);
Now after setting image the two buttons are touching each other, i mean they have no space between them.I have tried with setPadding(...),it's not working.Even if i am changing the image size(i.e. width) it is not working.Any help !!!
I think its better to create layout xml file what you want ...
and set Like alertDialog.setContentview(R.layout.mylayout);
try this code
private Dialog myDialog;
myDialog = new Dialog(ShowReportActivity.this);
myDialog.setContentView(R.layout.alert);// your xml
myDialog.setTitle("Send Email");
myDialog.setCancelable(true);
Button set = (Button) myDialog .findViewById(R.id.alert_bnt_send_email);
Button exit = (Button) myDialog.findViewById(R.id.alert_bnt_exit);
set.setTextColor(Color.WHITE);
set.setBackgroundResource(R.drawable.custom_button);
getMailId = (EditText) myDialog.findViewById(R.id.alert_editT_email_Id);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
.........
myDialog.dismiss();
});
exit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.show();
use custome dialog using code like given below
Dialog windialog = new Dialog(YourActivity.this);
windialog.setContentView(R.layout.win_dialog);
windialog.setTitle("Congratulation");
windialog.setCancelable(true);
final EditText et_emailverification=EditText)windialog.findViewById(R.id.et_emailveri);
et_emailverification.setText(UserEmailOrName);
Button submit=(Button)windialog.findViewById(R.id.bt_sub_que);
submit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//write here your code what you want onclick
}
});
Button cancel=(Button)windialog.findViewById(R.id.bt_sq_cancel);
cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windialog.cancel();
});
windialog.show();
and xml like win_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et_emailveri"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</LinearLayout>
<RelativeLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/bt_sub_que"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/bt_sq_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Cancel" />
</RelativeLayout>
</LinearLayout>
I have an image view, i want to to create two buttons which will enable users to go to next or previous image. When a user taps the image , the buttons should appear and on the second tap , the button should disappear. How to go about doing this ?
Fortunately currently I am working on the same thing and here is my code modify it to suit your needs
xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:visibility="gone">
<Button
android:id="#+id/buy"
android:text="Buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/cancel"
android:text="Cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Main code
Button mBuy = (Button) findViewById(R.id.buy);
mBuy.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
Button mCancel = (Button) findViewById(R.id.cancel);
mCancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
ImageView mView = (ImageView) findViewById(R.id.image);
mView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout mLayout = (LinearLayout) findViewById(R.id.button_holder);
if(!isVisible)
{
isVisible = true;
mLayout.setVisibility(View.VISIBLE);
}
else
{
isVisible = false;
mLayout.setVisibility(View.GONE);
}
}
});