how to add close button on the top-center of BottomSheetDialog android? - android

want to add close button on BottomSheetDialog to close it.
I'm using with custom view(Linearlayout) for BottomSheetDialog.
filterDialog = new BottomSheetDialog(context);
filterDialog.setContentView(rootView);
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
below image is my filterDialog:
And this is what I want to achieve:
Any help would be greatly appreciated.

I have solved this by using Dialog(full screen) with custom(bottom) animations.
Design:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorTransparent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ImageView
android:id="#+id/img_close"
android:layout_width="#dimen/fifty_dp"
android:layout_height="#dimen/thirty_dp"
android:src="#drawable/ic_close_filter"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<!-- All your required widgets here-->
</LinearLayout> </LinearLayout>
Java Code:
Dialog filterDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.dialog_filter, null);
final ImageView close = (ImageView) rootView.findViewById(R.id.img_close);
builder.setView(rootView);
filterDialog = builder.create();
WindowManager.LayoutParams lp2 = new WindowManager.LayoutParams();
Window window = filterDialog.getWindow();
filterDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
lp2.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp2.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp2);
Window dialogWindow = filterDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.BOTTOM );
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();

You can do it as -
<!--you bottom sheet linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Layout contains button-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#android:color/transparent"
android:clickable="false"
>
<!--Your button-->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your drawable"
android:background="#android:color/transparent"
android:clickable="true"/>
</LinearLayout>
<!--put Your other views in below linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
Note : clickable false is very important point. Doing this you can access view behind your linear layout. Also set background color to transparent

Related

Layout weight not working as intended

I have a slight problem getting my layout_weight to work. I am creating a custom bottomBar. But i cant make it to stretch as i want it to.
Bottom Bar View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_bar_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/bottom_bar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:orientation="horizontal"/>
</RelativeLayout>
This is the big container i am adding my buttons (items) to.
Bottom Bar Item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/bottom_bar_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/arrow_up_float"/>
<TextView
android:id="#+id/bottom_bar_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>
</LinearLayout>
This is my items that i add dynamically to the view. But somehow the view is not stretched properly.
But when i hardcode it in. It works. So could it be that layout weight does not work dynamically?
How i add the views (items)
private void updateItems(final List<BottomBarTab> bottomBarTabs) {
if (bottomBarTabs.size() < TABS_COUNT) {
throw new RuntimeException("Not enough buttons for bottomBar");
}
for (int i = 0; i < TABS_COUNT; i++) {
bottomBarTabs.get(i).setOnClickListener(this);
bottomBarTabs.get(i).prepareLayout();
container.addView(bottomBarTabs.get(i));
}
}
Screenshot
LayoutWeight is given to inner components of layout only not on parent Linearlayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:weightSum="2">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/bottom_bar_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/arrow_up_float"/>
<TextView
android:id="#+id/bottom_bar_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEXT"/>
</LinearLayout>
public void prepareLayout() {
View view = inflate(getContext(), R.layout.bottom_bar_item,this);
LinearLayout.LayoutParams params =
new LayoutParams(0,LayoutParams.MATCH_PARENT,LAYOUT_WEIGHT);
view.setLayoutParams(params);
if(backgroundColor != null) {
view.setBackgroundColor(backgroundColor);
}
TextView titleText = (TextView) view.findViewById(R.id.bottom_bar_item_text);
titleText.setText(title);
AppCompatImageView imageView = (AppCompatImageView) view.findViewById(R.id.bottom_bar_item_icon);
imageView.setImageResource(iconResId);
}
I changed in my prepareLayout function and put new layoutParams. Because somehow after inflation. The view ignores the weight that was set to it. So i had to force it by code. Maybe this is a android bug?

Android create rectangle with four different colors

