How do I add a view in relative layout at runtime, so that it aligns itself at the Top of the parent. Say I am adding an EditText on clicking the button. Now this EditText should come in such a way that EditText comes above the Button.
here is an example of what I am doing.
button = new Button(this); //here I am just checking if the button can come above the EditText
button.setText("I am a button");
params.addRule(RelativeLayout.BELOW, editText.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);
relativeLayout.addView(button);
I am not able to make the alignment at all.
Any clue is highly appreciated.
Thanks in Advance.
You need to use the overload of RelativeLayout.addView that takes your LayoutParams as a second parameter
button = new Button(this); //here I am just checking if the button can come above the EditText
button.setText("I am a button");
params.addRule(RelativeLayout.BELOW, editText.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);
relativeLayout.addView(button, params);
addView method
Dont go such complex
Instead
Step 1 : Add a Linear Layout with Vertical orientation where so ever you are adding your button
Step 2 : First Add EditText and Set its Visibility to GONE
Step 3 : Add Your Button
Step 4 : On button click simply make your button VISIBLE
This will work for sure and is easy to implement to
Hope it helps :)
Related
I am creating an android layout in which I will place 3 buttons vertically with some space between them. But based on the requirements, the button count can come down to 2, and I need the buttons to repositioned based on the available screen space.See the screens below.
now the second case with two buttons should be like
How should I go about doing this ?
You can just set visibility of buttons depending upon your condition just like the following:
button.setVisibility(View.GONE);
When you set visibility to gone then the space will considered available for other layouts and your will be set as per your desire.
When you set visibility to invisible then the space will considered as allocated.
You can try below code for achieving the same based on your condition for adding buttons to your layout
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
You can define all three buttons in the layout XML and then :
if you want to show the third button
thirdButton.setVisibility(View.VISIBLE);
if you want to hide
thirdButton.setVisibility(View.GONE);
at least if using a Linear or Relative Layout it should repositione by itself.
I have written a code to change the minWidth of a button so that I can get rid of the padding around the button text.
for(int i=0;i<26;i++){
Button alphabet = new Button(getContext());
alphabet.setText(""+(char)('A'+i));
alphabet.setMinWidth(0);
allAlphabets.addView(alphabet);
}
I am having a letter in each button and finally adding it to a layout dynamically. Due to padding, it is taking up much space, because of which I have set the minimum width to 0, but still it doesn't work. It shows the same default button.
Please help.
as you want a clickable view look like button with text
you can make a xml layout with your button or a cardview and textview inside it
then add it like this:
Button alphabetButton = LayoutInflater.from(context).inflate(R.layout.alphabet_layout);
so it's better than code design it's now a layout so you can modify the layout as you want
also you can add to your button in alphabet_layout.xml
android:background="#null"
You have to apply LayoutParams with your Button alphabet.
pls try set padding of button like
Button alphabet = new Button(getContext());
alphabet.setText(""+(char)('A'+i));
alphabet.setPadding(0,0,0,0);
allAlphabets.addView(alphabet);
You can set using below code :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setTextSize(32);
I've been making all of my views dynamically, and now I've come to the point where I want to add an EditText for people to write in.
I've been able to accomplish this for the most part, but it doesn't look right. I have a linear layout that I'm adding a relative layout to. I'm making the relative layout have a white background, then adding the EditText. Problem is, it always adds it to the direct center of the relative layout, and options to align it vertically to the top have so far failed.
I also need to be able to pull the text from it later when a separate button was pressed (I know how to make the button work, it's the pulling text from it part I'm a bit iffy on). Here's my code so far:
public void addEditText(LinearLayout L){
EditText myEditText = new EditText(c);
myEditText.setSingleLine(false);
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(scWidth1, 300);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER_VERTICAL);
l1.addView(myEditText);
L.addView(l1);
}
l1.setGravity(Gravity.CENTER_VERTICAL); places the EditText in center vertical of the parent container i.e RelativeLayout, remove that line.
Basic question regarding setting the text of a programatically created button. As seen in my code below I've done the basics in terms of creating the button but my button appears as seen in my attached image. Basically the text in the button doesn't appear as expected. Any ideas why?
Note: I've declared button as a public instance variable right above my onCreate() and has been added correctly to my relative layout using addView();
// Create User button
btnUserAdmin = new Button(this);
// Customise the UserAdmin button
btnUserAdmin.setBackgroundColor(Color.BLUE);
btnUserAdmin.setTextSize(13.7f);
btnUserAdmin.setTextColor(Color.parseColor("#FFCC00"));
btnUserAdmin.setText("USER ADMINISTRATION");
btnUserAdmin.setGravity(Gravity.LEFT);
Thanks.
You should specify the dimensions of the button, otherwise the size could be unexpected. For instance
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT );
btnUserAdmin.setLayoutParams(lp);
also, you can directly set them when you add the buttom
yourRelativeLatout.addView(btnUserAdmin, lp);
Also remember that numeric values for the dimensions (of the bottom or the layout) usually are evil. As you can, use only WRAP_CONTENT and MATCH_PARENT
I'm creating some 50 button dynamically.
Text is getting set as followed:
btn.Text=result.Rows[i]["Col1"].ToString()+"\n"+result.Rows[i]["Col2"].ToString()
+"\n"+result.Rows[i]["Col3"].ToString();
where result is DataTable & btn is object for button.
Now the problem is some of the buttons are not getting displayed appropriately.
Referring to screenshot below,
in img1 - An unnecessary blank line is getting displayed after the first row.
in img2 - Text is not center aligned.
in img3 - TATAMOTORS is not getting displayed in single line even though there is a space on either side of t he button.
Please note that I'm not setting padding which can be the reason for this.
Any idea how to solve this?
Also, how alignment of text of a button can be set programmatically?
I know that this is not the Best of the question, but after spending hours on it, I'm unable to crack it.
Any help appreciated...
You can set the gravity of the button to customize how the text is aligned. This is exposed on the button by using the Gravity property. From the docs:
Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.
The values you can assign are found in the GravityFlags enum. For example:
button.Gravity = GravityFlags.Center;
Alignment of button content can be set with .setGravity(int)
In the original question, I wonder if the source text contained some extra space characters? That would explain img1 and img2.
Anyway, my need to left-align the text in a button led me to this page, and here's the solution I ended up with, based on Alix Bloom's answer:
Button myButton = new Button(getActivity());
myButton(Gravity.LEFT);
Button myButton = new Button();
myButton.setGravity(Gravity.RIGHT); //LEFT
Hope this help you. Worked for my (i was also strugling with this issue for some time)