Center image on splash screen while taking into account system bars - android

On my Android app (react-native but it doesn't matter) I have a splash screen. I created res/drawable and added a image called "emblem.png". I then center that with gravity of center as seen in my xml here:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item name="android:windowBackground">#drawable/background</item>
<item>
<bitmap android:gravity="center" android:src="#drawable/emblem"/>
</item>
</layer-list>
However this centering does not take into consideration the "top status bar" height nor the "width of 'system nav bar' when landscape"/"height of 'system nav bar' when in portrait". Is there a way to do this?
Thanks

THIS IS NOT AN ANSWER, I'M JUST SHARING SOME CODELayout file - activity_splash.xml
<?xml version="1.0+ encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
xmlns:tools="http://schemas.android.com/tools"
android:id=#+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context="com.splash.splash.Splash">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/iV"
android:gravity="center"
android:src="#drawable/applogo"/>
<TextView
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:text="My welcome screen"
android:textsize="25sp"
android:textStyle="bold"
android:textcolor="#color/white"
android:layout_marginTop="10dp"
android:id="#+id/tV" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class Splash extends AppCompatActivity{
private TextView tv;
private ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState){
super.OnCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
tv= findViewById(R.id.tV);
iv= findViewById(R.id.iV);
final intent i = new intent(this,MainActivity.class);
Thread timer = new thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
startActivity(i);
finish();
}
}
};
timer.start();
}
}
This is how I set a splash screen in android. Hope this helps you.

Related

App is not opening in Android 5.1 and lower version devices

I created an app using android studio. Uploaded to play store. Its working fine in all devices except android version 5.1 and lower version devices. The app is not even opening in those devices.
I have tried my best to solve this problem but I failed.
.
So, please tell me guys how to solve it??
.
In stack trace it is showing :
.
Caused by : android.view.inflateException
.
At packageName.splash.oncreate (Splash.java:23)
.
Here Splash.java is my splash screen activity...
Here is my Splash.java code :
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setupWindowAnimations();
TextView textView = findViewById(R.id.textviewmain);
TextView textView1 = findViewById(R.id.textviewmain2);
ImageView logo = findViewById(R.id.logo_s);
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
logo.startAnimation(animation1);
textView.startAnimation(animation1);
textView1.startAnimation(animation1);
Thread timer = new Thread(){
#Override
public void run(){
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
super.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
timer.start();
}
private void setupWindowAnimations() {
Fade fade = new Fade();
fade.setDuration(3000);
getWindow().setExitTransition(fade);
}
}
and activity_splash.xml code is :
<?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"
android:background="#092865"
tools:context=".Splash"
android:theme="#style/AppTheme.NoActionBar">
<ImageView
android:id="#+id/splashbg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-315dp"
android:src="#drawable/splashbg2"
/>
<RelativeLayout
android:id="#+id/slidesplash"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:background="#null"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/logo_s"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:contentDescription="#string/daily_highlights_dose"
android:src="#drawable/icon24"
android:transitionName="logo" />
<TextView
android:id="#+id/textviewmain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_centerVertical="true"
android:layout_toEndOf="#id/logo_s"
android:text="#string/sports"
android:textColor="#ffffff"
android:textSize="25sp" />
<TextView
android:id="#+id/textviewmain2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/textviewmain"
android:text="Highlights"
android:textColor="#1FD883"
android:textSize="27sp" />
</RelativeLayout>
<!-- #f93959 -->
</RelativeLayout>
I think this problem is ocuuring just because of size of images,
You should check your image size which are put in splash Activity
or replace those images by others and then check it

Android -TranslateAnimation not working

I am trying to start a shining affect animation on an Image view.
the problem is that the animation does not show on screen.
It is work only when I start it inside a button listener.
this is my shine_effect.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:centerColor="#beffffff"
android:endColor="#00ffffff"
android:startColor="#00ffffff" />
</shape>
this is my layout xml file:
<?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"
android:background="#drawable/gradient"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/img"
android:layout_width="170dp"
android:layout_height="170dp"
android:contentDescription="#string/desc"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/shine"
android:layout_width="50dp"
android:layout_height="170dp"
android:layout_marginStart="-50dp"
android:contentDescription="#string/desc"
android:src="#drawable/shine_effect" />
</RelativeLayout>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="#string/pending"
android:textColor="#android:color/white" />
</RelativeLayout>
and my activity onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_pending);
img=(ImageView)findViewById(R.id.img) ;
shine=(ImageView)findViewById(R.id.shine) ;
shine_anim=new TranslateAnimation(0, img.getWidth()+ shine.getWidth(),0,
0);
shine_anim.setDuration(550);
shine_anim.setFillAfter(true);
shine_anim.setRepeatMode(Animation.RESTART);
shine_anim.setInterpolator(new AccelerateDecelerateInterpolator());
shine_anim.setRepeatMode(Animation.INFINITE);
shine.startAnimation(shine_anim);
}
It is because the ImageView has not layouted yet so the width is 0. You can give it a layout cycle and start the animation then.
img.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
img.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Create and start animation
}
});
Alternatively if your image width is always static, you can convert e.g. that 170dp to px and use that value when creating the TranslateAnimation instead of using getWidth() methods of views.
Call your animation in onWindowFocusChanged method
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
shine_anim = new TranslateAnimation(0, img.getWidth() + shine.getWidth(), 0,
0);
shine_anim.setDuration(550);
shine_anim.setFillAfter(true);
shine_anim.setRepeatMode(Animation.RESTART);
shine_anim.setInterpolator(new AccelerateDecelerateInterpolator());
shine_anim.setRepeatMode(Animation.INFINITE);
shine.startAnimation(shine_anim);
}
}

