Adding View dynamically to RelativeLayout not working - java

I am working on an app where I need to extensively work with animations. My problem is, I added an some ImageViews to my RelativeLayout through Java. But next to that when I am trying to add another ImageView applying rules like BELOW,ALIGN_LEFT etc with respect to previously added views, those images don't show up. It's annoying :( primarily, my question is, do these rules(LEFT,RIGHT_OF,BELOW etc) work with respect to already dynamically added views?

Q : do these rules(LEFT,RIGHT_OF,BELOW etc) work with respect to already dynamically added view
Ans: Yes they were much work. Sample Code
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
You need to set Ids and then give Whatever RULE you want to add for that view.

Related

How to fix right button border not showing in app?

I am trying to make a game with multiple levels. My plan is to print 15 levels on 1 page. I have attached a photo of the simple design I am currently using.
As you can see in the picture the right border of a button is not showing. For the parent I am using this java code:
LinearLayout horl = new LinearLayout(this);
horl.setId(worldVar);
horl.setOrientation(LinearLayout.HORIZONTAL);
horl.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
And to set 3 buttons horizontally in this LinearLayout I use this code for the buttons. This code just has a simple for loop around to put 3 buttons in this LinearLayout:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(getMarginInDp(100), getMarginInDp(100));
params.setMargins(getMarginInDp(1), getMarginInDp(25), getMarginInDp(1), 0);
button.setLayoutParams(params);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);horl.addView(button);
The function getMarginInDp looks like this:
public int getMarginInDp(int dp){
return (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
Does anybody have any idea how to make the border appear because I want to keep the buttons approximately this size and I may even slightly increase the margins?
You can set the weight to 0.33f for each button:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getMarginInDp(100), getMarginInDp(100), 0.33f);
so they are distributed equally in the width of the layout.

Textview not sticking to the right edge of the screen (Android/Java)

I'm trying to have some textview stick to the right side of the screen (and align it right). I want to do this programmatically but can't find a proper way to do so.
I tried using setGravity and setTextAlignment. But none seem to work.
answershort.setGravity(Gravity.RIGHT);
answershort.setTextAlignment(TEXT_ALIGNMENT_VIEW_END)
Any ideas what I'm doing wrong?
public void createanswershort(String key){
answershort = new TextView(getApplicationContext());
RelativeLayout.LayoutParams av = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, // Width of TextView
RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView
av.addRule(RelativeLayout.BELOW, laatstetxtview.getId());
av.addRule(RelativeLayout.RIGHT_OF, laatstetxtviewsmall.getId());
av.setMargins(35,10,25,0);
answershort.setLayoutParams(av);
int sID = Tools.generateViewId();
answershort.setId(sID);
answershort.setGravity(Gravity.RIGHT);
answershort.setTextAlignment(TEXT_ALIGNMENT_VIEW_END);
answershort.setText(Html.fromHtml(key).toString());
answershort.setTextSize(15);
answershort.setTextColor(Color.parseColor("#000000"));
answershort.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
rl.addView(answershort);
laatstetxtview = ts;
}
Try adding the following rule.
av.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
Also, you should probably remove
av.addRule(RelativeLayout.RIGHT_OF, laatstetxtviewsmall.getId());
You are trying to align the views to the right of the screen, not to the right of each other.

How to properly set settings for RelativeLayout programatically?

I'm creating a RelativeLayout programatically and I want to place three objects into it: a ListView and two Buttons. I need to set some settings to properly locate them in the window. For now I'm doing it this way:
RekativeLayout rl = new RelativeLayout(context);
rl.setBackgroundColor(Color.WHITE);
rl.setPadding(10, 10, 10, 10);
ListView listView = new ListView(context);
ListViewAdapter adapter = new ListViewAdapter(jParser.getArrayList(), context);
listView.setLayoutParams(new LayoutParams(wWidth, wHeight));
listView.setAdapter(adapter);
RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(wWidth, wHeight);
listParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
listParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
listView.setId(98515);
listView.setLayoutParams(listParams);
Button moreButton = new Button(context);
moreButton.setLayoutParams(lp);
moreButton.setBackgroundColor(Color.GRAY);
moreButton.setText("More");
RelativeLayout.LayoutParams mButtonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mButtonParams.addRule(RelativeLayout.ALIGN_LEFT, listView.getId());
mButtonParams.addRule(RelativeLayout.BELOW, listView.getId());
mButtonParams.setMargins(0, 10, 0, 0);
moreButton.setLayoutParams(mButtonParams);
closeButton.setLayoutParams(lp);
closeButton.setBackgroundColor(Color.GRAY);
closeButton.setText("Close");
Button closeButton = new Button(context);
RelativeLayout.LayoutParams cButtonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cButtonParams.addRule(RelativeLayout.ALIGN_RIGHT, listView.getId());
cButtonParams.addRule(RelativeLayout.BELOW, listView.getId());
cButtonParams.setMargins(0, 10, 0, 0);
closeButton.setLayoutParams(cButtonParams);
rl.addView(listView);
rl.addView(moreButton);
rl.addView(closeButton);
but I guess this method requires much system resources and isn't acceptable. How should I set the settings properly?
What you are doing is fine, given the question you asked. For the sake of the question I will still answer both.
I think you meant RelativeLayout.LayoutParams. Which let's you set any attributes and customize how the View should behave.
For instance, to create and set the basic height/width:
RelativeLayout myRelativeLayout = new RelativeLayout(this);
myRelativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
===========================================================
Now, as far as you other doubts, your real question is if this is optimal and if there are any other ways. (While you should really create another question, I will still answer).
As mentioned in the commments, this is the main reason of xml resources. Here you create your layouts or views and just populate them with data.
Here's a quick guide: http://developer.android.com/guide/topics/ui/declaring-layout.html
A quick breakdown:
-res/layout/ is the directory where your layout xmls will be. (LayoutInflater)
-Inside those you decalre other views (TextView, ImageView, etc).
-You must reference and inflate the resources to be able to call them in code.
-Once inflated you can call findViewById(resourceId) and cast View to the proper Widget (view) type.
Inflating and referencing view items requires a Context. Here's a quick guide to understand how it works:
http://www.doubleencore.com/2013/06/context/
Other references:
You might want to take a deeper look into resources.
Hope this helps, and happy coding!

Issue with Button in AlertDialog (Floating off screen)

Here's an image of what my issue looks like:
That box at the bottom is the top half of a button. Whenever I've got too many Stock Options in the dialog box, it first forces the button off screen, THEN it scrolls.
I want the button fixed at the bottom of the dialog and then have the scrolling happen.
Here's the code:
public void buyStock(View view){
Context context = getApplicationContext();
//create ScrollView to hold everything
ScrollView scrollView = new ScrollView(context);
//generate content for dialog
LinearLayout dialogContainer = new LinearLayout(context);
dialogContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 400, 1);
dialogContainer.setLayoutParams(params);
dialogContainer.setPadding(15, 15, 0, 15);
dialogContainer.setBackgroundColor(Color.WHITE);
//each hotel stock options
for (int i = 0; i < hotels.size(); i++) {
Hotel testHotel = hotels.get(i);
testHotel.setPrice(200);
View stockPicker = getStockPicker(testHotel);
LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 100, 1.0f);
pickerParams.gravity = Gravity.LEFT;
stockPicker.setLayoutParams(pickerParams);
dialogContainer.addView(stockPicker);
stockPicker.setBackgroundColor(0xffffff);
}
scrollView.addView(dialogContainer);
LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
Button buyButton = new Button(context);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
buyButton.setLayoutParams(buttonParams);
LinearLayout buttonLayout = new LinearLayout(context);
buttonLayout.addView(buyButton);
dialogLayout.addView(scrollView);
dialogLayout.addView(buttonLayout);
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(ctw);
buyStockDialog.setTitle("Buy Stock: ");
buyStockDialog.setView(dialogLayout);
buyStockDialog.show();
}
Make dialogLayout a RelativeLayout, and then declare buttonParams as a RelativeLayout.LayoutParams (layout params are declared according the parent viewgroup type, which for buyButton is now a RelativeLayout). Your also going to need to declare a RelativeLayout.LayoutParams for scrollView.
note: This code is in c#/monodroid, not raw java/android, so some quick porting of some of the methods and constants may need to be required, but shouldn't be too time consuming.
If this doesn't work out exactly trying experimenting with other combinations of the RelativeLayout position rules and/or gravity. Let me know if it doesn't work out.
RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, some_height);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
Then, add these statements to control where scrollView and buyButton are positioned
scrollParams.AddRule(LayoutRules.AlignParentLeft);
scrollParams.AddRule(LayoutRules.AlignParentTop);
buttonParams.AddRule(LayoutRules.Below, scrollView.Id);
The for the getting the button on the bottom of the screen, try one of two things
buttonParams.AddRule(LayoutRules.AlignParentBottom);
or
buyButton.Gravity = Gravity.BOTTOM
Try explicitly setting a fixed height to the scrollView
scrollView.LayoutParameters.Height = HEIGHT;
I think it defaults to WRAP_CONTENT if not specified, and is why its scaling.
You may have to set it in a layout file if you cant from code
<ScrollView
android:id="#+id/scroll_view_1
android:layout_width="wrap_content"
android:layout_height="100"/>
and then change
ScrollView scrollView = new ScrollView(context);
to
ScrollView scrollView = findViewById(R.Id.scroll_view_1, this);
Try wrapping the scrollView in another LinearLayout, and set this new layout's height to a fixed width that keeps the button low enough to your liking. You can actually replace the Scrollview with a ListView, but don't do this until after at least trying to get this fix working first. ListViews scroll, but you'd still face this problem without this fix.
To fix the new issue you mentioned below (with some quick and dirty code, you should use trial and error to fix it correctly), try the following
//each hotel stock options
for (int i = 0; i < hotels.size(); i++)
{
// ...
}
if( hotels.size() < numberOfItemsNeededForCorrectView )
{
for( int i=0; i < numberOfItemsNeededForCorrectView - hotels.size(); i++ )
{
View blankView = new View(context);
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(1, 100);
blankView.setLayoutParams(viewParams);
blankView.setViewState(ViewState.INVISIBLE);
dialogContainer.addView(stockPicker);
}
}
You can try replacing your scrollView with a list view still and see if that fixes things. You can also try tweaking layout and gravity settings until you get it, or trying introducing other layouts or reorganizing your layouts (or using RelativeLayout, its really not that bad).
So, now in 2022 I've also had this problem. I've just set fixed size for ScrollView in fragment layout xml-file. When you have this problem, you can set, for example, 150dp or 200dp and be happy! And for more convenience, you can create special layout for landscape screen orientation and set fixed layout_height size there. I'm really confused that there's no information about this problem besides this page on the Internet.
<ScrollView
android:layout_width="match_parent"
android:layout_height="200dp">
<!--your view into scrollView here-->
</ScrollView>

