0

I have a drawable pasek_postepu.xml file which I use as a background of View in activity_add.xml. the pasek_postepu.xml is a layer-list, containing 2 items: rectangles.

activity_add.xml

<?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"
    tools:context=".AddActivity"
    android:background="@color/background">

    <View
        android:id="@+id/pasek_postepu_dodawanie"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_margin="5dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/pasek_postepu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

pasek_postepu.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/pasek_postepu_1_element"
        android:left="20dp"
        android:right="200dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/red"/>
        </shape>
    </item>
    <item
        android:id="@+id/pasek_postepu_2_element"
        android:left="200dp"
        android:right="20dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/green"/>
        </shape>
    </item>
</layer-list>

I want to programmatically set the size of each of them, depending on screen width, so each one of them would take 1/2 of the screen, including margins and some space between them, let's say 10dp. I have already extracted screen width to int dpWidth, so that's not the problem. This is my java file:

AddActivity.java

package com.example.kamil.math;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class AddActivity extends AppCompatActivity {
private static int ilosc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ChangeSize();
    }

    @SuppressLint("ResourceAsColor")
    public void ChangeSize(int ktory_cykl){
        LayerDrawable pasek = (LayerDrawable)getResources().getDrawable(R.drawable.pasek_postepu);
        GradientDrawable pasek1 = (GradientDrawable) pasek.findDrawableByLayerId(R.id.pasek_postepu_1_element);
        GradientDrawable pasek2 = (GradientDrawable) pasek.findDrawableByLayerId(R.id.pasek_postepu_2_element);
        pasek1.setColor(Color.rgb(31,135,31)); //green
        pasek2.setColor(Color.rgb(196,32,32)); //red

        View pasek_id = findViewById(R.id.pasek_postepu_dodawanie);
        pasek_id.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.pasek_postepu));
    }
}

I can "access" each item, and change their color, but I can't see the way to change size. I tried to modify Left and Right e.g. pasek1.setLeft(...); but I get a message "cannot resolve symbol setLeft". I can't use width because it works on Android 6+, and I need Android 5 compatibility.

Is there a way I can set size of each item form java file?

K.Mat
  • 1,341
  • 11
  • 17

1 Answers1

0

instead of constraintLayout use LinearLayout
in LinearLayout there is an attribute called android:weightSum

here is an example

<LinearLayout
    android:background="@color/DarkGoldenrod"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="2" />
<TextView
    android:background="#00FF00"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1" />

<TextView
    android:background="@color/red"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1" />

by giving weightSum = 2 and giving each TextView = 1
they will each take same amount space no matter the device size

here is a link for more information

Kevin Kurien
  • 812
  • 6
  • 14
  • To my knowledge, I cannot set Layout file as a background – K.Mat Nov 17 '18 at 23:20
  • O want to set color of each part depending on whether answer was good or not. Something like this: https://drive.google.com/file/d/1NPQLKlHHKibjsEj5CMHeggiJ-reZWYjF/view?usp=drivesdk I don't know any method to change progress bar background, so I decided to do 10 separate items in layer-list – K.Mat Nov 19 '18 at 09:52