Android adding dynamic button click disabled when adding edittext - android

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.

Related

Android: Align a Button to the Right in LinearLayout

I have a piece of code which I use to create a new LinearLayout. Within the layout I wish to add a TextView which contains both a label and a value. Then next to it on the right I want to display the button. I want the button to be located toward the end of the screen, without stretching the button. I am happy with the button width and height as WARP_CONTENT.
How can I achieve this in code? I have barely any XML so using XML is not an option. I am trying to make the app as dynamic as possible, so I decided to steer clear of XML.
Please see the code below:
// Build a button
final Button addButton = new Button(task.getParent());
addButton.setText("Add New");
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Open a file picker here to let the user pick a file
}
});
// Build a new layout to hold all the elements
LinearLayout verticalLayout = new LinearLayout(task.getParent());
verticalLayout.setOrientation(LinearLayout.HORIZONTAL);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(addButton);
Thank you guys in advance.
Try this: Add Space (View) between TextView & Button.
// View space = new View(parent_context);
View space = new View(task.getParent());
// Width:0dp, Height:1 & Weight: 1.0
LinearLayout.LayoutParams spaceLP = new LinearLayout.LayoutParams(0, 1, 1.0f);
space.setLayoutParams(spaceLP);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(space);
verticalLayout.addView(addButton);
Add textview with size and gravity,like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setLayoutDirection(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
verticalLayout.addView(sizeTextView,params);
To achieve this you should use Relative layout and RelativeLayout.LayoutParams. By using LayoutParams you can set the rule to align your views as per your requirements.
for example
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);

Android dynamically creating a Button

I am quite new to Android, and is having problem in creating a Button dynamically. I have it working by creating in the Xml layout and working fine. Hope someone can help, to write code to create the button below during runtime, and after that Add it into a TableRow.
Note: I wanted to create a Button which is equivalent to the Xml below, but NOT using findViewById(), because this button doesn't exist. I know how to new a Button, but I am having difficulties on setting all the attributes below. Especially the layout_weight, background and drawableTop.
<Button
android:id="#+id/BtnRating1"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/Rating_1"
android:drawableTop="#drawable/face_1"
style="?android:attr/borderlessButtonStyle"
android:background="?android:selectableItemBackground"
android:gravity="center"
android:onClick="OnRating_1" />
Assuming the above xml is named button_view.xml
View view = LayoutInflater.from(context).inflate(R.layout.button_view);
Button button = (Button) view.findViewById(R.id.BtnRating1);
This way you can inflate directly from xml
I gave you code how to create a button on dynamic time, you can set all property which you want for this button.
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.BtnRating1);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
This below line will create the button programetically
Button btn = new Button(ActivityContext);
Then you can also add button properties as below
RelativeLayout.LayoutParams btnparamLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, 90);
btn.setGravity(Gravity.CENTER_VERTICAL);
btnparamLayoutParams.height = adjustedDp;
btn1paramLayoutParams.setMargins(0, 0, 0, 100);
btn.setLayoutParams(btnparamLayoutParams);
btn.setBackgroundColor(Color.WHITE);
at the end add your button to the parentlayout.
LinearLayout ll = (LinearLayout)findViewById(R.id.linID);
ll.addView(btn)
You can do it in two way
create a table row layout with button and inflate it.
you can create button dynamically and add it in your table row see below code for creating buttons dynamically.
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
params.weight = 1;
params.gravity = Gravity.CENTER;
tablerow.addView(button,params);
Here this is context of activity.

Adding button on top of Imageview at runtime

I am creating a imageview at runtime and now want a add a button on top of the Imageview dynamically at run time and if I pan the image in the imageview the button in it should also pan accordingly. The problem is that Imageview does not allow to add subviews in it.
I am able to add the button on top of the imageview but when I pan the imageview the button remains constant in the screen. Any idea on how to do this?
Here is what I did:
panImageView = new PanView(this); //Custom Imageview class to handle panning
panImageView.setScaleType(ImageView.ScaleType.CENTER);
//panimg.setAdjustViewBounds(true);
FrameLayout mainRL = (FrameLayout) findViewById(R.id.mainRelativeLayout);
mainRL.addView(panImageView);
ImageWorker imgTask = new ImageWorker(panImageView, is);
imgTask.execute(2000, 2000);
//Start button
Button startButton = new Button(this);
int iRndBtn = R.drawable.round_button;
startButton.setBackgroundResource(iRndBtn);
startButton.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
//layoutParams.setMargins(intialPage.mapStartX, intialPage.mapStartY, 0, 0);
layoutParams.setMargins(200, 200, 0, 0);
startButton.setLayoutParams(layoutParams);
//startButton.
mainRL.addView(startButton,layoutParams);
Add Image view first in any Layout and the add Button in that Layout.
Use a RelativeLayout with the ImageView & button. set the button visibility to gone at start. When you need change the visibility.

Android: Add new edittext onto new lines

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.

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.

Categories

Resources