Cannot position a dialog at the center of the view - android

I want to put a custom dialog at the center point of a specific layout.
However the dialog does not go to the center. actually i tried various ways however now i am stuck... such a headache.
I tried like the code below.
showCustomDialog(){
View view = (View) getLayoutInflater().inflate(R.layout.abc, null);
view.measure(view.MeasureSpec.UNSPECIFIED, view.MeasureSpec.UNSPECIFIED);
LinearLayout BlackBox = (LinearLayout) view.findViewById(R.id.txt_layout);
Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.x=blackBox.getMeasuredWidth()/2;
params.y=blackBox.getMeasuredHeight()/2;
System.out.println("params " + params.x + " " + params.y);
dialog.getWindow().setAttributes(params);
dialog.getWindow().setLayout(view.getMeasuredWidth(), WindowManager.LayoutParams.WRAP_CONTENT);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.cust_dialog);
dialog.show();
}
The thing is that i want to show the dialog at the center of the "txt_layout" named layout.
Thanks for reading this..

Please try this if you haven't tried before:
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
And in the xml please check if you set this params:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/PauseDialogTitle</item>
</style>
<style name="PauseDialogTitle" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#android:style/TextAppearance.DialogWindowTitle</item>
</style>
</resources>
And you layout should look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>

Related

How to define the text color of all children in an Android Layout?

I am currently filling a TableLayout with data from external sources. After creating the TableRows and TextViews programmatically, their text color is set to white (seems to be the default).
I know that I can set the color by using myTextView.setTextColor(Color.BLACK);, but I am looking for a way to define the color in the XML layout file (I want to separate visuals from the logic). Does anyone know how to do this?
My layout (filled with example rows):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/mytable"
android:stretchColumns="0,1,2"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
style="#style/AppTheme" >
<TableRow>
<TextView android:text="field1" />
<TextView android:text="field2" />
<TextView android:text="field3" />
</TableRow>
</TableLayout>
My java code:
public void updateTable() {
TableLayout table = (TableLayout) findViewById(R.id.mytable);
table.removeAllViewsInLayout();
TableRow row = new TableRow(getApplicationContext());
TextView text;
// Add Heading
text = new TextView(getApplicationContext());
text.setText("Head1");
text.setTextColor(this.textColor);
row.addView(text);
text = new TextView(getApplicationContext());
text.setText("Head2");
text.setTextColor(this.textColor);
row.addView(text);
text = new TextView(getApplicationContext());
text.setText("Head3");
text.setTextColor(this.textColor);
row.addView(text);
table.addView(row);
for(Data d : this.getData()) {
TableRow r = new TableRow(getApplicationContext());
TextView t1 = new TextView(getApplicationContext());
TextView t2 = new TextView(getApplicationContext());
TextView t3 = new TextView(getApplicationContext());
t1.setTextColor(this.textColor);
t2.setTextColor(this.textColor);
t3.setTextColor(this.textColor);
t1.setText(d.getData1());
t2.setText(d.getData2());
t3.setText(d.getData3());
r.addView(t1);
r.addView(t2);
r.addView(t3);
table.addView(r);
}
}
Create a styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyTextViewStyle</item>
</style>
<style name="MyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
Then just apply that theme to your application in AndroidManifest.xml:
<application […] android:theme="#style/MyTheme">…
1.Create one table_item.xml file of TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="#string/hello_world"
android:textStyle="bold"
android:textColor="#ff00ff"/>
2.Inflate table_item.xml
LayoutInflater inflater = LayoutInflater.from(getContext());
TextView text = (TextView)inflater.inflate(R.layout.table_item, this);
3.Add into TableLayout
r.addView(text);
table.addView(r);
If you want to do dynamically then after initializing mytable in java and then proceed as
for (int i = 0; i < mytable.getChildCount(); i++) {
View view = mytable.getChildAt(i);
if(view instanceof EditText)//
((EditText)view).setTextColor(Color.BLACK);
}
Create one common style in style.xml in values folder
<style name="WelcomeTextViewTitleStyle">
<item name="android:textSize">#dimen/welcome_textview_title_textsize</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
</style>
apply to in your textview
<TextView android:id="#+id/tv_title2"
style="#style/WelcomeTextViewTitleStyle"
android:text="#string/wantToKnow" />
try this code
<TextView android:text="assads"
android:textColor="#000000"/>
</TableRow>

How to make an alert dialogue transparent in android

I am using an alert dialog for displaying some checkboxes.I need a TRANSPARENT view for the whole alertview and for it contents.I tried many way...but not helps me...can anybody help me...any help will be highly apppreciated....
code for creating alert dialogue in java
Context c = getParent();
m_adapter = new Intrested_in_adapter(
Requestclass.this,
R.layout.intrestedin, offferList);
// Dialog dialog = new Dialog(this, R.style.NewDialog);
// mDialog = new AlertDialog.Builder(c,R.style.NewDialog);
mDialog = new AlertDialog.Builder(c);
mDialog.setTitle("Intrested In");
mDialog.setAdapter(m_adapter,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int item) {
}
});
alertDialog = mDialog.create();
Log.e(tag,"before background setting");
// alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Log.e(tag,"after background setting");
alertDialog.show();
xml for alert dialogue(intrestedin.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>
May be this helps
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/transparent" //make background transparent here by using this line
>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
<style name="TransparentDialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#android:color/transparent</item>
</style>
use in java
Dialog dialog = new Dialog(this, R.style.TransparentDialog);
Hope this will help you!!..
You can just set a default android theme so, there is no need to set NoTitleBar in extra params, then try a once it will working.
dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);

