View appears on Android Emulator but not on physical phone - java

I have an activity layout consisting of just a ListView. It is supposed to use a list format xml which consists of a custom shape with three TextViews inside. In code I populate the ListView with the desired text, and when I run the app on an AVD, everything works as planned. When I deploy to my Droid Razr, however, the entire screen is blank when I run that activity.
Edit: Added code below
This is my custom shape (drawable):
<stroke
android:width="1dp"
android:color="#ff9f9f9f" />
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp" />
<solid
android:color="#color/white" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
This is my list format:
<?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="?android:attr/listPreferredItemHeight"
android:background="#drawable/rounded_edges"
android:orientation="vertical"
android:padding="6dip"
android:theme="#android:style/Theme.Holo.Light" >
<TextView
android:id="#+id/details_list_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
And this is how I use it in code:
ListView listView = (ListView) findViewById(R.id.event_details_listView);
DetailAdapter adapter = new DetailAdapter(this, R.layout.listformat_event_details, details);
listView.setAdapter(adapter);
Where details is an ArrayList> of my text. DetailAdapter has a getView method which adds text to the textview and inflates the layout.

I believe the problem may be with the <corners /> tag, which can be a bit buggy; such as the bottomRightRadius and bottomLeftRadius properties are reversed.
You can try using <corners android:radius="7dp"/> instead.
android: shape corners do not work when setting individual corners
Android - Can't create simple rectangle shape... UnsupportedOperationException?

I was able to fix the problem by refreshing the Detail Adapter more frequently; apparently the lag in the emulator was enough time for the views to compute but the physical device was too fast.

Related

How do i change the background on the buttons XML-AndroidStudio?

I am developing a small app in Android Studio, but I can't change the button color. I've tried everyting, changing only the background, the backgroundtint, to make a new drawable object, but I can't seem to find any answer, this is my button XML layout:
<Button
android:id="#+id/calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="55dp"
android:layout_marginTop="45dp"
android:background="#drawable/round_green_shape"
android:text="CALCULA">
</Button>
And this is the XML for my background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding = "10dp">
<solid android:color="#color/buttonsGreen">
</solid>
<corners
android:radius="15dp">
</corners>
</shape>
But this is how my bottons look:
And on the app they look the same:
I would appreciate any help,
Thank you
Done, i just had to delete the
android:background = ""
and just set the:
android:backgroundTint = ""
to green...

How to define a round ImageView on Android?

All the view classes on Android are define in rectangle shape.When you touches it detects in the rectangle zone,when you set image it fills the rectangle.How to make a class which is round by default?
Features I am looking for:
Support all the ImageView functions
Square Image input with setImageBitmap or onDraw are converted to round.
Dectect circular touch zone instead of sqaures
Small in size and overhead.
3rd Party is fine but prefer just use interal library.
Create a shape inside your drawable package.
// res/drawable/circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="10dp"
android:color="#android:color/white" />
</shape>
Then create a layer list inside the drawable
// res/drawable/img.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/circle"/>
<item android:drawable="#drawable/ic_launcher"/>
</layer-list>
Then create the image view with layer list
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img"/>

How to change iconTint of a specific item in BottomNavigationView

