Unable to correctly use .setLayoutParams(...) - Android - java

I wanted to try using a database on android so I made a small app that allows you to add contacts to the database and then it displays them.
I have been able to display contacts (from the database) if I create the layout on xml and then edit the text fiels in code. But I wanted to build the layouts in code so I can add as many contacts as I want.
The following code is the method I use to create the layout and the app crashes whenever I make it run this code. My guess is that there is a problem with the params. If only type LayoutParams.MatchContent it asks me to import and it gives me many options, that0s why it says LinearLayout.LayoutParams...;
I add the resulting layout to a LinearLayout.
Any help is much apreciated.
private LinearLayout createContactView (Contact contact) {
LinearLayout contactInfoWrapper = new LinearLayout(this);
contactInfoWrapper.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
contactInfoWrapper.setOrientation(LinearLayout.VERTICAL);
TextView nameView = new TextView(this);
nameView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
nameView.setText(contact.getName());
contactInfoWrapper.addView(nameView);
TextView numberView = new TextView(this);
numberView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
numberView.setText(contact.getPhoneNumber());
contactInfoWrapper.addView(numberView);
return contactInfoWrapper;
}

Instead of using setLayoutParams, instead use the LayoutParams in the addView(View v, LayoutParams params) method of your LinearLayout:
TextView nameView = new TextView(this);
nameView.setText(contact.getName());
contactInfoWrapper.addView(nameView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

can you change the line
numberView.setText(contact.getPhoneNumber());
to this
numberView.setText(String.valueOf(contact.getPhoneNumber()));

Related

How add array of TextViews into Relative layout without overlapping programmatically in android?

I'm looking for a solution to solve my problem which all my TextViews overlaps on themselves, when are added to Relative Layout. In fact, I need to do put them after each. other I've read existed answers, I followed them but nothing could solve it yet. can someone tell me where I did wrongly?
here is my code:
for (int i=0;i<parts.length;i++)
{
valueTV[i] = new TextView(this);
valueTV[i].setText(parts[i]);
valueTV[i].setId(i);
valueTV[i].setWidth(300);
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
linearLayout_Skills.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
linearLayout_Skills.setBackgroundColor(getResources().getColor(R.color.blue));
if(i>=1)
{
lparams.addRule(RelativeLayout.END_OF, valueTV[i-1].getId());
valueTV[i].setLayoutParams(lparams);
}else {
lparams.addRule(RelativeLayout.ALIGN_PARENT_START);
valueTV[i].setLayoutParams(lparams);
}
linearLayout_Skills.addView(valueTV[i]);
}
XML code:
<RelativeLayout
android:id="#+id/linearSkills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:paddingTop="5dp"
</RelativeLayout>
You can achieve this using Relative layout
So this is the main idea , first you define your relation layout
//layout variable is your relative layout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
Then you define a param variable like this
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
After that you define your textView with an id (in your case this id can be its position in the array)
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
The next textView will be declared like this
TextView tv2 = new TextView(this);
params1.addRule(RelativeLayout.BELOW, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
Finally you set your view using the params you defined
layout.addView(tv2, params1);
Here is a complete example you can check answer by #AndiM

How to remove a TextView that was added dynamically

So I have a snippet of code where I create a textView on a user's click on a button. Since it was created after runtime I'm unsure what the ID is. But what I'm trying to do is have the option of deleting the last textView that is added and also clear all of the TextViews that were added. Thank you
private TextView createNewTextView(String text)
{
ArraySize++;
final LinearLayout mLayout=findViewById(R.id.linearLayout);
String newLine=System.getProperty("line.separator");
final LinearLayout.LayoutParams lparams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams params=(RelativeLayout.LayoutParams) mLayout.getLayoutParams();
final TextView textView=new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New texT:: "+text+newLine);
listOfNames.add(text);
return textView;
}
you could create a XML File in this path res/values/ids.xml;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="view_1" type="id"/>
</resources>
then in Java you add the id, like this
textView.setId(R.id.view_1);
and when you want to remove it
LinearLayout mLayout=findViewById(R.id.linearLayout);//to find in this specific view
TextView textView = (TextView)mLayout.findViewById(R.id.view_1);
mLayout.removeView(textView);
You use removeViewAt with the index
final LinearLayout mLayout=findViewById(R.id.linearLayout)
mLayout.removeViewAt(mLayout.getChildCount()-1); // get the last view
//or using index counter variable
//mLayout.removeViewAt(ArraySize-1); // adjust the value accordingly
or you can fetch the view and remove it using removeView
final LinearLayout mLayout=findViewById(R.id.linearLayout)
View v = mLayout.getChildAt(mLayout.getChildCount()-1);
mLayout.removeView(v);
As mentioned in this question, you can simply use
((ViewGroup) textView.getParent()).removeView(textView);
To remove all views:
mLayout.removeAllViews();
To remove last TextView:
mLayout.removeView(mLayout.getChildAt(mLayout.getChildCount()-1));
You can give an Id as edgar mentions or use the View.setTag , you can then find views by tag using viewGroup.findViewWithTag.

How to set the align parent right attribute of TextViews in relative layout programmatically

I have a relative layout which I am creating programmatically:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Now I have two TextViews which I want to add in this relative layout. But the problem is both TextViews are being shown on the left of the RelativeLayout overlapping on each other.
textViewContainer.addView(textView1);
textViewContainer.addView(textView2);
Now I want to know how can I programmatically set the the
`android:layout_alignParentRight="true"`
or
`android:layout_toLeftOf="#id/textView"`
attribute of TextViews as we do in the xml?
You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using
RelativeLayout.LayoutParams#addRule(int verb) and
RelativeLayout.LayoutParams#addRule(int verb, int anchor)
You can get to it via code:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
textView.setLayoutParams(params); //causes layout updat
Use the method addrule
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
You need to align LayoutParams to each view:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Then add appropriate rule to each view:
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
to the first one and
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
to the second one.
then apply it to the view:
view.setLayoutParam(params)

Text View not displaying (Android Studio)

So I'm having trouble getting a TextView to appear programmatically with Java. Here's the code:
LinearLayout layout=new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx = new TextView(this);
tx.setText("Hello World");
layout.addView(tx);
Are you ever setting the content view for the current activity? The LinearLayout you're dynamically creating doesn't appear to ever be displayed.
Do something like this after you create it (or create the linear layout in your XML first, then dynamically add the text box):
setContentView(layout)

Android ImageView Layout Issues

There very well may be a duplicate question, but I have yet to find it. I am doing thing all programmatically, not using the xml. Basically what I am trying to do is to have an EditText appear below an image. I am using RelativeLayout with an ImageView and and EditText.
These are the parameters that I am setting up for the ImageView and EditText:
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
Which I have verified correctly places the EditText in the bottom right corner and the image above. What I run into is if the picture it "too tall" then it covers the EditText. I also tried using
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
and it stretches it over the EditText as well.
The full code that I am using is this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
EditText editText = new EditText(this);
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
imageView.setLayoutParams(imageParams);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
editText.setLayoutParams(editTextParams);
Bitmap image = getImage();
imageView.setImageBitmap(image);
layout.addView(editText);
layout.addView(imageView);
Thanks. Any suggestions would be greatly appreciated.
Create a LinearLayout and then add the two components in it (ImageVIew and EditText)
Here is what you should do;
Set the orientation to vertical for the horizontal layout and width to whatever you need
Set the with of both components to 0 and weights to 1 each
After that, you should have the two items one above the other;
I hope this helps
One thing you can do is fix the imageview width and height. This way you can control the maximum size without the imaging going over the edit text.
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(256, 256);
Hopefully this helps.
Your editText has a vertical MATCH_PARENT. Shouldn't it be WRAP_CONTENT?
(I am not sure if this helps, but it might help, as the MATCH_PARENT contradicts your planned layout, and your screenshot indicates that the editText is vertically centered.)

Categories