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!
Related
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);
<ImageView
android:id="#+id/wipeID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/popup_window"
android:layout_alignBottom="#+id/linerID"
android:src="#drawable/wiper_btn"/>
In the above one android:layout_alignBottom="#+id/linerID" is remove programmatically how to remove that one?
If I don't get you wrong you want to change your layout parameters...
So this is roughly how you can achive it:
get your imageview with findViewById(...)
get the layout parameters somehow like this:
LinearLayout.LayoutParams params = ( LinearLayout.LayoutParams) imageView.getLayoutParams();
change them by setting the values you want:
params.setMargins(newX, newY, 0, 0); //for margins
imageView.setLayoutParams(params);
if this does not work (param for align bottom does not exist) you can add rules:
this could help or google something like "android add rule layout param"
hope this leads you to the right track...
Try using this :
imageView.setBaselineAlignBottom(false);
As title says. I need to put button to the right bottom corner what can be done in xml by:
android:layout_gravity="bottom|right"
Since I needed some margins which are changed dynamically to keep the button always at the exact part of the image I did this in java:
// get and then set button params
FrameLayout.LayoutParams buttonParams = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0, paddingTopFollowers, paddingRight, 0);
talkBtn.setLayoutParams(buttonParams);
The button is down as I wanted because there is paddingTopFollowers but it is not on the right side. Even though if I add this line: talkBtn.setGravity(Gravity.RIGHT);
Add:
buttonParams.gravity=Gravity.RIGHT|Gravity.BOTTOM;
as part of your buttonParams configuration.
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 have an ImageView created in my xml layout. I then access the ImageView in my Java with the following:
ImageView iv1 = (ImageView) findViewById(R.id.iv1);
I'm then accessing the ImageView's OnClick method. When the ImageView is clicked I would like to change it's position on the screen. In the XML side I can do this with layout_margin, but I can't figure out how that is done in the Java side.
Could anyone point me in the right direction? Thanks in advance!
You can set margins programmatically using LayoutParams, please try with following code:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
and set this layout param object to your view by using:
view.setLayoutParams(layoutParams);
i think you should change layout parameters by setLayoutParamerter method of View class see Discription.
this is not the big task, even you can add this view at any particular place by using Relative layout.