How do I create a colored Rectangle like above one. Is it possible to control the length of each colored rectangle from java code? (above figure has a single rectangle with 4 colored inside it). I've been searching for answers since a few hours & was unable to find a solution.
EDIT:
I'm using AppCompatActivity and here's content_main.xml & activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="#+id/parentis"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#dc3838"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#c4c11f"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#2c64e7"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".25"
android:background="#a11a7d"/>
</LinearLayout>
</RelativeLayout>
activity_main.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
Finally java code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View child1= new View(this);
View child2= new View(this);
View child3= new View(this);
View child4= new View(this);
LinearLayout parent = (LinearLayout) findViewById(R.id.parentis);
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.15f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.15f;
child2.setLayoutParams(childParam2);
LinearLayout.LayoutParams childParam3 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam3.weight = 0.20f;
child3.setLayoutParams(childParam3);
LinearLayout.LayoutParams childParam4 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam4.weight = 0.50f;
child4.setLayoutParams(childParam4);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
parent.addView(child3);
parent.addView(child4);
}
you can display colored rectangles on the screen by simply setting the backgroundcolor of imageviews:
imageview.SetBackground(R.color.mycolor);
Then you just need to put 4 of those ImageViews inside a LinearLayout and change their size programmatically.
first, you should get the screen size:
private Point getScreenSize(){
WindowManager windowManager = (WindowManager) YamsaferApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
}
then write the following code:
int width = getScreenSize().x;
int redWidth = width / 4;
int orangeWidth = width / 4;
int greenWidth = width / 4;
int cyanWidth = width / 4;
LinearLayout container = new LinearLayout(getContext());
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT,70);
container.setLayoutParams(params);
// create the red area:
View redView = new View(getContext());
redView.setBackgroundColor(Color.RED);
redView.setLayoutParams(new LinearLayout.LayoutParams(redWidth,70));
container.addView(redView);
// create the orangeArea:
View orangeView = new View(getContext());
orangeView.setBackgroundColor(Color.RED);
orangeView.setLayoutParams(new LinearLayout.LayoutParams(orangeWidth,70));
container.addView(orangeView);
//create the green area
View greenView = new View(getContext());
greenView.setBackgroundColor(Color.RED);
greenView.setLayoutParams(new LinearLayout.LayoutParams(greenWidth,70));
container.addView(greenView);
create the cyan area:
View cyanView = new View(getContext());
cyanView.setBackgroundColor(Color.RED);
cyanView.setLayoutParams(new LinearLayout.LayoutParams(cyanWidth,70));
cyanainer.addView(cyanView);
If you would like to have a simpler layout with just one view and be able to change the sizes of the rectangles dynamically you can create a simple custom view. Override the onDraw(Canvas canvas) method and inside you can do something like this:
int rectWidth = getWidth() / 4;
canvas.drawRect(0, 0, rectWidth, getHeight(), redPaint);
canvas.drawRect(rectWidth, 0, 2 * rectWidth, getHeight(), orangePaint);
canvas.drawRect(2 * rectWidth, 0, 3 * rectWidth, getHeight(), greenPaint);
canvas.drawRect(3 * rectWidth, 0, getWidth(), getHeight(), pinkPaint);
You can create paints like this:
redPaint = new Paint();
redPaint.setColor(*Your red color here*);
If you need the black border around the smaller rectangles you can just add some margin to the drawRect(...) calls above and add canvas.drawARGB(255,0,0,0) above them. For round rectangles you can use the drawRoundRect(...) methods.
use this for same size rectangle .
rectangle.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="horizontal">
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#dc3838"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#c4c11f"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#2c64e7"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#a11a7d"/>
</LinearLayout>
and this different size rectangle....
<?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="horizontal">
<View
android:layout_width="80dp"
android:layout_height="100dp"
android:background="#dc3838"/>
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#c4c11f"/>
<View
android:layout_width="50dp"
android:layout_height="100dp"
android:background="#2c64e7"/>
<View
android:layout_width="90dp"
android:layout_height="100dp"
android:background="#a11a7d"/>
</LinearLayout>
output like that .......
Note:-set id to each one and change its size using LayoutPrams....
Take a linear layout with android:weightsum="1" .Then create 4 different views inside the linear layout with android:weight=".25"
Set your desired color for each views.
Do not forget to set the orientation of linear layout as horizontal
Sample code:
<?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="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#color/your_color"/>
</LinearLayout>
This will distribute each color equally.If you not want to distribute each color equally then you can redistribute weight for each views.
To set the view length consisting each color:
First create a blank linear layout in your xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="#+id/ll_parent"
android:orientation="horizontal">
</LinearLayout>
Then in the java file:
LinearLayout parent=(LinearLayout)findViewById(R.id.ll_parent)‌​;
View child1= new View(this);
View child2= new View(this);
View child3= new View(this);
View child4= new View(this);
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.25f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.25f;
child2.setLayoutParams(childParam2);
LinearLayout.LayoutParams childParam3 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam3.weight = 0.25f;
child3.setLayoutParams(childParam3);
LinearLayout.LayoutParams childParam4 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam4.weight = 0.25f;
child4.setLayoutParams(childParam4);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
parent.addView(child3);
parent.addView(child4);

Inflating View into LinearLayout and editing cmponents in this view

