I intend to create a splash screen in my Xamarin Android project.
I have the following layout:
<?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:layout_gravity="center"
android:gravity="center" android:background="#11aaff">
<ImageView
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/splash" />
</LinearLayout>
The following code:
[Activity(Label = "My Xamarin App", MainLauncher = true, NoHistory = true, Theme = "#android:style/Theme.Light.NoTitleBar.Fullscreen",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class SplashScreenActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.SplashScreen);
// Create your application here
//var intent = new Intent(this, typeof(MainActivity));
//StartActivity(intent);
//Finish();
}
protected override void OnStart()
{
base.OnStart();
// Create your application here
var intent = new Intent(this, typeof(MainActivity));
StartActivity(intent);
}
}
After starting the app, I get a white screen (note the theme) and my second activity (MainActivity) shows up after a few seconds.
If I remove the StartActivity and show the splash screen only, it shows the white screen for about 1-2 seconds, then the image and the blue bacground appear (as expected) - obviously the second activity is not started.
What should I do to make the layout appear immediately?
You could use a custom Theme, instead of a custom layout.
Just a note, for this solution to work you must add the following Nugget Packages to the project:
Xamarin.Android.Support.v4 and Xamarin.Android.Support.v7.AppCompat
As stated in the reference link bellow.
I had to do that a while back and used this link as reference:
Creating a Splash Screen
Basically, you create a .xml file in your drawable folder with something like the following:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/splash_background"/><!-- Your BG color here -->
</item>
<item>
<bitmap
android:src="#drawable/splash"<!-- your splash screen image here -->
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
Then you edit the styles.xml file (by default in Resources/values), and add:
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light">
<item name="android:windowBackground">#drawable/splash_screen</item><!-- here you should put the name of the file you just created in the drawable folder -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
Finally your Splash Screen should extend AppCompatActivity instead of Activity and the Theme should be your custom, like this:
[Activity(Label = "My Xamarin App", MainLauncher = true, NoHistory = true, Theme = "#style/MyTheme.Splash",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class SplashScreenActivity : AppCompatActivity
I Hope this helps.
Related
I have a ScrollView added to a FrameLayout. In a button click I remove the ScrollView from the FrameLayout and add an element derived from ViewGroup. If the button is clicked again the ViewGroup is removed and the ScrollView is added back. While adding the ScrollView back to the FrameLayout after removing the ViewGroup I get the following exception from the AddView method of the FrameLayout. Actually it is thrown from the AddView() method of ViewGroup class, since FrameLayout is derived from ViewGroup.
Unhandled Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.resolveLayoutParams()' on a null object reference
occurred
I have already tried to toggle the visibility of both Views instead of adding one and removing the other. But after toggling visibility, the application crashes every time when the FrameLayout's OnLayout is called such as when the Android keyboard appears or if the device orientation changes.
I am sorry that I am not able to provide the code. Because my code is very complex and I could not reproduce this problem in a simple sample. I just posted this question as a last resort.
I know it is not possible to get solution if code is not included. But any input related to this problem will be helpful.
Since you cannot provide the code here, I can only write part of the code to show your requirements:
in xaml,i add a ScrolView (with its children) and a LinearLayout(which as your ViewGroup) into a FrameLayout:
<?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">
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above = "#+id/click"
>
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="text in scrollview"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="text iFn LinearLayout"
/>
</LinearLayout>
</FrameLayout>
<Button
android:id = "#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
in activity show and hide the Scrolview and LinearLayout by click Button:
private int num = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Button button = FindViewById<Button>(Resource.Id.click);
FrameLayout frameLayout = FindViewById<FrameLayout>(Resource.Id.framelayout);
ScrollView scrollView = FindViewById<ScrollView>(Resource.Id.scroll);
LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout);
button.Click += delegate
{
if (num % 2 ==0)
{
scrollView.Visibility = Android.Views.ViewStates.Gone;
linearLayout.Visibility = Android.Views.ViewStates.Visible;
}
else
{
linearLayout.Visibility = Android.Views.ViewStates.Gone;
scrollView.Visibility = Android.Views.ViewStates.Visible;
}
num++;
};
}
I want to combine status bar and toolbar with gradient like this
I have tried to set status bar transparent and make the toolbar big and set the gradient .
But It also makes your UI goes under the Soft navigation keys.
And also soft keyboard is covering the EditTexts.
android:windowSoftInputMode="adjustPan" not working
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
Window window = Window;
window.AddFlags(WindowManagerFlags.LayoutNoLimits);
window.AddFlags(WindowManagerFlags.TranslucentNavigation);
}
Is there any way only the status bar make transparent rather than transparent both status bar and navigation button on the bottom.
You could alse do like this,add the code into the OnCreate method :
protected override void OnCreate(Bundle savedInstanceState)
{
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Kitkat)
{
Window w = Window;
w.AddFlags(WindowManagerFlags.TranslucentStatus);
w.SetSoftInputMode(SoftInput.StateHidden|SoftInput.AdjustResize);
}
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_other);
}
then create the gradient.xml in Resources/drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="135"
android:startColor="#f56f2c"
android:endColor="#fa9f46"/>
</shape>
at last in the layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation = "vertical"
android:layout_height="match_parent"
android:fitsSystemWindows= "true"
android:background= "#drawable/gradient"
>
<!--use Toolbar-->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="标题"
app:titleTextColor="#fff"
app:subtitle="副标题"
app:subtitleTextColor="#fff"/>
<!--if dont use Toolbar,RePresent Toolbar-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="44dp">
<ImageView
android:layout_gravity="center_vertical"
android:src="#drawable/ic_arrow_back_white_24dp"
android:layout_marginLeft="10dp"
android:layout_width="35dp"
android:layout_height="35dp" />
<TextView
android:layout_gravity="center_vertical"
android:textSize="25sp"
android:gravity="center_horizontal"
android:textColor="#fff"
android:text="Test Title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="#+id/scrollView1"
android:background = "#fff" //set the background color of your content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation ="vertical" >
<!--your content -->
...
</LinearLayout>
</ScrollView>
</LinearLayout>
Why don't you set the color of status bar same as the startColor of gradient and let the toolbar start from where it does.
Create a static method that sets the status bar colour something like below:
public static void SetStatusBarGradiant(Activity activity)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window window = activity.Window;
Drawable background = ResourcesCompat.GetDrawable(activity.Resources, Resource.Drawable.abc_ab_share_pack_mtrl_alpha, null);
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
window.SetStatusBarColor(Android.Graphics.Color.Transparent);
window.SetNavigationBarColor(Android.Graphics.Color.Transparent);
window.SetBackgroundDrawable(background);
}
}
Then add a gradient.xml in drawable something like below see to it you set the start color and end color as per your requirement:
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
Goodluck
Revert if you have queries
I'm working on Xamarin android. I want a spinner to show up on a button click or the spinner should be on top of a button, the other way I can say. Sounds crazy but that's the stuff I got in my head a couple of days back! Yeah I've seen Spinner. May be what I'm asking is not part of the plan, but I wanna implement that!
UPDATE 2: Let me explain with an example.
Consider the Web 2.0 scientific calculator. There is a button π. You long press the button and you will get a drop down. The constant that you select from the drop down will be displayed in the textbox. I want the same functionality with the spinner. Just the difference is I want it on the button click rather than a long press. Hope I'm clear now.
That's the part of code I tried with:
<!--main.axml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="#+id/myTitle">
<!--some stuff-->
<ImageButton android:onClick="onClick"
android:id="#+id/btnPi"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:padding="100dip"
android:src="#drawable/pi" />
<Spinner
android:onClick="onClick"
android:id="#+id/btnConst"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:prompt="#string/cons" />
<!--And few other stuffs-->
</LinearLayout>
<!--Strings.xml-->
<string name="cons">const</string>
<string-array name="ConsArray">
<item>Angstrom star</item>
<item>Faraday constant</item>
<item>Planck constant</item>
<item>Rydberg constant</item>
<item>Stefan-Boltzmann constant</item>
<item>electric constant</item>
<item>mag. constant</item>
<item>neutron mass</item>
</string-array>
It doesn't matter's to me whether I use a Button or a ImageButton ! I'm happy with either of it. Basically I just want a drop-down menu to popup, as soon as I click that button. Can anyone help me with this? Thanks in advance.
UPDATE: When I worked on York Shen's answer , I got this blank space occupied by the spinner:
I want a spinner to show up on a button click.
You could put a Button on Spinner by using a FrameLayout :
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/planet_prompt"
/>
<Button
android:id="#+id/bt_spinner"
android:text="Spinner Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
Then, every time your Button is click, you could use spinner.PerformClick() method to show your Spinner :
bt_spinner.Click += (sender, e) =>
{
spinner.PerformClick();
};
Effect.
Update :
I cant reproduce your problem, but here is my complete code from the document, you could use it and try again :
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
bt_spinner.Click += (sender, e) =>
{
spinner.PerformClick();
};
...
private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string toast = string.Format("The planet is {0}", spinner.GetItemAtPosition(e.Position));
Toast.MakeText(this, toast, ToastLength.Long).Show();
}
In your XAML, set the spinner to not be visible. In your onClick event, set change the visibility. You may have to do more in onClick if you want the things below the spinner in the linear layout to move out of the way.
The YouTube Player will play videos no problem either without the transparent panel on the form or it will play them in full screen, the transparent panel has some images in it nothing special. If I take out the transparent panel, the YouTube Player works as desired, embedded in app. If I add the transparent panel to the form, this is when it will not play but in full screen. The video starts and then stops instantly. I assume it has something to do with the transparent panel but I can not understand what is happening. Any help or thoughts would be great. My java file does not change except for the initPopup would not be there. Shortened Java file version below.
XML Layout File Below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mlayout"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spnrPSA" >
</com.google.android.youtube.player.YouTubePlayerView>
</RelativeLayout>
<com.TransparentPanel
android:id="#+id/popup_window"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="left"
android:orientation="vertical"
android:padding="1px" >
<com.TransparentPanel>
</RelativeLayout>
JAVA File Below:
public final class PSA extends YouTubeFailureRecoveryActivity{
private Animation animShow, animHide;
private YouTubePlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.psa);
initPopup();
}
private void initPopup() {
final TransparentPanel popup = (TransparentPanel) findViewById(R.id.popup_window);
// Start out with the popup initially hidden.
popup.setVisibility(View.GONE);
if (PubVars.ScreenOrientation==0){
animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);
animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);
}
if (PubVars.ScreenOrientation==1){
animShow = AnimationUtils.loadAnimation( this, R.anim.l_popup_show);
animHide = AnimationUtils.loadAnimation( this, R.anim.l_popup_hide);
}
//animShow = AnimationUtils.loadAnimation( this, R.anim.popup_show);
//animHide = AnimationUtils.loadAnimation( this, R.anim.popup_hide);
final ImageView showButton = (ImageView) findViewById(R.id.show_popup_button);
final ImageView hideButton = (ImageView) findViewById(R.id.hide_popup_button);
showButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.setVisibility(View.VISIBLE);
popup.startAnimation( animShow );
showButton.setEnabled(false);
hideButton.setEnabled(true);
}});
hideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
popup.startAnimation( animHide );
showButton.setEnabled(true);
hideButton.setEnabled(false);
popup.setVisibility(View.GONE);
}});
}
}
I must be missing something simple. Thanks in advance, I am fairly new to android.
Youtube Player View does not authorize overlays. Even if it's a transparent Layout(Relative, frame, ..).
The player stops the video and log into logcat some useful info. But this case is reported as "youtubeplayer is completely covered".
This api not permit anyone layer over this. "Layout is the problem"
I suggest you use the hierarchy View to see this on your layout.
I have many problems with it, because my layout stay over a little point over player.
This FragmentActivity creates a few fragments that are put in a slider (horizontal. Only one fragment is shown here):
public class MainActivity2 extends FragmentActivity{
private ViewPager mPager;
private FragmentStatePagerAdapter mPagerAdapter;
private Logger mLogger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_container);
mLogger = new Logger();
mPager = (ViewPager)findViewById(R.id.pager);
mPagerAdapter = new SliderAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
... //goes on with activity, the adapter is set correctly..
The Logger fragment creates its view inflating this xml:
<TextView
android:id="#+id/LoggerLoTitle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:ems="10"
android:layout_weight="2"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000033"
android:textSize="16sp"
android:text="#string/logger"
android:layout_marginTop="30sp" />
<TextView
android:id="#+id/numberOutput"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_weight="13"
android:layout_height="fill_parent"
android:gravity="top"
android:freezesText="true"
android:maxLines = "50"
android:layout_marginLeft="22dp"
android:scrollbars = "vertical"
android:layout_marginTop="45sp"/>
<Button
android:background="#80FFFFFF"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="fill_parent"
android:freezesText="true"
android:scrollbars = "vertical"
android:layout_marginTop="45sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/logger_ad"
android:onClick="whatsNew"/>
</LinearLayout>
For API 7, everything works (all fragments are loaded and the slider works) fine except that the TextView "numberOutput" does not scroll and can't be selected. For API 15, 16, 17 it wors fine. Any idea?
Solved it by getting that TextView (numberOutput) at runtime and setting a movement method:
rootView = (ViewGroup) inflater.inflate(R.layout.dicelogger_lo, container, false);
mLoggerBody = (TextView)rootView.findViewById(R.id.numberOutput);
mLoggerBody.setMovementMethod(new ScrollingMovementMethod());
I did not find in the documentation that higher api levels automatically set any movement method.. so the why would be another question