I have the following layout files:
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/frame_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0px"
android:layout_marginRight="0px"
android:layout_marginTop="0px"
android:padding="0dp"
>
...
</FrameLayout>
And some other fragments like
fragment_init.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragmentInit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_marginTop="0px"
android:layout_marginLeft="0px"
android:layout_marginRight="0px"
android:background="#549F07"
>
<TextView
...
>
...
</RelativeLayout>
Everything looks fine in Lint, but when I execute my application on my Nexus 7 5.0.2, every container is displayed with a padding or margin of maybe 10 px.
This is illustrated by the arrows on the following image
How to force the layouts to not add these padding/margin?
Edit: I should add how I insert my fragment.
Activiy
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.replace(R.id.frame_container, new Fragment_init(), "Fragment_init").commit();
}
}
and I don't use any dimens anywhere...
Thks
Go into this file res/values/dimens.xml and change the values to 0dp just like in the code below.
<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>
I know this is already answered question but, i just come across such an issue, the answer helped but not completely, as there was a right padding. I found out that, it is because of the padding set in the Theme i used. It might be helpful for those still see the padding. All you need to do is remove it.
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" >
Image as seen in ICS
Where as when run in Gingerbread
XML code for the fragment
<?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="#drawable/background"
android:id="#+id/homelayout" >
<ImageView
android:id="#+id/homepageLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"
android:alpha="255"
android:scaleType="fitXY" />
</LinearLayout>
Any reason for such strange behavior?
try changing
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
in your ImageView. I don't know how you use your XML, but by the looks of it you might also want to change te same parameters in the LinearLayout to "wrap_content". Now you are telling the ImageView to (probably) fill the whole area.
// Alex
The problem is observed only when you dynamically request for the actionbar from the code.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
If you do it using styles, this problem is not seen.
I have been reading your posts and they are very helpful. However, I really need your experienced advise in this and what would you do if you were me.
I am doing and application in which is has 4 tabs. The layout of the 4 tabs is similar (a table that has values in its cells and 3 buttons, and textview). The only thing that changes from one tab to another is the table values and textview. However, I need to share data between the tabs as the values on each tab are dependent on previous tab
How do you think I should approach? I have been reading that using views is generally recommended over activites. Can I use the same view layout for all the tabs?
Please any help on how you would design it will be great. I am on 2.1 and targetting pretty much all platforms .THANK U
PS: I tried (as an example) having textview under the framelayout, but the problem is that changing the text in Java code will make the textview changes in all the tabs. For some reason, I am feeling that having 4 text views (one for each tab) is kinda redundant and bad design but I dont know!
I would approach this by defining a layout, and using that same layout in each of your tabs. E.g.
public class MyTabActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.tab_layout);
}
You can go for TabHost and TabWidget to solve your problem. Below is a sample demo for with the implementation. Tab_Layout.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">
<TabHost android:id="#android:id/tabhost" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:paddingTop="4dip">
<TabWidget android:id="#android:id/tabs"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0" android:orientation="horizontal" />
<FrameLayout android:id="#+android:id/tabcontent"
android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
</LinearLayout>
I want to customize the title bar for an Android application. I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="80dp" <!-- Problem here -->
android:gravity="center_vertical"
>
<ImageView
android:id="#+id/header"
android:background="#drawable/windowtitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
I also have a the following code in the targeted activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.home);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
...
}
I don't understand why the height of the title bar doesn't change when changing the android:layout_height value.
Did I miss something?
Thanks!
According to this article, you should create a custom style for this. See if it works for you.
set the layout_height as wrap_content or fill_parent of the linear layout
and then try it
you can use :
getWindow().setLayout(50, 50);
of course this code use to set width and hiegth of window