Popup Library
I have to display my own header in popup and it
here + sign and back arrow is header i want to exactly like this
you can use Costume Dialog like below
public class dialogMesssageType {
Dialog dialog;
Context m_context;
Activity act;
public void showDialog(Activity activity, Context context, final String titre, final String message, final int type) {
dialog = new Dialog(activity);
m_context = context;
act = activity;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_message_type);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.transparent)));
TextView txt = (TextView) dialog.findViewById(R.id.txt);
TextView titre_id = (TextView) dialog.findViewById(R.id.txt_titre);
TextView ok = (TextView) dialog.findViewById(R.id.confirmation);
ImageView img_type(ImageView)dialog.findViewById(R.id.img_type);
ImageView close = (ImageView) dialog.findViewById(R.id.close);
txt.setText(message);
titre_id.setText(titre);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/rounded_gray"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/blue_neigra"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="32dp"
android:id="#+id/img_type"
/>
<TextView
android:id="#+id/txt_titre"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:gravity="center"
android:text="Warning"
android:textColor="#039470"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="18dp"
android:gravity="center"
android:text="Please fill in all fields"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:background="#color/blue_neigra"></View>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:id="#+id/confirmation"
android:textColor="#color/blue_neigra"
android:textSize="20sp"
android:text="Ok"/>
</LinearLayout>
<ImageView
android:id="#+id/close"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:background="#drawable/selector_blue_gray_cercle"
android:padding="12dp"
android:src="#drawable/close_blue" />
</RelativeLayout>
i removed Animation .
Related
I am developing app having a popup window showing successfully but button inside is not working. When the button is click the event is not listening. The code is shown below.
private void showPopup() {
//LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup_layout,null);
mPopupWindow = new PopupWindow(
customView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
Button btn_popup_submit = (Button)customView.findViewById(R.id.btn_popup_submit);
Button btn_popup_cancel = (Button)customView.findViewById(R.id.btn_popup_cancel);
btn_popup_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "aaa", Toast.LENGTH_SHORT).show();
Log.d("LOG","aaaa");
}
});
btn_popup_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "bbbb", Toast.LENGTH_SHORT).show();
Log.d("LOG","bbbb");
}
});
if(Build.VERSION.SDK_INT>=21){
mPopupWindow.setElevation(5.0f);//5.0f
}
mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext, android.R.color.transparent)));
mPopupWindow.setOutsideTouchable(false);
mPopupWindow.setTouchable(false);
mPopupWindow.update();
}
The pop up layout code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:background="#5b5e93"
>
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email Sent."
android:textSize="20sp"
android:textColor="#color/colorWhite"
android:padding="25sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_above="#+id/editText"
android:layout_marginBottom="50dp"
android:background="#color/colorGray"
android:orientation="horizontal"></LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:textSize="16sp"
android:textColor="#color/colorWhite"
android:text="#string/email_sent" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:ems="4"
android:maxLength="4"
android:minLines="4"
android:inputType="number" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editText"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btn_popup_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/buttoncolor"
android:text="Submit"
android:textAllCaps="false" />
<Button
android:id="#+id/btn_popup_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/buttoncolor"
android:text="Cancel"
android:textAllCaps="false" />
</LinearLayout>
</RelativeLayout>
The button names are btn_popup_submit and btn_popup_cancel.
I tried different methods but the problem. I don't know where is the problem facing. Please help me thanks.
Replace this line of code.Allow popWindow Touch.
mPopupWindow.setTouchable(false);
to
mPopupWindow.setTouchable(true);
I am trying to make custom dialog in which i want to make selection of textview and image visible if user is going to selection .
in below code i have given three text views and four image view, want that if user is going to select text view or image view it should be text color change and on image it should be tick mark should be appear.
like this attached image .
attached image
As per attached image i want to make selection if user select image it should appear as a tick mark and if user selects any text views then tick should below of the text views and color also changed.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.cust);
dialog.setTitle("Your title");
dialog.setCancelable(false);
dialog.show();
TextView d_btn_ok = (TextView) dialog.findViewById(R.id.btnOk);
TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel);
d_btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
d_btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
TextView tvMsg11 =(TextView) dialog.findViewById(R.id.tvMsg11);
TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
image1.setImageDrawable(getResources().getDrawable(R.drawable.one));
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
image2.setImageDrawable(getResources().getDrawable(R.drawable.two));
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
image3.setImageDrawable(getResources().getDrawable(R.drawable.three));
ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);
image4.setImageDrawable(getResources().getDrawable(R.drawable.four));
}
}
cust.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="430dp"
android:orientation="vertical"
android:background="#ffffff"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textColor="#293448"
android:text="Update your water intake" />
<TextView
android:id="#+id/tvMsg1"
android:layout_below="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="21.5dp"
android:textSize="16sp"
android:textColor="#293448"
android:text="How many glasses of water do you
drink daily?" />
<LinearLayout
android:id="#+id/linearImage21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvMsg1"
android:weightSum="3">
<TextView
android:id="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="23.5dp"
android:layout_height="wrap_content"
android:text="4 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg111"
android:layout_toRightOf="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="6 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg1111"
android:layout_toRightOf="#+id/tvMsg111"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="8 Glasses"
android:textSize="14sp"/>
</LinearLayout>
<TextView
android:id="#+id/container"
android:layout_below="#+id/linearImage21"
android:layout_marginTop="48dp"
android:layout_marginLeft="21.5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Your container size?" />
<LinearLayout
android:id="#+id/linearImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/container"
android:weightSum="4">
<ImageView
android:id="#+id/image1"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/custom_button_background"
android:checked="true"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- <LinearLayout
android:id="#+id/linearImag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/linearImage1"
android:weightSum="2">
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearImage1"
android:orientation="horizontal"
android:layout_marginTop="28dp"
android:weightSum="2"
>
<TextView
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_marginLeft="86.5dp"
android:textSize="16sp"
android:text="CANCEL"
android:textAllCaps="true"
android:textColor="#293448" />
<TextView
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="48.5dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_marginRight="6dp"
android:background="#fff"
android:text="SUBMIT"
android:textAllCaps="true"
android:textColor="#21c8d7" />
</LinearLayout>
</RelativeLayout>
After apply above code
I am getting like this image
this image
We can do it easily as per attached image
Made visible when need of Highlighter under text views or adjacent of image.
or Made invisible when not need or pressed That's it .
Wrote code for same which is working .
text color change on the basis of text selection :
text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#00b200"></item>
<item android:state_focused="true" android:color="#00b200"/>
<item android:state_selected="true" android:color="#000000"/>
<item android:color="#d3d3d3"></item>
</selector>
For Dialog :-
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.cust);
dialog.setTitle("Your title");
dialog.setCancelable(false);
dialog.show();
TextView d_btn_ok = (TextView) dialog.findViewById(R.id.btnOk);
TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel);
d_btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
d_btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
final TextView tvMsg1111=(TextView) dialog.findViewById(R.id.tvMsg1111);
final TextView tvMsg11 = (TextView) dialog.findViewById(R.id.tvMsg11);
ImageView tick5=(ImageView) dialog.findViewById(R.id.tick5);
ImageView tick6=(ImageView) dialog.findViewById(R.id.tick6);
ImageView tick7=(ImageView) dialog.findViewById(R.id.tick7);
tvMsg11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
//tvMsg11.setImageDrawable(getResources().getDrawable(R.drawable.tick));
tvMsg111.setSelected(false);
tvMsg1111.setSelected(false);
tvMsg11.setSelected(true);
tick5.setVisibility(View.VISIBLE);
tick6.setVisibility(View.INVISIBLE);
tick7.setVisibility(View.INVISIBLE);
}
});
// final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
tvMsg111.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvMsg11.setSelected(false);
tvMsg1111.setSelected(false);
tvMsg111.setSelected(true);
tick6.setVisibility(View.VISIBLE);
tick7.setVisibility(View.INVISIBLE);
tick5.setVisibility(View.INVISIBLE);
}
});
//final TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);
tvMsg1111.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvMsg11.setSelected(false);
tvMsg111.setSelected(false);
tvMsg1111.setSelected(true);
tick7.setVisibility(View.VISIBLE);
tick6.setVisibility(View.INVISIBLE);
tick5.setVisibility(View.INVISIBLE);
}
});
/*TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);*/
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
ImageView tick1=(ImageView)dialog.findViewById(R.id.tick1);
ImageView tick2=(ImageView)dialog.findViewById(R.id.tick2);
ImageView tick3=(ImageView)dialog.findViewById(R.id.tick3);
ImageView tick4=(ImageView)dialog.findViewById(R.id.tick4);
image1.setImageDrawable(getResources().getDrawable(R.drawable.one));
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick1.setVisibility(View.VISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
// ImageView image21 = (ImageView) dialog.findViewById(R.drawable.four);
// for replacing image on click of image
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick2.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image2.setImageDrawable(getResources().getDrawable(R.drawable.two));
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick3.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image3.setImageDrawable(getResources().getDrawable(R.drawable.three));
ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);
image4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick4.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image4.setImageDrawable(getResources().getDrawable(R.drawable.four));
cust.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="430dp"
android:orientation="vertical"
android:background="#ffffff"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textColor="#293448"
android:text="Update your water intake" />
<TextView
android:id="#+id/tvMsg1"
android:layout_below="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="21.5dp"
android:textSize="16sp"
android:textColor="#293448"
android:text="How many glasses of water do you
drink daily?" />
<LinearLayout
android:id="#+id/linearImage21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvMsg1"
android:weightSum="3">
<TextView
android:id="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="23.5dp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"
android:text="4 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg111"
android:layout_toRightOf="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#drawable/text_color"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
android:text="6 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg1111"
android:layout_toRightOf="#+id/tvMsg111"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"
android:text="8 Glasses"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/lineartick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="2dp"
android:layout_below="#+id/linearImage21"
android:weightSum="3">
<ImageView
android:id="#+id/tick5"
android:layout_width="16dp"
android:layout_weight="1"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_marginRight="12dp"
android:layout_height="10dp" />
<ImageView
android:id="#+id/tick6"
android:layout_width="16dp"
android:layout_weight="0.7"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_marginLeft="1dp"
android:layout_toRightOf="#+id/image1"
android:layout_height="10dp" />
<ImageView
android:id="#+id/tick7"
android:layout_width="16dp"
android:layout_weight="1.3"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_height="10dp" />
</LinearLayout>
<TextView
android:id="#+id/container"
android:layout_below="#+id/linearImage21"
android:layout_marginTop="48dp"
android:layout_marginLeft="21.5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#293448"
android:alpha="80"
android:text="Your container size?" />
<LinearLayout
android:id="#+id/linearImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/container"
android:weightSum="4">
<ImageView
android:id="#+id/image1"
android:layout_width="0dp"
android:layout_weight="1"
android:checked="true"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick1"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image1"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick2"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image2"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick3"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image3"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick4"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_marginRight="25dp"
android:layout_toRightOf="#+id/image4"
android:layout_height="13dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:layout_below="#+id/linearImage1"
android:weightSum="4">
<TextView
android:id="#+id/onne"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="150 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/twoe"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="250 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="500 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/foure"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginRight="6dp"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="1 LTR"
android:textAllCaps="true"
android:textSize="14sp"/>
</LinearLayout>
<View
android:id="#+id/viewp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:alpha="4"
android:background="#000000"
android:layout_below="#+id/linearscale"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearscale"
android:orientation="horizontal"
android:layout_marginTop="28dp"
android:weightSum="2"
>
<TextView
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_marginLeft="86.5dp"
android:textSize="16sp"
android:text="CANCEL"
android:textAllCaps="true"
android:textColor="#293448" />
<TextView
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="48.5dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_marginRight="6dp"
android:background="#fff"
android:text="SUBMIT"
android:textAllCaps="true"
android:textColor="#21c8d7" />
</LinearLayout>
</RelativeLayout>
I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button
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 want to create a pop up in my Android app as shown in the image in the above link. I am building a quiz app, in which I want to navigate between a set of questions. How can I get a pop up as shown?
You can create it with an AlertDialog, doing something like this:
First of all, create a layout where you put what you want to be shon on the AlertDialog. In this case, I'll show the first 5-numbers row (for example, this file layout/example.xml)
layout/example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/tv1"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/tv2"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/tv3"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/tv4"
android:layout_gravity="center_horizontal"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/tv5"
android:textSize="30dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
Then invoque this with an AlertDialog, just like this:
In your activity
LayoutInflater factory = LayoutInflater.from(this);
final View myCustomView = factory.inflate(R.layout.example, null);
final TextView tv1 = (TextView) myCustomView.findViewById(R.id.tv1);
final TextView tv2 = (TextView) myCustomView.findViewById(R.id.tv2);
final TextView tv3 = (TextView) myCustomView.findViewById(R.id.tv3);
final TextView tv4 = (TextView) myCustomView.findViewById(R.id.tv4);
final TextView tv5 = (TextView) myCustomView.findViewById(R.id.tv5);
/* Set the listeners */
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
tv5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* Do stuff */
}
});
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Goto").setView(textEntryView); /*Remember to use #string */
alert.show();