Android: Add new edittext onto new lines - android

I have added a button and an edittext onto a framelayout. The problem is I appear to lose the functionality of being able to click the button. When I click the button, the keyboard appears for the edittext. How can I add button/editext objects into a layout so that they appear on a new row and do not affect each other. I was using the padding option to move them about, but this still causes the button to be non-clickable.
Here is my code: -
FrameLayout reportLayout = (FrameLayout) findViewById(R.id.reportDetailLayout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button executeButton = new Button(this);
executeButton.setClickable(true);
executeButton.setOnClickListener(handleOnClick(executeButton));
EditText text1 = new EditText(this);
executeButton.setText("Execute");
executeButton.setMinimumHeight(10);
executeButton.setMinimumWidth(150);
text1.setId(1);
text1.setHint("Enter Value");
executeButton.setPadding(0, 0, 0, 0);
text1.setPadding(12, 70, 0, 0);
executeButton.setLayoutParams(params);
text1.setLayoutParams(params);
reportLayout.addView(executeButton);
reportLayout.addView(text1);

FrameLayout will cause Views to overlap. Try LinearLayout with setOrientation().
And you don't use padding for placement. Padding is not same thing as a margin.

Related

ScrollView seems to set a top margin for its child

I have an issue with ScrollView which seems to add a top margin for its child. I have a LinearLayout above it, which sets Top margin in order for the layout to be below other elements (its parent is FrameLayout), then ScrollView is inserted into the LinearLayout, and a finally a child is attached to ScrollView.
The following image shows empty space that appears between "border" and order details (TextView):
Image
Java Code:
// Inner layout to accommodate ScrollView
LinearLayout inner_layout = new LinearLayout(app.getApplicationContext());
LinearLayout.LayoutParams inner_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 440);
inner_params.setMargins(0, 135, 0, 0);
inner_layout.setLayoutParams(inner_params);
// ScrollView for details of the order (in case everything doesn't fit in the body)
ScrollView scrollView = new ScrollView(app.getApplicationContext());
params_in = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(params_in);
scrollView.setFillViewport(false);
TextView body_text = new TextView(app.getApplicationContext());
params_in = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params_in.setMargins(10, 0, 10, 10);
body_text.setLayoutParams(params_in);
body_text.setTextColor(getResources().getColor(R.color.colorTextSecondary, null));
It's almost as if top margin of 135 is also used for ScrollView's child.
If someone could help understand what I'm doing wrong I would appreciate it a lot.

How to add multiple different text views in linear layout

Hi I was want to add 2 different text views to my linear layout but somehow when i try to add both of them only the first one appears why is this the case? Here is my code :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// create the text view for the main string to
// be displayed
TextView displayMainText = new TextView(this);
displayMainText.setTextSize(15);
displayMainText.setText(mainString);
displayMainText.setLayoutParams(layoutParams);
displayMainText.setPadding(0, 20, 0, 0);
// Create the text view for the optional string to
// be displayed
TextView displayOppText = new TextView(this);
displayOppText.setTextSize(15);
displayOppText.setText(optionalString);
displayOppText.setLayoutParams(layoutParams);
displayOppText.setPadding(0, 20, 0, 0);
// add text views to the layout
LinearLayout studyTLayout = (LinearLayout) findViewById(R.id.study_time_layout);
studyTLayout.addView(displayMainText);
studyTLayout.addView(displayOppText);
setContentView(studyTLayout);
With my code it only adds the first text view correctly
It seems like you didn't set the orientation of the LinearLayout to Vertical.
studyTLayout.setOrientation(LinearLayout.VERTICAL);

Android adding dynamic button click disabled when adding edittext

