Adding a button to 2 LinearLayout? - android

I made a design for my app but as far as I can imagine
I have to a button into two different LinearLayouts.
Is it possible under Android?
Edit:
Code
_configureButton = new Button(context);
_callButton = new Button(context);
_messageButton = new Button(context);
_contactImage = new ImageView(context);
LinearLayout _verticalMainview = new LinearLayout(context);
LinearLayout _callAndMessageView = new LinearLayout(context);
LinearLayout _horizontelMainview = new LinearLayout(context);
LinearLayout _settingsAndMessageView = new LinearLayout(context);
//...
_callAndMessageView.addView(_callButton);
_callAndMessageView.addView(_messageButton);
_verticalMainview.addView(_configureButton);
_verticalMainview.addView(_contactImage);
_verticalMainview.addView(_callAndMessageView);
_horizontelMainview.addView(_contactImage);
_horizontelMainview.addView(_settingsAndMessageView);
_settingsAndMessageView.addView(_configureButton);
_settingsAndMessageView.addView(_messageButton);
Now code crashes. Or I should use GridLayout?

It isn't possible that a view has two parents.

Related

Radio Buttons don't appear on screen when added programatically

I have the following code in a button click handler:
LinearLayout linearLayoutParent = FindViewById<LinearLayout>(Resource.Id.linearLayoutParent);
LinearLayout linearLayoutFileTransferVia = new LinearLayout(this);
TextView labelFileTransferViaInfo = new TextView(this);
labelFileTransferViaInfo.LayoutParameters = lp;
labelFileTransferViaInfo.Text = "Choose file transfer via to FTP Server";
labelFileTransferViaInfo.SetTextAppearance(Android.Resource.Style.TextAppearanceLarge);
RadioButton rbWiFi = new RadioButton(this);
RadioButton rb3G = new RadioButton(this);
RadioGroup radioGroupFileTransferType = new RadioGroup(this);
radioGroupFileTransferType.Orientation = Orientation.Horizontal;
rbWiFi.Text = "Wi-Fi";
rb3G.Text = "3G";
radioGroupFileTransferType.AddView(rbWiFi);
radioGroupFileTransferType.AddView(rb3G);
linearLayoutFileTransferVia.AddView(labelFileTransferViaInfo);
linearLayoutFileTransferVia.AddView(radioGroupFileTransferType);
linearLayoutParent.AddView(linearLayoutFileTransferVia);
When I click the button labelFileTransferViaInfo appears on screen but radioGroupFileTransferType doesn't. What do you think is the problem for that to happen?
I think you need to add LayoutParams just like that
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroupFileTransferType.AddView(rbWiFi, layoutParams);
radioGroupFileTransferType.AddView(rb3G, layoutParams);

How to make the scrollview do not overflow in tablerow

I want to make a layout that it
LinearLayout
ScrollView
LinearLayout
but it seems like the ScrollView has cover the rest of LinearLayout.
LinearLayout all = new LinearLayout(this);
LinearLayout upper = new LinearLayout(this);
LinearLayout footer = new LinearLayout(this);
ScrollView sv = new ScrollView(this);
all.setOrientation(LinearLayout.VERTICAL);
upper.setOrientation(LinearLayout.HORIZONTAL);
footer.setOrientation(LinearLayout.HORIZONTAL);
TextView code = new TextView(this);
code.setText("Code : ");
EditText codeEdit = new EditText(this);
upper.addView(code);
upper.addView(codeEdit);
LinkedList<LinearLayout> lk = new LinkedList<LinearLayout>();
TextView[] tt = new TextView[100];
LinearLayout ll= new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
for(int a = 0 ; a < 100; a++){
LinearLayout temp = new LinearLayout(this);
temp.setOrientation(temp.HORIZONTAL);
tt[a] = new TextView(this);
tt[a].setText("Hello " + a);
temp.addView(tt[a]);
lk.add(temp);
ll.addView(temp);
}
sv.addView(ll);
Button next = new Button(this);
next.setText("Next");
footer.addView(next);
all.addView(upper);
all.addView(sv);
all.addView(footer);
setContentView(all);
The scrollview is too large and cover the rest of the linearlayout, the linearlayout after is not showing becasue of the scrollview.
How can I actually solve this?
Do like this
LinearLayout dummy = new LinearLayout(this);
dummy.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
dummy.addView(sv);
add one linearlayout to wrap the scrollview inside it
instead of adding scroolview add this dummy linearlayout