I am trying to change the iconTint of the favorites menuItem in the bottomNavigationView.
To achieve so, I've tried the following:
Creating a selector with colors
Creating a selector with drawables
Programatically setting icon drawable
programatically setting iconItemTint of bottomNavigationView to null
Using colored icon drawables, instead of iconItemTint
Setting the icon of favorites to the drawable selector
Setting the background of favorites to the desired color
Setting iconTint of favorites to desired color
None of the above solutions worked for me. Then I figured that it might be because I'm using com.google.android.material.bottomnavigation.BottomNavigationView instead of android.support.design.widget.BottomNavigationView
My bottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
app:labelVisibilityMode="labeled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:itemTextColor="#232323"
app:menu="#menu/bottom_nav_menu" />
My bottom_nav_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
<item
android:title="Favorites"
android:id="#+id/btn_favorites"
android:enabled="true"
android:background="#color/red_heart"
android:icon="#drawable/ic_favorite_red"
app:showAsAction="ifRoom" />
...
Here is a screenshot of my app:
The selected color is blue for all menuItems. I want the favorites to have the color #ff4444 (pale red) when selected.
I wasted a lot of time searching the correct answer to "How to change color of one specific icon of Bottom Navigation View" and I resolved this by myself. Here is the example:
val view = viewBinding.bottomNavigationView.menuView as NavigationBarMenuView
val itemToChange = view.findViewById<NavigationBarItemView>(R.id."item_to_change_id")
itemToChange.setIconTintList(
ColorStateList.valueOf(
ContextCompat.getColor(requireContext,R.color."color you need")
)
)
I didn't try this with android.support.design.widget.BottomNavigationView, but with com.google.android.material.bottomnavigation.BottomNavigationView it works correctly :)
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp"
app:itemTextColor="#color/bootom_text_color"
app:itemIconTint="#color/bootom_text_color"
android:background="#color/bottom_color"
app:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_navigation" />
create colour.xml in values
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#FFFF00" />
<item android:color="#color/white" /> </selector>

Custom android controls with elements

I have a pretty basic idea and experience with creating custom android controls.
I know I can create a custom control that is basically a layout with a number of controls in it, add attributes and any kind of logic I would want.
But now I need something a bit different. I have an app in which all of my EditText elements have a gray horizontal line under them.
I know this can be achived with backgroundTint but this is only for api 21 and more.
So instead of adding this gray line below every EditText element I use, I would like to create a custom element that extends EditText but also has this gray line. Something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:src="#drawable/gray_divider"/>
</LinearLayout>
And in the code behind:
public class MyEditText extends LinearLayout {
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
Init();
}
private void Init() {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_edit_text, this);
InitViews(view);
}
So my problem is this:
In order for MyEditText to function like an EditText I have to create an attribute for every EditText attribute. I want to do this:
<MyEditText android:layout_width="match_parent"
android:layout_height="match_parent"
android:text_color="#android:color/white" />
Without adding anything in the code behind. Is this possible?
For a good practice in this case, Use a background with bottom line.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-10dp"
android:right="-10dp"
android:top="-10dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#color/transparent"
android:startColor="#color/transparent" />
<stroke
android:width="#dimen/border"
android:color="#color/border" />
<corners android:radius="#dimen/border" />
</shape>
</item>
Make a .xml resource and put it in Drawable folder.Eg border_bottom.xml
And for Edit text just give background like
android:background="#drawable/border_bottom"
Hope it helps.

FloatingActionButton black box

I'm using this https://github.com/makovkastar/FloatingActionButton library for FloatingActionButtons in my project.
On some smaller devices, for example the Acer Liquid Z4 (Android version 4.2.2, screen 480x800) or the Genymotion Galaxy S2 image (Android version 4.1.1, screen 480x800) the FABs have a black box around them:
I tried to narrow this down, removed any special stuff from my theme making it look like this:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
and also put the buttons in the most simple layout:
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="de.immowelt.android.immobiliensuche.ui.FabTestActivity">
<com.melnykov.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
but the box remains. On the other hand I tried to recreate the effect with a new project but couldn't do it.
The box disappears when using the mini size version or disabling the shadow.
Any ideas on what my problem is?
PS: I tried cleaning the project ;)
I was using that library but then decided to make my own, set this as the background to an imageButton or something. I used a textview because all I needed was a +. It looks pretty good in my app.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Drop Shadow Stack -->
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <padding android:right="1dp" android:bottom="1dp" />
            <solid android:color="#00262626" />
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <padding android:right="1dp" android:bottom="1dp" />
            <solid android:color="#0D262626" />
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <padding android:right="1dp" android:bottom="1dp" />
            <solid android:color="#26262626" />
        </shape>
    </item>
    <!-- Background -->
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <solid android:color="#448AFF"/>
        </shape>
    </item>
</layer-list>
The problem was that the FloatingActionButton class makes use of a drawable called shadow.9.png for the shadow. I also had a shadow.9.png in my drawables and the library class obviously picked up the wrong one (mine).
Renaming my drawable solved the problem.

Categories