Android - Activity that does not fill the parent screen - android

Any idea why this doesn't create an activity that looks like a popup instead of an activity that completely fills the screen?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dip"
android:layout_height="120dip"
android:layout_marginTop="100dip">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="120dip"
android:layout_width="300dip">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</RelativeLayout>
</LinearLayout>
I assumed that I only needed to set the layout height and layout width to something other than "fill_parent", but it still shows up as a black screen that completely fills the screen.
Ultimately, I simply want to create a popup, but I do not want to use an AlertDialog. Is this possible?

You must set your Activity's window to be floating. You can do this either by giving your activity the Dialog style defined by Android (android:style/Theme.Dialog), or define your own style, like this:
<style name="MyFloatingWindow">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
Then set the style on your activity in the application's Manifest.

On my phone but check this website here it shows how to use PopupWindow correctly.
Hope this helps or points you in the right direction.

Related

Change the default color of main activity when it starts, The starting and opening default color

When user opens any application by clicking on app icon, the app starts with mainactivity.java. and its xml layout is shown to user.
I am making a splash screen with simple background color and text. But what my problem is, when it starts it shows a default layout color for a very little fraction of time and then it draws my given color. I do not want default color to be there even for that small fraction of time.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.rushi.demon.MainActivity"
android:background="#color/SplashScreen" >
<RelativeLayout
android:id="#+id/SplashScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/SplashScreen">
<TextView
android:id="#+id/SplashText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="#string/splash_name"
android:textColor="#color/SplashText"
android:textSize="40sp"
android:textStyle="bold"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
The SplashScreen color is shown after a lag and starting default color is shown for little time, which i do not want. I want to open this activity with my set color only from starting.
Add this to your AppTheme (theme of MainActivity) ..
<item name="android:windowDisablePreview">true</item>
Edit
Go to app -> res -> values -> styles.xml open styles.xml there you do this to the theme you have..
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowDisablePreview">true</item> //add this line
</style>
The best way to achieve this is well described here, By this way Splash Activity will start instantly
Refer this: https://medium.com/#ssaurel/create-a-splash-screen-on-android-the-right-way-93d6fb444857

Theme.AppCompat.Dialog theme creates empty (dead) space

