Button position in a linear layout - java

i'm trying to create button programmatically on a linear layout and i want to have "columns" of buttons but it seems the linear layout automatically put buttons under each other and i can't find a way to make my button position to the top again
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_branch);
LinearLayout linear = (LinearLayout) findViewById(R.id.content);
for(int i =0; i <10; i++) {
Button btn = new Button(this);
btn.setText("Branch1");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(330,200);
layoutParams.setMargins(rowNb*330+5, 3, 0, 0); // left, top, right, bottom
btn.setLayoutParams(layoutParams);
linear.addView(btn);
btnNb++;
if(btnNb>5)
{
rowNb = 1;
}
}
}
here's what i got
Can someone help me out to find a way to make my 2nd columns like the first ? I already tried a btn.SetY or a btn.setGravity

Try GridLayout instead of LinearLayout
https://developer.android.com/reference/android/widget/GridLayout.html

Related

Create multiple dynamic CardView in Constraint Layout programmatically?

I wish to get data from database and replicate this format of CardView with different information but I' facing issue regarding the constraints of the constraint layout. I want to set top of next CardView to the bottom of previous one but ids of both would be generated in real time.
At this time I have this code for sample CardView but it appears at (0,0) position right now. Does anyone have an opinion or an alternative solution to do the same.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_appointment);
//activity constraint layout
ConstraintLayout constraintLayout = findViewById(R.id.parentlayout);
//Adding 2 CardViews
for (int i = 1; i <= 2; i++) {
CardView cardview = new CardView(this);
layoutparams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
layoutparams.bottomMargin = 10;
cardview.setLayoutParams(layoutparams);
cardview.setRadius(5);
cardview.setPadding(10, 10, 10, 10);
cardview.setCardBackgroundColor(Color.GRAY);
cardview.setMaxCardElevation(8);
TextView textview = new TextView(this);
textview.setLayoutParams(layoutparams);
textview.setText("CardView Programmatically");
textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
textview.setTextColor(Color.WHITE);
textview.setPadding(25, 25, 25, 25);
textview.setGravity(Gravity.CENTER);
cardview.addView(textview);
constraintLayout.addView(cardview);
id = i;
}
}

Combining 2 views into one, and placing one below the other

I add 2 layout types (on top of the default one), one of which adds some names to the top of some rows and another layout to add buttons to the screen (and a black line).
However I want the bar at the top with the names to remain constant, and to add the buttons in a loop, that I can scroll through. So far the code for this I have written is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollPictures = new ScrollView(this);
RelativeLayout mainApp = new RelativeLayout(this);
LinearLayout appLayout = new LinearLayout(this);
appLayout.setOrientation(LinearLayout.VERTICAL);
appLayout.setClipBounds(null);
for(int x = 1; x <= 15; x++){
View view = LayoutInflater.from(this).inflate(R.layout.button_layout, appLayout, false);
Button StudentSubmission = (Button) view.findViewById(R.id.button1);
StudentSubmission.setText("Button 1");
StudentSubmission.setBackgroundColor(Color.LTGRAY);
Button SoundButton = (Button) view.findViewById(R.id.button2);
SoundButton.setText("Button 2");
SoundButton.setBackgroundColor(Color.LTGRAY);
appLayout.addView(view);
}
scrollPictures.addView(appLayout);
View topmenuview = LayoutInflater.from(this).inflate(R.layout.topofmenu, mainApp, false);
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, scrollPictures.getId());
mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);
setContentView(mainApp);
}
However, with this code the bar with the names of columns is placed ON TOP of the list of buttons rather than being ABOVE them, resulting in the top button being cut off. Is there a way to get the buttons to appear UNDER the names of the columns?
q.addRule(RelativeLayout.BELOW, scrollPictures.getId());
mainApp.addView(scrollPictures, p);
mainApp.addView(topmenuview, q);
should be
q.addRule(RelativeLayout.BELOW, topmenuview.getId());
mainApp.addView(topmenuview, p);
mainApp.addView(scrollPictures, q);
right?

