making buttons appear and disappear on an image view - android

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);
}
}
});

Related

adding view to linear layout not visible

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);
}
});

How to make the parent view to handle all the OnClick events?

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);
}
}
});

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();

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"

Android AlertDialog issue

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>

Categories

Resources