Replace layout inflation by code

pull/286/head
Alinson S. Xavier 9 years ago
parent 7d9d45ffed
commit 1d68122a6f

@ -135,7 +135,7 @@ public class CheckmarkButtonView extends View
paint.setTypeface(InterfaceUtils.getFontAwesome(getContext()));
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(res.getDimension(R.dimen.regularTextSize));
paint.setTextSize(res.getDimension(R.dimen.smallTextSize));
rect = new RectF();
color = Color.BLACK;

@ -24,20 +24,22 @@ import android.content.*;
import android.graphics.drawable.*;
import android.os.*;
import android.support.annotation.*;
import android.text.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.common.views.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.utils.*;
import java.util.*;
import butterknife.*;
import static android.os.Build.VERSION.*;
import static android.os.Build.VERSION_CODES.*;
import static android.view.ViewGroup.LayoutParams.*;
import static org.isoron.uhabits.utils.InterfaceUtils.*;
public class HabitCardView extends FrameLayout
implements ModelObservable.Listener
@ -53,19 +55,14 @@ public class HabitCardView extends FrameLayout
"Get a haircut"
};
@BindView(R.id.checkmarkPanel)
CheckmarkPanelView checkmarkPanel;
@BindView(R.id.numberPanel)
NumberPanelView numberPanel;
@BindView(R.id.innerFrame)
LinearLayout innerFrame;
@BindView(R.id.label)
TextView label;
@BindView(R.id.scoreRing)
RingView scoreRing;
private final Context context = getContext();
@ -101,11 +98,6 @@ public class HabitCardView extends FrameLayout
numberPanel.setButtonCount(buttonCount);
}
public void setThreshold(double threshold)
{
numberPanel.setThreshold(threshold);
}
public void setController(Controller controller)
{
checkmarkPanel.setController(null);
@ -150,6 +142,11 @@ public class HabitCardView extends FrameLayout
updateBackground(isSelected);
}
public void setThreshold(double threshold)
{
numberPanel.setThreshold(threshold);
}
public void setUnit(String unit)
{
numberPanel.setUnit(unit);
@ -158,7 +155,7 @@ public class HabitCardView extends FrameLayout
public void setValues(int values[])
{
double dvalues[] = new double[values.length];
for(int i = 0; i < values.length; i++)
for (int i = 0; i < values.length; i++)
dvalues[i] = (double) values[i] / 1000;
checkmarkPanel.setValues(values);
@ -207,13 +204,26 @@ public class HabitCardView extends FrameLayout
private void init()
{
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
res = new StyledResources(getContext());
setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
setClipToPadding(false);
inflate(context, R.layout.list_habits_card, this);
ButterKnife.bind(this);
int margin = (int) dpToPixels(context, 3);
setPadding(margin, 0, margin, margin);
initInnerFrame();
initScoreRing();
initLabel();
checkmarkPanel = new CheckmarkPanelView(context);
numberPanel = new NumberPanelView(context);
numberPanel.setVisibility(GONE);
innerFrame.addView(scoreRing);
innerFrame.addView(label);
innerFrame.addView(checkmarkPanel);
innerFrame.addView(numberPanel);
addView(innerFrame);
innerFrame.setOnTouchListener((v, event) ->
{
@ -239,6 +249,50 @@ public class HabitCardView extends FrameLayout
checkmarkPanel.setButtonCount(5);
}
private void initInnerFrame()
{
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
innerFrame = new LinearLayout(context);
innerFrame.setLayoutParams(params);
innerFrame.setOrientation(LinearLayout.HORIZONTAL);
innerFrame.setGravity(Gravity.CENTER_VERTICAL);
if (SDK_INT >= LOLLIPOP)
innerFrame.setElevation(dpToPixels(context, 1));
}
private void initLabel()
{
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(0, WRAP_CONTENT, 1);
label = new TextView(context);
label.setLayoutParams(params);
label.setMaxLines(2);
label.setEllipsize(TextUtils.TruncateAt.END);
if (SDK_INT >= M)
label.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
}
private void initScoreRing()
{
scoreRing = new RingView(context);
int ringSize = (int) dpToPixels(context, 15);
int margin = (int) dpToPixels(context, 8);
float thickness = dpToPixels(context, 3);
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(ringSize, ringSize);
params.setMargins(margin, 0, margin, 0);
params.gravity = Gravity.CENTER;
scoreRing.setLayoutParams(params);
scoreRing.setThickness(thickness);
}
private void refresh()
{
int color = getActiveColor(habit);
@ -276,11 +330,9 @@ public class HabitCardView extends FrameLayout
else
{
Drawable background;
if (isSelected)
background = res.getDrawable(R.attr.selectedBackground);
else background = res.getDrawable(R.attr.cardBackground);
innerFrame.setBackgroundDrawable(background);
}
}

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
~
~ This file is part of Loop Habit Tracker.
~
~ Loop Habit Tracker is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by the
~ Free Software Foundation, either version 3 of the License, or (at your
~ option) any later version.
~
~ Loop Habit Tracker is distributed in the hope that it will be useful, but
~ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
~ more details.
~
~ You should have received a copy of the GNU General Public License along
~ with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<merge android:id="@+id/outerFrame"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:habit="http://isoron.org/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/innerFrame"
style="@style/ListHabits.HabitCard"
android:layout_width="match_parent">
<org.isoron.uhabits.activities.common.views.RingView
android:id="@+id/scoreRing"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
habit:thickness="3"/>
<TextView
android:id="@+id/label"
style="@style/ListHabits.Label"/>
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
android:id="@+id/checkmarkPanel"
style="@style/ListHabits.CheckmarkPanel"/>
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
android:id="@+id/numberPanel"
android:visibility="gone"
style="@style/ListHabits.CheckmarkPanel"/>
</LinearLayout>
</merge>

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
~
~ This file is part of Loop Habit Tracker.
~
~ Loop Habit Tracker is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by the
~ Free Software Foundation, either version 3 of the License, or (at your
~ option) any later version.
~
~ Loop Habit Tracker is distributed in the hope that it will be useful, but
~ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
~ more details.
~
~ You should have received a copy of the GNU General Public License along
~ with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvCheck"
style="@style/ListHabits.Checkmark"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@color/transparent"
android:textColor="?mediumContrastTextColor"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="10sp"/>

@ -24,59 +24,6 @@
<item name="android:background">@color/transparent</item>
</style>
<style name="ListHabits.Header">
<item name="android:background">?headerBackgroundColor</item>
<item name="android:paddingRight">4dp</item>
<item name="android:gravity">end</item>
</style>
<style name="ListHabits.Star">
<item name="android:paddingTop">2dp</item>
<item name="android:layout_width">30dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:gravity">center</item>
<item name="android:textSize">16sp</item>
<item name="android:background">@color/transparent</item>
</style>
<style name="ListHabits.Label">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:breakStrategy">balanced</item>
<item name="android:maxLines">2</item>
<item name="android:ellipsize">end</item>
<item name="android:scrollHorizontally">true</item>
</style>
<style name="ListHabits.CheckmarkPanel">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:orientation">horizontal</item>
</style>
<style name="ListHabits.Item">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@color/transparent</item>
<item name="android:clipToPadding">false</item>
<item name="android:orientation">horizontal</item>
</style>
<style name="ListHabits.HabitCard" parent="Card">
<item name="android:orientation">horizontal</item>
<item name="android:padding">0dp</item>
<item name="android:gravity">center_vertical</item>
</style>
<style name="ListHabits.Checkmark">
<item name="android:focusable">false</item>
<item name="android:minHeight">@dimen/checkmarkHeight</item>
<item name="android:width">@dimen/checkmarkWidth</item>
<item name="android:gravity">center</item>
</style>
<style name="ListHabits.EmptyState">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>

Loading…
Cancel
Save