Android Studio: Button always appears at the front

I have a RelativeLayout to which I add Views.
I added a button to it, and the button always appears in front of all the other Views that are added to it, regardless of the order in which things were added. How come?
I'm coding purely in Java, no XML.
Here's a simple example, the button will appear here in front of the text, even though the text was added last:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setText("Button");
text.setText("Text");
layout.addView(button);
layout.addView(text);
setContentView(layout);
}
Starting with Lollipop, a StateListAnimator controlling elevation was added to the default Button style. In my experience, this forces buttons to appear above everything else regardless of placement in XML (or programmatic addition in your case). That might be what you're experiencing.
To resolve this you can add a custom StateListAnimator if you need it or simply set it to null if you don't.
XML:
android:stateListAnimator="#null"
Java:
Button button = new Button(this);
button.setText("Button");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
button.setStateListAnimator(null);
}
More details:
Android 5.0 android:elevation Works for View, but not Button?
In the Android 5.0 (API 21) and above, you must add android:elevation into the view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setText("Button");
text.setText("Text");
button.setElevation(3.0f); // add this line, you could try with values > 3.0f
layout.addView(button);
layout.addView(text);
setContentView(layout);
}
From android developer docs :
By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
http://developer.android.com/guide/topics/ui/layout/relative.html
Try the following snippet :
RelativeLayout layout = new RelativeLayout(this);
Button button = new Button(this);
TextView text = new TextView(this);
button.setId(View.generateViewId());
text.setId(View.generateViewId());
button.setText("Button");
text.setText("Text");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, text.getId());
button.setLayoutParams(params);
layout.addView(button);
layout.addView(text);
The button appears to float to the forefront of the RelativeLayout it's in so...
Try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Button");
RelativeLayout groupContainingButton = new RelativeLayout(this);
groupContainingButton.addView(button);
TextView text = new TextView(this);
text.setText("Text");
RelativeLayout activityLayout = new RelativeLayout(this);
activityLayout.addView(groupContainingButton);
activityLayout.addView(text);
setContentView(activityLayout);
}
Check the button's state (enabled/disabled):
loginButton.setEnabled(false);

How to edit/delete objects from another method that were created programatically

