Android Studio - issue with manipulating Layouts/Views programmatically - java

I have a few issues with setting LayoutParams and other parameters of my layouts/views programmatically. I cannot specify these in a XML layout file because whether they appear depends on the data held in the database.
The following is a function I use to create a new "Section" which consists of a FrameLayout with its children being View and TextView:
public FrameLayout createSection(long id, String name) {
FrameLayout frame = new FrameLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(15, 15, 15, 15);
frame.setLayoutParams(params);
View view = new View(this);
LayoutParams viewParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
view.setLayoutParams(viewParams);
view.setId(toIntExact(id));
view.setBackgroundResource(R.color.colorButton);
frame.addView(view);
TextView text = new TextView(this);
LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
textParams.setMarginStart(15);
text.setGravity(Gravity.CENTER);
text.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_START);
text.setTextColor(getResources().getColor(R.color.colorTextSecondary));
text.setText(name);
frame.addView(text);
return frame;
}
The parent of this newly created FrameLayout is LinearLayout and so based on the other similar questions on StackOverflow I figured setting parameters for FrameLayout should be done through LinearLayout.LayoutParams. However, this does not make a change. The initial XML page contains this:
Initial XML page
The first "SECTION" is created in the XML file, and the other two are created through 'createSection' function. This is the outcome: Design outcome
The issue is that the margins are not set properly and the TextView doesn't seem to care about the Gravity + TextAlignment combination that I'm using.
I would appreciate any help that I could get to resolve this issue.

I apologise for wasting anyone's time. The code seems to work and the margin sizes are different due to these being set in terms of pixels (px) rather than dp as it is in the XML file.
I also forgot to add text.setLayoutParams(textParams); to the TextView object.

Related

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>

set Width dynamically of textview

In my project, i need to draw rows of textview dynamically according to the data received to show it. To align them properly, i have used Table Row n xml, and called it in java code. Now, in their LayoutParam i have given MATCH_PARENT, but it wraps the text according to the length of data received. Now, i want to fix width of the fields for a tabular view. I do all this process in postExecute method. In this method, i used setWidth function to set it according to the width of header row element.Here, Sno is a view, while size is array containing width of all elements of headerRow.
Sno.setTag(patient);
Sno.setWidth(size[0]);
But it didn't solve this problem, when i tried getWidth to see its width, it was showing its value 0. Then i tried to set this width using LinearLayout.LinearParams
LinearLayout.LayoutParams params_sno = new LinearLayout.LayoutParams(size[0],
LinearLayout.LayoutParams.MATCH_PARENT);
Sno.setLayoutParams(params_sno);
but still no benefit, secondaly, if i remove MATCH_PARENT from width of LayoutParams, its width got increased from width of header row element.
fields without data are invisible.
Try this
TableRow row = new TableRow(activityContext);
TableLayout.LayoutParams td_tr = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
row.setWeightSum(10);
row.setLayoutParams(td_tr);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.parseColor("#D2D2D2"));
TextView tv= new TextView(activityContext);
tv.setText(String.valueOf(i));
tv.setTextAppearance(activityContext, style.tvBoldRow);
tv.setLayoutParams(new TableRow.LayoutParams(0 , LayoutParams.WRAP_CONTENT, 1)); // Here you can set weight to your TextView.
tv.setPadding(2, 2, 2, 2);
tv.setGravity(Gravity.CENTER);
row.addView(tv);
try this:
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
tv.setLayoutParams(Params1);
Add the following parameter
android:weight = "1"
in xml to the textview and set
android:layout_width = "0dp".
I think this should work for you.
Yes you can set weight from the java code.
To set weight from java code :
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
The last parameter is the weight.
As tsp said
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
then while adding it in table layout you have overrided add methode which takes view and layout params.
You can also consider TypedValue.applyDimention() to give values in DP
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(TaypedValue.applyDimension(TaypedValue.COMPLEX_UNIT_DIP, 15, objDeisplayMetrics),TaypedValue.applyDimension(TaypedValue.COMPLEX_UNIT_DIP, 50, objDeisplayMetrics));

Android - textviews are not visible when dynamically created

