Android: Start new activity smaller than the screensize - android

I have a default activity with just a button
<Button
android:id="#+id/openActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:text="Click to open activity" />
and a function that opens a new activity:
public void openActivity(){
final Button openActivity = (Button) findViewById(R.id.openActivity);
openActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.this.startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
For the second activity I have just a TextView with a custom message:
<?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"
android:background="#a56699"
tools:context="com.example.tge_00.myapplication.SecondActivity">
<TextView
android:id="#+id/textViewTextToChange"
android:layout_width="121dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="120dp"
android:text="Second Activity" />
</android.support.constraint.ConstraintLayout>
that is supposed to be displayed on top of the main activity, smaller than the screens size:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.widthPixels;
getWindow().setLayout((int)(width*.8), (int)(height*0.6));
}
public boolean onTouchEvent(MotionEvent event){
this.finish();
return true;
}
But the problem is that the activity from behind should still be visible in the portions where the second activity doesn't cover the screen but those portions are black and I don't know why.
I'm new in Android programming so please be gentle :)

just use this theme in your activity.
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.Dialog">
<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">false</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="colorAccent">#color/White</item>
</style>

Related

Insert loading ring in the splash screen

I followed Microsoft tutorial on how to make a splash screen in xamarin.android, and it works properly. Now I would like to insert a progress ring under the bitmap. I've tried different approaches but i can't seem to find the right way to do it
This is the splash screen code:
splash_screen.xml
<?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"/>
</item>
<item android:left="20dp">
<bitmap
android:src="#mipmap/coronamap_bianco"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
splash style in resources > values > styles.xml
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionBar">true</item>
<item name="android:statusBarColor">#304057</item>
</style>
SplashActivity.cs
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
Finish();
}
}
EDIT
Since the constraint layer didn't work, I tried to simplify it using a linear layout. As output I get a white screen.
SplashActivity.cs
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
SetContentView(Resource.Layout.SplashLayout);
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
Finish();
}
}
SplashLayout.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_splash_screeen"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/coronamap_bianco" />
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
</LinearLayout>
Output: splash screen
You can create a layout for the splash screen , in that layout create an image and a loading circle
splash_screen.xml activity code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ImageView
android:id="#+id/iv_splash_screeen"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45"
app:srcCompat="#drawable/my_logo" />
<ProgressBar
android:id="#+id/progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_splash_screeen" />
</androidx.constraintlayout.widget.ConstraintLayout>
In your activity, OnCreate set that layout
SplashActivity.cs
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.splash_screen);
}
.....
...
}
Output:
Based on the official sample , we can create a .xaml layout for SplashActivity. Then can achieve that .
Splashlayout.xaml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_gravity="center"
android:background="#drawable/splash_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="#+id/progressBar1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Used in SplashActivity :
//public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
//{
// base.OnCreate(savedInstanceState, persistentState);
// Log.Debug(TAG, "SplashActivity.OnCreate");
//}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Splashlayout);
}
Here not forgettingn to modify in style.xml :
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<!--<item name="android:windowBackground">#drawable/splash_screen</item>-->
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionBar">true</item>
</style>
Then effect as follow :
Here is the modified sample link for reference .

App name showing on top of Alert Activity popup