In my application, I am using an Activity with the theme "Theme.AppCompat.Dialog" to display it as a dialog. That works out well, however, the dialog fills the entire screen height, leaving a lot of space empty. To illustrate my issue, here is a picture of opening the dialog (on an unusually high resolution to demonstrate the issue better):
The higher the resolution, the greater this space.
Here is a code snippet:
<RelativeLayout
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="wrap_content"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools">
<!--This is the yellow box-->
<LinearLayout
android:id="#+id/dialog_button_bar"
android:layout_alignParentBottom="true"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
[Buttons...]
</LinearLayout>
<!--This is the red box-->
<ScrollView
android:layout_above="#id/dialog_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
[LinearLayout containing rows...]
</ScrollView>
If I remove the android:layout_alignParentBottom="true" and the android:layout_above="#id/dialog_button_bar" attributes, the whole layout jumps to the top and now the empty space is below my layout.
What am I doing wrong? :(
It seems like this is some kind of intended behavior. The standard Android app installation dialog seems to behave the same way (leaving a lot of blank space between the permission part and the buttons) so I guess I'll keep it this way...
Create new Style in styles.xml
<style name="MyCustomDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Now in AndroidManifest.xml, add android:theme="#style/MyCustomDialog" to your Dialog activity.

how to show an activity over another on half of the screen in the right position?

I am new in android.
I have 2 activities and I want to show the second activity over the first one, but the second activity has a layout:width : 250dp and layout height is fill-parent(fill the screen) and start from the right of the screen.
How can I do that?
I want something like this
screen
You can make a transparent theme for the second activity so it is shown as a floating window. Then for the second activity, restrict the width of the view layout inside a Viewgroup that defines the position.
Here is my style definition for the Transparent theme. Setting this, f.e. in the Manifest <activity> declaration, makes it possible to achieve a floating activity on top of the other:
<style name="Theme.Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
The layout is like this, for example, showing a CardView from the right (end):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="top|end">
</android.support.v7.widget.CardView>
</FrameLayout>
I wonder why you would fixate the width to 250, what would happen on bigger/smaller devices? Maybe better to define a margin or padding...?

Android - ListView doesn't scroll inside DialogFragment

I am starting developing for Android. I wanted t create my sort of modal alert (like UIAlertView in iOS). I considered using Activity which worked fine. It took some time for me to do it. But later, I found a better solution using DialogFragment. I changed my activity to a dialog fragment and modified all required parts with respect to a fragment. It works fine. Except that the ListView in my fragment doesn't scroll any more! What the problem could be?
Note: It was working already in the Activity solution. There is no scroll view.
Here is the XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/modal_list_outter_frame_margin_vertical"
android:layout_marginLeft="#dimen/modal_list_outter_frame_margin_horizontal"
android:layout_marginRight="#dimen/modal_list_outter_frame_margin_horizontal"
android:layout_marginTop="#dimen/modal_list_outter_frame_margin_vertical"
android:background="#drawable/modal_list_outter_frame"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/modal_list_padding_bottom" >
<TextView
android:id="#+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/modal_list_title_horizontal_margin"
android:layout_marginRight="#dimen/modal_list_title_horizontal_margin"
android:layout_marginTop="#dimen/modal_list_title_top_margin"
android:gravity="center"
android:maxLines="#integer/modal_list_title_number_of_lines"
android:shadowColor="#color/modal_list_text_shadow_color"
android:shadowDx="0"
android:shadowDy="#integer/modal_list_title_shadow_offset_y"
android:shadowRadius="#integer/modal_list_title_shadow_radius"
android:text="#string/modal_list_title_small"
android:textColor="#color/modal_list_text_color"
android:textSize="#dimen/modal_list_title_font_size"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/modal_list_inner_frame_margin_bottom"
android:layout_marginLeft="#dimen/modal_list_inner_frame_margin_horizontal"
android:layout_marginRight="#dimen/modal_list_inner_frame_margin_horizontal"
android:layout_marginTop="#dimen/modal_list_inner_frame_margin_top"
android:background="#drawable/modal_list_inner_frame"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/modal_list_padding_bottom" >
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/modal_list_list_view_margin_horizontal"
android:layout_marginRight="#dimen/modal_list_list_view_margin_horizontal"
android:layout_marginTop="#dimen/modal_list_list_view_margin_top"
android:divider="#null"
android:dividerHeight="0dp"
android:listSelector="#color/modal_list_selector_color_selected"
>
</ListView>
</LinearLayout>
</LinearLayout>
Update: I found something really strange! It's like the fragment is transparent! If I tap anything in the fragment, it seems like I am tapping the buttons below it! Here is the code I am using to show the fragment:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.addToBackStack("SingleSelectionCustomRowModalList");
modalList = ASModalList.newInstance(modalListStateForCustomRow) ;
modalList.show(ft, "SingleSelectionCustomRowModalList");
Update 2: It seems the problem is the DialogFragment is not modal. I am using this style:
int style = DialogFragment.STYLE_NO_TITLE | DialogFragment.STYLE_NO_FRAME;
setStyle(style, R.style.ASModaListDialogStyle);
The used theme is:
<style name="ASModaListDialogStyle" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/modal_list_background_view</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
I am using this theme to make the background of the dialog dimmed.
Old question without an answer that actually worked for me.
Hope it'll help someone.
The solution is to set the list height to 0dp, and add a weight field. And remove the inner LinearLayout. This way the outer LinearLayout can set the correct size to the list, and it will scroll.
<android.support.v7.widget.ListViewCompat
android:id="#+id/ev_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
Please provide some code, otherwise it will be difficult to say what is wrong. For example the Layout XML or your DialogFragment Class.
It ends up with the dialog being non modal. Fixed it using:
int style = DialogFragment.STYLE_NORMAL | DialogFragment.STYLE_NO_FRAME;
setStyle(style, R.style.ASModaListDialogStyle);

Are layout_alignParentRight or layout_alignParentLeft work for include layout?

I have a little problem.
I am creating a custom button widget
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/barBtn"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textColor="#color/white"
android:background="#drawable/sel_btn_bar"
android:layout_centerVertical="true"
/>
Then I place it into the RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/bar"/>
<include layout="#layout/segm_btn_stores"/>
<include
layout="#layout/btn_bar"
android:layout_alignParentRight="true"/>
</RelativeLayout>
But align doesn't work until I place include tag into RelativeLayout which I can move as I want. But this method is creating another problems: button will be narrower than if it was outside RelativeLayout. What can I do? I want to setup button params in one place.
And yes, I can add this line: android:layout_alignParentRight="true" to Button tag and it will work!
So the question is: why it works for Button tag and doesn't work for include tag?
Update
Button and fragments layout are there.
I think the android:layout_centerVertical attribute (for your Button layout) is only valid in a RelativeLayout (at least it's documented in the RelativeLayout.LayoutParams document: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html), hence I wouldn't expect it to work in, say, a LinearLayout.
As of the case when the button gets narrower within the RelativeLayout than outside it, I don't really know what to say. Given the XML snippet you provided it seems like your RelativeLayout is the document root, i.e. moving the <include ... /> tag outside it would generate illegal XML, hence compile errors (if using Eclipse).
I would actually prefer to define button themes in custom styles, which you then could set up in a application global style xml, like this, for example:
<resources>
<style name="my_custom_style" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">#style/my_button_style</item>
</style>
<style name="my_button_style" parent="#android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:background">#drawable/my_button_background</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textColor">#color/my_custom_red_color</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">11sp</item>
</style>
</resources>
and then you'd set your custom style in the AndroidManifest.xml, like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/application_title"
android:theme="#style/my_custom_style">
<!-- Your Activities, Services etc goes here -->
</application>
I'm confused about what your problem is. You are trying to take your view out of the relativelayout? If your making a layout, your views are going to have to belong to a ViewGroup. Align doesn't work because your view is outside of the ViewGroup which defines that attribute "RelativeLayout"

Categories

Resources