I need to create a dialog that contains a matrix of simple square ImageButtons (like icons), in unknown number, and a Cancel button. The dialog should be inflatable programmatically and scrollable.
I really am confused: should I create a custom layout and apply it to AlertDialog? And how could I intercept the clicks? Should I use PopUpWiew? Should I make it as Activity or not?
And if I create a new Activity... should I use a Runnable?
Please don't provide me a complete source, I just need to understand what is the correct direction to go for this need.
Thank you in advance.
You can use a Dialog and set your layout file to it. A dialog is something like a mini activity which can be called onto an actual activity and dismissed at will.
Let this be the custom layout file - customLayout.xml
<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"
android:background="#drawable/bg"
tools:context=".AutoMode" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/blue_gradient"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_alignTop="#+id/relativeLayout1"/>
<Button
android:id="#+id/button2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/black_gradient"
android:layout_alignTop="#+id/relativeLayout1"
android:layout_toRightOf="#+id/button1"/>
<Button
android:id="#+id/button3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/button2"
android:background="#drawable/red_gradient" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/button4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/yellow_gradient"
android:layout_alignRight="#+id/relativeLayout2"
android:layout_alignTop="#+id/relativeLayout2" />
<Button
android:id="#+id/button5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/metallic_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button4" />
<Button
android:id="#+id/button6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/purple_gradient"
android:layout_alignTop="#+id/relativeLayout2"
android:layout_toRightOf="#+id/button5"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_below="#+id/relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/green_gradient"
android:layout_alignRight="#+id/relativeLayout3"
android:layout_alignTop="#+id/relativeLayout3" />
<Button
android:id="#+id/button8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/orange_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button7" />
<Button
android:id="#+id/button9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/violet_gradient"
android:layout_alignTop="#+id/relativeLayout3"
android:layout_toRightOf="#+id/button8" />
</RelativeLayout>
Here's how you can set this layout to a dialog box in the activity where you want to call it:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle("Matrix");
disclaimer.setContentView(R.layout.customLayout);
Button b1 = (Button) customDialog.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button1 is clicked
}
});
.
.//same for other buttons
.
Button b9 = (Button) customDialog.findViewById(R.id.button9)
b9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Do whatever you want when button9 is clicked
}
});
Button close = (Button) customDialog.findViewById(R.id.dismiss);
customDialog.show();
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
disclaimer.dismiss();
}
});
Related
How to generate custom dialog box in android like this,
I want just like this.
How to generate it. please give me suggestion.
i have used below code for dialog box, what is the problem in my code?
I have not identify it. please share me any Idea.
<?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:background="#android:color/transparent">
<RelativeLayout
android:id="#+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/btn_white"
android:paddingBottom="#dimen/thirty_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/ten_dp">
<TextView
android:id="#+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/ten_dp"
android:text="Quit LEarning?"
android:textSize="#dimen/twenty_sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/twenty_dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:id="#+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video" />
</LinearLayout>
</RelativeLayout>
Please share me any Idea.
Thanks.
Simple, 1st need to create an newcustom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_marginLeft="55dp"
android:layout_marginRight="55dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="165dp"
android:layout_gravity="center"
android:layout_marginEnd="60dp"
android:layout_marginStart="60dp"
app:cardCornerRadius="8dp"
app:cardElevation="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="Quit Earning?"
android:textColor="#android:color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. quit Earning?"
android:textSize="18dp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/frmNo"
android:layout_marginRight="45dp"
android:layout_marginTop="75dp">
<android.support.design.widget.FloatingActionButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#android:color/transparent"
app:backgroundTint="#color/fab2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="6dp"
android:text="No"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/frmOk"
android:layout_marginLeft="50dp"
android:layout_marginTop="75dp">
<android.support.design.widget.FloatingActionButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#android:color/transparent"
app:backgroundTint="#color/fab1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="6dp"
android:text="Ok"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
</FrameLayout>
Then, in java file (in activity) paste this code
public class ViewDialog {
public void showDialog(Activity activity) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.newcustom_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
FrameLayout mDialogNo = dialog.findViewById(R.id.frmNo);
mDialogNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
FrameLayout mDialogOk = dialog.findViewById(R.id.frmOk);
mDialogOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
dialog.show();
}
}
Finally you can call it wherever you want.
ViewDialog alert = new ViewDialog();
alert.showDialog(CustomDialogActivity.this);
Inside your dailog.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:background="#android:color/white">
<RelativeLayout
android:id="#+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/btn_white"
android:paddingBottom="#dimen/thirty_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/ten_dp">
<TextView
android:id="#+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/ten_dp"
android:text="Quit LEarning?"
android:textSize="#dimen/twenty_sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/twenty_dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
<Button
android:id="#+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
</LinearLayout>
</RelativeLayout>
open dimens.xml and add code mentioned below
<dimen name="thirty_dp">30dp</dimen>
<dimen name="ten_dp">10dp</dimen>
<dimen name="twenty_sp">20sp</dimen>
<dimen name="twenty_dp">20dp</dimen>
<dimen name="sixteen_sp">16sp</dimen>
open drawable and create btn_white.xml add code mentioned blow
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp"></corners>
<solid android:color="#android:color/white"></solid>
</shape>
open mainactivity.java and add the code mentioned below
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dailog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
Button videoButton = (Button) dialog.findViewById(R.id.btn_video);
// if decline button is clicked, close the custom dialog
videoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />
<Button
android:id="#+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="#android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>
You can change your button by using
android:src=#drawable/image
You have to extends Dialog and implements OnClickListener
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}
Call Dialog
CustomDialogClass cdd=new CustomDialogClass(Activity.this);
cdd.show();
Maybe try this method
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="#color/colorPrimary"
app:cardCornerRadius="16dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_all"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorButtonNormal"
android:text="Tebrikler!"
android:layout_gravity="center_horizontal"
android:textSize="36sp"
android:padding="8dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnDialogCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Çıkış"
android:textColor="#FFF"
android:layout_marginRight="5dp"
android:background="#drawable/dialog_button_background"
android:layout_gravity="center_horizontal"/>
<Button
android:id="#+id/btnDialogOk"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Devam"
android:textColor="#FFF"
android:layout_marginLeft="5dp"
android:background="#drawable/dialog_button_background"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
main.java
Dialog dialog = new Dialog(context, R.style.CustomDialog);
LayoutInflater layoutInflater = LayoutInflater.from(context);
CardView cardView = (CardView) layoutInflater.inflate(R.layout.dialog, null);
dialog.setContentView(cardView);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
dialogBtnCancel();
private void dialogBtnCancel(){
mBtnDialogCancel = dialog.findViewById(R.id.btnDialogCancel);
mBtnDialogOk = dialog.findViewById(R.id.btnDialogOk);
mBtnDialogCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
mBtnDialogOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
}
I have an app that uses two buttons and a number picker. Everything works fine when running an emulator with SDK 22 but when running SDK 23 the dialog is significantly smaller and does not include the leftmost button.
Here is the xml for the dialog:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="#+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
Here is the code I use to initialize the dialog:
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("Delay");
d.setContentView(R.layout.dialog);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
np.setMaxValue(100);
np.setMinValue(0);
np.setWrapSelectorWheel(false);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeDelay(np.getValue(), user);
d.dismiss();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
Try the layout below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:layout_marginBottom="98dp"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentRight="true"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_alignParentLeft="true"
android:text="Set" />
</RelativeLayout>
Some problems, are:
android:layout_alignBottom will cause the RelativeLayout to expand until the bottom of the screen. This is not good for dialog. You can find details here
You defined some huge margins. Your dialog in landscape mode is not displayed properly... Some contents become hidden.
But I'm not sure why it was working fine on API 22 and not on API 23
I have an linear layout in my application,In that i am using one root layout(invitation_single) and one nested layout(hidden).when i am onclick root layout at run time the nested layout visible(it contains"yes,no,maybe" buttons)successfully.Now my need is if i have similar layout values(consider event 2) immediately below the previous layout values(consider event 1),when i am on click "event 2" i need to hide nested layout of "event 1"(atpresent when i am click button the nested layout gone)automatically.how can i achieve this..here is my layout code,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/invitation_single"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerVertical"
android:dividerPadding="5dp"
android:showDividers="middle"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_action_event" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical">
<TextView
android:id="#+id/invitation_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="3dp"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/invitation_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:textColor="#color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/hidden"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="-270dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingTop="1dp"
android:visibility="gone"
android:weightSum="3">
<Button
android:id="#+id/yesbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="7dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Yes"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/nobutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="No"
android:textColor="#color/black"></Button>
<Button
android:id="#+id/buttonmaybe"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="Maybe"
android:textColor="#color/black"></Button>
</LinearLayout>
</LinearLayout>
my programming code is below,
final LinearLayout first = (LinearLayout) convertView.findViewById(R.id.invitation_single);
final LinearLayout second = (LinearLayout) convertView.findViewById(R.id.hidden);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(View.VISIBLE);
break;
}
}
});
is this what you're looking for?
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.invitation_single:
second.setVisibility(second.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
break;
}
}
});
I have created a recyclerview usin cardview and added button in each of the items of the recyclerview. I have already integrated the button click event . Now, when I click the button of one of the recyclerview item it responds normally and the button of other items responds too at the same time as if they are also clicked.what is the problem? how can it be solved?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="95dp"
android:orientation="horizontal"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:cardCornerRadius="1dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true">
<ImageView
android:id="#+id/icon_vendor1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_marginRight="10dp"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#drawable/ic_direction" />
<TextView
android:id="#+id/vendorName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/icon_vendor1"
android:text="Kozzaja Infotech Pvt Ltd"
android:textSize="20sp" />
<RatingBar
android:id="#+id/rating"
style="#style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vendorName1"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/imageView2"
android:layout_toRightOf="#+id/icon_vendor1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/vendorName1"
android:layout_marginBottom="3dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button4"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="10dp"
android:src="#drawable/ic_call_black_36dp" />
<ImageButton
android:id="#+id/button3"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="20dp"
android:src="#drawable/ic_messenger_outline_black_36dp" />
<ImageButton
android:id="#+id/button2"
android:layout_width="38dp"
android:layout_height="match_parent"
android:background="#drawable/circle_button"
android:src="#drawable/ic_directions_black_24dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/layout"
android:background="#drawable/circle_button"
android:tag="off"
android:text="+"
android:textSize="30sp" />
<View
android:layout_width="11dp"
android:layout_height="38dp"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is my adapter's code.
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(workers.get(position).getName());
holder.rating.setRating(workers.get(position).getRatings());
Picasso.with(context).load(workers.get(position).getImageLogo()).fit().into(holder.imageView);
holder.plus.setTag("off");
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/// button click event
if (holder.plus.getTag().toString().equals("off")) {
holder.plus.setTag("on");
holder.plus.setText("-");
//show the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_to_right));
holder.layout.setVisibility(View.VISIBLE);
} else if (holder.plus.getTag().toString().equals("on")) {
holder.plus.setTag("off");
holder.plus.setText("+");
//hide the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_to_left));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//disappear the button after 500ms
holder.layout.setVisibility(View.GONE);
}
}, 500);
}
}
});
}
I am inflating a layout inside a dialog, which consist of a scrollview but the layout is not scrolling. I have referred so many questions from stackoverflow but then also its not working. My layout.xml is given below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/White"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:text="Update available"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/upgrade_msg"
android:textColor="#000000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_margin="3dp"
android:layout_weight="0.02"
android:src="#drawable/updateimg2" />
<Button
android:id="#+id/btn_upgrade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Upgrade Now"
android:textColor="#color/Black" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
<Button
android:id="#+id/btn_remind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_forget_pwd"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/White"
android:gravity="center"
android:text="Remind Me Later"
android:textColor="#color/Blue" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="#drawable/line_y_h" />
</LinearLayout>
</ScrollView>
Dialog code
public void show_Alert_version_custom(String str, final String url,
final String exitStatus)// use for
{
final Dialog dialog = new Dialog(Splash.this);
dialog.setContentView(R.layout.version_update);
dialog.setTitle(getString(R.string.alert_title));
Button dialogButton2 = (Button) dialog.findViewById(R.id.btn_remind);
dialogButton = (Button) dialog.findViewById(R.id.btn_upgrade);
ImageView viewLine = (ImageView) dialog.findViewById(R.id.imageView2);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
// if button is clicked, close the custom dialog
dialogButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});
dialog.show();
}
android:fillViewport="true" in ScrollView solved my problem. Now its scrolling even inside dialog.
You can use simply below code on ScollView. it's working fine.
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
</LinearLayout>
</ScrollView>