Programmatically RelativeLayout.ALIGN_RIGHT with a given View.getId() not working - java

i try to align a TextView programmatically to the right/bottom edge of a button.
However, this static xml code is working:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView10"
android:layout_alignRight="#+id/button"
android:layout_alignBottom="#+id/button" />
</RelativeLayout>
If i try to do it by code, it is not working.
The TextView is getting aligned top/left as it is by default.
The RelativeLayout.ALIGN_RIGHT and ALIGN_BOTTOM seems to be ignored...
// Adding the LinearLayout
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
final LinearLayout.LayoutParams LLParamsCont = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, Utils.dpToPx(getContext(), 50));
// Adding the RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(getContext());
relativeLayout.setLayoutParams(LLParamsCont);
// Adding the Button
Button button = new Button(getContext());
button.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
relativeLayout.addView(button)
// Adding the TextView
TextView textView = new TextView(getContext());
textView.setGravity(Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
Utils.dpToPx(getContext(), 20),
Utils.dpToPx(getContext(), 20));
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, button.getId()); // <== THIS DOESN'T SEEM TO WORK
layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, button.getId()); // <== THIS DOESN'T SEEM TO WORK
textView.setLayoutParams(layoutParams);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundResource(R.drawable.score_count);
relativeLayout.addView(textView);
linearLayout.addView(relativeLayout)
The TextView is just not beeing aligned to the right/bottom line of the Button.
Does anyone has an idea why that is not working by code, but with static xml?
What did i miss?
Thank you!
With best regards,
Juergen

Try to set a unique id to the Button before create the rule (more info here).
Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!

I found the solution at this post:
How to lay out Views in RelativeLayout programmatically?
I need to set an id with setId(int) to the Button, then it is working.

Related

Android Adding Buttons to Toolbar Programmatically

I am trying to create a toolbar programmatically and add left and right bar buttons without using XML.
But the button is not getting aligned to right.
Toolbar TopTulBarVar = new Toolbar(this);
TopTulBarVar.setId(View.generateViewId());
TopTulBarVar.setBackgroundColor(Color.parseColor("#DDDDDD"));
TopTulBarVar.setTitle("Ttl Txt");
TopTulBarVar.setSubtitle("Dtl Txt");
Button NamBarBtnVar = new Button(this);
NamBarBtnVar.setText("Select");
NamBarBtnVar.setBackgroundColor(Color.GREEN);
LinearLayout.LayoutParams NamBarBtnRulVar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
NamBarBtnRulVar.gravity = Gravity.RIGHT;
TopTulBarVar.addView(NamBarBtnVar, NamBarBtnRulVar);
Also tried
RelativeLayout.LayoutParams RloRulVar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RloRulVar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
I am getting as below image
But need like
In Activity code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar t = (Toolbar) findViewById(R.id.tool);
setSupportActionBar(t);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//left side button
Button b = new Button(this);
Toolbar.LayoutParams l1=new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
l1.gravity = Gravity.START;
b.setLayoutParams(l1);
b.setText("left");
t.addView(b);
//center Textview
TextView text=new TextView(this);
text.setText("Text:1");
Toolbar.LayoutParams l2 = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
l2.gravity = Gravity.CENTER;
text.setLayoutParams(l2);
t.addView(text);
//Right side button
Button b1=new Button(this);
b1.setText("Right");
Toolbar.LayoutParams l3=new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
l3.gravity=Gravity.END;
b1.setLayoutParams(l3);
t.addView(b1);
}
Toolbar XML code
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/tool"
app:contentInsetLeft="0dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
OUTPUT
For anyone struggling with this nowadays: please make sure to use androidx.appcompat.widget.Toolbar.LayoutParams while adding view programmatically as for me just typing Toolbar.LayoutParams were accidentally resolved to android.widget.Toolbar.LayoutParams by Android Studio. I spent some time trying to figure out why gravity is not changed: no exception occures if set wrond layoutParams before adding a view to the toolbar. I have androidx.appcompat.widget.Toolbar in my layout.
for me I added one switch button in Toolbar and it does work :
<Toolbar
android:id="#+id/toolbar"
style="#style/Toolbar"
android:visibility="visible"
android:background="#color/white">
<Switch
android:id="#+id/btn_accessible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_weight="1"
android:visibility="visible"
android:paddingLeft="25sp"
android:paddingRight="25sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textColor="#color/white"
android:textSize="12sp"
android:thumb="#drawable/custom_switch_thumb"
android:track="#drawable/custom_switch_track"
/>
</Toolbar>
and in activity i just set onClick listener

Aligning textview through android java

