I'm using ShowCaseView library in my app. And I want to move the 'OK' button to the left.
I used this code (the same in the library):
// The following code will reposition the OK button to the left.
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lps.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
int margin = ((Number) (getResources().getDisplayMetrics().density * 12))
.intValue();
lps.setMargins(margin, margin, margin, margin);
View showcasedView = findViewById(R.id.ib_next);
ViewTarget target = new ViewTarget(showcasedView);
ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
co.buttonLayoutParams = lps;
co.fadeInDuration = 1000;
co.fadeOutDuration = 1000;
ShowcaseView sv = ShowcaseView.insertShowcaseView(target, this,
R.string.showcase_title, R.string.showcase_details, co);
But it doesn't work? Can anyone tell Where is the problem?
I know the question is pretty old, but in case someone needs a working solution:
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// This aligns button to the bottom left side of screen
lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lps.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Set margins to the button, we add 16dp margins here
int margin = ((Number) (getResources().getDisplayMetrics().density * 16)).intValue();
lps.setMargins(margin, margin, margin, margin);
ViewTarget target = new ViewTarget(R.id.buttonBlocked, this);
sv = new ShowcaseView.Builder(this)
.withMaterialShowcase()
.setTarget(target)
.setContentTitle(R.string.showcase_main_title)
.setContentText(R.string.showcase_main_message)
.build();
// Set declared button position to ShowcaseView
sv.setButtonPosition(lps);
This worked for me, hope it helps somebody.
Related
I've tried with below code
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
dTextView.setLayoutParams(new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView);
still, it is not exact size what I want. These things are the same for CheckBox view as well. I need to draw this view with an exact pinpoint position with size.
Try this way:
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int) measureWidth, (int)measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView, params);
Why do you need to do it like this.
You could create a textview component and add it programmatically to your UI with the help of ListView, Recyclerview etc.
Finally, I've created all views (Checkbox, EditText, and TextView) with exact width and height. EditText and TextView was an easy solution:
dEditText = new EditText(getContext());
dEditText.setPadding(10,5,10,5);
dEditText.setTextSize(6);
dEditText.setTextColor(Color.WHITE);
dEditText.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dEditText.setLayoutParams(params);
rootView.addView(dEditText);
setMargin(dEditText, item,params);
// set margin of every created view
private void setMargin(View view, TagsItem item,RelativeLayout.LayoutParams layoutParams) {
layoutParams.setMargins(item.getCurrentXPos(), item.getCurrentYPos(), 0, 0);
// Apply the updated layout parameters to TextView
view.setLayoutParams(layoutParams);
}
But when I try to create CheckBox with exact width and height with this procedure it didn't create a view with exact width and height. I need to follow the below code[Need to set the background and set null for setButtonDrawable ]
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {Color.BLACK, Color.BLACK};
dCheckBox = new CheckBox(getContext());
CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors));
dCheckBox.setChecked(item.isCheckState());
//dCheckBox.setText("");
dCheckBox.setBackgroundResource(R.drawable.check_box_selector);
dCheckBox.setButtonDrawable(null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dCheckBox.setLayoutParams(params);
// checkBoxLayout.addView(dCheckBox);
rootView.addView(dCheckBox);
setMargin(dCheckBox, item,params);
the thing which I want to achieve in my application is to create dynamically a set of buttons with different text size and after that align these view inside a linear layout. Here is a sample of what I want to achieve :
. Depending on the textsize I want the buttons to be align in a single line, if the last button is bigger than the screen size, it should properly go to the next line.
Any suggestions how can I achieve this or what should I look for, because I know that if I try button.getWidth(); it will return 0 and it won't work.
Thanks for any kind of suggestions!
EDIT
To achieve this you can use a new layout open sources by Google called: FlexboxLayout. You can learn more about it in this post from Android Developers Blog.
Thanks to #Kameswari 's code which gave me the right direction I achieve this by using :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
String[] mKeywordsArray = mKeywords.split(", ");
if(mKeywordsArray != null){
LinearLayout mNewLayout = new LinearLayout(getActivity()); // Horizontal layout which I am using to add my buttons.
mNewLayout.setOrientation(LinearLayout.HORIZONTAL);
int mButtonsSize = 0;
Rect bounds = new Rect();
for(int i = 0; i < mKeywordsArray.length; i++){
String mButtonTitle = mKeywordsArray[i];
Button mBtn = new Button(getActivity());
mBtn.setPadding(10, 3, 10, 3);
mBtn.setText(mButtonTitle);
Paint textPaint = mBtn.getPaint();
textPaint.getTextBounds(mButtonTitle, 0, mButtonTitle.length(), bounds);
// size of all buttons in current horizontal layout
// i am using +45 because of extra padding which is set in xml for this layout
mButtonsSize += ( bounds.width() + 45);
if(mButtonsSize < (mScreenWidth - 32)){ // -32 because of extra padding in main layout.
mNewLayout.addView(mBtn, params);
} else {
mButtonsLayout.addView(mNewLayout);
mNewLayout = new LinearLayout(getActivity());
mNewLayout.setOrientation(LinearLayout.HORIZONTAL);
mButtonsSize = bounds.width();
mNewLayout.addView(mBtn, params); // add button to a new layout so it won't be stretched because of it's width.
}
}
mButtonsLayout.addView(mNewLayout); // add the last layout/ button.
}
You can try the following
While adding the buttons you can calculate the how much width till occupied as
occupiedWidth = button1Width + button2Width + ...
Every button width = textWidth + 2*margin + 2*padding
You can get your width of the text which you put on the button like
String buttonText = "<Your Button Text>"
paint.getTextBounds(buttonText, 0, buttonText.length(), bounds);
int textWidth = bounds.width();
Add this new button margins and paddings as
int newWidth = textWidth + buttonMargin*2 + buttonPadding*2;
if the total width is exceeding the screenwidth then move to next row.
According to me, you have 2 options:
1) Take a linear layout with horizontal orientation.
and keep adding buttons to this layout programmatically.
if the width of the button is not able to fit in the line, it will automatically go to next line in linear layout.
ViewGroup linearLayout = (ViewGroup) findViewById(R.id.linearLayoutID);
Button bt = new Button(this);
bt.setText("A Button");
bt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linerLayout.addView(bt);
2) You can calculate x,y location of each button as mentioned by #kameswari and draw corresponding button at x,y location
Button button = (Button)findViewById(R.id.button);// or new Button(this);
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams)button.getLayoutParams();
absParams.x = X;
absParams.y = Y;
button.setLayoutParams(absParams);
u can try this `enter code here`
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50;
Button btn1 = new Button(this);
btn1.setText("Button1");
linLayout.addView(btn1, leftMarginParams)
I created a Dialog using LinearLayout and in this Dialog, I have a Button. I want to set its width to 50% of the Dialog's width, but no matter what I do, the button is still filling the entire width. The main problem seems to be that I cannot set the button's weight programmatically. I tried creating a second horizontal LinearLayout that would hold the button with weight set to 0.5f (the parent has a weightSum of 1), but that did not change anything. How can I set the button to be half the width?
I've looked at all the other questions but most of them were using xml so they were not relevant, and the one that solved it programmatically was using hardcoded values in pixels which is obviously an incorrect solution to the problem.
EDIT: Adding some code
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setWeightSum(1);
// I use these params when I call addContentView
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
testButton = new Button(mContext);
testButton.setText("Test");
testButton.setId(1);
testButton.setWidth(0);
testButton.setGravity(Gravity.CENTER);
LinearLayout buttonLayout = new LinearLayout(mContext);
butonLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
testButton.setLayoutParams(buttonParams);
buttonLayout.addView(testButton);
linearLayout.addView(buttonLayout);
...
this.addContentView(scrollView, layoutParams);
As I tried more and more options, the code became a mess. I decided to start from scratch and this code worked in the end:
LinearLayout forButton = new LinearLayout(mContext);
forButton.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams forButtonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
forButton.setGravity(Gravity.CENTER);
DisplayMetrics dm = new DisplayMetrics();
this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
forButton.addView(testButton);
testButton.getLayoutParams().width = width/2;
Are you setting the width value to 0dp as well? This is required for the layout engine to automatically to use layout weight respectively.
Edit
I think this line will get you the results you are looking for:
LayoutParams buttonParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
To find dialog width
DisplayMetrics dm = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
To Set Value programmatically
final LayoutParams lparams = new LayoutParams(Height,width/2); // Width , height
final Button button = new Button(this);
button.setLayoutParams(lparams);
EDIT
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 200;
lp.height = 200;
alertDialog.getWindow().setAttributes(lp);
I am developing an UI(Home screen) just like this screen
given in this link
http://www.flickr.com/photos/78431491#N07/7023063473/
-all information are comming from xml which is available on server means
1- number of button on screen
2- size, background images of screen and buttons,text on button each and everythings coming from
server
so we can't use xml for creating layout.I have been comleted tabbar and when I am creating buttons that is not taking any properties code used by me is as-
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
int id = getResources().getIdentifier("com.correlation.edumationui:drawable/" + img, null, null);
intent = new Intent().setClass(this, HomeScreen.class);//HomeScreen.class);
Bundle objbundle = new Bundle();
objbundle.putSerializable("screen", mscreen);
intent.putExtras(objbundle);
spec = tabHost.newTabSpec("title").setIndicator(title,getResources().getDrawable(id))
.setContent(intent);
tabHost.addTab(spec);
for creating button i am using this code--in homescreen Activity( HomeScreen.class)
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
Button btn = new Button(this);
btn.setText("A");
btn.setHeight(30);
btn.setWidth(224);
btn.setPadding(10,10,10,10);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
give me your valuable help....
Instead of
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn, lp);
Use
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(30, 224); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn,lp);
to reflect the width and height of buttons in layout parameters.
Use LayoutParameters to set width, height and margins on button.
Try setting Height, Width and Padding using setLayoutParams as:
Button btn = new Button (this);
LayoutParams params = Prams // Set Params Which you want to set for your view.
btn.setLayoutPrams(params);
Hope this will help you.
Set Height and width programmatically, you have use set layoutparams like below.
LinearLayout ly = (LinearLayout) findViewById(R.id.verify);
Button buyButton = new Button(this);
buyButton.setText("button");
buyButton.setLayoutParams(new LayoutParams(50,
50));
buyButton.setPadding(10, 10, 10, 10);
ly.addView(buyButton);
Is there an easy and simple way to set LayoutParams ? Or to be exactly, the MarginLayoutParams? I want to set the MarginRight to a few dp, unfortunately im not able to set these in the LayoutFile cause the Target is a ListFragment and in Code-Behind looks it very ugly.
The reason i do this not in the Layout of the items is so the code is optimized and perfomant.
To sum up: Is there any very simple and clean way to set Params ?
Yes, you can do something like this:
MyImageView i2 = new MyImageView(context);
LayoutParams lp = new LayoutParams(300, 300);
lp.LeftMargin = 100;
lp.TopMargin = 100;
lp.Gravity = 0;
this.AddView(i2, lp);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
lp.setMargins( left, top, right, bottom );
You might need to write it as LinearLayout.LayoutParams, depending on what type of layout is the container.
And then you call the method setLayoutParams( lp ); on the given view/layout/widget.