I am trying to make an app that lets the user store and saves contacts. It can save, but it has problems listing.
I have a for loop that runs through the database and prints a set of data for each contact (each row), an image (actually its a string because it passes the path) and a string. It prints them in a scrollview with a linear layout for each contact (each contact has a linear layout of its own, so that i can let one contact occupy a row each). The images come out, however, the textviews are nowhere to be found.
Using log.d(textview.getText()); it confirms that the textviews are created and take up space.
http://chesnutcase.heliohost.org/derpbox/itv1.png
Two "contacts" with names, not printed out. The space inbetween is presumbly by the textview.
http://chesnutcase.heliohost.org/derpbox/itv2.png
Another two "contacts", but without names. The dont have a space between each other. Or at least, a significantly smaller space.
Code:
DatabaseHandler db = new DatabaseHandler(this);
int a = (int) (long) db.countRows();
LinearLayout theLayout = (LinearLayout)findViewById(R.id.contactsList);
for(int i = 0;i<a;i++){
ImageButton image = new ImageButton(this);
int density=getResources().getDisplayMetrics().densityDpi;
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(density,density, 0.5f);
image.setLayoutParams(vp);
image.setScaleType(ImageView.ScaleType.FIT_END);
int b = a - i;
try {
image.setImageBitmap(decodeUri(Uri.parse(db.getContactData("photo_path")[i])));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
theLayout.addView(image);
TextView tv = new TextView(this);
tv.setText(db.getContactData("name")[i]);
Log.d("UserLog","name is " + db.getContactData("name")[i]);
Log.d("UserLog","textfield contains " + tv.getText());
LinearLayout.LayoutParams vp2 = new LinearLayout.LayoutParams(0,0,1f);
tv.setLayoutParams(vp2);
tv.setBackgroundColor(Color.RED);
theLayout.addView(tv);
}
Any solutions? Thanks in advance
Double check which orientation you've applied to the LinearLayout of your contacts list.
You are setting bad LayoutParams to your TextView. You're making your TextView 0px by 0px with a weight of 1.
LinearLayout.LayoutParams vp2 = new LinearLayout.LayoutParams(0,0,1f);
tv.setLayoutParams(vp2);
Try using one of the MATCH_PARENT or WRAP_CONTENT constants. They're listed here: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT
If you want your TextView to take up the remaining width of the screen I would leave the weight as 1, the width as 0, but you need to set the height to a constant like WRAP_CONTENT.
You're also setting the size of your ImageView to the device screen density (which is a constant) instead of setting a scaling size based off your screen density.
You probably need to call requestLayout(); in order to update the current view layout.
theLayout.requestLayout();
Also it seems you are creating a view with 0 width and 0 height with that layout params:
LinearLayout.LayoutParams vp2 = new LinearLayout.LayoutParams(0,0,1f);
It is better to use ListView for such list.
You will need to write only one Adapter, that's represent logic for creating one row on list.
I would recommend using a CursorAdapter and a layout xml file, this way you can design it to look exactly how you want, and preview it. It is a lot easier than setting all those fiddly LayoutParams
If you have to create them dynamically you may find the text colour is the same as the background, try setting it to something visible like bright red for testing. If you still don't see the text it may be that it's visibility isn't set to View.VISIBLE finally the layout may not be the correct size, a handy tip for this is set the background to a suitably eye catching colour, even if there is no text you should see a shaded block.

Setting up RelativeLayout in java code

I'm having a hard time getting two text views to appear on top of each other in my java code. Here's the code I'm experimenting with:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
This stacks the two text views on the same line, but I want TextView text2, to appear below TextView text1, so in my app I want the following to appear as the output:
1
2
I've tried all sort of things with the "addRule" method, I'm not sure why this isn't working. I want to know how to do this without XML because I plan to build a library of methods that can build up a layout that is easily adjustable through editing an array.
Your TextViews don't have an id (by default the id is -1)... put this after their initialization:
text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique
I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.
EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.
So here:
text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);
you can use xml layout for this :
in relative layout
u set the first textview and assign it some id
fot the next text view we can assign parameter
android:layout_below="id of above text view"
in this way we get 2nd text view below 1st text view

Categories

Resources