I am dynamically adding a button to a frame layout. Which works great. When I add an edittext object to the same frame layout, it appears the functionality of being able to click the button appears to stop.
Can anybody help me with this. Here is my code: -
FrameLayout reportLayout = (FrameLayout) findViewById(R.id.reportDetailLayout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button executeButton = new Button(this);
executeButton.setClickable(true);
executeButton.setOnClickListener(handleOnClick(executeButton));
EditText text1 = new EditText(this);
executeButton.setText("Execute");
executeButton.setMinimumHeight(10);
executeButton.setMinimumWidth(150);
text1.setId(1);
text1.setHint("Enter Value");
executeButton.setPadding(0, 0, 0, 0);
text1.setPadding(12, 70, 0, 0);
executeButton.setLayoutParams(params);
text1.setLayoutParams(params);
reportLayout.addView(executeButton);
reportLayout.addView(text1);
Thanks
Martin
your edit text is top of your button. your touch event can clicks only edit texts area.
you change button or edit text's location, i think this problem will solve.
It needed to be a Linear Layout.

Adding margins to button object programatically

Need to set left margin to a button object programatically.
This is the code segment:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.for_button);
MarginLayoutParams ml = new MarginLayoutParams(-2,-2);
ml.setMargins(5, 0, 0, 0);
Button btn = new Button(this);
btn.setText("7");
btn.setTextColor(Color.WHITE);
btn.setBackgroundResource(R.drawable.date_button);
rl.addView(btn,ml)
I also tried
btn.setLayoutParams(ml);
rl.addView(btn);
Whats the big problem. Or is there any alternative way?
Alright, I'm gonna give this a shot IronBlossom; this is how I do it and I hope it works:
LinearLayout myLinearLayout = (LinearLayout)findViewById(R.id.my_linear_layout);
Button myButton = new Button(this);
// more myButton attribute setting here like text etc //
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.setMargins(5,0,0,0);
myLinearLayout.addView(myButton, params);
best,
-serkan
You use a RelativeLayout as the parent for the button, but you don't specify any rules for the it where to place the button (e.g. ALIGN_PARENT_LEFT and ALIGN_PARENT_TOP).
You have to set rules for position when using a RelativeLayout though, so this messes with the layout calculation. This means that you have to use RelativeLayout.LayoutParams instead of the MarginLayoutParams because the former allows these rules and has proper default values set.
Alter this line:
MarginLayoutParams ml = new MarginLayoutParams(-2,-2);
to
RelativeLayout.LayoutParams ml = new RelativeLayout.LayoutParams(-2,-2);
Chances are that you also want to add rules because the default positioning values don't suit you (views get positioned in the top left corner of the parent layout by default). You can use RelativeLayout.LayoutParams.addRule() for that.

Custom view: nested linearlayout not showing up

I created a custom view. In it, theres a line, a textview, another line. beneath the bottom line, i wanted to put a new horizontally oriented linearlayout. when i run it, this nested linearlayout doesnt seem to show up at all. Instead, i can see the test button right underneath the bottom line. what am i doing wrong?
public class MyView extends LinearLayout {
public MyView(Context context, Question question) {
super(context);
// this.setLayoutParams(params);
this.setOrientation(VERTICAL);
this.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 2);
View topLine = new View(context);
lineParams.setMargins(0, 15, 0, 0);
topLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
topLine.setLayoutParams(lineParams);
this.addView(topLine);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//Challenge Question
TextView questionText = new TextView(context);
questionText.setTextColor(Color.BLACK);
questionText.setTextSize(14);
questionText.setLayoutParams(params);
questionText.setGravity(Gravity.CENTER_HORIZONTAL);
questionText.setText(question.getQuestion());
this.addView(questionText);
View bottomLine = new View(context);
bottomLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
bottomLine.setLayoutParams(lineParams);
this.addView(bottomLine);
LinearLayout innerLayout = new LinearLayout(context);
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
innerLayout.setLayoutParams(innerLayoutParams);
innerLayout.setBackgroundColor(Color.RED);
innerLayout.setOrientation(HORIZONTAL);
//TableLayout for the multiple choices
TableLayout tableLayout = new TableLayout(context);
LayoutParams tableLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// tableLayoutParams.weight = .8f;
tableLayout.setBackgroundColor(Color.RED);
tableLayout.setLayoutParams(tableLayoutParams);
innerLayout.addView(tableLayout);
this.addView(innerLayout);
Button button = new Button(context);
button.setLayoutParams(params);
button.setText("testing 123");
this.addView(button);
}
Note that I pasted the code without all the stuff that I added to the tablelayout. I probably should have pasted that too. But it didn't work when I did that either. but either way, if i set the nested linearlayout to 300 width and set a background color of red to it, i should at least see it, no?
Think about what the height of the inner layout should be. Right now it is wrap_content and contains a TableLayout (with no rows) with its height also set to wrap_content. There doesn't seem to be anything in that inner layout giving it a height dimension, so that may be why it is not being displayed.
Trying the following will make your layout visible:
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, 300);
More usefully, you can try adding something with a real width/height to the TableLayout.
Also consider writing your layout in XML to better separate your application logic and the presentation.

Categories

Resources