1

I'm a student who learns this year now Android studio with java. I have learned last week about the progress bar, seek bar, and radio buttons. My homework is to use radio buttons to change between photos and using a progress bar, textView (for the percentage), and seek bar to change the opacity of the chosen photo.

I tried as I could, and actually, when I look at "design", I can see my progress bar, Seek bar, and Text View, and all is good, but when I start the app (on SDK 19), the radio buttons don't change the chosen photo, and I even can't see the seek bar, progress bar, and text view.

Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@drawable/appbackground">

    <ImageView
        android:layout_width="318dp"
        android:layout_height="353dp"
        android:src="@drawable/avatar"
        android:layout_gravity="center"
        android:layout_marginBottom="200sp"/>

    <ImageView
        android:id="@+id/element"
        android:layout_width="137dp"
        android:layout_height="128dp"
        android:layout_marginStart="135sp"
        android:layout_marginTop="195sp"
        android:src="@drawable/abc_vector_test"
        android:elevation="2sp"/>


    <RadioGroup
        android:id="@+id/radioG"
        android:layout_width="wrap_content"
        android:layout_height="133dp"
        android:layout_marginTop="400dp"
        android:background="@drawable/background"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/w"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Water"
            android:textColor="#03A9F4" />


        <RadioButton
            android:id="@+id/e"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Earth"
            android:textColor="#652F00" />


        <RadioButton
            android:id="@+id/f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Fire"
            android:textColor="#FF5722" />


        <RadioButton
            android:id="@+id/a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Air"
            android:textColor="#9C9C9C" />

    </RadioGroup>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:text="50%"
        android:gravity="center"
        android:layout_marginTop="570sp"
        android:textSize="40sp"/>


    <ProgressBar
        android:id="@+id/prog"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="625sp" />

    <SeekBar
        android:id="@+id/seek"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="650sp"
        android:max="100"
        android:progress="50"
        android:thumb="@drawable/uncleiroh2" />


</FrameLayout>

and here is the java:

package com.example.avatarphotos;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, RadioGroup.OnCheckedChangeListener {

    private ImageView Element;
    private TextView textView;
    private ProgressBar progressBar;
   private RadioGroup radioGroup;
    private SeekBar seekBar;
    //private RadioButton air;
    //private RadioButton earth;
    //private RadioButton water;
    //private RadioButton fire;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Element = findViewById(R.id.element);
        textView=findViewById(R.id.text);
        progressBar=findViewById(R.id.prog);
        seekBar=findViewById(R.id.seek);
        radioGroup=findViewById(R.id.radioG);
        radioGroup.setOnCheckedChangeListener(this);
        seekBar.setOnSeekBarChangeListener(this);

    }
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        progressBar.setProgress(i);
        textView.setText(""+i+"%");
        Element.setImageAlpha((int)(i/100.0*255));
    }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

    }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

    }



    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case 0:
                Element.setImageResource(R.drawable.water);
                break;
            case 1:
                Element.setImageResource(R.drawable.earth);
                break;
            case 2:
                Element.setImageResource(R.drawable.fire);
                break;
            case 3:
                Element.setImageResource(R.drawable.air);
                break;
        }
    }

      }

design photo: design app: app and well while writing this I got this error message so I don't know if it is really related: problem message

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

0

Why don't you try updating to a newer ADB version of ADB (or Update the whole Android Studio, maybe).

Fabian Montossi
  • 189
  • 3
  • 16
0

You've put all of this into a FrameLayout with large fixed margins. Your missing views are likely offscreen (android:layout_marginTop="625sp" means that you're offsetting it by quite a lot; also, you should use dp). The reason it works in the preview is likely because you're previewing what it would look like on a larger screen.

I'm assuming you did this because otherwise FrameLayout just puts all the views on top of each other. You could solve this by building your UI using ConstraintLayout, or some combination of RelativeLayout and LinearLayout. Position the views relative to each other and the screen, not at large fixed offsets.

Ryan M
  • 18,333
  • 31
  • 67
  • 74