Android set gravity of a textview dynamically

I dynamically added horizontal layouts to my activity, and I am trying to put textviews inside of them.
The problem is, I can't manage to put some textviews To the right and some to the left.
I tried textview.setgravity, but it just changed the place of the text inside the textview instead of moving the textview itself.
Edit : here is some of my code to help you get an idea :
for(int i=0;i<nbelem;i++){ //génération des boutons des commandes
layouts[i] = new RelativeLayout(this);
layouts[i].setClickable(true); //toute la zone est cliquable
layouts[i].setId(i);
layouts[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 150));
Info[i] = new TextView(this);
Info[i].setClickable(true);
Info[i].setId(i);
Info[i].setText("Pizza 4 fromages x2 \n Pizza chevre x2");
Info[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
layouts[i].addView(Info[i]);
Numcom[i] = new TextView(this);
Numcom[i].setClickable(true);
Numcom[i].setId(i);
Numcom[i].setText("n°1522");
Numcom[i].setGravity(Gravity.RIGHT | Gravity.TOP);
Numcom[i].setTextSize(12);
Numcom[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,50));
It sounds that RelativeLayout may suit your needs and save you lots of nested LinearLayouts which are not recommended (performance wise).
You have to set a layout gravity to the LinearLayout that contains your textview.
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.CENTER_HORIZONTAL;
textview1.setLayoutParams(lp);
Be sure that you use LinearLayout.LayoutParams

Categories

Resources