I am trying to add multiple LinearLayouts into one declared in xml. Everyone has 3 textView which will be edited in code. My problem is, when i am inflating xml layout to View object in code, all margins are ignored. My second question:
How can i dynamically set ids to textViews and then edit text in it?
LinearLayout xml which is inflating:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/pointsAwaiting"
android:layout_marginTop="40dp"
android:background="#drawable/background_blue"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:textColor="#FFFFFF"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>
He is inflating into this piece of code:
<
ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">
<LinearLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/historyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
And finnaly java code: (loop counter is for example)
LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);
for (int i=0; i<=2; i++){
View layout = View.inflate(this, R.layout.history_bar, null);
mainView.addView(layout);
}
The reason all your margins are being ignored is that you are passing null into your inflater here:
View layout = View.inflate(this, R.layout.history_bar, null);
When you do this all the LayoutParams of your view are thrown away. You should replace it with:
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
To set text in the TextViews you don't need to set a different id for each one, just give each one an id. Then in your loop you can do something like:
List<TextView> textViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this);
for (int i=0; i<=2; i++){
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
mainView.addView(layout);
TextView textView = (TextView) layout.findViewById(R.id.text1);
textViews.add(textView);
}
You would then have a reference to each of your TextViews in the List

Add view to a vertical LinearLayout at bottom (programmatically)

I need to add views to a vertical Linearlayout at bottom programmatically. (Yes we know that, it's in the title).
Ok : i can add views to the linearlayout, it's not a problem. But it's for a chat, so i need to add views at the bottom for each message which is displayed. I don't find any attributes to the LinearLayout to do that.
Adding messages :
mHandler.post(new Runnable() {
#Override
public void run() {
TextView message = new TextView(ChatActivity.this);
String[] splitMessage = text.split(":");
message.setText(splitMessage[0]+"\n"+splitMessage[1]);
message.setGravity(Gravity.LEFT);
message.setMaxWidth(200);
message.setBackgroundColor(getResources().getColor(android.R.color.tertiary_text_light));
message.setTextColor(getResources().getColor(android.R.color.black));
message.setSingleLine(false);
mLayout.addView(message);
}
});
chat_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/live_obs_mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/empty"
android:orientation="vertical" >
<ScrollView
android:id="#+id/chat_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:isScrollContainer="tue"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_chat"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="left|center_vertical"
android:layout_weight="5" />
<Button
android:id="#+id/chat_submit"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="right|center_vertical"
android:onClick="onSubmit"
android:text="OK" />
</LinearLayout>
</LinearLayout>
You can do it pragmatically like this, this is just an example. You may want to change your code based on that.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
TextView txt1 = new TextView(MyClass.this);
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
txt1.setLayoutParams(layoutParams);
linearLayout.addView(txt1);

Android not sizing Custom Dialog big enough

I am using a custom Dialog that contains a text field, an image, and a button. The text can contain HTML. Sometimes the bottom of the dialog gets chopped off the bottom when the text is long enough. How can I prevent this? I want Android to determine the size of the dialog but it doesn't seem to be doing that. DO I need to size the Dialog myself in this case?
Here is the layout...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/alert_root_incorrect"
style="#style/AlertDialogTheme"
android:background="#drawable/rounded_alert"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/rounded_alert"
>
<TableLayout
android:stretchColumns="0"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold"
android:text="Sorry, that's wrong!"
android:textColor="#color/gray_dark" />
<ImageView
android:id="#+id/check"
android:background="#drawable/xmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/alert_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:text="In fact, this is where the explanation will go. Something about how this passage related to the topic"
android:textColor="#000000" />
<Button
android:id="#+id/okay_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_alert_button"
android:text="Okay"
android:textColor="#color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
And here is the code I am using to load it...
if ( null == layout ) {
this.layout = inflater.inflate(R.layout.alert_incorrect, (ViewGroup) findViewById(R.id.alert_root_incorrect));
}
TextView message = (TextView) layout.findViewById(R.id.alert_text);
message.setText(Html.fromHtml(card.getConclusion()));
((Button) layout.findViewById(R.id.okay_button)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismissDialog(INCORRECT_DIALOG);
nextQuestion();
}
});
layout.requestLayout();
dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
dialog.setCancelable(false);
return dialog;
And here's a snap of what I mean..
Thanks,
John
This is not perfect but I've corrected it by setting the layout of the dialog relative to the default display.
dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(
(int)(window.getWindowManager().getDefaultDisplay().getWidth() * .90),
(int)(window.getWindowManager().getDefaultDisplay().getHeight() * .90 ));
dialog.setCancelable(false);
Just tweak the ".90" values until it feels right.
Here is the solution:
You should add a Linearlayout at the outside of your dialog's xml file
Then set this Linearlayout's gravity as "center"
the last step is creating a LayoutParams and set it to the dialog
android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams();
lp.width = android.view.WindowManager.LayoutParams.FILL_PARENT;
lp.height = android.view.WindowManager.LayoutParams.FILL_PARENT;
alertDialog.getWindow().setAttributes(lp);

Categories

Resources