Create a custom number picker

So because of the limited support library for the number picker (And because it's WAAY too big), I'm making my own number picker. Unfourtunately, it's not appearing correctly.
Any help on fixing it would be great.
Up arrow and down arrows occupy the same space (overlapping each other so that only the down arrow appears). The down arrow should be UNDER the text representing the number.
Any idea why this is?
Here's the screenshot:
And here's the code for it:
//"Number-Picker"
LinearLayout numPicker = new LinearLayout(context);
numPicker.setOrientation(LinearLayout.VERTICAL);
numPicker.setLayoutParams(pickerItemParams);
LinearLayout.LayoutParams upDownParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
//Up button
LinearLayout upSpace = new LinearLayout(context);
ImageView upArrow = new ImageView(context);
upArrow.setBackgroundResource(R.drawable.arrow_up);
upSpace.setLayoutParams(upDownParams);
upSpace.addView(upArrow);
//text
LinearLayout numSpace = new LinearLayout(context);
TextView pickerNum = new TextView(context);
pickerNum.setText(String.valueOf(textValue));
numSpace.setLayoutParams(upDownParams);
numSpace.addView(pickerNum);
//down
LinearLayout downSpace = new LinearLayout(context);
ImageView downArrow = new ImageView(context);
upArrow.setBackgroundResource(R.drawable.arrow_down);
downSpace.setLayoutParams(upDownParams);
downSpace.addView(downArrow);
numPicker.addView(upSpace);
numPicker.addView(numSpace);
numPicker.addView(downSpace);
//down
LinearLayout downSpace = new LinearLayout(context);
ImageView downArrow = new ImageView(context);
upArrow.setBackgroundResource(R.drawable.arrow_down); // <----- Should be downArrow
downSpace.setLayoutParams(upDownParams);
downSpace.addView(downArrow);
This might cause your downArrow to actually exist but its ImageView is missing its image.

android dynamically created layouts overlapping each other

I want to create a relative layout inside a linear layout dynamically.This relative layout contains a text view and button which are also created in dynamic method.The alignment of text view and button not work properly.The code which i have tried is given below.
final LinearLayout lab_linear = (LinearLayout) findViewById(R.id.contact_list_layout);
lab_linear.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value);
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton);
lab_linear.addView(layout);
}
Try this..
Give layout parems for RelativeLayout individually to TextView and Button and also add addRule(RelativeLayout.ALIGN_PARENT_RIGHT); or addRule(RelativeLayout.ALIGN_PARENT_LEFT); for that views
Or add the below in for Button
lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value,lp);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//Or below remove above code and uncomment the below code
//lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton,lp1);
lab_linear.addView(layout);
}
Might I suggest using LinearLayouts instead of RelativeLayouts in your loop? That way, you won't have the overlap problem.

How to Create multiple buttons in android

I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.
Try this pls,
LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);
for (int i = 0; i < 10; i++)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
You are creating multiple new linear layouts within the for loop. Linear layout need to be taken out of the for loop and just try adding the buttons in it.
1st run of for loop creates a new linear layout and adds a button
2nd run of for loop creates a new linear layout and adds a button on the top of previous added layout
Here you are creating a new LinearLayout per button... Try something more like...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
}
main_layer.addView(layout);
Try this
LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
.get(i).getLabel(), bsList.get(i).getThumbUrl()));
LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
llDynamic.weight = 1f;
llParent.addView(b, llDynamic);
}
llParent is a defined Layout and make sure you call removeAllViews() before adding layouts dynamically into it.
BSView is my own custom View. You can set BSView into ordinary Button as you need it.

Categories

Resources