I want to get a View from xml file to normal class, I used the LayoutInflater, but it gives me only copy of this view instead of original (I would like to set changes to elements).
Is that possible to get original the layout from xml? How can I fix it?
Class in which I want to get layout
public class Board {
private Context context;
private View inflated;
private RelativeLayout screen;
public Board(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated = inflater.inflate(R.layout.board, null);
screen = (RelativeLayout) inflated.findViewById(R.id.custom_board_screen);
this.context = context;
}
public TrippleToggleButton getButton(int id) {
//return (TrippleToggleButton) inflated.findViewById(id);
TrippleToggleButton btn;
for(int i=0;i<screen.getChildCount();i++)
{
View v1 = screen.getChildAt(i);
if(v1 instanceof TableLayout)
{
for(int j=0;j<((TableLayout) v1).getChildCount();j++)
{
View v2 = ((TableLayout) v1).getChildAt(j);
if (v2 instanceof TableRow)
{
for(int k=0;k<((TableRow) v2).getChildCount();k++)
{
View v3 = ((TableRow) v2).getChildAt(k);
if (v3 instanceof TrippleToggleButton)
{
Log.i("2014.07.10",screen.getId()+" ; "+v3.getId());
//originally id of button are 0-8; inflated 9 - 17
if (v3.getId()==id) return (TrippleToggleButton)v3;
}
}
}
}
}
}
return null;
}
}
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_board_screen"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/custom_board_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TableRow
android:id="@+id/custom_board_table_row1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<View style="@style/DividingHorizontalLine"/>
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<board.TrippleToggleButton
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff000000">
<View style="@style/DividingHorizontalLine"/>
</TableRow>
<TableRow
android:id="@+id/custom_board_table_row2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<View style="@style/DividingHorizontalLine"/>
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<board.TrippleToggleButton
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff000000">
<View style="@style/DividingHorizontalLine"/>
</TableRow>
<TableRow
android:id="@+id/custom_board_table_row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<View style="@style/DividingHorizontalLine"/>
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<board.TrippleToggleButton
/>
</TableRow>
</TableLayout>
</RelativeLayout>
My custom button class:
public class TrippleToggleButton extends Button implements View.OnClickListener {
private Sign actual_state;
private final float scale = getContext().getResources().getDisplayMetrics().density;
private int dps = 50;
private int pixels = (int) (dps * scale + 0.5f);
private static int buttonCount = 0;
public TrippleToggleButton(Context context) {
this(context, null);
}
public TrippleToggleButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TrippleToggleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
addFeatures();
}
private void addFeatures() {
actual_state = Sign.UNCHECKED;
setOnClickListener(this);
ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(pixels, pixels);
setLayoutParams(param);
setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
setMinimumHeight(pixels);
setMinimumWidth(pixels);
setMinHeight(pixels * 2);
setMinWidth(pixels * 2);
setPadding(pixels / 10, pixels / 10, pixels / 10, pixels / 10);
setTextSize(pixels);
setId(IDAllocator.getNextID()); // this is just a static method which sets ID's starting from 0
}
private void onUncheckedState() {
}
public Sign getActual_state() {
return actual_state;
}
public boolean isUnchecked() {
return getActual_state() == Sign.UNCHECKED;
}
public void setState(Sign newState) {
actual_state = newState;
setText(actual_state.toString());
setClickable(false);
}
@Override
public void onClick(View v) {
TrippleToggleButton btn = (TrippleToggleButton) v;
if (btn.isUnchecked()) {
Toast t = Toast.makeText(getContext(),""+getId(),Toast.LENGTH_SHORT); // it shows correct ID, everything goes ok here
t.show();
Container.performAction(btn);
}
}
}