At the moment everything is done through the java code. I'm creating a relative layout then adding my GL surface view and some text views to it. the problem is I cant align one text view to the top left and the other to the top right. Whats wrong with my code. Also is there a more efficient way of doing this such as doing this through XML instead. Cause I've tried this and it didn't work.
r1 = new RelativeLayout(this);
view = new MyGLSurfaceView(this);
view.setKeepScreenOn(true);//keeps activity from going to sleep
r1.addView(view);
TextView text1 = new TextView(this);
TextView text2 = new TextView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200);
lp.addRule(RelativeLayout.ALIGN_RIGHT);
text1.setLayoutParams(lp);
text1.setText("3 Star");
text1.setBackgroundColor(0x4060ff70);
rp.addRule(RelativeLayout.ALIGN_LEFT);
text2.setLayoutParams(rp);
text2.setText("4 Star");
text1.setTextSize(30);
text2.setTextSize(30);
//text2.setBackgroundColor(0x4060ff70);
r1.addView(text1);
r1.addView(text2);
setContentView(r1);
This can be done in XML like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RelativeLayout>

android Layout_gravity (not gravity) and Layout_marginTop(not marginTop) programmatically

I need to programmatically create a Toast that use an ImageView and a TextView, and that appears in the middle of the screen, and I have done it.
THIS IS THE RENDER I WANT
(The black square with the heart is the ImageView image, and the "J'aime" is the TextView)
XML Code :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/toastImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:src="#drawable/like" />
<TextView
android:id="#+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
android:text="J'aime !"
android:textColor="#FFFFFF" />
</FrameLayout>
THIS IS THE RENDER I HAVE with JAVA code :
JAVA CODE :
FrameLayout toastLayout = new FrameLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
I have tried with a relativelayout or a linearlayout, but the framelayout works better for this.
So I have a question :
What is the JAVA equivalent of :
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
? Because this is visibly not :
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
Need help.
Try this:
RelativeLayout toastLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
RelativeLayout.LayoutParams toastTextLp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastTextLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
Try this...
LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsOfAnim.gravity = Gravity.center_horizontal;
paramsOfAnim.topMargin = 85;
Let's take that A contains B and B contains C. You set B.layoutParams for describing the placement of B in the A, not for placement of items inside B, such as C.
The other thing is that layout parameters are complex enough, it is difficult not to forget something, and you don't need really to set all parameters by hand. So, it is better to read them, change and set back.
As here.

How to set all elements of linear layout to be same width in android?

In my android app, I want to create a linear layout of 1 row and 3 columns. Then in each cell, add a new linear layout with 1 column and 2 rows.
Currently it does that, but the width of each of the three cells are wrapping the content, so the overall width of the main linear layout is smaller than the screen width. I want it to match the screen width and then horizontally center the contents of the content of each of the three cells.
Does anyone know how to fix this?
Thanks
This is the code I have
LinearLayout layout = new LinearLayout(this);
layout.setWeightSum(1);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// ---------
LinearLayout Celllayout = new LinearLayout(this);
Celllayout.setOrientation(LinearLayout.VERTICAL);
Celllayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView titleView = new TextView(this);
titleView.setLayoutParams(lparams);
titleView.setText("Use This");
RadioButton useMapRadio = new RadioButton(this);
Celllayout.addView(titleView);
Celllayout.addView(useMapRadio);
layout.addView(Celllayout);
// ---------
Celllayout = new LinearLayout(this);
Celllayout.setOrientation(LinearLayout.VERTICAL);
Celllayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
titleView = new TextView(this);
titleView.setLayoutParams(lparams);
titleView.setText(date);
ImageButton imagebutton = new ImageButton(this);
imagebutton.setImageResource(R.drawable.calendar);
imagebutton.setBackgroundResource(0);
Celllayout.addView(titleView);
Celllayout.addView(imagebutton);
layout.addView(Celllayout);
// ---------
Celllayout = new LinearLayout(this);
Celllayout.setOrientation(LinearLayout.VERTICAL);
Celllayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
titleView = new TextView(this);
titleView.setLayoutParams(lparams);
titleView.setText("Delete");
imagebutton = new ImageButton(this);
imagebutton.setImageResource(R.drawable.x);
imagebutton.setBackgroundResource(0);
Celllayout.addView(titleView);
Celllayout.addView(imagebutton);
layout.addView(Celllayout);
// ---------
linearlayout.addView(layout);
This is 3 textView in horizontal layout, the main idea is : width of all items in
row must be ==0, and weigth for all items must be the same
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/start_training"
android:textColor="#84d9f4"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/upd_params"
android:textColor="#84d9f4"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="df"
/>
</LinearLayout>
Use Styles, it works like CSS :: Styles in Android
And yeah, it would be better to make layout's in XML

How to write this xml in java code

Android XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_height="28dp" android:layout_width="180dp"
android:layout_toRightOf="#+id/signinemailtxt"
android:paddingLeft="50dp"
android:layout_marginTop="65dp"
android:background="#ffffff"
/>
Java:
layout= new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TextView Txt=new TextView(this);
Txt.setText("Name");
How to get layout_marginTop,layout_toRightOf options in java code
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
For layout_margin :
LayoutParams params = new LayoutParams(context, attrs);
params.setMargins(0, 5, 0, 5); //setMargins (int left, int top, int right, int bottom)
Txt.setLayoutParams(params);
Don't declare new instances of layout and text view, but get the ones you created in the xml:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.LayoutId);
TextView Txt = (TextView) findViewById(R.id.TextViewId);
Now you can edit the layout and textview you are seeing on the screen.

Categories