This is my first question on here so don't go too hard on me :D
My plan is to make a seat plan for teachers, where the students are being shown. This seat plan is being created dynamically from a SQLiteDatabase. While approaches with GridView/GridLayout/TableLayout have all failed, now I'm just having a vertical LinearLayout, adding horizontal ones to it.
Now, in order to know the position of a student, I've been trying to extend from TextView, a class called SchuelerView (Schüler = student in German) that does the same as a normal TextView, simply with one more attribute: a Schueler.
If I do everything the same way, just by using TextViews, it works, but when it comes to my custom class, it doesn't work.
This is the SchuelerView code:
public class SchuelerView extends TextView {
private Schueler schueler;
private int posx, posy;
public SchuelerView(Context context) {
super(context);
}
public SchuelerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public int getPosx() {
return posx;
}
public int getPosy() {
return posy;
}
public void setPosx(int posx) {
this.posx = posx;
}
public void setPosy(int posy) {
this.posy = posy;
}
public void setSchueler(Schueler s) {
schueler = s;
posx = schueler.getPosx();
posy = schueler.getPosy();
}
public Schueler getSchueler() {
return schueler;
}
}
And this is the method that is using these views:
public void schuelerZuGrid() {
lv.removeAllViews(); // Vertikales LinearLayout
lv.setWeightSum(reihen);
for(int i = 0; i < reihen; i++) {
LinearLayout lh = new LinearLayout(this);
lh.setOrientation(LinearLayout.HORIZONTAL);
lh.setWeightSum(spalten); // DESIGN
lv.addView(lh);
for(int j = 0; j < spalten; j++) {
Cursor c = db.rawQuery("SELECT vorname, nachname, posx, posy, sid FROM Schueler, Hat "
+ "WHERE Hat.schuelerid = Schueler.sid AND Hat.kfid =" + kfid + " AND posx="+j+" AND posy="+i, null);
if(c.getCount() > 0) {
c.moveToFirst();
String vorname = c.getString(c.getColumnIndex("vorname"));
String nachname = c.getString(c.getColumnIndex("nachname"));
int posx = c.getInt(c.getColumnIndex("posx"));
int posy = c.getInt(c.getColumnIndex("posy"));
int sid = c.getInt(c.getColumnIndex("sid"));
c.close();
Schueler schueler = new Schueler(vorname, nachname, posx, posy, sid);
SchuelerView t = new SchuelerView(this);
t.setSchueler(schueler);
t.setText(vorname + " " + nachname);
t.setGravity(Gravity.CENTER); // DESIGN
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1); // DESIGN
t.setLayoutParams(lp); // DESIGN
t.setOnClickListener(this);
lh.addView(t);
} else {
SchuelerView t = new SchuelerView(this);
t.setPosx(j);
t.setPosy(i);
t.setText("/");
t.setGravity(Gravity.CENTER); // DESIGN
t.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1)); // DESIGN
t.setOnClickListener(this);
lh.addView(t);
}
}
}
}
As I've said, the only error I get is that I don't see anything on the Activity. However, the SQL statements all are being executed correctly, and as I've said, doing everything the same way, just with TextViews, works out fine.
Thank you in advance!
Related
I have read out many questions but I don't get the answer to mine.
So I am posting this new question.
I am working on a live streaming app. Basically I want to change the surface view size on click event like Whatsapp video calling.
I have two scenarios:-
Scenario 1:- One is relative layout which contains a surface view and there is a frame layout which also contains a surface view and I want to exchange when click event happens but the Click event listener is not working.
XML file
<FrameLayout
android:id="#+id/liveStreaming_videoAndControlsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<com.sos.GD_Live.utils.liveUtils.VideoGridContainer
android:id="#+id/liveStreaming_videoContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.sos.GD_Live.utils.liveUtils.VideoGridContainer>
<FrameLayout
android:id="#+id/videoChat_localVideoRender"
android:layout_marginBottom="130dp"
android:layout_marginRight="20dp"
android:layout_width="100dp"
android:onClick="frameclick"
android:visibility="visible"
android:layout_height="150dp"
android:layout_gravity="bottom|right"
/>
Its parent layout is also a frame layout
public class VideoGridContainer extends RelativeLayout implements Runnable {
private static final int MAX_USER = 4;
private static final int STAT_LEFT_MARGIN = 34;
private static final int STAT_TEXT_SIZE = 10;
private SparseArray<ViewGroup> mUserViewList = new SparseArray<>(MAX_USER);
private List<Integer> mUidList = new ArrayList<>(MAX_USER);
private Handler mHandler;
private int mStatMarginBottom;
private VideoViewEventListener mEventListener;
public VideoGridContainer(Context context) {
super(context);
init();
}
public VideoGridContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VideoGridContainer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mHandler = new Handler(getContext().getMainLooper());
}
public void addUserVideoSurface(int uid, SurfaceView surface, boolean isLocal) {
if (surface == null) {
return;
}
int id = -1;
if (isLocal) {
if (mUidList.contains(0)) {
mUidList.remove((Integer) 0);
mUserViewList.remove(0);
}
if (mUidList.size() == MAX_USER) {
mUidList.remove(0);
mUserViewList.remove(0);
}
id = 0;
} else {
if (mUidList.contains(uid)) {
mUidList.remove((Integer) uid);
mUserViewList.remove(uid);
}
if (mUidList.size() < MAX_USER) {
id = uid;
}
}
if (id == 0) mUidList.add(0, uid);
else mUidList.add(uid);
if (id != -1) {
mUserViewList.append(uid, createVideoView(surface));
requestGridLayout();
}
}
private ViewGroup createVideoView(SurfaceView surface) {
RelativeLayout layout = new RelativeLayout(getContext());
layout.setId(surface.hashCode());
LayoutParams videoLayoutParams =
new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layout.addView(surface, videoLayoutParams);
TextView text = new TextView(getContext());
text.setId(layout.hashCode());
LayoutParams textParams =
new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textParams.bottomMargin = mStatMarginBottom;
textParams.leftMargin = STAT_LEFT_MARGIN;
text.setTextColor(Color.WHITE);
text.setTextSize(STAT_TEXT_SIZE);
layout.addView(text, textParams);
return layout;
}
public void removeUserVideo(int uid, boolean isLocal) {
if (isLocal && mUidList.contains(0)) {
mUidList.remove((Integer) 0);
mUserViewList.remove(0);
} else if (mUidList.contains(uid)) {
mUidList.remove((Integer) uid);
mUserViewList.remove(uid);
}
requestGridLayout();
if (getChildCount() == 0) {
mHandler.removeCallbacks(this);
}
}
private void requestGridLayout() {
removeAllViews();
layout(mUidList.size());
}
private void layout(int size) {
LayoutParams[] params = getParams(size);
for (int i = 0; i < size; i++) {
addView(mUserViewList.get(mUidList.get(i)), params[i]);
}
}
private LayoutParams[] getParams(int size) {
int width = getMeasuredWidth();
int height = getMeasuredHeight();
LayoutParams[] array =
new LayoutParams[size];
for (int i = 0; i < size; i++) {
if (i == 0) {
array[0] = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
} else if (i == 1) {
array[1] = new LayoutParams(width, height / 2);
array[0].height = array[1].height;
array[1].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(0)).getId());
array[1].addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
} else if (i == 2) {
array[i] = new LayoutParams(width / 2, height / 2);
array[i - 1].width = array[i].width;
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
array[i].addRule(RelativeLayout.ALIGN_TOP, mUserViewList.get(mUidList.get(i - 1)).getId());
} else if (i == 3) {
array[i] = new LayoutParams(width / 2, height / 2);
array[0].width = width / 2;
array[1].addRule(RelativeLayout.BELOW, 0);
array[1].addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
array[1].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(0)).getId());
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[2].addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
array[2].addRule(RelativeLayout.RIGHT_OF, 0);
array[2].addRule(RelativeLayout.ALIGN_TOP, 0);
array[2].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(0)).getId());
array[3].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(1)).getId());
array[3].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(2)).getId());
}
}
return array;
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
clearAllVideo();
}
private void clearAllVideo() {
removeAllViews();
mUserViewList.clear();
mUidList.clear();
mHandler.removeCallbacks(this);
}
#Override
public void run() {
}
public void setItemEventHandler(VideoViewEventListener listener) {
this.mEventListener = listener;
}
}
This code where in Java file, when I added surface view for both of the layouts
mVideoLayout = findViewById(R.id.liveStreaming_videoContainer);
mVideoLayout.addUserVideoSurface(0, surface, true);
SurfaceView surface = prepareRtcVideo(remotehost, false);
surface.setZOrderMediaOverlay(true);
mCameraBtn.setVisibility(View.VISIBLE);
remotehostlist.add(remotehost);
surface.setZOrderOnTop(true);
runOnUiThread(() -> {
surface.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(LiveStreamingActivity.this, "it's good", Toast.LENGTH_SHORT).show();
return true;
}
});
surface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(LiveStreamingActivity.this, "it's good", Toast.LENGTH_SHORT).show();
}
});
});
mLayoutSmallView.addView(surface);
mLayoutSmallView.setVisibility(View.VISIBLE);
mLayoutSmallView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
Toast.makeText(LiveStreamingActivity.this, "Focus changed", Toast.LENGTH_SHORT).show();
}
});
mRtcEngine.setupLocalVideo(new VideoCanvas(surface, VideoCanvas.RENDER_MODE_HIDDEN, 0));
mRtcEngine.enableVideo();
I have already tried onclick in XML, ontouchlistener, onfocuschange listener. But no one of them is working for it. Also tried is focusable.
The problem is I am unable to click the frame layout named mLayoutSmallView.
I want to switch the surface views when it is clicked.
I need to make a 2048 game in Android.
For the Board we need use a GridLayout and set it up programatically.
It will be a 4x4 grid just like the original game.
For the card we have to use a FrameLayout with a TextView and center it.
Original game screenshot
My result screenshot
This is what I already have:
public class Board extends GridLayout {
private final int BOARD_WIDTH = 4;
private final int BOARD_HEIGHT = 4;
private Card[][] cards;
public Board(Context context) {
super(context);
initBoard();
}
public Board(Context context, AttributeSet attrs) {
super(context, attrs);
initBoard();
}
public Board(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initBoard();
}
public void initBoard() {
this.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorBoard));
this.setRowCount(BOARD_HEIGHT);
this.setColumnCount(BOARD_WIDTH);
Random random = new Random();
cards = new Card[BOARD_WIDTH][BOARD_HEIGHT];
for (int x = 0; x < BOARD_WIDTH; x++) {
for (int y = 0; y < BOARD_HEIGHT; y++) {
int n = random.nextInt(2);
cards[x][y] = new Card(this.getContext(), (n * 2));
}
}
}
public void addCardsToBoard() {
for (int x = 0; x < BOARD_WIDTH; x++) {
for (int y = 0; y < BOARD_HEIGHT; y++) {
GridLayout.LayoutParams param = new LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(x);
param.rowSpec = GridLayout.spec(y);
param.topMargin = 50;
Card c = cards[x][y];
c.setLayoutParams(param);
this.addView(cards[x][y], 230, 230);
}
}
}
}
-
public class Card extends FrameLayout {
private int number = 0;
private TextView txtNumber;
public Card(Context context, int n) {
super(context);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
txtNumber = new TextView(context);
txtNumber.setTextSize(30);
txtNumber.setLayoutParams(params);
this.addView(txtNumber);
setNumber(n);
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
this.setBackgroundColor(ContextCompat.getColor(getContext(), number == 0 ? R.color.colorCard : R.color.colorCardActive));
txtNumber.setText(number == 0 ? "" : "" + number);
}
}
Anything I can do to make it look better (more like the original game).
Any tips/advice is appreciated.
Add horizontal and vertical spacing to GridLayout, that is applicable to it's column and row.
view.setHorizontalSpacing(10);
view.setVerticalSpacing(10);
I just followed this tutorial, to create a custom View as an item of a GridLayout.
That's my CustomView
public class RowView extends View{
boolean touchOn;
boolean mDownTouch = false;
private OnToggledListener toggledListener;
int _IdRow = 0;
int _IdColumn = 0;
public RowView(Context context, int Rows, int Columns) {
super(context);
this._IdRow = Rows;
this._IdColumn = Columns;
init();
}
public RowView(Context context) {
super(context);
init();
}
public RowView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
touchOn = false;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
#Override
protected void onDraw(Canvas canvas) {
if (touchOn) {
canvas.drawColor(Color.RED);
} else {
canvas.drawColor(Color.GRAY);
}
}
//onClick not possible to use on custom View so, onTouchEvent is the solution
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
//if Click
case MotionEvent.ACTION_DOWN:
touchOn = !touchOn;
invalidate();
if(toggledListener != null){
toggledListener.OnToggled(this, touchOn);
}
mDownTouch = true;
return true;
case MotionEvent.ACTION_UP:
if (mDownTouch) {
mDownTouch = false;
performClick();
return true;
}
}
return false;
}
#Override
public boolean performClick() {
super.performClick();
return true;
}
public void setOnToggledListener(OnToggledListener listener){
toggledListener = listener;
}
public int get_IdRow() {
return _IdRow;
}
public int get_IdColumn() {
return _IdColumn;
}
On this class I can detect when user clicks on an item of GridLayout and change it to another color, that's ok.
But the problem comes at the time to create this :
This is my MainActivity where I show the GridLayout :
int numOfCol = mGridLayout.getColumnCount();
int numOfRow = mGridLayout.getRowCount();
mRowViews = new RowView[numOfCol*numOfRow];
for(int yPos=0; yPos<numOfRow; yPos++){
for(int xPos=0; xPos<numOfCol; xPos++){
RowView tView = new RowView(this, xPos, yPos);
tView.setOnToggledListener(this);
mRowViews[yPos*numOfCol + xPos] = tView;
mGridLayout.addView(tView);
}
}
mGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
final int MARGIN = 5;
int pWidth = mGridLayout.getWidth();
int pHeight = mGridLayout.getHeight();
int numOfCol = mGridLayout.getColumnCount();
int numOfRow = mGridLayout.getRowCount();
int w = pWidth/numOfCol;
int h = pHeight/numOfRow;
for(int yPos=0; yPos<numOfRow; yPos++){
for(int xPos=0; xPos<numOfCol; xPos++){
GridLayout.LayoutParams params =
(GridLayout.LayoutParams)mRowViews[yPos*numOfCol + xPos].getLayoutParams();
params.width = w - 2*MARGIN;
params.height = h - 2*MARGIN;
params.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
mRowViews[yPos*numOfCol + xPos].setLayoutParams(params);
}
}
}});
Also there is a method of the Interface OnToggledListener that gives to me the row and column of my GridLayout when an item of it is clicked :
#Override
public void OnToggled(MyView v, boolean touchOn) {
//get the id string
String idString = v.get_IdRow() + ":" + v.get_IdColumn();
}
I'd like to avoid to create that mGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() because it fills on the screen thing that I don't want... I tried to put GridLayout 6x6 with android:layout_height="400dp" and it only show 3x3 and this is the LogCat message
D/android.widget.GridLayout: vertical constraints: y6-y0>=1749, y6-y5<=291, y5-y4<=291, y4-y3<=291, y3-y2<=291, y2-y1<=291, y1-y0<=291 are inconsistent; permanently removing: y6-y5<=291.
I'd like to do something like GridLayout[row][colum] to get the color of background and then do stuff, but I'm not able to find this solution.
For simplifying, you can implement a custom Board view wrapping the GridLayout and related logic. Below I report a possible approach.
Expectation here is to have an ItemView for representing one single cell in the board.
public class Board extends FrameLayout implements View.OnClickListener {
private GridLayout mGridView;
private int mRowsCount;
private int mColsCount;
private int mCellSpace;
private OnItemClickListener mOnItemClickListener;
public Board(Context context) {
super(context);
init(context, null);
}
// other constructors
private void init(Context context, AttributeSet attrs) {
// default values
mRowsCount = 1;
mColsCount = 1;
View layout = inflate(getContext(), R.layout.view_lights_board, null);
mGridView = (GridLayout) layout.findViewById(R.id.view_grid);
mGridView.setRowCount(mRowsCount);
mGridView.setColumnCount(mColsCount);
mGridView.post(new Runnable() {
#Override
public void run() {
int width = getMeasuredWidth() / getColumnsCount();
int height = getMeasuredHeight() / getRowsCount();
for (int i = 0; i < getRowsCount(); i++) {
for (int j = 0; j < getColumnsCount(); j++) {
GridLayout.LayoutParams params = (GridLayout.LayoutParams)
getChildAt(i, j).getLayoutParams();
params.width = width;
params.height = height;
getChildAt(i, j).setLayoutParams(params);
}
}
}
});
addView(layout);
}
// this method allows to dinamically create grid
public void buildChildren(int rowsCount, int colsCount) {
mRowsCount = rowsCount;
mColsCount = colsCount;
mGridView.setRowCount(mRowsCount);
mGridView.setColumnCount(mColsCount);
buildChildren();
}
public void buildChildren() {
for (int i = 0; i < getRowsCount(); i++) {
for (int j = 0; j < getColumnsCount(); j++) {
ItemView view = new ItemView(getContext(), i, j);
view.setOnClickListener(this);
mGridView.addView(view);
}
}
}
public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
}
public ItemView getChildAt(int rowIndex, int columnIndex) {
int index = (getColumnsCount() * rowIndex) + columnIndex;
return (ItemView) mGridView.getChildAt(index);
}
public boolean isTouchOn(int rowIndex, int columnIndex) {
return getChildAt(rowIndex, columnIndex).isTouchOn();
}
public int getColumnsCount() {
return mGridView.getColumnCount();
}
public int getRowsCount() {
return mGridView.getRowCount();
}
#Override
public void onClick(View v) {
if (v instanceof ItemView) {
ItemView view = (ItemView) v;
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view);
}
}
}
public interface OnItemClickListener {
void onItemClick(ItemView view);
}
}
In your Activity layout you will have something like this (here I assume your app package is com.android.example):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.android.example.Board
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="400dp" />
</FrameLayout>
And this is possible implementation of the Activity:
public class MainActivity extends AppCompatActivity implements LightsOutBoard.OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Board board = (Board) findViewById(R.id.grid);
board.setOnItemClickListener(this);
board.buildChildren(3, 3);
}
#Override
public void onItemClick(ItemView view) {
String text = view.getRowIndex() + " - " + view.getColumnIndex();
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
Hope this could help.
I'm creating dinamically a LinearLayout (a custom Class that extends LinearLayout) with a RadioGroup inside it. I'm adding RadioButtons dinamically also. This LinearLayout class is used in several Activities and Fragments.
The strange behaviour is that the second time I add LinearLayout, when OnCheckedChanged is called, a get an index that doesn't exists in my current RadioGroup. I't seems that every time I create a new Instance of my LinearLayout, RadioGroup thinks that RadioButtons added previously are still there.
For example, if I create a custom LinearLayout and I add four items and after that I create another LinearLayout (in other Activity) with other 4 items, when I click in the first item (on the second Activity), I get a 5 as clicked item position.
This is my custom LinearLayout class:
public class AgrupacionConmutadorLayout extends LinearLayout{
private static final String TAG = "AgrupacionConmutadorLayout";
private int[] mColorFromLevel = {R.color.inspeccion_aparato_agrupacion_color_level_1, R.color.inspeccion_aparato_agrupacion_color_level_2,
R.color.inspeccion_aparato_agrupacion_color_level_3, R.color.inspeccion_aparato_agrupacion_color_level_4, R.color.inspeccion_aparato_agrupacion_color_level_5};
private Context mContext;
private LinearLayout dataContainer;
private TextView mTitle;
private RelativeLayout mSubAgrupacionesContainer;
private LinearLayout mDataAndSubAgrupacionesContainer;
private RadioGroup mConmutadorContainer;
private ArrayList<View> mLayoutsDataContained = new ArrayList<View>();
private int mLevel;
private LinearLayout mTitleContainer;
private InspeccionesFormWidgetReceiver mModificationReceiver;
private boolean isEdicion;
public AgrupacionConmutadorLayout(Context context) {
super(context);
this.mContext = context;
init(null, 0);
}
public AgrupacionConmutadorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init(attrs, 0);
}
public AgrupacionConmutadorLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
dataContainer = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.ins_agrupacion_conmutador_container, this);
mTitle = (TextView) dataContainer.findViewById(R.id.ins_agrupacion_container_title);
mSubAgrupacionesContainer = (RelativeLayout) findViewById(R.id.rl_ins_subagrupacion_container);
mConmutadorContainer = (RadioGroup) findViewById(R.id.ins_agrupacion_conmutador_radio_group);
mDataAndSubAgrupacionesContainer = (LinearLayout) findViewById(R.id.ll_ins_data_and_subagrupacion_container);
mTitleContainer = (LinearLayout) findViewById(R.id.ll_ins_agrupacion_container_title);
mConmutadorContainer.removeAllViews();
mConmutadorContainer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int pos) {
Log.d(TAG, "onCheckedChanged Clicked pos: " + pos);
Log.d(TAG, "onCheckedChanged RadioGroup size: " + radioGroup.getChildCount());
Log.d(TAG, "onCheckedChanged " + mLayoutsDataContained.size());
//OnCheckedChanged comienza a devolver en 1, al ArrayList en 0
for (int i = 1; i < mLayoutsDataContained.size() + 1; i++) {
if (i == pos) {
mLayoutsDataContained.get(i - 1).setVisibility(View.VISIBLE);
} else {
mLayoutsDataContained.get(i - 1).setVisibility(View.GONE);
}
}
}
});
}
public void initialice(AgrupacionSios agrupacionSios, InspeccionesFormWidgetReceiver modificationReceiver, boolean isEdicion) {
this.mModificationReceiver = modificationReceiver;
this.isEdicion = isEdicion;
mTitle.setText(agrupacionSios.getTitle());
mTitle.setTextColor(mContext.getResources().getColor(mColorFromLevel[agrupacionSios.getLevel()]));
if (agrupacionSios.isOcultarContenedor()) mTitleContainer.setVisibility(View.GONE);
if (agrupacionSios.getNumOfSons() > 0) {
drawSubAgrupaciones(agrupacionSios);
}
this.refreshDrawableState();
}
private void drawSubAgrupaciones(AgrupacionSios agrupacionSios) {
ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ArrayList<AgrupacionSios> agrupaciones = agrupacionSios.getAgrupacionesHijos();
Iterator itAgr = agrupaciones.iterator();
while (itAgr.hasNext()) {
AgrupacionSios agrupacionHija = (AgrupacionSios) itAgr.next();
if (agrupacionHija.getNumOfLevels() > 0 || agrupacionHija.getNumOfSons() > 0) {
switch (agrupacionHija.getTipoSubAgrupaciones()) {
case CONMUTADOR:
AgrupacionConmutadorLayout v = new AgrupacionConmutadorLayout(mContext);
v.initialice(agrupacionHija, mModificationReceiver, isEdicion);
mSubAgrupacionesContainer.addView(v, layoutParams);
mLayoutsDataContained.add(v);
break;
case COLUMNAS:
AgrupacionColumnasLayout vCol = new AgrupacionColumnasLayout(mContext);
vCol.initialice(agrupacionHija, mModificationReceiver, isEdicion);
mSubAgrupacionesContainer.addView(vCol, layoutParams);
mLayoutsDataContained.add(vCol);
break;
default:
AgrupacionGenericaLayout vGen = new AgrupacionGenericaLayout(mContext);
vGen.isSonOfConmutador(true);
vGen.initialice(agrupacionHija, mModificationReceiver, isEdicion);
mSubAgrupacionesContainer.addView(vGen, layoutParams);
mLayoutsDataContained.add(vGen);
break;
}
}
//Añadimos el botón de conmutación correspondiente a la subagrupación.
addAgrupacionToConmutadorContainer(agrupacionHija);
}
mConmutadorContainer.check(1);
}
public void setBackgroundColor(int color) {
setBackgroundResource(color);
}
private void addAgrupacionToConmutadorContainer(AgrupacionSios mAgrupacion) {
ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
RadioButton b = new RadioButton(mContext);
b.setLayoutParams(layoutParams);
b.setText(mAgrupacion.getTitle());
b.setButtonDrawable(android.R.color.transparent);
b.setBackground(mContext.getResources().getDrawable(R.drawable.ins_conmutador_radio_background));
b.setPadding(20,5,20,5);
RadioGroup.LayoutParams params
= new RadioGroup.LayoutParams(mContext, null);
params.setMargins(10, 0, 10, 0);
b.setLayoutParams(params);
mConmutadorContainer.addView(b);
}
}
And this is the way I create AgrupacionConmutadorLayout on Activities & Fragments:
AgrupacionConmutadorLayout vConm = new AgrupacionConmutadorLayout(this);
vConm.initialice(agrupacion, this, isEdicion);
vConm.setVisibility(View.VISIBLE);
mAgrupacionesContainer.addView(vConm, layoutParams);
-----------------SOLUTION---------------------------
Marius answer was the key, I was getting clicked RadioButton id, not his position inside RadioGroup.
Finally I have done it by using tags. I set its order to every RadioButton as a tag:
private void addAgrupacionToConmutadorContainer(AgrupacionSios mAgrupacion) {
ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
RadioButton b = new RadioButton(mContext);
b.setLayoutParams(layoutParams);
b.setText(mAgrupacion.getTitle());
b.setButtonDrawable(android.R.color.transparent);
b.setBackground(mContext.getResources().getDrawable(R.drawable.ins_conmutador_radio_background));
b.setPadding(20, 5, 20, 5);
RadioGroup.LayoutParams params
= new RadioGroup.LayoutParams(mContext, null);
params.setMargins(10, 0, 10, 0);
b.setLayoutParams(params);
b.setTag(mConmutadorContainer.getChildCount());
mConmutadorContainer.addView(b);
}
And I get that tag in OnCheckedChangeListener:
mConmutadorContainer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int clickedId) {
View radioButtonClicked = radioGroup.findViewById(clickedId);
if (radioButtonClicked != null) {
Integer pos = (Integer) radioButtonClicked.getTag();
Log.d(TAG, "onCheckedChanged RadioGroup clicked position: " + pos);
for (int i = 0; i < mLayoutsDataContained.size(); i++) {
View view = mLayoutsDataContained.get(i);
if (pos.intValue() == i) {
view.setVisibility(View.VISIBLE);
} else view.setVisibility(View.GONE);
}
}
}
});
You must have misread the API. Second parameter is NOT position. It's the id, of the button. There are several solutions to your problem.
Adding id, based on position:
//not recommended way, as activity.findViewById uses this id, I GUESS something wrong may happen
private int currId;
...
b.setId(currId);
currId++;
container.addView(b);
Adding a tag:
//recommended
b.setTag(mAgrupacion);
container.addView(b);
//then...
AgrupacionSios tag = (AgrupacionSios) b.getTag();
//do action based on tag information
Use this.
for (int i = 0; i < mLayoutsDataContained.size(); i++) {
if (i == pos) {
mLayoutsDataContained.get(i - 1).setVisibility(View.VISIBLE);
} else {
mLayoutsDataContained.get(i - 1).setVisibility(View.GONE);
}
}
I have searched a great deal and have not found a solution to my problem. When I create multiple views and try to add them to a LinearLayout only the first view (cake) displays.
Here is where I create and add the views.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.image_View);
PlayAreaView cake = new PlayAreaView(SecondTestActivity.this, R.drawable.cake);
views.add(cake);
PlayAreaView bomb = new PlayAreaView(SecondTestActivity.this, R.drawable.bomb);
views.add(bomb);
PlayAreaView crown = new PlayAreaView(SecondTestActivity.this, R.drawable.crown);
views.add(crown);
PlayAreaView scissors = new PlayAreaView(SecondTestActivity.this, R.drawable.cut);
views.add(scissors);
PlayAreaView trash = new PlayAreaView(SecondTestActivity.this, R.drawable.bin_closed);
views.add(trash);
PlayAreaView key = new PlayAreaView(SecondTestActivity.this, R.drawable.bullet_key);
views.add(key);
LayoutParams params
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(View v : views){
Log.v("created", "view created");
v.setLayoutParams(params);
linearLayout.addView(v);
}
}
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/image_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</FrameLayout>
I can create a single view and be fine but I am unable to add multiple views the the LinearLayout. Why is this?
If you look here, there was another person with basically the same problem. However, they were not declaring the orientation of their layout so it defaulted to horizontal. In your layout you have explicitly declared horizontal. Is this intended (for example to have the items show up side-by-side)? If not, change the orientation to vertical and you should be good.
If you need them to show side-by-side, then I am not sure off the top of my head how to do that, but I would guess you need to declare each view as next to the view placed before it (e.g. using something like 'alignToRightOf'. Again, this is just a stab-in-the-dark but it may get you going on a correct path.
Hope this helps.
I found the answer to my problem. I did not fully understand how the Activity handled views. For me to draw multiple separate views I have to loop over each view that I add to an array and call an overridden draw method in the custom view. After I understood this I was able to create multiple views and add separate dragging functions on each view. Here's the code.
public class ThirdTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.main_View);
layout.addView(new MyCircles(this));
}
private class MyCircles extends View{
private Context myContext;
private ArrayList<MyCircle> circles = new ArrayList<MyCircle>();
private int size = 10;
public MyCircles(Context context) {
super(context);
myContext = context;
addCircles();
}
private void addCircles(){
for (int i = 0; i < size; i++){
circles.add(new MyCircle(myContext, R.drawable.skullcrossbones, i * 40, 50));
}
}
#Override
protected void onDraw(Canvas canvas){
for (View v : circles){
v.draw(canvas);
}
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
int mouseX = (int)event.getX();
int mouseY = (int)event.getY();
MyCircle image = null;
for(MyCircle images : circles){
//Log.v("image checked X: " + images.imageX + ", Y: " + images.imageY, "checked");
// Is the event inside of this view?
if(images.getImageRect().contains((int)event.getX(), (int)event.getY()))
{
image = images;
}
}
if (image != null){
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
Log.v("touched down", "touched down at X: " + mouseX + ", Y: " + mouseY);
image.dragDistance = new Point(mouseX, mouseY);
bringToFront();
isSelected();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_MOVE)
{
Log.v("move", "moving to X: " + mouseX + ", Y: " + mouseY);
image.dragDistance.set(mouseX, mouseY);
invalidate();
return true;
}
}
return super.onTouchEvent(event);
}
}
private class MyCircle extends View{
private int imageId;
private Drawable image;
private Context myContext;
private int size = 48;
private int imageOffset = size/2;
private int imageX;
private int imageY;
private Point dragDistance;
public MyCircle(Context context, int id, int x, int y) {
super(context);
myContext = context;
imageId = id;
imageX = x;
imageY = y;
dragDistance = new Point(imageX + imageOffset, imageY + imageOffset);
}
public Rect getImageRect(){
return image.getBounds();
}
#Override
public void draw(Canvas canvas) {
//Log.v("draw","drawn");
super.onDraw(canvas);
image = myContext.getResources().getDrawable(imageId);
imageX = (dragDistance.x - imageOffset);
imageY = (dragDistance.y - imageOffset);
image.setBounds(imageX, imageY, imageX + size, imageY + size);
image.draw(canvas);
}
}
}
This is written for Android version 2.1 API 7