I am writing an android app,
I have an activity, inside it i have a button, and it's on click listener opens a dialog box from a custom XML using the following code:
I would like to add that dialog box another button that is not set in it's XML file.
All the components are excising in the XML and working just fine, the dialog opens but I can't add the b button.
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LinearLayout layout = findViewById(R.id.dialog_layout_root);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
I tried to use this example but it does not work for me. What am i doing wrong here?
Thanks!
Just try below code
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
LinearLayout layout = d.findViewById(R.id.dialog_layout_root);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
layout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
You were using only findViewById(R.id.dialog_layout_root); instead of d.findViewById(R.id.dialog_layout_root);
Here ll_button is the id of layout which contains the two xml buttons you have.
LinearLayout ll_button = d.findViewById(R.id.ll_button);
LinearLayout.LayoutParams layoutParams = new inearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll_button.addView(b, layoutParams);
Remove your last line.
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Related
I'm trying to get my button to create a new TextInputEditText field every time the user presses the button. So far I've managed to dynamically add a field but I can't figure out how to increment the hint of the EditText.
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
TextInputEditText et = new TextInputEditText(MainActivity.this);
et.setHint("Enter Member + 1");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
}
});
For example, I want the hint to increase the number to "Enter Number + X" each time the button is clicked.
You can use the LinearLayout's getChildCount api, the demo code could be:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
TextInputEditText et = new TextInputEditText(this);
int childNum = layout.getChildCount();
et.setHint(String.format("Enter Member + %d", childNum+1));
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
I don't know what is the problem. I am creating the simple phone-book, so i have name of abonent, image and call button under the name. And when i try to set width with the layoutParams parameter for button, it doesn't change anything, btn.setWidth(); doesn't work either.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
LinearLayout.LayoutParams sublparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams childparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(200, 200);
linear.setOrientation(LinearLayout.VERTICAL);
for(int i = 0; i < 10 ; i++){
LinearLayout sublinear = new LinearLayout(this);
Button btn = new Button(this);
ImageView img = new ImageView(this);
TextView txt = new TextView(this);
img.setImageResource(R.drawable.img);
txt.setText("Abonent " + (i+1));
btn.setBackgroundColor(getColor(R.color.mygreen));
btn.setText("+3800000");
txt.setLayoutParams(childparams);
btn.setLayoutParams(imageparams);
img.setLayoutParams(imageparams);
linear.addView(txt);
sublinear.addView(img);
sublinear.addView(btn);
linear.addView(sublinear, sublparams);
}
}
}
result of programm, green vertical lines are buttons, that i cannot to make wider
your button is using LayoutParams to set width and heigth so you need to change this properties here (LinearLayout.LayoutParams(widht, height)):
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(500, 300);
and the size of your button must change, because you are setting this values.
btn.setLayoutParams(imageparams);
I have created programmatically a RelativeLayout which contains a button. I have also created a ScrollView which contains a LinearLayout in which are more than 10 TextViews. I want to have the RelativeLayout to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews to go behind the fixed RelativeLayout. I want that button to be always visible. With this code, the RelativeLayout and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
Thanks!
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout) and the put there you scrollView also via addView(scrollView)
EDIT:
your new code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment
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);
Let's say I have a LinearLayout, and I want to add two View to it. The first one contain editText, and the other one contain listview. I have been try code in java follows:
EditText inputViaText;
ListView historyInput;
protected static LinearLayout askTextLayout = null;
askTextLayout = new LinearLayout(this);
askTextLayout.setVisibility(LinearLayout.VISIBLE);
askTextLayout.setOrientation(LinearLayout.HORIZONTAL);
inputViaText = new EditText(this);
LinearLayout.LayoutParams askTextParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
historyInput = new ListView(this);
LinearLayout.LayoutParams historyInputParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,70);
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
FrameLayout.LayoutParams frameAskTextParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
setContentView(R.layout.activity_main);
addContentView(askTextLayout, frameAskTextParams);
But, It just show the first one that i add. So when i code like follows:
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
It just show listView. When i code like follows:
askTextLayout.addView(inputViaText,askTextParams);
askTextLayout.addView(historyInput,historyInputParams);
It just show edittext. Anyone can help me?
Try this code in oncreate Method of your activity
context = this;
setContentView(R.layout.activity_main);
container = (LinearLayout) findViewById(R.id.Linear);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setWeightSum(100);
ListView v1 = new ListView(context);
v1.setBackgroundColor(Color.CYAN);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(0,
50);
p1.weight = 90;
v1.setLayoutParams(p1);
EditText v2 = new EditText(context);
v2.setText("Hello");
v2.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(0,
50);
p2.weight = 10;
v2.setLayoutParams(p2);
linearLayout.addView(v1, p1);
linearLayout.addView(v2, p2);
View view = new View(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
1);
view.setLayoutParams(lp);
view.setBackgroundColor(Color.BLACK);
container.addView(linearLayout);
container.addView(view);
Write this in activity_main:
<LinearLayout
android:id="#+id/Linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>