Changing the position of editText in android when it's needed [duplicate] - java

I'm creating a number of edit texts next to each other programmatically using RelativeLayout. The default width of each edit text is wrap_content, but when the edit text reaches the edge of the screen, it visually changes it's sizes. So how can I make it move to the next line when this happens?
private EditText createEditText(EditText editText1, EditText editText2, String word){
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
editText.getLayoutParams().width,
editText.getLayoutParams().height
);
layoutParams.addRule(RelativeLayout.RIGHT_OF, editText1.getId());
layoutParams.addRule(RelativeLayout.BELOW, textView.getId());
layoutParams.leftMargin += 60;
editText2.setHint("" + word);
editText2.setHintTextColor(editText.getSolidColor());
editText2.setBackgroundResource(R.drawable.rounded_edittext);
editText2.setLayoutParams(layoutParams);
editText2.setPadding(editText.getPaddingLeft(), editText.getPaddingTop(), editText.getPaddingRight(), editText.getPaddingBottom());
editText2.setId(View.generateViewId());
relativeLayout.addView(editText2);
return editText2;
}

Try FlowLayout in android and dynamiaclly inflate TextViews in it.
Rather than that you can use Linearlayout with fixed number of children in each row and if doing this the children must having same layout_weight.
But rather than going in such big disputes i will request you to simply replace RelativeLayout With FlowLayout in android

What you can try. Not sure, if it will work.
what you can do is calculate the width of screen on onCreate and make a check if the view exceeds that, make a new relative below the last one.
To be honest linear layout will be more easier to maintain using weight property.

Related

How to change the position of edit text programmatically in android?

I'm creating a number of edit texts next to each other programmatically using RelativeLayout. The default width of each edit text is wrap_content, but when the edit text reaches the edge of the screen, it visually changes it's sizes. So how can I make it move to the next line when this happens?
private EditText createEditText(EditText editText1, EditText editText2, String word){
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
editText.getLayoutParams().width,
editText.getLayoutParams().height
);
layoutParams.addRule(RelativeLayout.RIGHT_OF, editText1.getId());
layoutParams.addRule(RelativeLayout.BELOW, textView.getId());
layoutParams.leftMargin += 60;
editText2.setHint("" + word);
editText2.setHintTextColor(editText.getSolidColor());
editText2.setBackgroundResource(R.drawable.rounded_edittext);
editText2.setLayoutParams(layoutParams);
editText2.setPadding(editText.getPaddingLeft(), editText.getPaddingTop(), editText.getPaddingRight(), editText.getPaddingBottom());
editText2.setId(View.generateViewId());
relativeLayout.addView(editText2);
return editText2;
}
Try FlowLayout in android and dynamiaclly inflate TextViews in it.
Rather than that you can use Linearlayout with fixed number of children in each row and if doing this the children must having same layout_weight.
But rather than going in such big disputes i will request you to simply replace RelativeLayout With FlowLayout in android
What you can try. Not sure, if it will work.
what you can do is calculate the width of screen on onCreate and make a check if the view exceeds that, make a new relative below the last one.
To be honest linear layout will be more easier to maintain using weight property.

Android Text View at run time - Text views inflating as multiple row

How to create multiple text views at run time in multiple rows and column? I have inflated a linear layout and created the text views using for loop. Text Views were created successfully, but i'm facing the issue that all created text views are only in single row. I tried to set it with the LayoutParams also, but cant fixed it. How to fix this? Below is my code
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
appointmentSlotList = appointmentSlot.getAppointmentSlots();
if(appointmentSlotList != null && appointmentSlotList.size()>0){
for(int i = 0; i<appointmentSlotList.size(); i++){
View appointmentInflater = layoutInflater.inflate(R.layout.appointment_time, null);
TextView lblDate = (TextView) appointmentInflater.findViewById(R.id.appointmentTime);
lblDate.setText(appointmentSlotList.get(i));
lblDate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//lblDate.setLayoutParams(lparams);
lblDate.setOnClickListener(onclickTime);
try {
//if(previousSelected!=i)
lnrDateContainer.addView(appointmentInflater);
}catch (Exception e){
e.printStackTrace();
}
}
}
and my output is shown below:
Output for the inflated text views shown below the date picker
Set the textview android layout gravity to fill
android:layout_gravity="fill"
If above wont work then refer to this question : Android multi line linearlayout
As you want to add TextView in rows and columns, you can use TableLayout and add TextView in it pragmatically
Check this answer : https://stackoverflow.com/a/16939325/5345482
To achieve these I think FlowLayout library might be fit to your requirements.
With this lib you can create your TextViews in a single line and when doesn't have space then auto inserts the view in the next line.
The link: https://github.com/ApmeM/android-flowlayout
I hope helps you!
Specify the orientation in your Linear Layout as VERTICAL
<LinearLayout
----
android:orientation="vertical"
---- />

