I am creating an activity to show timetable. I have a list of lectures, and the respective day and duration of each lecture. I am trying to inflate a compound view from a layout xml file, and then adding the inflated view to one of the relative layouts in an arraylist, where one element of the arraylist represents one column or a day.
days is an arraylist of relativelayouts. The below function is a member of the activity, and is the logic to add the lectures. According to the length of the lecture, I adjust the height with ViewGroup LayoutParams for the card inside the view, and then add the view in relative layout with relative layout params. But the views do not show up.
Edit : The Code is working fine, The arraylist of the model class was a problem.
private void addTimeTableViews(){
for (LectureModel lecture : lectures) {
LayoutInflater inflater = LayoutInflater.from(this);
int currentDay = lecture.getDayOfTheWeek();
RelativeLayout layout = days.get(currentDay);
View view = inflater.inflate(R.layout.layout_tt_box, layout);
int boxHeight = getTimeDifference(lecture.getStartTime(),lecture.getEndTime());
//to adjust height of the card according to length of lecture
MaterialCardView card = view.findViewById(R.id.tt_box_card);
int cardWidth = card.getLayoutParams().width;
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(cardWidth, boxHeight - 4);
card.setLayoutParams(cardParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.topMargin = getTimeInMinutes(lecture.getStartTime());
layout.addView(view, params);
}
}
Related
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.
Hello how can i set the item liner Layout background color from this array that it have just 3 color , position one color one , position two color two , position three color three and then position one color one and so on..
the position of the list view
int[] androidColors = context.getResources().getIntArray(R.array.randomColor);
viewHolder.linearLayout.setBackgroundColor(androidColors[position]);
You can try to create your own custom adapter and implement getView function like this :
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view.
//or an image on an ImageView.
return convertView;
}
try to have a look to this post:
Change background colour of current listview item in adapter getView method
I have an array of results and i want to display each one of them in a different FrameLayout. My activity already contains a ScrollView that contains a RelativeLayout. I am using a 'for' to create each FrameLayout but right now every one of the results' parameters displayed on the same spot - This is probably because the gravity parameters are not set correctly.
This is my code:
for (int i = 0; i < results.length; ++i) {
LayoutParams textViewsLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
Gravity.CLIP_VERTICAL);
LayoutParams dividerLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 1,
Gravity.CLIP_VERTICAL);
LayoutParams frameLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CLIP_VERTICAL);
frameLayoutParams.setMargins(5, 5, 5, 5);
//Initializing text views
TextView domain = new TextView(SinglePhotoResults.this);
domain.setText(results[i].getDomain());
domain.setTextSize(22);
domain.setGravity(Gravity.CLIP_VERTICAL);
domain.setTextColor(Color.parseColor("#1A1AFF"));
domain.setLayoutParams(textViewsLayoutParams);
TextView url = new TextView(SinglePhotoResults.this);
url.setText(results[i].getURL());
url.setTextSize(22);
url.setGravity(Gravity.CLIP_VERTICAL);
url.setTextColor(Color.parseColor("#00CC00"));
url.setLayoutParams(textViewsLayoutParams);
View divider = new View(SinglePhotoResults.this);
divider.setBackgroundColor(Color.parseColor("#00ff00"));
divider.setLayoutParams(dividerLayoutParams);
TextView percents = new TextView(SinglePhotoResults.this);
Integer percentage = results[i].getPrecents();
percents.setText(percentage.toString());
percents.setTextSize(22);
percents.setGravity(Gravity.CLIP_VERTICAL);
percents.setTextColor(Color.parseColor("#000000"));
percents.setLayoutParams(textViewsLayoutParams);
TextView copiedWords = new TextView(SinglePhotoResults.this);
Integer copiedWordsNum = results[i].getNumberOfCopiedWords();
copiedWords.setText(copiedWordsNum.toString());
copiedWords.setTextSize(22);
copiedWords.setGravity(Gravity.CLIP_VERTICAL);
copiedWords.setTextColor(Color.parseColor("#000000"));
copiedWords.setLayoutParams(textViewsLayoutParams);
//Initializing frame layout
FrameLayout frameLayout = new FrameLayout(SinglePhotoResults.this);
frameLayout.setLayoutParams(frameLayoutParams);
frameLayout.setBackgroundColor(Color.parseColor("#ffffff"));
frameLayout.setPadding(5, 5, 5, 5);
frameLayout.setForegroundGravity(Gravity.CLIP_VERTICAL);
//Adding views to FrameLayout
frameLayout.addView(domain);
frameLayout.addView(url);
frameLayout.addView(divider);
frameLayout.addView(percents);
frameLayout.addView(copiedWords);
////Adding frameLayout to relativeLayout
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.singlePhotoResultRelativeLayout);
relativeLayout.addView(frameLayout);
}
I want it to be displayed one rectangle after another.
How can i set the Gravity parameters correctly of the FrameLayouts and inside it?
Instead of using Relative layout you should use LinearLayout. If you still want to use Relative layout you need to specify below which element you put your next element.
(I would suggest moving to listview instead).
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>
I am developing an app which basically should download a list of events from a website and then views them in a in a calendar like layout. I already covered the first part. But I don't have an idea how to make a layout which would be a grid (tume on one axis and date on second axis) and the events in form of listview would be placed on the grid based on the start and end time. I just need a hint how to start this. The problem is that i have to add the events depending on user choices.
This will create calender like grid programtically..
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
/* INFLATE MAIN TABLE LAYOUT */
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.table_root, null);
TableLayout tbl = (TableLayout) root.findViewById(R.id.tblLayout);
this.setContentView(root);
ViewGroup row = (ViewGroup) inflater.inflate(R.layout.table_row, root, false);
/* GENERIC LOOP FOR CREATING TABLE */
int count = 1;
for (int day = 0; day < calendarCount; day++)
{
row.addView(day);
if (count <= weekCount) {
root.addView(row);
row = (ViewGroup) inflater.inflate(R.layout.popup_row, root, false);
}
count++;
}
Also you can see..
http://w2davids.wordpress.com/android-simple-calendar/