I have a method that creates layouts vertically each with 3 buttons aligned horizontally with onClick creating one at a time with each click to a max of 5 layouts. How do I remove those layouts with a button that was created.
public void addTroop(Editable name){
LinearLayout mainPage = (LinearLayout) findViewById(R.id.manageTroopsMain);
if (count <= 5)
{
//CREATE NEW LINEAR LAYOUT
LinearLayout addTroopLayout = new LinearLayout(this);
//CREATE LAYOUT PARAMS FOR LAYOUT
LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newLayout.bottomMargin = 10;
//STYLE NEW LINEAR LAYOUT
addTroopLayout.setTag("addTroopLayout" + count);
addTroopLayout.setLayoutParams(newLayout);
addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);
//CREATE NEW BUTTONS
Button newTroop = new Button(this);
Button remove = new Button(this);
Button change = new Button(this);
//CREATE LAYOUT PARAMS FOR BUTTONS
LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
//STYLE NEW BUTTONS
newTroop.setText(name);
newTroop.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
newTroop.setLayoutParams(newTroopParam);
remove.setTag("rmvBtn" + count);
remove.setText("-");
remove.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
remove.setLayoutParams(rmvBtnParam);
change.setText("...");
change.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
change.setLayoutParams(chngNameParam);
//ADD VIEWS TO NEW LAYOUT
addTroopLayout.addView(newTroop);
addTroopLayout.addView(remove);
addTroopLayout.addView(change);
//ADD NEW LAYOUT TO mainPage LAYOUT
mainPage.addView(addTroopLayout);
//Increment Counter
count++;
}
}
Each time a layout is created it set with the following so that each layout will be named addTroopLayout1, addTroopLayout2, etc etc.
addTroopLayout.setTag("addTroopLayout" + count);
I did the same thing with the remove button
remove.setTag("rmvBtn" + count);
So now how do I access/edit/remove these? Could I do something like
Button rmvBtn1 = (Button) findViewByTag(R.id.rmvBtn1);
rmvBtn1.setOnClickListener(new view.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//REMOVE CREATED LAYOUT
//DECREMENT ALL LAYOUT TAGS BY 1 IF NOT LAST LAYOUT
//DECREMENT count VAR BY ONE
//ERASE ALL VARIABLES ASSOCIATED WITH THIS VIEW
}
});
This code is obviously giving me errors and im sure this is something simple. Thanks.
////////////////////EDIT/////////////////////
Sorry if I wasent clear on what I wanted. So I have created/styled a button using my if statement that is called from a method using an onClick:
Button newTroop = new Button(this);
remove.setTag("rmvBtn" + count);
Count starts at 1 and is incremented in when a button is pressed. Now I want to findViewByTag for that button to use it with something like:enter code here
Button rmvBtn1 = (Button) findViewByTag(rmvBtn1);
rmvBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//REMOVE CREATED LAYOUT
//DECREMENT ALL LAYOUT TAGS BY 1 IF NOT LAST LAYOUT
//DECREMENT count VAR BY ONE
//ERASE ALL VARIABLES ASSOCIATED WITH THIS VIEW
}
});
So specifically how to I use a button that was created with my if statement? Do I still have to define it by
Button rmvBtn1 = (Button) findViewByTag(rmvBtn1);
or is there another way I should be doing this?
keep view in remove button, and keep view's parent int view's tag
addTroopLayout.setTag(mainPage);
remove.setTag(addTroopLayout);
and then use it like this
rmvBtn1.setOnClickListener(new view.OnClickListener() {
#Override
public void onClick(View v) {
View willRemove = (View)v.getTag();
LinearLayout mainPage = (LinearLayout)willRemove.getTag();
mainPage.removeView(willRemove);
}
});

How to set align of view without xml in android

I don't use xml in my UI.
My code of mainlayout:
public class MainActivity extends ActionBarActivity {
LinearLayout LinLay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
this.setContentView(sv);
LinLay = new LinearLayout(this);
LinLay.setOrientation(LinearLayout.VERTICAL);
LinLay.setBackgroundColor(Color.parseColor("#000000"));
sv.addView(LinLay);
And adding a Edittext:
EditText txt = new EditText(this);
txt.setBackgroundColor(Color.parseColor("#ff9a16"));
txt.setHint("Txt(0,0)");
LayoutParams txtParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
txtParams.width = 300;
txtParams.height = 70;
txtParams.leftMargin = 0;
txtParams.topMargin = 0;
txt.setLayoutParams(txtParams);
LinLay.addView(txt);
Finally adding a Button:
Button btn = new Button(this);
btn.setBackgroundColor(Color.parseColor("#cb0016"));
btn.setText("Btn(0,0)");
LayoutParams btnParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
btnParams.width = 300;
btnParams.height = 70;
btnParams.leftMargin = 0;
btnParams.topMargin = 0;
btn.setLayoutParams(btnParams);
LinLay.addView(btn); }
The Edittext's must be X position = 0 and Y position = 0 of MainScreen.
The Button's must be X position = 0 and Y position = 0 of MainScreen.
But the Button taking referance is the Edittext and it takes position in X:0,Y:70 of MainScreen.
The program screen: prntscr.com/4ixo7z
If my LinearLayout that's LinLay is Horizontal, the button takes position in X:300,Y:0 of MyScreen. I want to taking position in MainScreen. I don't want to taking referance. Help me.
The Horizontal screen: prntscr.com/4ixo8i
Change your LinearLayout and use RelativeLayout, remove orientation. add your product relatively, this will solve your issue.

Categories