In my layout
<?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"
android:id="#+id/user_pswd_new_root" android:scrollbars="vertical"
android:soundEffectsEnabled="true">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/ScrollViewLogin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarStyle="outsideInset" android:scrollbars="vertical|horizontal" android:visibility="visible">
<RelativeLayout android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_height="fill_parent">
<ImageView android:background="#drawable/logo_login" android:layout_height="wrap_content" android:id="#+id/imageView1"
android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:padding="0dp" android:layout_margin="0dp"/>
...............
</RelativeLayout>
</ScrollView>
</RelativeLayout>
With the above code, I set in a Dialog and things are shown proeprly, but there is lot of unwanted space above the image which unnecessarily increases the height of the dialog. See the results :
Any idea why the top space is occupied. And how do I get rid of it. Where am I going wrong ?
It's the title of the Dialog, which is empty because you didn't specify a title (but the view is still there). You have to remove it, for example like this:
class MyDialog extends Dialog {
#Override
protected void onCreate(Bundle savedInstanceState) {
// make sure to call requestWindowFeature before setContentView
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog_layout);
// other initialization code
}
// ...
}
But that depends on whether you are using a simple Dialog or an AlertDialog. If this doesn't work for you, post your dialog-creation code (Java) and I'll update my answer to show how to remove the title in your case.
Related
I am trying to display a splash screen for 3 seconds and then go to the main screen, both of them being in the same class. However, my problem is that, when I try calling the new layout.activity, before the original layout.acitity for the class Im in, the program crash. Why?
Here is a little example of what I am talkin about:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);//new activity
displaySplash();
setContentView(R.layout.activity_visualizer);//original activity
The only way that I can get this to run is if I comment out the splash activity completely, but I need it! I wont work if I comment out the other layout activity...
Here are my two activities if it helps:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
tools:context="comp380.musicvisualizer.Visualizer" >
<ListView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
tools:context="comp380.musicvisualizer.Visualizer" >
<ImageView
android:id="#+id/splashscreen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/logo"
android:layout_gravity="center"/>
I'd be tempted to have both as fragments and switch between them. However if you don't want to do that you could just have both the views in the same layout and change the visibility after three seconds. Your view would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
tools:context="comp380.musicvisualizer.Visualizer" >
<ImageView
android:id="#+id/splashscreen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/logo"
android:layout_gravity="center"/>
<ListView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
</ListView>
And then in the Activity do something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout); //the layout above
displaySplash(); // I guess this waits three seconds
// get refs to the views
ImageView splashScreen = (ImageView) findViewById(R.id.splashscreen);
ListView songList = (ListView) findViewById(R.id.song_list);
// swap the visibility
splashScreen.setVisibility(View.GONE);
songList.setVisibility(View.VISIBLE);
}
You cannot call setContentView() twice for an Activity.
One possible solution would be to have the layout of the splash screen in the same place where your original activity is and manage its visibility.
Otherwise, you could have a splash screen activity with R.layout.activity_splash as layout and after 3 sec launch your main activity.
Why not use the same layout for both of them and put them into FrameLayout, after 3 seconds hide the splash layout and show the normal layout?
You can also create two fragments, one SplashFragment and the other NomralFragment and just swap them after 3 secs if you really want to do this in the same class.
I just figured out my problem and you guys will never guess it: my code was failing because the picture I was using for my splash screen was way too big. I randomly changed the pic and everything works now...HUGE SIGH
Thank you all for trying though!
This question already has answers here:
Software keyboard resizes background image on Android
(16 answers)
Closed 6 months ago.
I am developing an application in which the background image get shrink on keyboard pop-up. My .xml is as follows :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
/**
Other stuff
*/
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I searched on Google and found that, to add
android:windowSoftInputMode="stateVisible|adjustPan"
in my manifest file. But its of no use.
Edit :
My Layout before key board pop up looks like :
And after pop up like:
Please check the difference in background image. In image 1 image is in size and in image 2 background image shrink-ed. I need the footer and background get shift upward on keyboard popup.
What I am missing or doing wrong please suggest me.
Just use in your onCreate() this code:
protected void onCreate(Bundle savedInstanceState) {
...
getWindow().setBackgroundDrawableResource(R.drawable.your_image_resource);
...
}
and eliminate this line in your xml:
android:background="#drawable/background"
Read more at:
http://developer.android.com/reference/android/view/Window.html
I had faced the similar issue once, mine was solved by using the below properties in manifest use it where your activity is declared.
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
Hey can you please try adding this
android:isScrollContainer="false"
in your ScrollView. It has solved my problem once. Might help u too.
Also, add this to your activity in manifest.xml
android:windowSoftInputMode="adjustPan"
Hope it helps..!! :)
Ok if I got your question right, you want a layout with a background and a scrollview. And when the softkeyboard pops up, you want to resize the scrollview, but keep the background at full size right?
if that's what you want than I may have found a workaround for this issue:
What you can do is make 2 activities.
Activity 1:
public class StartActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent StartApp = new Intent(this, DialogActivity.class);
startActivity(StartApp); // Launch your official (dialog) Activity
setContentView(R.layout.start); // your layout with only the background
}
}
Activity2:
public class DialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // get rid of dimming
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //get rid of title bar (if you want)
setContentView(R.layout.dialog); //Dialog Layout with scrollview and stuff
Drawable d = new ColorDrawable(Color.BLACK); //make dialog transparent
d.setAlpha(0);
getWindow().setBackgroundDrawable(d);
}
}
start layout:
<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/test"> //your background
</LinearLayout>
dialog layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- All your Scrollview Items -->
</LinearLayout>
</ScrollView>
</RelativeLayout>
AndroidManifest.xml
start Activity:
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
dialog Activity
android:theme="#android:style/Theme.Dialog"
android:windowSoftInputMode="adjustResize"
android:excludeFromRecents="true"
Edit:
To finish Activities at once use the following somewhere inside your DialogActivity (example: override the backbutton):
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
}
and in onCreate() of StartActivity:
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
else {
Intent StartApp = new Intent(this, TestActivity.class);
startActivity(StartApp);
}
Screenshots!
Normal
Clicked the EditText box
After Scrolled down (ps: i put textbox inside scrollview thats why its gone ;) )
I hope this will help your out ;)
I had the same issue.
Add your outer layout background inside onCreate method as show below
getWindow().setBackgroundDrawableResource(R.drawable.login_bg);
and add your outer layout as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4d000000"
><other code></ScrollView></RelativeLayout>
Add android:layout_weight="1" for the outer layout and don't set background in xml file
I think this helps someone
You should move the android:background="#drawable/background" in an ImageView above ScrollView. When the keyboard pops up it effectivly makes the screen smaller, and having an image as background you have no control on how it is resized/cropped.
So, assuming you want to have the image full width but keep the ascpect ratio try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/background"
android:scaleType="centerCrop" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
/**
Other stuff
*/
</RelativeLayout>
</ScrollView>
</RelativeLayout>
You can try different android:scaleType values to achive your desired effect. If you want the image to be cropped from the top instead of the default middle crop, see here on how to achieve this, or try using this library
Instead of android:windowSoftInputMode="stateVisible|adjustPan", you should change to android:windowSoftInputMode="stateVisible|adjustResize". It worked for me.
Use ScrollView as Top parent.
It will be scrolled up and down automatically when you open and close keyboard.
Why don't you do like this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:focusable="true"
android:background="#drawable/background"
android:focusableInTouchMode="true">
<RelativeLayout
android:id="#+id/relative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
/**
Other stuff
*/
>
</RelativeLayout>
</ScrollView>
You should use the adjustResize option for the windowSoftInputMode setting on the Activity in your AndroidManifest.xml file.
i am using below view heirarchy and its working fine for me:
<?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" >
<ScrollView
android:id="#+id/rfqItemsParentScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
I created a menuView.xml layout to be in all of the layouts of my activity. This layout has one column on each border and a title bar like this:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
I insert this layout in the other layouts this way:
<!-- Show menu -->
<com.example.MenuView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But if one of the layouts has full screen view, part of this view gets covered by the MenuView, so...
How could I tell to this view to adapt its size to the blank space inside the MenuView to not get covered by it?
UPDATE -- full XML included
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/degradado">
<!-- Show menu -->
<com.example.MenuView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/Left_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
<RelativeLayout
android:id="#+id/Right_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
What happens here is that these 2 Relative layouts get covered by the MenuView (The darkest gre borders and the top black bar), and the ideal way would be that these 2 layouts get fitted to the blank space (the clearest gray).
I can solve this setting margin sizes to the Relative layouts to fit inside of it, but i know this is not the best way to do it, so I don't know if there is another way.
I think the best way to solve your issue is with inheritance.
If you define an Activity that can be used as a template for all your fleshed out Activitys to add their content to.
I don't know what you custom menu is 'made of' but as a simple example:
Create a basic Activity with code:
public class ActivityWithMenu extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_menu_layout);
}
}
and xml:
<RelativeLayout 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"
tools:context=".ActivityWithMenu" >
<TextView
android:layout_width="match_parent"
android:layout_height="20dip"
android:background="#ff000000"
android:textColor="#ffffffff"
android:text="Main Menu Title Bar"
android:id="#+id/mainmenutitle" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentLeft="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/lefthandmenu" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentRight="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/righthandmenu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/righthandmenu"
android:layout_toRightOf="#+id/lefthandmenu"
android:layout_below="#+id/mainmenutitle"
android:orientation="vertical"
android:id="#+id/activitycontent"
/>
</RelativeLayout>
Then create your xml for a specific Activity, in this case a simple 'Hello World' layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:text="Hello World!"
/>
</LinearLayout>
</ScrollView>
But now when you write the code for this Activity, extend 'ActivityWithMenu' instead of the Activity class direct and inflate this xml layout as follows:
public class Activity1 extends ActivityWithMenu
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent);
ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false);
ll.addView(sv);
}
}
I have added the code for making the Activity fullscreen here instead of in the parent ActivityWithMenu class assuming that you wouldn't want them all displayed that way but you could move it into the parent class if appropriate.
Hope this helps.
I want to make a activity like a "Dialog", and I know two ways so far:
Way 1) In Android ApiDemos, it is implemented by adding the attribute to the activity like
android:theme="#android:style/Theme.Dialog">
The result is: the new Activity appears on top of the existing activity, that is what I want.
Way 2) I try to invoke setTheme(android.R.style.Theme_Dialog) in the Activity.onCreate(Bundle) method, and the new activity also appears, but the background is all black. This is not what I want. Code is as below:
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Dialog);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.selecte_date);
Can anybody tell me how to implement the effect by writing Java code?
The easiest way is to add a second layout in you xml which is a overlay of existing one.
then you can set the overlay to visible in your activity. So then you got multiple views in 1 activity.
<?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"
android:background="#android:color/transparent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#android:color/transparent">
<ImageView
android:id="#+id/icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_alignParentTop="true"
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_black"
android:visibility="false" >
</LinearLayout>
</RelativeLayout>
I want to delete the title bar from my app, but when use the following code, the title bar isn't hidden, in fact it becomes transparent and the content is pushed downwards:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
So, I have a gap where the title bar used to be (between the notification bar and the app).
I have searched everywhere but haven't found something similar.
Edit:
I have tried both of the solutions given by seretum and harism (shown below) but I'm still unable to find the problem, this is a part of my xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<ImageView android:id="#+id/title"
android:src="#drawable/main"
android:layout_width="480dp"
android:layout_height="155dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"/>
Somehow, the ImageView isn't aligned to the Top of the relative layout...
Last Edit. SOLUTION:
So, I tinkered with the xml and found out that when you set a custom dimension to a widget and so use the Realtuve Layout; no matter if the Title bar is present or not, the Widget will always be under it. So, here is the solution (you need to put the object in a layout):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<LinearLayout android:id="#+id/title"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:maxHeight="155dp"
android:maxWidth="480dp"
android:layout_height="wrap_content">
<ImageView android:src="#drawable/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"/>
Have you tried requesting no title in different order;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Works for me.
In your app's AndroidManifest.xml, inside the "application" tag, put
android:theme="#android:style/Theme.NoTitleBar"
That will remove your bar from every activity.