How to center ListView footer horizontally - android

I've added a footer to my ListView like so:
...{
listView.addFooterView(getButtonFooter(context));
...}
private View getButtonFooter(Context context) {
Button button = new Button(context);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
button.setId(ADD_REMINDER);
button.setOnClickListener(this);
return button;
}
I'm trying to figure out how I can now center the button horizontally.
EDIT: oh, maybe like this? button.setGravity(Gravity.CENTER_HORIZONTAL);
EDIT: lol, nope. That didn't seem to work. Guess that just center's the text inside the button...

LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
button.setId(ADD_REMINDER);
button.setOnClickListener(this);
layout.addView(button);
return layout;

Related

Adding a dynamic view - Layout gravity problem

I am adding a dynamic layout view which looks like this.
I am appending this view on button click. Here is the code for this view on button click
public void addDynamicView() {
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lHori = new LinearLayout(this);
lHori.setOrientation(LinearLayout.HORIZONTAL);
//lHori.setWeightSum(3);
lHori.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lVertical = new LinearLayout(this);
lVertical.setOrientation(LinearLayout.VERTICAL);
//lVertical.setWeightSum(2);
lVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)));
button.setVisibility(View.VISIBLE);
button.setText("button");
TextView txtEmpName = new TextView(this);
txtEmpName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
txtEmpName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpName.setText("txt1");
TextView txtEmpDropTime = new TextView(this);
txtEmpDropTime.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpDropTime.setText("txt2");
lVertical.addView(txtEmpName);
lVertical.addView(txtEmpDropTime);
lHori.addView(lVertical);
lHori.addView(button);
relativeLayout.addView(lHori);
linear.addView(relativeLayout);
}
The problem is I want all the buttons to be placed below the click button.
But I get output like this
How can I place all Button with text Button below the Click button?
If you two do with xml than it's so simple just make a layout which you want after click on click button after that set visibility gone for all layout accept click button next in Java on button click you can call visibility view to make that layout ...
Hope so it will works for you..
You can create a LinearLayout lButton:
LinearLayout lButton = new LinearLayout(this);
lButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
then set its gravity as Gravity.END
lButton.setGravity(Gravity.END)
and add the button into it
lButton.addView(button);
lHori.addView(lVertical);
lHori.addView(lButton);

How to make two buttons on the same row in eclipse programmatically?

LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
btnControl = new Button(this);
btnControl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnControl.setPadding(0,40,0,40);//x,y,w,h
btnControl.setBackgroundColor(Color.parseColor("#1C344E"));
btnControl.setTextColor(Color.WHITE);
setMargins(btnControl, 50, 50, 50, 50);
btnLogs = new Button(this);
btnLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
btnLogs.setPadding(0,40,0,40);//x,y,w,h
btnLogs.setBackgroundColor(Color.parseColor("#1C344E"));
btnLogs.setTextColor(Color.WHITE);
setMargins(btnLogs, 50, 50, 50, 50);
btnLogs.setId(5);
btnLogs.setOnClickListener(this);
btnTermLogs = new Button(this);
btnTermLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
btnTermLogs.setPadding(0,40,0,40);//x,y,w,h
btnTermLogs.setBackgroundColor(Color.parseColor("#CC6675"));
btnTermLogs.setTextColor(Color.WHITE);
setMargins(btnLogs, 50, 50, 50, 50);
btnTermLogs.setId(6);
btnTermLogs.setOnClickListener(this);
btnLogout = new Button(this);
btnLogout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnLogout.setPadding(0,40,0,40);//x,y,w,h
btnLogout.setBackgroundColor(Color.parseColor("#CC6675"));
btnLogout.setTextColor(Color.WHITE);
table.addView(btnLogs);
table.addView(btnTermLogs);
I want the two buttons to be aligned together horizontally. However it just align themselves vertically which i dont want.
My buttons look like this:
what i want is to look like this:
Try this below function. It will help
public void addButtons(){
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams LayoutParamsview = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(this);
button1.setText("Btn 1");
button1.setLayoutParams(LayoutParamsview);
linearLayout1.addView(button1);
table.addView(linearLayout1);
//
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
Button button2 = new Button(this);
button2.setText("Btn 2");
button2.setLayoutParams(LayoutParamsview);
Button button3 = new Button(this);
button3.setText("Btn 3");
button3.setLayoutParams(LayoutParamsview);
linearLayout2.addView(button2);
linearLayout2.addView(button3);
table.addView(linearLayout2);
//
LinearLayout linearLayout3 = new LinearLayout(this);
linearLayout3.setOrientation(LinearLayout.VERTICAL);
Button button4 = new Button(this);
button4.setText("Btn 4");
button4.setLayoutParams(LayoutParamsview);
linearLayout3.addView(button4);
table.addView(linearLayout3);
}
Horizontal linear layout supports left to right widgets approach, When
app developer selects horizontal orientation in linear layout then it
will automatically start adding components and widgets to left to
right side after adding them. So here is the complete step by step
tutorial for Creating horizontal linearlayout programmatically
android.
Write this code
//Creating LinearLayout.
LinearLayout Horizontallinearlayout = new LinearLayout(this);
//Setting up LinearLayout Orientation
Horizontallinearlayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams Horizontallinearlayoutlayoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
setContentView(Horizontallinearlayout, Horizontallinearlayoutlayoutparams);
LayoutParams LayoutParamsview = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Creating textview .
Button button1 = new Button(this);
Button button2 = new Button(this);
//Adding text to TextView.
button1.setText("First Button");
button2.setText("Second Button");
button1.setLayoutParams(LayoutParamsview);
button2.setLayoutParams(LayoutParamsview);
//Adding textview to linear layout using Add View function.
Horizontallinearlayout.addView(button1);
Horizontallinearlayout.addView(button2);
Hope this helps you
You should use setLayoutParams
Add setOrientation(LinearLayout.HORIZONTAL);
Set the layout parameters associated with this view. These supply
parameters to the parent of this view specifying how it should be
arranged. There are many subclasses of ViewGroup.LayoutParams, and
these correspond to the different subclasses of ViewGroup that are
responsible for arranging their children.
Code Structure
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
table.setOrientation(LinearLayout.HORIZONTAL);
Button Btn1 = new Button(Activity.this);
Btn1.setText("Button 1");
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam.weight = 0.5f;
Btn1.setLayoutParams(childParam);
Button Btn2 = new Button(Activity.this);
Btn2.setText("Button 2");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.5f;
Btn2.setLayoutParams(childParam2);
table.addView(Btn1);
table.addView(Btn2);
Respected OP
You can create another XML file, that contains a horizontal LinearLayout, then inflate it and insert the two buttons into it, and then add the whole horizontal LinearLayout to the vertical LinearLayout:
Create an XML file e.g. horizontal_layout.xml:
<LinearLayout
android:id="#+id/horizontal_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
// You will insert the buttons programmatically.
</LinearLayout>
In your Activity use this method to inflate the Layout:
LinearLayout verticalLinearLayout = rootView.findViewById(R.id.vertical_linear_layout);
// Add the first vertical Button regularly.
Button firstVerticalButton = new Button(Activity.this);
firstVerticalButton.setText("Vertical");
verticalLinearLayout.addView(firstVerticalButton);
// Inflate the horizontal LinearLayout and add the two horizontal buttons.
LinearLayout horizontalLinearLayout = getLayoutInflater().inflate(R.layout.horizontal_layout,
linearLayout, false);
Button horizontalOne = new Button(Activity.this);
horizontalOne.setText("Horizontal");
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
horizontalOne.setLayoutParams(childParam);
Button horizontalTwo = new Button(Activity.this);
horizontalTwo.setText("Horizontal");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
horizontalTwo.setLayoutParams(childParam2);
horizontalLinearLayout.addView(horizontalOne);
horizontalLinearLayout.addView(horizontalTwo);
// Insert the inflated layout to the main layout.
verticalLinearLayout.addView(horizontalLinearLayout);
// Add the second vertical Button
Button secondVerticalButton = new Button(Activity.this);
secondVerticalButton.setText("Vertical");
verticalLinearLayout.addView(secondVerticalButton);
use table.setOrientation(LinearLayout.HORIZONTAL); .
Hope it Helps.

Set Button on Runtime

I am creating button on runtime like below...
Button newCategoryButton = new Button(this);
newCategoryButton.setText(catName);
newCategoryButton.setWidth(30);
newCategoryButton.setBackgroundResource(R.drawable.bnt);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(newCategoryButton, lp);
But my problem is that it disappear when I back from that activity.
I want this permanent. And another problem is that how to set this button below on other button??
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
Button newCategoryButton=new Button(this)
newCategoryButton.setText("Push Me");
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ll.addView(view, newCategoryButton);
You should put this code in the onResume(), not on the onCreate() method ;-)
If you want to use the button under another one, you will need to use a LinearLayout with a vertical orientation!
LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout);
myll.setLayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myll.setOrientation(LinearLayout.VERTICAL);
myll.addView(view, Button1);
myll.addView(view, Button2);
setContentView(myll)

How to declare wrap content to a dynamic button in code

Here is my code
Button myButton = new Button(this);
myButton.setText("Press Me");
myButton.setTextColor(Color.WHITE);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
layout.addView(myButton);
How do I add wrapcontent to this button?
Use this line (I used FILL_PARENT to demonstrate)
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
set the wrap content for button
myButtonsetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
or for layout
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Kotlin
val button = Button(context)
button.layoutParams = ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)

dynamically add buttons?

i want to add some buttons dynamically at some positions,can anyone suggest a sample code for this??
That's how you can add a button to a group at runtime:
final ViewGroup group = (ViewGroup)findViewById(R.id.some_group);
final Button button = new Button(context);
button.setText("Some text");
group.addView(button,
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(R.layout.medplan_index);
Take Orientation of linear Layout vertical.
LinearLayout linearLayout = (LinearLayout ) findViewById(R.id.linearLayout);
Button btn = new Button (this);
btn.setText("button name");
btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
linearLayout.addView(btn);

Categories

Resources