I am using a layout inflater instead of menu inflater in which the layout gets inflated in center of the page instead of showing at the right corner.
I have tried changing the gravity to right but it didn't works.
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="#color/primary"
android:orientation="vertical"
android:layout_alignParentEnd="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linshare"
android:paddingTop="5dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/share"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Share"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:id="#+id/lininvite"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/invite"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Invite Friends"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linrate"
android:paddingTop="5dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/rate"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Rateus"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:id="#+id/linhelp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:background="#drawable/help"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:text="Help"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Code:
private void menz() {
LayoutInflater layoutInflater = LayoutInflater.from(Additionone.this);
View promptView = layoutInflater.inflate(R.layout.menuinflate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Additionone.this);
alertDialogBuilder.setView(promptView);
final LinearLayout share = (LinearLayout) promptView.findViewById(R.id.linshare);
final LinearLayout invite = (LinearLayout) promptView.findViewById(R.id.lininvite);
final LinearLayout rate = (LinearLayout) promptView.findViewById(R.id.linrate);
final LinearLayout help = (LinearLayout) promptView.findViewById(R.id.linhelp);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Future of Learning http://www.pencilruler.com/ ");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
invite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
i.putExtra(Intent.EXTRA_SUBJECT, "QUIZ APP");
i.putExtra(Intent.EXTRA_TEXT, "You have to try this quiz app");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "NO EMAIL CLIENTS INSTALLED", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
});
help.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(Additionone.this,HelpAct.class);
startActivity(i);
Additionone.this.finish();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Screenshot:
You need to set your Alert Dialog Builder Alignment as you are prompting your view in it.
Using setGravity(Gravity.TOP | Gravity.RIGHT);
Refer this.
Window window = alertDialogBuilder.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.TOP | Gravity.RIGHT);
NOTE: You can change Gravity of Alert Dialog as per your choice.
Related
I am trying to add textview dynamically in alertdialog. But end up with null pointer error.
My code...
att_layout = (LinearLayout)findViewById(R.id.att_layout);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
TextView con = (TextView) dialogView.findViewById(R.id.textView29);
con.setText("Consultation on " + item.getString(4));
TextView des = (TextView) dialogView.findViewById(R.id.textView28);
des.setText(item.getString(2));
TextView pre = (TextView) dialogView.findViewById(R.id.textView31);
pre.setText(item.getString(3));
TextView fee = (TextView) dialogView.findViewById(R.id.textView32);
fee.setText(item.getString(5));
if (att.getCount() == 0) {
att.close();
} else {
//here is the part where error occurs
View addView = inflater.inflate(R.layout.att_values, null);
TextView att_name=(TextView)addView.findViewById(R.id.textView34);
att_name.setText(att.getString(3));
TextView att_value=(TextView)addView.findViewById(R.id.textView26);
att_value.setText(att.getString(4));
att_layout.addView(addView);
}
adapter1 = new GridViewAdapter(FilePathStrings, getApplicationContext());
grid = (GridView) dialogView.findViewById(R.id.gridview);
grid.setAdapter(adapter1);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + FilePathStrings[position]), "image/*");
startActivity(intent);
}
});
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
alert_label_editor.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView29"
android:layout_weight="1"
android:gravity="center"
android:textSize="24sp"
android:layout_marginBottom="10dp"
android:textStyle="normal|bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView27"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView28"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Prescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView30"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView31"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="fee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView33"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView32"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/att_layout"></LinearLayout>
</LinearLayout>
</ScrollView>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
att_values.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView34"
android:layout_weight="1"/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView26"
android:layout_weight="1"/>
When i add those dynamically adding code.. its stops worling... The whole other code is working fine...
Logcat...
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
How can i resolve this issue.?
Your alert_label_editor.xml file which contains the R.id.att_layout LinearLayout is not inflated until you run this line:
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
So when you try to find it in the current view, it is not found and att_layout is null
Move this line after you actually inflate the layout, and use dialogView:
att_layout = (LinearLayout) dialogView.findViewById(R.id.att_layout);
the issue is whithin 3 imageviews don't display toast and also i tried to log any text whithin its onlick and from anonymous also not working any suggestion?
Screenshot
My XML
<LinearLayout 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="match_parent"
android:background="#mipmap/bg_login"
android:orientation="vertical"
android:paddingBottom="19dp"
android:paddingLeft="10dp"
android:paddingRight="9dp"
tools:context="com.example.mahmoud.blink.LoginFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="217dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:src="#mipmap/logo_small" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#mipmap/overlay_white"
android:clickable="false"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="#+id/iv_tm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
app:srcCompat="#mipmap/t" />
<ImageView
android:id="#+id/iv_fbm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="31dp"
android:layout_marginRight="31dp"
android:layout_toLeftOf="#+id/iv_tm"
android:layout_toStartOf="#+id/iv_tm"
android:background="#mipmap/f"
android:clickable="true" />
<ImageView
android:id="#+id/iv_googlem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_toEndOf="#+id/iv_tm"
android:layout_toRightOf="#+id/iv_tm"
android:clickable="true"
app:srcCompat="#mipmap/g" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.5dp"
android:layout_marginRight="12dp"
android:drawableLeft="#mipmap/mail2"
android:drawablePadding="10dp"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.5dp"
android:layout_marginRight="12dp"
android:drawableLeft="#mipmap/password"
android:drawablePadding="10dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
</LinearLayout>
<com.wang.avi.AVLoadingIndicatorView
android:id="#+id/avi"
style="#style/AVLoadingIndicatorView.Large"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical|center_horizontal"
app:indicatorColor="#color/colorPrimary"
app:indicatorName="BallClipRotatePulseIndicator" />
</FrameLayout>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="15dp"
android:background="#mipmap/normal_btn"
android:text="Sign In"
android:textColor="#8b572a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_forget_pass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/sencondry_btn"
android:text="Forgot Password"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="6dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/sencondry_btn"
android:text="Sign up"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Java Code
in LoginFragment.java
//decleration within the fragment class
ImageView iv_g;
ImageView iv_fb;
ImageView iv_t;
From onCreateView method
View view = inflater.inflate(R.layout.fragment_login, container, false);
iv_fb = (ImageView) view.findViewById(R.id.iv_fbm);
iv_t = (ImageView) view.findViewById(R.id.iv_tm);
iv_g = (ImageView) view.findViewById(R.id.iv_googlem);
iv_fb.setOnClickListener(this);
iv_g.setOnClickListener(this);
iv_t.setOnClickListener(this);
From onClick method
if (v.getId() == R.id.iv_googlem) {
Toast.makeText(context, "google", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "google");
context.startService(intent);
} else if (v.getId() == R.id.iv_fbm) {
Toast.makeText(context, "facebook", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "facebook");
context.startService(intent);
} else if (v.getId() == R.id.iv_tm) {
Toast.makeText(context, "twitter", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "twitter");
context.startService(intent);
}
the 3 buttons works right , but the 3 ImageViews don't triggerd when clicked
Try this syntax and see if it works :
iv_fb.setOnClickListener(new view.OnClickListener{ public void onClick()
{
Toast.makeText(context, "google", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MyService.class);
intent.putExtra("type", "google");
context.startService(intent);
} });
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);
}
});
}
<LinearLayout 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:orientation="horizontal"
android:padding="10dp"
android:paddingTop="8dp"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/view"
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/accent"/>
<TextView
android:id="#+id/m_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF6600"
android:textSize="30dp"
android:layout_toRightOf="#id/view"
android:text="hey th"
android:textStyle="bold" />
<Button
android:id="#+id/m_delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/m_name"
android:layout_weight="0"
android:text="Delete" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I have put a button inside like this and this is shown in recycler view.
The problem I am facing is that the button click only works if I long press on the button else if i click normally then intent is called.
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MetricsDAO dao = new MetricsDAO(context);
dao.deleteMetrics(metrices.get(position).getEventName());
dao.close();
Intent i = new Intent(context, HealthGraph.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// ((Activity) context).finish();
}
});
I am doing this inside on onBindViewHolder()..
What is it that i am doing wrong or missing?
I have a listview contains items and footer views. Items layout works as it should, but footer's layout doesn't work like item views...
I could't understand where my fault is.
This is my item's layout 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:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lstCardItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/pm_dashboard_cardshadow" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5.33dp"
android:layout_marginLeft="11.67dp"
android:layout_marginRight="11.67dp"
android:layout_marginTop="6dp" >
<ImageView
android:id="#+id/ivCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:scaleType="fitXY" />
<com.controls.DynamicResizeImageView
android:id="#+id/ivCardLock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#drawable/pm_dashboard_cardmask"
android:scaleType="center"
android:src="#drawable/selector_btn_dashboard_lock" />
<LinearLayout
android:id="#+id/llInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:gravity="top|right"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:visibility="visible" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:gravity="center|right"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="#+id/tvCardBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvCurrency"
android:gravity="top"
android:text="10,344"
android:textColor="#color/white"
android:textSize="18dp"
android:typeface="serif" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:gravity="top"
android:text="#string/global_currency"
android:textColor="#color/white"
android:textSize="10dp"
android:typeface="sans" />
<TextView
android:id="#+id/tvCardBalanceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/pgDashboard_lblBalanceBanner"
android:textColor="#color/white"
android:textSize="11.33dp"
android:typeface="monospace" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/pm_dashboard_seam" />
</LinearLayout>
For my businness side lifecycle:
private void refreshCards() {
if (applyButtons != null)
DashboardFragment.this.lvCards.removeFooterView(applyButtons);
applyButtons = new LinearLayout(getActivity());
applyButtons.setTag("FOOTER");
applyButtons.setOrientation(LinearLayout.VERTICAL);
AbsListView.LayoutParams LLParams = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
applyButtons.setLayoutParams(LLParams);
if (ininalCount < 32) {
View createNewCardButton = getApplyButtonView(R.string.pgDashboard_lblCreateIninalCard, R.drawable.pm_dashboard_card_yenikartolustur, R.drawable.pm_dashboard_arrow_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CreateNewCardActivity.class);
startActivity(intent);
}
});
View AddCard = getApplyButtonView(R.string.pgDashboard_lblAddCard, R.drawable.pm_dashboard_card_yenikartekle, R.drawable.pm_dashboard_plus_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AddCardActivity.class);
startActivity(intent);
}
});
applyButtons.addView(createNewCardButton);
applyButtons.addView(AddCard);
}
final View applyIngCard = getApplyButtonView(R.string.pgDashboard_lblApplyToIngCard, R.drawable.pm_dashboard_card_orange, R.drawable.pm_dashboard_arrow_icon, new View.OnClickListener() {
#Override
public void onClick(View v) {
CardApplicationActivity.Synchronizer.synchronize((BaseActivity) getActivity(), new OnSyncCompletedListener() {
#Override
public void onSyncCompleted() {
Intent intent = new Intent(getActivity(), CardApplicationActivity.class);
startActivity(intent);
}
});
}
});
applyButtons.addView(applyIngCard);
View view = new View(getActivity());
view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics())));
view.setBackgroundResource(getResources().getColor(R.color.transparent));
applyButtons.addView(view);
bindAdapter();
}
private View getApplyButtonView(int stringId, int backgroundId, int iconId, View.OnClickListener onClickListener) {
final View view = inflater.inflate(R.layout.dashboard_list_card_item, null);
return view;
}
public void bindAdapter() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
DashboardFragment.this.lvCards.setLayoutTransition(new LayoutTransition());
DashboardFragment.this.lvCards.addFooterView(applyButtons, null, false);
if (adapter == null) {
adapter = new CardListAdapter(getActivity(), R.layout.dashboard_list_card_item, productModelContainerList);
controller = new CardListController(lvCards);
lvCards.setDropListener(this);
controller.setDragInitMode(CardListController.ON_LONG_PRESS);
lvCards.setFloatViewManager(controller);
lvCards.setOnTouchListener(controller);
lvCards.setAdapter(adapter);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
DashboardFragment.this.lvCards.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
DashboardFragment.this.lvCards.setLayoutTransition(null);
}
}
Here i made some changes,, use these and let me know... otherwise i need your drawable resources to set this.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/lstCardItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/background_holo_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5.33dp"
android:layout_marginLeft="11.67dp"
android:layout_marginRight="11.67dp"
android:layout_marginTop="6dp" >
<ImageView
android:id="#+id/ivCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/ivCardLock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#color/background_holo_light"
android:scaleType="center"
android:src="#color/bgColor" />
<LinearLayout
android:id="#+id/llInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cropBG"
android:gravity="top|right"
android:orientation="vertical"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:visibility="visible" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|center_horizontal"
android:gravity="center|right"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingTop="4dp" >
<TextView
android:id="#+id/tvCardBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvCurrency"
android:gravity="top"
android:text="10,344"
android:textColor="#android:color/white"
android:textSize="18dp"
android:typeface="serif" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="2dp"
android:gravity="top"
android:text="Currency"
android:textColor="#android:color/white"
android:textSize="10dp"
android:typeface="sans" />
<TextView
android:id="#+id/tvCardBalanceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="pgDashboard_lblBalanceBanner"
android:textColor="#android:color/white"
android:textSize="11.33dp"
android:typeface="monospace" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bottom_btn_bg_color" />
</LinearLayout>