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));
}
Related
In a Table Row, there is a horizontal scroll view containing a Linear Layout,
The intention is to place 5 Imageview's inside the Linear Layout dynamically
The problem is it seems the Imageview's are shifted and the Linear Layout does not fill the horizontal scroll view
Here is the xml:
<TableRow
android:id="#+id/table_3_Row_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0.1mm"
android:background="#color/color21"
android:gravity="center"
>
<HorizontalScrollView
android:layout_span="3"
android:id="#+id/table_3_Row_7_HSV1"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:fadeScrollbars="false"
android:scrollbarSize="0.5mm"
android:scrollbarThumbHorizontal="#color/colorDevider"
>
<LinearLayout
android:id="#+id/table_3_Row_7_HSV1LL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/color02"
android:layout_gravity="center"
>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/table_3_Row_7_ImageView1"
android:layout_span="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/color21"
>
</LinearLayout>
</TableRow>
The mentioned Liniear Layout is table_3_Row_7_HSV1LL
And the code is as following:
int mMaxCount = 5;
ImageView[]mImageView = new ImageView[mMaxCount];
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params1.setMargins(10, 75, 0, 0);
params1.setMargins(100, 0, 0, 0);
params1.height = 200;
params1.width = 200;
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params2.setMargins(100, 85+20, 0, 0);
params2.setMargins(100, 0, 0, 0);
params2.height = 170;
params2.width = 170;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params3.setMargins(100, 95+20, 0, 0);
params3.setMargins(100, 0, 0, 0);
params3.height = 140;
params3.width = 140;
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params4.setMargins(100, 105+20, 0, 0);
params4.setMargins(100, 0, 0, 0);
params4.height = 110;
params4.width = 110;
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//params5.setMargins(100, 115+20, 0, 0);
params5.setMargins(100, 0, 0, 0);
params5.height = 80;
params5.width = 80;
params5.gravity = Gravity.CENTER;
mLayoutStruct.getLayout_table_3_Row_7_HSV1LL().setGravity(Gravity.CENTER);
for (int i = 0; i < mMaxCount; i++)
{
mImageView[i] = new ImageView(mContext);
//mImageView[i].setBackgroundColor(Color.TRANSPARENT);
mImageView[i].setTag("T"+"I"+i);
mImageView[i].setId(i);
mImageView[i].setFocusable(true);
mImageView[i].setClickable(true);
if (i == 0)
{
//Picasso.with(mContext).load(R.drawable.size1).placeholder(R.drawable.ic_launcher).resize(200, 200).into(mImageView[i]);
mImageView[i].setBackgroundResource(R.drawable.size1);
mLayoutStruct.getLayout_table_3_Row_7_HSV1LL().addView(mImageView[i], params1);
}
else if (i == 1)
{
//Picasso.with(mContext).load(R.drawable.size1).placeholder(R.drawable.ic_launcher).resize(170, 170).into(mImageView[i]);
mImageView[i].setBackgroundResource(R.drawable.size1);
mLayoutStruct.getLayout_table_3_Row_7_HSV1LL().addView(mImageView[i], params2);
}
else if (i == 2)
{
//Picasso.with(mContext).load(R.drawable.size1).placeholder(R.drawable.ic_launcher).resize(140, 140).into(mImageView[i]);
mImageView[i].setBackgroundResource(R.drawable.size1);
mLayoutStruct.getLayout_table_3_Row_7_HSV1LL().addView(mImageView[i], params3);
}
else if (i == 3)
{
//Picasso.with(mContext).load(R.drawable.size1).placeholder(R.drawable.ic_launcher).resize(110, 110).into(mImageView[i]);
mLayoutStruct.getLayout_table_3_Row_7_HSV1LL().addView(mImageView[i], params4);
}
else if (i == 4)
{
//Picasso.with(mContext).load(R.drawable.size1).placeholder(R.drawable.ic_launcher).resize(80, 80).into(mImageView[i]);
mImageView[i].setBackgroundResource(R.drawable.size1);
mLayoutStruct.getLayout_table_3_Row_7_HSV1LL().addView(mImageView[i], params5);
}
}
The background color of horizontal scroll view is White
The background color of the Linear Layout is Red
I expect the Red color continues till the end of White color and covers it
Another point is when horizontally scrolling, I just see 4 Imageview's instead of 5
First of All:
Its Better to Use RecylerView to Add Items to a list. this example may help you.
RecylerView is Backward compatible so it runs on older version that api 22 you dont need to set minSDK to 22.
Your Solution for Adding item to Linearlayout should be better. you can make array of your params to avoid if/else inside loop.
Answer:
You See 4 ImageView because You Forget To Add
mImageView[i].setBackgroundResource(R.drawable.size1);
inside your else if (i == 3) So it adds imageview with empty Background that is not visible.
And
for covering all the WHITE color of your horizontal scroll view by RED you have to set android:fillViewport="true" on the HorizentalScrollView like below:
<HorizontalScrollView
android:id="#+id/table_3_Row_7_HSV1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_span="3"
android:background="#FFFFFF"
android:fadeScrollbars="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbarSize="0.5mm"
android:scrollbarThumbHorizontal="#color/gray"
android:scrollbars="horizontal">
I solved the issue by
1 - Deleting android:layout_gravity="center" from the LinearLayout
2 - Utilizing setPadding to the horizontal scroll view to shift it down to the center of the table row
For the below point you need to change the code as shown below.
"Another point is when horizontally scrolling, I just see 4 Imageview's instead of 5"
for (int i = 0; i < mMaxCount; i++)
to
for (int i = 0; i <=mMaxCount; i++)
In my layout I have 6 spinners that are not necessaritly displayed.
When no option is selected, the height is good, but when an option is selected, it looks bigger and I didn't find anything about that online.
Here is the spinners without an option selected
And when an option is selected
Here is the Layout which contains the spinners:
<LinearLayout
android:id="#+id/layoutDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/details">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/txtDetail1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="#dimen/medium_text"/>
<Spinner
android:id="#+id/spinDetail1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/spinner"/>
</LinearLayout>
... Same thing 5 more times ...
</LinearLayout>
And here is my java code for the spinners:
private void initializeDetails() {
List<Detail> details = mainAct.details;
detailSpinners = new Spinner[details.size()];
int marginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
int marginEnd = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
int marginStart = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.TOP;
for (int i = 0; i < details.size(); i++) {
int tvID = getResources().getIdentifier("txtDetail" + (i + 1), "id", mainAct.getPackageName());
int spinID = getResources().getIdentifier("spinDetail" + (i + 1), "id", mainAct.getPackageName());
TextView txtDetail = inputView.findViewById(tvID);
Spinner spinDetail = inputView.findViewById(spinID);
txtDetail.setText(details.get(i).getTitle());
List<String> answers = details.get(i).getAnswers();
answers.add("");
final int answersSize = answers.size() - 1;
ArrayAdapter<String> detailAdapter = new ArrayAdapter<String>(mainAct, R.layout.support_simple_spinner_dropdown_item, answers) {
#Override
public int getCount() {
return answersSize;
}
};
spinDetail.setAdapter(detailAdapter);
spinDetail.setSelection(answersSize);
params.setMargins(marginStart, marginTop, marginEnd, 0);
spinDetail.setLayoutParams(params);
params.setMargins(0, marginTop, marginEnd, 0);
txtDetail.setLayoutParams(params);
detailSpinners[i] = spinDetail;
}
}
I can't put a wrap_content height for the spinners because if I do they aren't visible.
I don't think that the xml is the problem because I used a lot of spinners with the same code and I never had this issue. The difference is that I edit the LayoutParams programmatically and there's probably something I'm doing wrong.
I just want the spinners to keep the same height.
Set the padding to 0 on the spinner xml element.
<Spinner
android:id="#+id/spinDetail1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="0dp"
android:background="#drawable/spinner"/>
Update:
The wrapping LinearLayout has a height of "wrap_content" while the Spinner has a height of "match_parent", assuming this will act the same as wrap_content as it is only constrained by the LinearLayout.
Setting a specified height in pixels to the Spinner should resolve this.
android:layout_height="60dp"
I want XML for the TextViews and EditTexts created dynamically. Some blogs suggest that there are some Third Party Libraries that can do that but I wasn't able to find one. I am basically creating TextViews & EditTexts dynamically in my code on a button click.
Code:
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
for (int x = 0; x < 1; x++) {
Display display = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth() / 3;
TextView et1 = new TextView(this);
et1.setBackgroundColor(color.transparent);
et1.setText("Untitled");
et1.setGravity(Gravity.LEFT);
EditText et = new EditText(this);
et.setHint("Click to add");
et.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LayoutParams lp2 = new LayoutParams(width,
LayoutParams.WRAP_CONTENT);
// lp1.addRule(RelativeLayout.BELOW, et1.getId());
linearLayout1.addView(et1, lp2);
linearLayout1.addView(et, lp2);
XML:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/addImage" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/addEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/scrollView1"
android:text="Edit" />
The question is, how do I achieve the XML for the TextView & EditText as a String value? Do I give them tags and ids statically in the code or is there any other way?
public static final void writeMapXml(Map val, String name, XmlSerializer out)
throws XmlPullParserException, java.io.IOException
{
if (val == null) {
out.startTag(null, "TextView");
out.endTag(null, "TextView");
return;
}
Set s = val.entrySet();
Iterator i = s.iterator();
out.startTag(null, "TextView");
if (name != null) {
out.attribute(null, "name", "TextView");
}
out.endTag(null, "TextView");
}
XMLSerializer provided by android itself is enough to make dynamic XMLs.
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.
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);
}