I am trying to create a stack of cards dynamically by using a RelativeLayout and setting the ALIGN_PARENT_BOTTOM rule and the bottomMargin. But the cards keep getting drawn over each other disregarding the margin. But they draw properly when I use ALIGN_PARENT_TOP and topMargin.
activity_single_player_game.xml (Code creating RelativeLayout)
<!-- other code -->
<RelativeLayout
android:id="#+id/frame_card_stack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom" >
<ImageView
android:id="#+id/image_hand_value"
android:layout_width="40dip"
android:layout_height="20dip"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dip" />
<TextView
android:id="#+id/text_hand_value"
android:layout_width="40dip"
android:layout_height="20dip"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#88000000" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frame_card_stack_split"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:gravity="bottom" >
<ImageView
android:id="#+id/image_hand_value_split"
android:layout_width="40dip"
android:layout_height="20dip"
android:layout_alignParentBottom="true" />
<TextView
android:id="#+id/text_hand_value_split"
android:layout_width="40dip"
android:layout_height="20dip"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#88000000" />
</RelativeLayout>
<!-- other code -->
SinglePlayerGameActivity.java (Code creating the stack of cards)
RelativeLayout playerView;
RelativeLayout playerViewSplit;
#Override
protected void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_player_game);
playerView = (RelativeLayout) findViewById(R.id.frame_card_stack);
playerViewSplit = (RelativeLayout) findViewById(R.id.frame_card_stack_split);
}
public ImageView getCardImage(BJCard card, int offset, boolean playerCard) {
ImageView cardImage = new ImageView(this);
cardImage.setImageResource(BJUtility.drawableIdForCard(card));
cardImage.setBackgroundColor(0x40000000);
if (playerCard) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.bottomMargin = dip(20 * offset);
cardImage.setLayoutParams(params);
} else {
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
cardImage.setLayoutParams(params);
}
return cardImage;
}
public void populatePlayer() {
BJPlayerBox box = MenuActivity.bjmc.playerBoxes.get(MenuActivity.bjmc.activePlayerBox);
BJCardDeck deck = box.cardDecks.get(box.activeDeckIndex);
for (int i = 0; i < deck.cards.size(); i++) {
BJCard card = deck.cards.get(i);
playerView.addView(getCardImage(card, i, true), playerView.getChildCount() - 2);
}
updatePlayerSumView(deck, BEGIN, box.activeDeckIndex);
}
public void updatePlayer(int flag) {
BJPlayerBox box = MenuActivity.bjmc.playerBoxes.get(MenuActivity.bjmc.activePlayerBox);
switch (flag) {
case HIT:
BJCardDeck deck = box.cardDecks.get(box.activeDeckIndex);
for (int i = playerView.getChildCount() - 2; i < deck.cards.size(); i++) {
BJCard card = deck.cards.get(i);
playerView.addView(getCardImage(card, i, true), playerView.getChildCount() - 2);
}
updatePlayerSumView(deck, flag, box.activeDeckIndex);
break;
case SPLIT:
playerViewSplit.setVisibility(android.view.View.VISIBLE);
ImageView splitCardView = (ImageView) playerView.getChildAt(playerView.getChildCount() - 2);
playerView.removeView(splitCardView);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.bottomMargin = dip(0);
playerViewSplit.addView(splitCardView, playerViewSplit.getChildCount() - 2);
splitCardView.setLayoutParams(params);
// update first deck
BJCardDeck splitDeck = box.cardDecks.get(0);
BJCard card = splitDeck.cards.get(1);
playerView.addView(getCardImage(card, 1, true), playerView.getChildCount() - 2);
updatePlayerSumView(splitDeck, SPLIT, 0);
// update split deck
splitDeck = box.cardDecks.get(1);
card = splitDeck.cards.get(1);
playerViewSplit.addView(getCardImage(card, 1, true), playerViewSplit.getChildCount() - 2);
updatePlayerSumView(splitDeck, SPLIT, 1);
break;
case SURRENDER:
updatePlayerSumView(box.activeCardDeck(), flag, box.activeDeckIndex);
}
}
public void updatePlayerSumView(BJCardDeck deck, int flag, int splitIndex) {
ImageView iv;
TextView tv;
if (splitIndex == 0) {
iv = (ImageView) findViewById(R.id.image_hand_value);
tv = (TextView) findViewById(R.id.text_hand_value);
} else {
iv = (ImageView) findViewById(R.id.image_hand_value_split);
tv = (TextView) findViewById(R.id.text_hand_value_split);
}
if (flag == SURRENDER) {
tv.setText(null);
iv.setImageResource(R.drawable.flag);
return;
}
if (flag != SPLIT && BJUtility.isBlackjack(deck)) {
iv.setImageResource(R.drawable.crown);
} else {
int[] playerSum = BJUtility.currentCardHandTotal(deck.cards);
if (playerSum[0] > 21) {
tv.setText(null);
iv.setImageResource(R.drawable.bust);
} else {
if (playerSum.length == 1) {
tv.setText(Integer.toString(playerSum[0]));
tv.setTextColor(getResources().getColor(R.color.orangish_yellow));
} else {
// TODO: if deckState = active, show both, else, show one
tv.setText(playerSum[1] + "/" + playerSum[0]);
tv.setTextColor(getResources().getColor(R.color.orangish_yellow));
}
}
}
}
Here are some screenshots-
This is what I get when I use ALIGN_PARENT_TOP and topMargin. I want exactly this, except that the circled cards should be down with the text.
This is what I get when I use ALIGN_PARENT_BOTTOM and bottomMargin. Here the cards are in the correct position as a group, but they are all drawn completely over each other, so I can see only one.
What is your playerView??
If it is a RelativeLayout and the android:layout_height="wrap_content" then android:layout_marginBottom does not work.
Since setting android:paddingBottom="" to the playerView is not an option for you, you can try setting the android:layout_marginTop for the previous child views.
Something like, adding android:layout_marginTop to the 1st card, while adding the 2nd card.
Related
I've a problem about remove CardView in layout I need to remove perfectly clear 100% not hide something like setvisibility(View.GONE) Please save my day Thanks.
this my code in layout
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="130dp">
<RelativeLayout
android:id="#+id/relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/textView_header1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Weather"
android:textColor="#color/colorWhite"
android:textSize="15sp" />
<!--<android.support.v7.widget.CardView--> this is simple cardview
<!--android:id="#+id/card_view_1_1"-->
<!--android:layout_width="270dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="#+id/textView_header1"-->
<!--android:layout_gravity="center"-->
<!--android:layout_marginLeft="15dp"-->
<!--android:layout_marginRight="15dp"-->
<!--app:cardBackgroundColor="#color/colorWhite"-->
<!--app:cardCornerRadius="4dp">-->
</RelativeLayout>
</HorizontalScrollView>
This is how i create card
enter code here
public void createCard(String hum_val, String temp_val) {
int humidValTextSize = 35;
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
humidValTextSize = 35;
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
humidValTextSize = 17;
}
/*
------Structure of item------
CardView
|__RelativeLayout (inside CardView)
|___ImageView
|___TextView
|___TextView
*/
// Relative Layout inside CardView ---
RelativeLayout layout_in_card = new RelativeLayout(getActivity());
layout_in_card.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
layout_in_card.setPadding(15, 15, 15, 15);
layout_in_card.setMinimumWidth(300);
// End initialize RelativeLayout_Inside_CardView
// ImageView inside Relative Layout ---
ImageView imageView1 = new ImageView(getActivity());
RelativeLayout.LayoutParams layoutImg = new RelativeLayout.LayoutParams(80, 80);
layoutImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutImg.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutImg.setMargins(0, 0, 16, 0);
imageView1.setLayoutParams(layoutImg);
if (hum_val == null) {
imageView1.setImageResource(R.drawable.dashboard_nodata);
} else {
imageView1.setImageResource(R.drawable.dashboard_humidicon);
}
imageView1.setId(Integer.parseInt(currentImageViewId.toString()));
currentImageViewId++;
layout_in_card.addView(imageView1);
// End initialize ImageView ---
// TextView inside Relative Layout ---
TextView txtViewName = new TextView(getActivity());
RelativeLayout.LayoutParams layoutTxtViewName = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutTxtViewName.addRule(RelativeLayout.ALIGN_PARENT_START);
layoutTxtViewName.addRule(RelativeLayout.BELOW, imageView1.getId());
layoutTxtViewName.setMargins(0, 20, 0, 0);
txtViewName.setText("Humidity");
//txtViewName.setText("Humidity");
txtViewName.setTextSize(humidValTextSize);
txtViewName.setLayoutParams(layoutTxtViewName);
layout_in_card.addView(txtViewName);
// End initialize TextView ---
// TextView inside Relative Layout ---
TextView txtViewValue = new TextView(getActivity());
RelativeLayout.LayoutParams layoutTxtViewValue = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutTxtViewValue.addRule(RelativeLayout.ALIGN_BASELINE, imageView1.getId());
layoutTxtViewValue.addRule(RelativeLayout.ALIGN_BOTTOM, imageView1.getId());
layoutTxtViewValue.addRule(RelativeLayout.ALIGN_PARENT_END);
layoutTxtViewValue.setMargins(1, 0, 0, 0);
layoutTxtViewValue.addRule(RelativeLayout.TEXT_ALIGNMENT_GRAVITY, RelativeLayout.CENTER_VERTICAL);
if (hum_val == null) {
txtViewValue.setText("N/A");
} else {
txtViewValue.setText(hum_val + "%");
}
txtViewValue.setTextSize(humidValTextSize);
txtViewValue.setLayoutParams(layoutTxtViewValue);
layout_in_card.addView(txtViewValue);
// End initialize TextView ---
// CardView Root
CardView cardView = new CardView(getActivity());
cardView.setId(Integer.parseInt(currentCardViewId.toString()));
currentCardViewId++;
if (currentRefCardViewId == null) {
// currentRefCardViewId = R.id.card_view_1_2;
}
RelativeLayout.LayoutParams layoutCardView = new RelativeLayout.LayoutParams(270, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutCardView.addRule(RelativeLayout.BELOW, R.id.textView_header1);
layoutCardView.addRule(RelativeLayout.TEXT_ALIGNMENT_GRAVITY, RelativeLayout.CENTER_VERTICAL);
if (currentRefCardViewId != null) {
layoutCardView.addRule(RelativeLayout.RIGHT_OF, currentRefCardViewId);
}
currentRefCardViewId = cardView.getId();
layoutCardView.setMargins(15, 0, 15, 0);
cardView.setLayoutParams(layoutCardView);
cardView.setRadius(4);
cardView.setCardBackgroundColor(ColorStateList.valueOf(Color.WHITE));
cardView.setPadding(15, 0, 15, 0);
cardView.addView(layout_in_card);
RelativeLayout relativeLayout = (RelativeLayout) getView().findViewById(R.id.relative1);
relativeLayout.addView(cardView);
createCard_temp(temp_val);
}
Here is a horizontal LinearLayout, I set it's gravity to center_vertical, then add two views to the LinearLayout, the height of first view is bigger the other, the result is the second view align top of the first view, not at vertical center of the LinearLayout.
How to make all child view at vertical center, any ideas?
Here is my code:
Context context = getActivity();
mPictureContainer = new LinearLayout(context);
mPictureContainer.setBackgroundColor(0xffffffff);
mPictureContainer.setGravity(Gravity.CENTER_VERTICAL);
mPictureContainer.setOrientation(LinearLayout.HORIZONTAL);
int leftRightMargin = getResources().getDimensionPixelSize(R.dimen.leftRightMargin);
mPictureContainer.setPadding(leftRightMargin, 0, leftRightMargin, 0);
//...
mAddPicBtn = new ImageView(context);
mAddPicBtn.setLayoutParams(new LinearLayout.LayoutParams(Utils.dip2px(77f), Utils.dip2px(77f)));
mAddPicBtn.setScaleType(ScaleType.CENTER_CROP);
mAddPicBtn.setImageDrawable(StateListFactory.createStateListDrawable(R.drawable.add_prs, -1, R.drawable.add));
//...
mPictureContainer.removeAllViews();
ArrayList<String> pictures = (ArrayList<String>) data.getSerializableExtra("pictures");
int size = pictures.size();
for (int i = 0; i < size; i++) {
String path = pictures.get(i);
final View view = LayoutInflater.from(getActivity()).inflate(R.layout.status_pic_item, mPictureContainer, false);
view.setTag(path);
ImageView imageView = (ImageView) view.findViewById(R.id.picture);
ImageView delPicBtn = (ImageView) view.findViewById(R.id.delPicBtn);
delPicBtn.setImageDrawable(StateListFactory.createStateListDrawable(R.drawable.del_prs, -1, R.drawable.del));
delPicBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mPictureContainer.removeView(view);
if (mPictureContainer.getChildCount() < 4 && mAddPicBtn.getParent() != mPictureContainer) {
mPictureContainer.addView(mAddPicBtn);
}
}
});
mPictureContainer.addView(view, i);
ImageLoader.loadImage(imageView, "file://" + path);
}
if (mPictureContainer.getChildCount() < 4) {
mPictureContainer.addView(mAddPicBtn);
}
And here is the layout status_pic_item :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="81dp"
android:layout_height="81dp"
android:layout_marginRight="3dp" >
<ImageView
android:id="#+id/picture"
android:layout_width="77dp"
android:layout_height="77dp"
android:layout_alignParentBottom="true"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/delPicBtn"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:clickable="true" />
</RelativeLayout>
And here is the screenshot:
I have written this piece of code. But it is not giving the proper result. Please let me know where is the mistake. And I don't want to use Linear Layout.
Here is the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/custom_relativeLayout1"
android:orientation="horizontal"
android:background="#ffffff">
</RelativeLayout>
</LinearLayout>
String[] but = {"Hello", "Bye"};
int buttonCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customLayout = (RelativeLayout) findViewById(R.id.custom_relativeLayout1);
//customLayout is object of relativelayout.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (butArray[i].getId() != 1)
{
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
else
{
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
}
RelativeLayout.RIGHT_OFhoryzontally or vertically align?
so for vertically
Btnparams.addRule(RelativeLayout.BELOW, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
or for horizontally
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
instead of
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
LinearLayout is very nice to use if you want do dynamically add Views and scale them the same. For example if you want to add buttons and have max 3 buttons per row, you can also use another LinearLayout to make a new row.
I 'abuse' the LinearLayout row and a Button for layoutparams templating.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/ll_row1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
</LinearLayout>
</LinearLayout>
Example code:
// Get LL and template params
LinearLayout root = (LinearLayout) findViewById(R.id.ll_root);
LinearLayout row1 = (LinearLayout) findViewById(R.id.ll_row1);
ViewGroup.LayoutParams llRowParams = row1.getLayoutParams();
ViewGroup.LayoutParams btnParams = findViewById(R.id.btn1).getLayoutParams();
// Make new button
Button newBtn = new Button(context);
newBtn.setLayoutParams(btnParams);
newBtn.setText("Button 3");
// Add button to row1
row1.addView(newBtn);
// Add new row
LinearLayout row2 = new LinearLayout(context);
row2.setLayoutParams(llRowParams);
root.addView(row2);
// TODO: add buttons to new row
// TODO: some logic to decide between adding to row or creating new row and adding button
Note: code is not tested, but you should get the idea.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (i != 0) {
Btnparams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
customLayout.addView(butArray[i], Btnparams);
}
I have an android activity which contains a horizontal list view. Below is the layout for it:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="#+id/topMostLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/grey">
<HorizontalScrollView
android:id ="#+id/horScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<LinearLayout
android:id ="#+id/dateRibbon"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:gravity="center"
>
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/line"
android:paddingTop="5dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background ="#37000000"
android:layout_below="#id/horScrollView"
/>
<LinearLayout
android:id="#+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_control_bar"
android:layout_below="#id/line" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/bottom_control_bar"
android:layout_below="#id/horScrollView"
android:text="#string/tasks_empty" />
</RelativeLayout>
The problem is when I add views to the linearlayout inside the horizontalscrollview, it will not scroll fully to the right, so the rightmost child view is invisible to the user. I am adding textviews to the linearlayout. Below is the code for where the linear layout gets populated:
LinearLayout dateRibbon = FindViewById<LinearLayout>(Resource.Id.dateRibbon);
dateRibbon.RemoveAllViews();
TextView tView;
m_dateRibbonTextViews = new List<TextView>();
m_dateRibbonDates = new List<DateTime>();
int currentJobId = Telecetera.Connect.JobLibrary.JobData.Job.GetCurrentJobID();
int defaultCurrentJobID = Telecetera.Connect.JobLibrary.JobData.Job.SYSDIR_CURRENTJOB_DEFAULT;
Telecetera.Connect.JobLibrary.JobData.JobDetailsList jobDetailsList;
int i = -1;
foreach (DateTime date in jobsByDay.Keys)
{
++i;
jobDetailsList = jobsByDay[date];
tView = new TextView(this);
tView.Gravity = GravityFlags.CenterHorizontal;
tView.Text = GetDateDisplayString(date);
tView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18);
tView.SetPadding(10, 0, 10, 0);
if (currentJobId != defaultCurrentJobID && jobDetailsList.GetByJobID(currentJobId) != null)
{
m_indicesDaysWithCurrentJob.Add(i);
tView.SetBackgroundColor(Android.Graphics.Color.Cyan);
}
else
{
tView.SetBackgroundColor(Android.Graphics.Color.White);
}
tView.Click += new EventHandler(tView_Click);
dateRibbon.AddView(tView);
m_dateRibbonTextViews.Add(tView);
m_dateRibbonDates.Add(date);
tView = new TextView(this);
tView.SetPadding(2, 0, 2, 0);
tView.SetBackgroundColor(Resources.GetColor(Resource.Color.grey));
dateRibbon.AddView(tView);
}
Any hints as to why it does not completely scroll to the right would be appreciated!
Thanks.
I was able to solve it by removing the
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
lines.
you can set padding to linear layout here's my code which show's the textview at center and give some space at start and at the end of view.
lLayTwo.setPadding((center - 2 - centerScreenChildLeft), 0, (center - 2 - centerScreenChildRight), 0);
private void addSubMenu(ArrayList<String> list,String mainMenu){
this.mainMenu = mainMenu;
lLayTwo.removeAllViews();
mViewFlipper.setDisplayedChild(mViewFlipper.indexOfChild(mViewPlain));
menu = list;
Log.d(TAG, "meulist "+menu.size());
range = list.size() * 2 + 1;
Log.d(TAG, "range "+range);
int menuItem = 0;
for ( int i = 0; i < range; i++ ) {
if ( i % 2 == 0 ){
View txt = new View(MCSDistrictMainActivity.this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setBackgroundResource(R.drawable.line);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 3, 0, 0);
lLayTwo.addView(txt,params);
}else{
TextView txt = new TextView(MCSDistrictMainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(parentChildWidh, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 8, 0, 5);
params.gravity = Gravity.CENTER;
txt.setText(menu.get(menuItem));
txt.setTag(menu.get(menuItem));
txt.setTextColor(getResources().getColor(R.color.violet));
txt.setTextSize(15f);
txt.setGravity(Gravity.CENTER_HORIZONTAL);
menuItem += 1;
lLayTwo.addView(txt,params);
}
}
Log.d(TAG, "llayout Two "+lLayTwo.getChildCount()+" width "+parentChildWidh+" LEFT "+parentChildLeft);
int left = parentChildLeft;
int width = parentChildWidh;
int centerTab = 1 + hsvLowerTab.getWidth() / 2;
Log.d(TAG, "Measures left "+left+" width "+width+" center "+centerTab+" all "+( left + width -centerTab ));
//(left + width/2)-centerTab
hsvLowerTab.scrollTo((left + width/2)-centerTab, 0);
hsvLowerTab.dispatchTouchEvent (MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,(left + width/2)-centerTab, 0, 0));
}
I'm currently working on an app that has a RelativeLayout that has 4 child FrameLayouts, each FrameLayout has a set of ImageViews inside it and the inside View has it's own behavior. I'm trying to implement a drag and drop to theFrameLayouts while implementing the onTouchListener I found in this tutorial. but unfortunately the drag 'n drop doesn't work properly and nothing is happening.
any thoughts on how to implement the drag and drop correctly? what am I missing?
here is the xml code for a single child of the FrameLayouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="108dp" >
<ImageView
android:id="#+id/secondImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/back" />
<ImageView
android:id="#+id/firstImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/c2" />
</FrameLayout>
here is the xml code for the main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pinecropped"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/cardNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp" >
<include layout="#layout/card" />
</FrameLayout>
<FrameLayout
android:id="#+id/cardNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cardNumber1"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp" >
<include layout="#layout/card" />
</FrameLayout>
<FrameLayout
android:id="#+id/cardNumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:layout_marginTop="60dp" >
<include layout="#layout/card" />
</FrameLayout>
<FrameLayout
android:id="#+id/cardNumber4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/cardNumber3"
android:layout_marginRight="50dp"
android:layout_marginTop="100dp" >
<include layout="#layout/card" />
</FrameLayout>
</RelativeLayout>
here is the Java code for the single child:
public class Card implements OnClickListener {
private int _resId;
private Context _context;
private ImageView firstImage, secondImage;
private boolean isFirst;
public Card(Context context, FrameLayout parent) {
_context = context;
firstImage = (ImageView) parent.findViewById(R.id.firstImage);
secondImage = (ImageView) parent.findViewById(R.id.secondImage);
}
public void setupCards(int resId, boolean hasBackSide) {
_resId = resId;
Bitmap temp = BitmapFactory.decodeResource(_context.getResources(),
_resId);
firstImage.setImageBitmap(temp);
if (hasBackSide) {
temp = BitmapFactory.decodeResource(_context.getResources(),
R.drawable.back);
}
secondImage.setImageBitmap(temp);
isFirst = true;
secondImage.setVisibility(View.GONE);
}
// MORE IMPLEMENTATION
}
this is my main activity Java code:
public class FaceUpActivity extends Activity {
FrameLayout firstCard, secondCard, thirdCard, forthCard;
Card cardNumber1, cardNumber2, cardNumber3, cardNumber4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
}
private void initUI() {
firstCard = (FrameLayout) findViewById(R.id.cardNumber1);
secondCard = (FrameLayout) findViewById(R.id.cardNumber2);
thirdCard = (FrameLayout) findViewById(R.id.cardNumber3);
forthCard = (FrameLayout) findViewById(R.id.cardNumber4);
cardNumber1 = new Card(this, firstCard);
cardNumber2 = new Card(this, secondCard);
cardNumber3 = new Card(this, thirdCard);
cardNumber4 = new Card(this, forthCard);
cardNumber1.setupCards(R.drawable.c2, true);
cardNumber2.setupCards(R.drawable.d0, true);
cardNumber3.setupCards(R.drawable.h5, true);
cardNumber4.setupCards(R.drawable.sj, true);
firstCard.setOnTouchListener(dragMe);
secondCard.setOnTouchListener(dragMe);
thirdCard.setOnTouchListener(dragMe);
forthCard.setOnTouchListener(dragMe);
}
OnTouchListener dragMe = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
FrameLayout.LayoutParams params = (LayoutParams) v
.getLayoutParams();
int maxWidth = getWindowManager().getDefaultDisplay().getWidth();
int maxHeight = getWindowManager().getDefaultDisplay().getHeight();
int topMargin, leftMargin;
int cond = v.getId();
if (cond == R.id.cardNumber1 || cond == R.id.cardNumber2
|| cond == R.id.cardNumber3 || cond == R.id.cardNumber4) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
topMargin = (int) event.getRawY() - (v.getHeight());
leftMargin = (int) event.getRawX() - (v.getWidth()) / 2;
if (topMargin < 0) {
params.topMargin = 0;
} else if (topMargin > maxHeight) {
params.topMargin = maxHeight - v.getHeight();
} else {
params.topMargin = topMargin;
}
if (leftMargin < 0) {
params.leftMargin = 0;
} else if (leftMargin > maxWidth) {
params.leftMargin = maxWidth - (v.getWidth() / 2);
} else {
params.leftMargin = leftMargin;
}
v.setLayoutParams(params);
break;
case MotionEvent.ACTION_MOVE:
topMargin = (int) event.getRawY() - (v.getHeight());
leftMargin = (int) event.getRawX() - (v.getWidth()) / 2;
if (topMargin < 0) {
params.topMargin = 0;
} else if (topMargin > maxHeight) {
params.topMargin = maxHeight - v.getHeight();
} else {
params.topMargin = topMargin;
}
if (leftMargin < 0) {
params.leftMargin = 0;
} else if (leftMargin > maxWidth) {
params.leftMargin = maxWidth - (v.getWidth() / 2);
} else {
params.leftMargin = leftMargin;
}
v.setLayoutParams(params);
break;
}
}
return true;
}
};
}
Why your D&D is not working:
D&D is not working because your Card (that useses the internal FrameLayout) is implementing the onClick event that "eat" the d&d event.
Solutions are two:
you can have 2 different areas, one for the click event (the biggest part of the card), one for the d&d event (the bottom-left edge of the card). You can see a live example in the Android Music player that implements this solution.
Otherwise you can handle the two events at the same time:
click == down+up in the same time (under 100ms) in the same x/y location
dd == down+up in different times ( > 50/100ms)
I think there are a couple of potential problems:
Are you sure you're getting the correct IDs in your onTouch()? (since the touch listener is only used with your card views, you propably can remove the if checking for the IDs)
You are using a FrameLayout - so use the x and y position instead of margins edit: my bad, you're using RelativeLayout, so the margins are kind of right. Though, I'd use a FrameLayout instead
the API says you need to return true in onTouch() if you consumed the event, false otherwise (you're returning true when you didn't move the cards and nothing when you did)