I want to add a button widget to a linear layout dynamically but it does not seem to work. In my sum_example.xml file I already have added three widgets in a linear layout that is the root layout.
//Finding the layout
LinearLayout linear = (LinearLayout) findViewById(R.id.sum_example_root);
//Creating a widget
Button button = new Button(this);
//Setting the required parameters of widget
button.setLayoutParams(new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//Setting optional parameters of widget
button.setId(R.id.added_btn_1);
button.setText("I am added");
button.setTextColor(0x000000);
button.setBackgroundColor(0x00ff00);
button.setOnClickListener(this::sum);
//Adding the widget to the layout
linear.addView(button);
You are on the right track but the Problem is your Colors arguments.
button.setText("I am added");
button.setTextColor(Color.BLACK);
button.setBackgroundColor(Color.MAGENTA);
Try using Color Class Constants instead as I have done above.
//You don't need this
button.setId(R.id.added_btn_1);
Also don't add set an Id on a button since you are not adding it to the View Hierachy. The name identifier should suffice
Related
I am trying to make a game with multiple levels. My plan is to print 15 levels on 1 page. I have attached a photo of the simple design I am currently using.
As you can see in the picture the right border of a button is not showing. For the parent I am using this java code:
LinearLayout horl = new LinearLayout(this);
horl.setId(worldVar);
horl.setOrientation(LinearLayout.HORIZONTAL);
horl.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
And to set 3 buttons horizontally in this LinearLayout I use this code for the buttons. This code just has a simple for loop around to put 3 buttons in this LinearLayout:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(getMarginInDp(100), getMarginInDp(100));
params.setMargins(getMarginInDp(1), getMarginInDp(25), getMarginInDp(1), 0);
button.setLayoutParams(params);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);horl.addView(button);
The function getMarginInDp looks like this:
public int getMarginInDp(int dp){
return (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
Does anybody have any idea how to make the border appear because I want to keep the buttons approximately this size and I may even slightly increase the margins?
You can set the weight to 0.33f for each button:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getMarginInDp(100), getMarginInDp(100), 0.33f);
so they are distributed equally in the width of the layout.
I have a few issues with setting LayoutParams and other parameters of my layouts/views programmatically. I cannot specify these in a XML layout file because whether they appear depends on the data held in the database.
The following is a function I use to create a new "Section" which consists of a FrameLayout with its children being View and TextView:
public FrameLayout createSection(long id, String name) {
FrameLayout frame = new FrameLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(15, 15, 15, 15);
frame.setLayoutParams(params);
View view = new View(this);
LayoutParams viewParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
view.setLayoutParams(viewParams);
view.setId(toIntExact(id));
view.setBackgroundResource(R.color.colorButton);
frame.addView(view);
TextView text = new TextView(this);
LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
textParams.setMarginStart(15);
text.setGravity(Gravity.CENTER);
text.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_START);
text.setTextColor(getResources().getColor(R.color.colorTextSecondary));
text.setText(name);
frame.addView(text);
return frame;
}
The parent of this newly created FrameLayout is LinearLayout and so based on the other similar questions on StackOverflow I figured setting parameters for FrameLayout should be done through LinearLayout.LayoutParams. However, this does not make a change. The initial XML page contains this:
Initial XML page
The first "SECTION" is created in the XML file, and the other two are created through 'createSection' function. This is the outcome: Design outcome
The issue is that the margins are not set properly and the TextView doesn't seem to care about the Gravity + TextAlignment combination that I'm using.
I would appreciate any help that I could get to resolve this issue.
I apologise for wasting anyone's time. The code seems to work and the margin sizes are different due to these being set in terms of pixels (px) rather than dp as it is in the XML file.
I also forgot to add text.setLayoutParams(textParams); to the TextView object.
I am developing an app related to hindi kavita(poems). I want the poems to be displayed in the way real poems are displayed like the image shown below
Now the problem is I dont know how to use a textview to show this kind of text
Use a linear layout with vertical orientation. Add a text view for each line, with layout_width="match_parent" and the appropriate gravity attribute.
https://developer.android.com/reference/android/widget/LinearLayout.html
EDIT:
If you have your poem as an arraylist of strings where each element is a line of your poem, you can do:
//Initialise your layout in your activity onCreate()
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOrientation(LinearLayout.VERTICAL);
//Start listening to your firebase data and put this somewhere in the callback:
// poemLines is a list of Strings you get from firebase
for(i=0; i<poemLines.size(); i++){
TextView view = new TextView(context);
view.setText(poemLines.get(i));
//set any other attributes to your textview that you want, width, height, font, etc
view.setGravity(i%2==0?END:START);
layout.add(view);
}
I have written is code below, but button isn't shown because i've set FILL_PARENT in bar layoutParametrs. How i could fixed it without using XML?
TableRow myLayout = new TableRow (this);
myLayout.setAlwaysDrawnWithCacheEnabled(true);
TextView text = new TextView (this);
text.setText("Test: ");
SeekBar bar = new SeekBar (this);
ToggleButton button = new ToggleButton (this);
myLayout.addView( text , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
myLayout.addView( bar , new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
myLayout.addView( button , new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
If FILL_PARENT set for bar
If isn't
both variant is wrong
When I need to fill a row with multiple items, I typically use layout_weight and a width setting of fill_parent. That distributes the full width of the row by the proportions for which you set the weight values.
Since you're doing this in code and not xml, look at the constructor for LayoutParams that takes a third parameter. This corresponds to layout_weight:
LayoutParams - int, int, float
Try using WRAP_CONTENT and a weight parameter like this.
myLayout.addView( text , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,5));
myLayout.addView( bar , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,10));
myLayout.addView( button , new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,85));
I'm having a hard time getting two text views to appear on top of each other in my java code. Here's the code I'm experimenting with:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
This stacks the two text views on the same line, but I want TextView text2, to appear below TextView text1, so in my app I want the following to appear as the output:
1
2
I've tried all sort of things with the "addRule" method, I'm not sure why this isn't working. I want to know how to do this without XML because I plan to build a library of methods that can build up a layout that is easily adjustable through editing an array.
Your TextViews don't have an id (by default the id is -1)... put this after their initialization:
text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique
I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.
EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.
So here:
text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);
you can use xml layout for this :
in relative layout
u set the first textview and assign it some id
fot the next text view we can assign parameter
android:layout_below="id of above text view"
in this way we get 2nd text view below 1st text view