Im showing a popup alert activity, when select a notification from tray.
But on top it's showing the App name (MCP), with improper alignment.
I don't want the top part, just a normal dialog.
Manifest:
<activity
android:name=".activity.AlertDialogActivity"
android:configChanges="orientation|locale|screenSize|uiMode|fontScale"
android:excludeFromRecents="true"
android:theme="#style/AlertDialogTheme" />
Style:
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Alert activity:
public class AlertDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
this.setFinishOnTouchOutside(false);
String message = getIntent().getExtras().getString("message");
TextView tvPushMessage = (TextView) findViewById(R.id.tvAlertMessage);
tvPushMessage.setText(message);
Button btnPushOk = (Button) findViewById(R.id.btnAlertOk);
btnPushOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#drawable/background_round_rectangle"
android:padding="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/tvAlertMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />;
<Button
android:id="#+id/btnAlertOk"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="#id/tvAlertMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/btn_use"
android:text="#string/ok"
android:textColor="#color/white" />
</RelativeLayout>`
Also tried:
requestWindowFeature(Window.FEATURE_NO_TITLE);
and:
getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert)
before setContentView()
Just Add Following code in style.xml
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
or You can hide it Programmatically.
getSupportActionBar().hide();
Try this code (without using "android:")
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
I used this:
In AppCompatActivity onCreate() add following code:
setTitle("")
and Add The following code in style.xml
<style name="PopupWindowTheme" parent="#style/Theme.AppCompat.Dialog.Alert">
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#style/AnimBottom</item>
</style>
Add requestWindowFeature in this way may be work proper.
First Call Super after add it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.add__favourite_layout);
}

change color of only ONE menu item icon in android

i want to change only one menu item in the action bar. I tried to change the same icon color instead of making it transparent into the desired color but the edges show the background of the action bar.
Here is my action bar what it looks like:
https://www.dropbox.com/s/1sjnk975dnj17kr/before.png?dl=0
And here is the goal i want to reach:
https://www.dropbox.com/s/ul7hh2gxtfox1pk/goal.png?dl=0
Here are my xml files:
menu_main.xml
<item
android:id= "#+id/location"
android:icon="#drawable/ic_room_white_48dp"
android:title=""
android:orderInCategory="2"
app:showAsAction="always"/>
<item
android:id= "#+id/report"
android:icon="#drawable/report"
android:title=""
android:orderInCategory="3"
app:showAsAction="always"/>
<item
android:id= "#+id/message"
android:icon="#drawable/ic_comment_white_48dp"
android:title=""
android:orderInCategory="4"
app:showAsAction="always"/>
styles.xml
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#B9ACC1</item>
<item name="colorPrimaryDark">#827689</item>
<item name="colorAccent">#827689</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="#android:style/Widget.Holo.Light.ActionButton">
<item name="android:padding">15dip</item>
</style>
UPDATE1 :
Adding a way to avoid xml and code duplication.
UPDATE2 :
Adding static listener.
I don't know if you can achieve your goal just by changing your menu.xml but the Toolbar is a viewGroup so you can achieve your goal with a view.xml which should looks like it :
toolbar.xml :
<LinearLayout
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:background="#000000">
<Button
android:id="#+id/button_first"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:background="#android:drawable/ic_lock_idle_lock" />
</LinearLayout>
<LinearLayout
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:background="#000000">
<Button
android:id="#+id/button_second"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:background="#android:drawable/ic_lock_idle_lock" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
It can be boring to add this code on all of your xml so i advice you to to use thé <include /> attribute like this :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
</RelativeLayout>
Then, you just have to set the Toolbar and use the Button
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonFirst = (Button) findViewById(R.id.button_first);
final Button buttonSecond = (Button) findViewById(R.id.button_second);
buttonFirst.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
buttonSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
If all of your button have the same listener, you can make them static into avoid the code duplication :
IconListener.java :
public class IconListener {
public static View.OnClickListener listenerButtonOne = new View.OnClickListener() {
#Override
public void onClick(View v) {
//DO what you want
}
};
public static View.OnClickListener listenerButtonTwo = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do what you want
}
};
}
So your MainActivity.java became
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonFirst = (Button) findViewById(R.id.button_first);
final Button buttonSecond = (Button) findViewById(R.id.button_second);
buttonFirst.setOnClickListener(IconListener.listenerButtonOne)
buttonSecond.setOnClickListener(IconListener.listenerButtonTwo);
}

Custom alert dialog android

I am working with custom alert dialog in android.
I have followed the link 1 and linke 2.
In my code using these styles.
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up_dialog</item>
<item name="android:windowExitAnimation">#anim/slide_out_down</item>
<!-- Animation for dialog box -->
<style name="DialogSlideAnim" parent="AppBaseTheme">
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
</style>
From these styles I am getting bordered dialog. But I need like the below.
I am using LG Nexus 4 device. What should I have to do to achieve this?
I have some solution. Please look at the example below.
In style xml use this:
<resources>
....
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/abc_slide_in_bottom</item>
<item name="android:windowExitAnimation">#anim/abc_slide_out_bottom</item>
</style>
<style name="DialogSlideAnim">
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
And dialog layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/male"
android:layout_marginBottom="-10dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/female"
android:layout_below="#id/male"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Cancel"
android:layout_below="#id/female"
android:id="#+id/cancel"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
And dialog java file:
public class YourDialog extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.DialogSlideAnim);
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.your_dialog, container, false);
return view;
}
#Override
public void onResume() {
super.onResume();
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
final Window window = getDialog().getWindow();
window.setGravity(Gravity.BOTTOM);
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
}
}
Result:

Android: dialog box without shadow around it

How do I remove this "shadowBox" effect around the dialog box that fades the background?
I know I can create like totally custom view but let's say I want to keep it simple. I have xml layout for dialog with only textView and 2 buttons and I want to stop the fade only for this one dialog.
Try:
dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
create style.xml inside values and put things like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="progress_dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#00ffffff</item>
</style>
And in the end, put this style to your dialog box.
Simply use
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
here give the sample class
public void showDialog () {
Dialog dialog = new Dialog(this);
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.requestFeature(window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sample);
ImageView imggameover, imgplayagain;
imggameover = (ImageView) dialog.findViewById(R.id.gameover);
imgplayagain = (ImageView) dialog.findViewById(R.id.playagain);
imgplayagain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), random.class));
onStop();
finish();
}
});
dialog.show();
}
Dialog's layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gameoverlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<ImageView
android:id="#+id/gameover"
android:layout_width="225dip"
android:layout_height="160dip"
android:background="#drawable/gameover"
></ImageView>
<ImageView
android:id="#+id/playagain"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginTop="100dip"
android:background="#drawable/playagain"
></ImageView>
</RelativeLayout>
Just that you know it is also possible to change the dim amount by writing this in your theme:
<item name="android:backgroundDimAmount">0.5</item>

Categories

Resources