Not removing the padding around button text - android

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);

Related

Remove unwanted padding around the background image from a button

This is the code used to generate the button
Button delBtn = new Button(activity);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
delBtn.setLayoutParams(params);
delBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_action_cancel, 0);
delBtn.setBackgroundColor(Color.rgb(208, 0, 0));
Result Looks like this:
Whereas I expect:
`
What is it that I missed?
You can use Imageview for this purpose as button has some padding value. And if the image has a background, you can make it transparent by:
imageButton.setBackgroundColor(android.R.color.transparent);
You can try this:
Try Using ImagView instead of Button.
Set your image in "src" attribute.
Set background null/transperant.
As, suggested by Mansi (as comment, above), a simple TextView did the work!

Setting the text of a programatically created button

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

android - How to set the button params in the Listview

I added the Button has footer view of the List view pro-grammatically. Now i want to set the params of the button and set the button gravity as center vertical. I did in the following way but it's not working.How to implement this?
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
You currently have
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
I assume this is an oversight and you meant CENTER_VERTICAL? This will vertically align the text inside the button which it should already be doing so your question seems confusing. Are you instead asking how to align the button itself vertically? If so you need to set the gravity on the buttons parent.
As for the params, you are already setting layout params. What else are you trying to do? You should aim to be more specific in your questions so it is easier to identify what you are trying to do.
Try this:
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(params);
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(btnMoreUpcomingExits);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
Check this this it might help you.
Check this link
you might use WindowManager.LayoutParams to set attribute of button in listview.

Add button to bottom of linear layout programmatically

I have a bunch of buttons displayed using the default gravity. The last button I add to the LinearLayout view is what I'd like to appear at the bottom of the view. How do I add it programmtically to appear at the bottom of the screen? I've tried setting the gravity, but everything falls to the bottom. I just want the one button to fall to the bottom of the screen. Ideally, I won't have to make another view.
Try this:
Button button = new Button(this);
youLinearLayout.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
edit
sorry, the code above dont work.
You cant do this using an single LinearLayout if orientation==vertical.
You'll need create another layout(RelativeLayout) and add TextView to it.
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(textView, params);
linearLayout.addView(relativeLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
If the rest of the screen is not empty, you can give android:layout_weight=0.0 to the Button, and 1.0 to the widget on the top.
In that way, widget with 1.0 weight will expand to fill empty areas, and Button with 0.0 will take only the default space, and being the last item addes to a vertical LinearLayout, it will be sticked to the bottom.

Aligning views at runtime in RelativeLayout

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 :)

Categories

Resources