How to create Diamond Progress Bar Android

My Client wants a diamond shaped progress that looks like this:
My first attempt was to use a library, but I can't find one that exists
My next attempt was to learn how to use the ProgressBar view that comes with android, and set my own drawable using this answer (the closest thing to an answer on stack overflow), but the answer only works on ring shapes, not rectangles.
What is the best way to create a diamond-shaped progress view? (By any means: custom view, progressDrawable) and how do I do that?
After some more tests, I came up with a hacky answer. It's just 4 progress bars aligned to the edge of a Relative layout, and a CardView on top of them. Rotate the whole thing, and wrap it in a class and bam, you got yourself a diamond progress bar. (Use math to calculate the progress of each bar)
It can be a little weird on the corners (where the progress bars overlap) but overall it works well enough
Usage:
ViewGroup background;
int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Something to add the progress bar to
background = (ViewGroup) findViewById(R.id.relative);
//initializing the progress bar
final DiamondProgress diamondProgress = new DiamondProgress(this);
diamondProgress.setMax(1000);
//adding the progress bar
background.addView(diamondProgress.getView());
/* Sample Code for animating the progress bar*/
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
diamondProgress.setProgress(count++);
}
});
}
}, 0, 25);
}
Code:
XML: layout/diamond_view
<?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"
android:orientation="vertical"
android:layout_width="wrap_content"
android:rotation="45"
android:padding="43dp"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp">
<RelativeLayout
android:layout_width="wrap_content"
android:rotation="90"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_alignParentBottom="true"
android:rotation="180">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:id="#+id/dp_progress4"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/progress_drawable"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_alignParentBottom="true"
android:rotation="180">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:progress="50"
android:id="#+id/dp_progress3"
android:progressDrawable="#drawable/progress_drawable"
style="?android:attr/progressBarStyleHorizontal"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:rotation="90"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:progress="100"
android:id="#+id/dp_progress2"
android:progressDrawable="#drawable/progress_drawable"
style="?android:attr/progressBarStyleHorizontal" />
</RelativeLayout>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:progress="100"
android:progressDrawable="#drawable/progress_drawable"
style="?android:attr/progressBarStyleHorizontal"
android:id="#+id/dp_progress1"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_margin="4dp"
android:id="#+id/dp_card"
android:elevation="10dp"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:rotation="-45"
android:id="#+id/dp_addView"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Sample Inside Content"
android:id="#+id/dp_text"
android:gravity="center"
android:textSize="24sp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</RelativeLayout>
XML: drawable/progress_drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- background -->
<item android:id="#android:id/background">
<shape>
<corners android:radius="3dp"/>
<solid android:color="#f2f2f2" />
</shape>
</item>
<!-- for the actual progress bar -->
<item android:id="#android:id/progress">
<clip android:gravity="left">
<shape>
<corners android:radius="3dp"/>
<solid android:color="#color/colorAccent" />
</shape>
</clip>
</item>
</layer-list>
Java Class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
/**
* Created by Pythogen on 9/27/2017.
*/
public class DiamondProgress {
Context context;
View view;
RelativeLayout addView;
int progress = 0;
int max = 100;
ProgressBar p1;
ProgressBar p2;
ProgressBar p3;
ProgressBar p4;
public DiamondProgress(Context context) {
this.context = context;
view = LayoutInflater.from(context).inflate(R.layout.diamond_view, null);
addView = ((RelativeLayout) view.findViewById(R.id.dp_addView));
p1 = (ProgressBar) view.findViewById(R.id.dp_progress1);
p2 = (ProgressBar) view.findViewById(R.id.dp_progress2);
p3 = (ProgressBar) view.findViewById(R.id.dp_progress3);
p4 = (ProgressBar) view.findViewById(R.id.dp_progress4);
}
public View getView() {
return view;
}
public RelativeLayout getHostOfInsideContent() {
return addView;
}
public void setProgress(int progress) {
this.progress = progress;
updateProgressBar();
}
public void setMax(int max) {
this.max = max;
p1.setMax(max);
p2.setMax(max);
p3.setMax(max);
p4.setMax(max);
}
public void updateProgressBar() {
double prog = ((double)progress)/max;
if (prog<.25) {
p1.setProgress(this.progress*4);
p2.setProgress(0);
p3.setProgress(0);
p4.setProgress(0);
} else {
p1.setProgress(max);
if (prog<.5) {
p2.setProgress((this.progress*4)-max);
p3.setProgress(0);
p4.setProgress(0);
} else {
p2.setProgress(max);
if (prog<.75) {
p3.setProgress((this.progress*4)-max*2);
p4.setProgress(0);
} else {
p3.setProgress(max);
p4.setProgress((this.progress*4)-max*3);
}
}
}
}
}
Oh, and if you plan on using this, be sure to add the CardView dependancy to your build.grade compile 'com.android.support:cardview-v7:25.1.1'