Remove alert dialog border in custom Dialog

In my application i am using custom dialog .my dialog have rounded rectangle border and i can not remove that.please help me to set style for that to remove border. Thanks
public class myDialog extends Dialog
{
private String mMessage = "";
public myDialog(Context context, String message)
{
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
View v = getLayoutInflater().inflate(R.layout.dialog, null);
mMessage = message;
setCancelable(false);
setCanceledOnTouchOutside(false);
setContentView(v);
getWindow().setLayout(300,150);
getWindow().setBackgroundDrawableResource(R.);
}
}
My dialog cutom Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dialog"
>
</LinearLayout>
This has been a long time frustrating issue with dialogs in Android. The only way I've been able to solve it, is by extending DialogFragment instead of Dialog and adding the following:
YourDialog.java
Dialog dialog = new Dialog(getActivity(), R.style.ActivityDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
styles.xml
<style name="ActivityDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowTitleStyle">#style/ActivityDialog</item>
</style>
android:border = "invisible"
or transparent. You can find the info online

Android make a dialog appear in fullscreen

I need the dialog to fill the screen except for some space at the top and the bottom.
I've search for a solution but couldn't find one probably because I'm declaring it in an onClickListener.
Can someone please give a solution?
Activity code:
sort.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
// Get the layout inflater
LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
View sortView = inflater.inflate(R.layout.sort_layout, null);
sort.setView(sortView);
sort.create().show();
}
});
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="fill_parent"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:background="#color/white"
android:orientation="vertical" android:layout_gravity="center">
+ some more stuff here I dont think it's relevant
</LinearLayout>
here set your screen width and width to the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = width;
lp.height = height;
dialog.getWindow().setAttributes(lp);
or in your styles create a style like this then
<style name="DialogTheme" parent="android:Theme.Dialog">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
</style>
create a dialog object like this
dialog = new Dialog(this, R.style.DialogTheme);
Edit ::
get width and height like this for any device it will fills..
WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
int width, height;
LayoutParams params;
if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
width = manager.getDefaultDisplay().getWidth();
height = manager.getDefaultDisplay().getHeight();
} else {
Point point = new Point();
manager.getDefaultDisplay().getSize(point);
width = point.x;
height = point.y;
}
first Make custom style for dialog in style.xml file:
<style name="full_screen_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Then after set this theme to your Alert Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.full_screen_dialog));
Or You can call new Activity and Give this Custom style to that activity as a theme in your manifest file...
This may helpful for someone.
I want a dialog to take full width of screen. searched a lot but nothing found useful. Finally this worked for me:
mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);
after adding this, my dialog appears in full width of screen.
Make object like below theme
Dialog objD = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
Dialog dialog2 = new Dialog(context, R.style.DialogTheme);
dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog2.getWindow().setBackgroundDrawable(null);
dialog2.setContentView(R.layout.dialog_img);
WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
Window window = dialog2.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
lp.gravity = Gravity.CENTER;
final ImageView imgprofile=(ImageView)dialog2.findViewById(R.id.img_centre);
Picasso.with(context)
.load(arrayImages.get(position).get("image"))
.resize(800,1000)
.centerInside()
.into(imgprofile, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
imgprofile.setImageResource(R.drawable.user);
}
});
dialog2.show();
And in your dialog_img.xml file add an Imageview with scaleType(fitXY).
I think you shoud try this one:
Inside the dialogFragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
The simplest way of creating a full screen dialog, create a style like this:
<style name="DialogFullScreenTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
</style>
then:
dialog = new Dialog(this, R.style.DialogFullScreenTheme);

Android: dialog box without shadow around it

How do I remove this "shadowBox" effect around the dialog box that fades the background?
I know I can create like totally custom view but let's say I want to keep it simple. I have xml layout for dialog with only textView and 2 buttons and I want to stop the fade only for this one dialog.
Try:
dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
create style.xml inside values and put things like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="progress_dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#00ffffff</item>
</style>
And in the end, put this style to your dialog box.
Simply use
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
here give the sample class
public void showDialog () {
Dialog dialog = new Dialog(this);
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.requestFeature(window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sample);
ImageView imggameover, imgplayagain;
imggameover = (ImageView) dialog.findViewById(R.id.gameover);
imgplayagain = (ImageView) dialog.findViewById(R.id.playagain);
imgplayagain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), random.class));
onStop();
finish();
}
});
dialog.show();
}
Dialog's layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gameoverlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<ImageView
android:id="#+id/gameover"
android:layout_width="225dip"
android:layout_height="160dip"
android:background="#drawable/gameover"
></ImageView>
<ImageView
android:id="#+id/playagain"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginTop="100dip"
android:background="#drawable/playagain"
></ImageView>
</RelativeLayout>
Just that you know it is also possible to change the dim amount by writing this in your theme:
<item name="android:backgroundDimAmount">0.5</item>

Categories

Resources