Hindi kavita proper alignment/justification

I am developing an app related to hindi kavita(poems). I want the poems to be displayed in the way real poems are displayed like the image shown below
Now the problem is I dont know how to use a textview to show this kind of text
Use a linear layout with vertical orientation. Add a text view for each line, with layout_width="match_parent" and the appropriate gravity attribute.
https://developer.android.com/reference/android/widget/LinearLayout.html
EDIT:
If you have your poem as an arraylist of strings where each element is a line of your poem, you can do:
//Initialise your layout in your activity onCreate()
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOrientation(LinearLayout.VERTICAL);
//Start listening to your firebase data and put this somewhere in the callback:
// poemLines is a list of Strings you get from firebase
for(i=0; i<poemLines.size(); i++){
TextView view = new TextView(context);
view.setText(poemLines.get(i));
//set any other attributes to your textview that you want, width, height, font, etc
view.setGravity(i%2==0?END:START);
layout.add(view);
}

Using getDimension() on a Button/TextView always returns the same value

I'm making an app exclusively for android tablets and part of it has buttons dynamically created based on various news headlines. I have a single dimens.xml and I use the scaling factor from DisplayMetrics to make the sizes appropriate for different densities. The problem is that when I change the size in my xml it has absolutely no effect on the dimension that the method returns.
RelativeLayout.LayoutParams announcementButtonLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
announcementButtonLayout.topMargin = topMargin;
announcementButtonLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
announcementButtonLayout.height = this.getResources().getDimensionPixelSize(R.dimen.news_button_height);
announcementButtonLayout.width = getResources().getDimensionPixelSize(R.dimen.dashboard_right_width);
Button button = new Button (this.getActivity());
button.setWidth(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width));
button.setBackgroundResource(R.drawable.photo_edited);
button.setTextColor(getResources().getColor(R.drawable.news_poll_item_text_color));
float f =Utilities.getPixelScaleMultiplier();
f *= getResources().getDimension(R.dimen.news_item_text);
button.setTextSize(f);
button.setText(announcement.getDescription());
button.setMaxLines(1);
button.setTag(announcement);
button.setOnClickListener(this);
button.setLayoutParams(announcementButtonLayout);
button.setId(index);
Based on my button code above, what could be causing this breach of trust?
Thanks,
Adurnari
EDIT: Included my LayoutParams as reference
Found the solution:
"Cleaning" was taking the values that I had changed in my xml and adding them on a later portion of the page where they were located previously. The lower ones were dictating the size.
Try to work with button.setBounds
Use LayoutParams to set view width and height, then add it to viewgroup.
Button button = new Button (this.getActivity());
//button.setWidth(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width));
button.setBackgroundResource(R.drawable.photo_edited);
button.setTextColor(getResources().getColor(R.drawable.news_poll_item_text_color));
float f =Utilities.getPixelScaleMultiplier();
f *= getResources().getDimension(R.dimen.news_item_text);
button.setTextSize(f);
button.setText(announcement.getDescription());
button.setMaxLines(1);
button.setTag(announcement);
button.setOnClickListener(this);
LayoutParams announcementButtonLayout = new LayoutParams(getResources().getDimensionPixelSize(R.dimen.dashboard_right_width), LayoutParams.WRAP_CONTENT);
button.setLayoutParams(announcementButtonLayout);
button.setId(index);
Clean the project after changing dimens.xml value, maybe the R file is not getting updated in your case.

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.

Categories

Resources