problems with saving information to Data in Android Studio - java

I used containers to build layouts which worked well everywhere, and here on the statistics where there is a pie chart, everything is floated.
If it is not the reason that it increases over time, and if so how to fix it ?
Here is an XML code. Please help or give any useful information on how to fix it.
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:srcCompat="#drawable/statisricfon" />
<LinearLayout
android:id="#+id/layout_for_stat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical">
<TextView
android:id="#+id/numpoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_marginRight="370dp"
android:text="120" />
<TextView
android:id="#+id/numtry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:layout_marginRight="235dp"
android:text="5" />
<TextView
android:id="#+id/numanswear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:layout_marginRight="80dp"
android:text="18" />
<TextView
android:id="#+id/numtruavt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="190dp"
android:layout_marginRight="620dp"
android:text="23" />
<TextView
android:id="#+id/numtopscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:layout_marginRight="425dp"
android:text="15" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/piecha"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="70dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="50dp"
></com.github.mikephil.charting.charts.PieChart>
</LinearLayout>
</RelativeLayout>
Screenshot :

I can see a few red flags in the way you decided to implement this layout.
First a bit context, there are endless variations of Android devices out there, with different physical screen sizes, resolutions, and subsequently dpis.
Ideally, you would move away from RelativeLayout and use ConstraintLayout instead. And if the PieChart width and height can change over time so the view itself has to manage that in its onMeasure method regardless.
It would also make sense to get rid of #drawable/statisricfon and replace it with a few Android XML drawables and vector images. This will ensure that the design would scale well on different screen configurations including portrait, landscape, and multi-window modes.
You can use one drawable for the gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="integer"
android:centerColor="integer"
android:endColor="color"
android:startColor="color"
android:gradientRadius="integer"
android:type=["linear" | "radial" | "sweep"] />
</shape>
And for the question marks, you can use a repeated pattern instead:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/pattern"
android:tileMode="repeat" />
And lastly, custom views for the TextViews with that white box background and title.

Related

I'm facing a problem with CardView, and Material Design elevation, when view's background is changed

When I'm trying to change the background of CardView, or it's child, which covers whole area of CardView,
the elevation shadow disappears.
However, when I used app:contentPadding attribute, the elevation
shadows became visible again, but now I have this weird padding on CardView, which is of course, not great to see. I found out, that views of Material Design also have such a problem.
Here is a MaterialToolbar without background, and here is with background. Here's my XML code below.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardUseCompatPadding="true"
app:cardElevation="4dp"
>
<androidx.appcompat.widget.LinearLayoutCompat
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:background="#android:color/holo_green_dark"
>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/news_image"
android:layout_width="match_parent"
android:layout_height="232dp"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/txt_news_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/numans"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="12dp"
android:textColor="#color/colorToolBarDarkSeaBlue"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/txt_news_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:layout_margin="12dp"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
And Here is the code with MaterialToolbar
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
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:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_margin="8dp"
android:elevation="8dp"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
Create a drawable with a corner radius of the card's inner radius,set it as the background of the layout inside the cardview.you can then adjust the layout's margin or backgrounds corner radius to tweek the view of the shadow.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#2e8b57" />
<corners android:radius="5dp"/>
</shape>

ANDROID STUDIO- layout background (image) doesn't show

everyone, first of all, I'm with a problem in my android project. I set an image as a layout background with the appropriate width (5.5), and when I run the application it appears white background instead of the intended image. The image is in at the drawable folder, all normal.
The image has the same dimensions of the layout (5.5)
Code:
<?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:animateLayoutChanges="true"
android:background="#drawable/fundo"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginTop="200dp"
app:srcCompat="#drawable/logo_branco" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:fontFamily="#font/advent_pro_thin"
android:text="O automóvel ideal para si!"
android:textColor="#E76C00"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
If you want to load a whole image, I'd use the approach of having an ImageView at the root ViewGroup of the layout with both dimensions set to match_parent.
This will solve the issue of the image not loading with an added benefit of being able to define scale type (like centerCrop) which enables you to have a picture with any side ratio and be sure it'll fill the screen both vertically and horizontally.
Edit:
Proposed solution:
<?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:animateLayoutChanges="true"
tools:context=".MainActivity">
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/fundo" />
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginTop="200dp"
app:srcCompat="#drawable/logo_branco" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:fontFamily="#font/advent_pro_thin"
android:text="O automóvel ideal para si!"
android:textColor="#E76C00"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>

image tab to fix for all devices

I want to set three image for create image tab.I set three image in activity_main.xml file.But it ok for only screen 5.0 inches.If over this,image is very small.How can I do that?Please help me kindly.
this is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".SettingActivity"
android:baselineAligned="false">
<Button
android:id="#+id/imageButtonHomeSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon_home_config"
android:onClick="imageButtonHomeSelector"
/>
<Button
android:id="#+id/imageButtonLoginSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon_login_config"
android:onClick="imageButtonLoginSelector"
/>
<Button
android:id="#+id/imageButtonSettingSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/setting_button_hover"
android:onClick="imageButtonSettingSelector"
/>
</LinearLayout>
I can suggest two ways:
1) Use mipmap based images for all the 5 screen sizes (hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi). And rather than
<Button
android:id="#+id/imageButtonLoginSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon_login_config"
android:onClick="imageButtonLoginSelector"/>
try this:
<Button
android:id="#+id/imageButtonLoginSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/icon_login_config"
android:onClick="imageButtonLoginSelector"/>
which in-turn uses the respective image automatically based on screen size.
2) Use Vector Drawables as per this reference

Analog Clock does not appear in the application

I'm trying to set a analog clock for my app but both the analog clock and the digital clock just dont appear when i run the app on my device, but if i look at the graphic view of the layout its there!
Heres my code:
<?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:weightSum="100" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="30" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
//Some Text Views and Edit Texts here!
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="20" >
<Space
android:layout_width="match_parent"
android:layout_height="2dp" />
<Button
android:text="Send"
android:id="#+id/bSendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="50" >
<Space
android:layout_width="match_parent"
android:layout_height="2dp" />
<AnalogClock
android:id="#+id/acClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
My guess is that your layout is not scaling well to varied resolutions. Click the Preview All Screens option (in in the Graphical Layout section of Eclipse) to see if you can see the same cropping problem there. If so, you'll need to experiment with different layout weights, etc. to ensure that the layout works well across different device resolutions.
I guess problem can be about ScrollView. Try to change it and let me know result. Because I don't know for which reason its happening but sometimes with ScrollView view is getting hidden...

Android App Layout

I'm developing an android app for 2.2 ver.
My app must hav such structure:
[ Spinner here (fixed height) ]
[ ListView (not-fixed height) ]
[ ImageView (fixed-height) ]
I have to use only portrait orientation.
I use linearLayout for it. How can I calculate listView height to show spinner on the top of the screen, imageview on the bottom, and listview cover all free space, but not push others away from field of view.
It will be cool to make it dinamic for lots of screen resolutions.
Thank you
Use a relative layout to accomplish what you need.
<?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" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/icon" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_above="#+id/imageView1" />
</RelativeLayout>
By doing this listview will adjust its size so that it fits between the spinner and imageview on your screen
You need to use the android:layout_weight attribute on the variable-height item, so it fills the available space.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Here is another way to do this, using weights. This may be the easiest approach.
You can put in to the weight a percentage of the screen you would like it to take up.
<?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" >
<Spinner
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"></ListView>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
</LinearLayout>
EDIT:
Now that i think about it, this will "fix" the size of all the widgets. They will be "fixed" at percentages of the screen. This will scale well with other screen sizes, but you did say you wanted ListView to not be fixed.

Categories