Set size of child items of arc menu

I downloaded arc menu library from https://github.com/daCapricorn/ArcMenu.
Here is arc_menu.xml:
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.arcmenu.lib.ArcLayout
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:childSize="44px"
custom:fromDegrees="270.0"
custom:toDegrees="360.0" >
</com.example.arcmenu.lib.ArcLayout>
<FrameLayout
android:id="#+id/control_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/button_option" >
<ImageView
android:id="#+id/control_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
here is FragmentActivity :
public class FregmentActivity extends FragmentActivity {
private static final int[] ITEM_DRAWABLES = {
R.drawable.button_contact_us_option,
R.drawable.button_back_bookmark, R.drawable.button_news_option,
R.drawable.button_product_option,
R.drawable.button_featured_option,
R.drawable.button_about_us_option };
ArcMenu arcMenu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arcMenu = (ArcMenu) findViewById(R.id.arc_menu);
initArcMenu(arcMenu, ITEM_DRAWABLES);
}
private void initArcMenu(ArcMenu menu, int[] itemDrawables) {
final int itemCount = itemDrawables.length;
for (int i = 0; i < itemCount; i++) {
ImageView item = new ImageView(this);
item.setImageResource(itemDrawables[i]);
final int position = i;
menu.addItem(item, new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("TAG", "IN ARC MENU");
}
});
}
}
}
here is main.xml :
<?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/com.example.vjmceramics"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<com.example.arcmenu.lib.ArcMenu
android:id="#+id/arc_menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
app:fromDegrees="180.0"
app:toDegrees="270.0" />
Whenever I run my project to nexus4 it shows child icons very small.
and shows different size of icon on different device.
Is there any way to set child size as per device runs on???
please help me. Thank you
1.remove the icons from the drawable folder(ex. drawadle-ldpi,drawable-hdpi,drawable-xhdpi etc. )
2.keep only one copy of the icon (that u want to show) in the drawable-nodpi folder.
note- if the folder is not present in the res directory then create it

Tap preview thumbnail to view high resolution image

I am a new android developer and I am trying to create an android app for android in which menu items will be displayed along with description cost and image.
I have implemented List activity to display the items in a list. I made an image Button which will show the image in a thumbnail. I want to be able to view a high resolution image of the thumbnail when I tap on the image button. I have implemented an onClickListener to listen to the tap event but I do not know the methods that will make the image zoom out on tap.
I have followed this link
but it is quite ambiguous to understand. The Animator class doesn't exist. Can someone guide me out o fthis?
I found the solution after searching for almost a week.
I used PopupWindow Class for the purpose
Following is the Java Code.
PopupWin.java
public class PopupWin extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button bPopup = (Button)findViewById(R.id.openpopup);
bPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
Button bDismiss = (Button)popupView.findViewById(R.id.dismiss);
bDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(bPopup, 50, -30);
}});
}
}
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dim_back" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dim_back"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:src="#drawable/appetimg01" />
<Button
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20sp"
android:text="Go Back"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/appetizerDes1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="0.40"
android:text="Tap the Image to view High Resolution Image."/>
<Button
android:id="#+id/openpopup"
android:layout_width="80sp"
android:layout_height="80sp"
android:layout_gravity="right"
android:layout_margin="40sp"
android:background="#drawable/custombutton"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
The background was appearing white but I wanted it transparent for which I created back_dim.xml in drawables and set it along with android:background="#drawable/back_dim".

Categories

Resources