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);
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);
This is the code I have in mainActivity. I want to add a button in the layout but in another activity. How can I do this?
layout = (LinearLayout)findViewById(R.id.linear);
Button btnTag = new Button(this);
btnTag.setText(name);
btnTag.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btnTag.setId(tel);
layout.addView(btnTag);
As #WoogieNoogie said, certainly there is much better ways to do what you are looking for, but if it is a must then save add button to the layout and keep it invisible, then set a Boolean variable and save it into preferences, then on activity oncreate read preference Boolean state and set visibility as visible or gone.
I hope you know how to work with preferences.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphTemperature GT = new GraphTemperature(getApplicationContext());
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(GT); // line 27
layout.addView(buyButton);
setContentView(layout);
}
If I want add 2 buttons to JFrame in Java with each press on certain button I create JPanel and add this 2 buttons to the Jpanel then add the JPanel to the JFrame
But in android I tried
public class object extends Activity {
ToggleButton togglebutton;
Button button;
public void onCreate(Bundle bundle){
super.onCreate(bundle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setWeightSum(100);
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,30);
LinearLayout.LayoutParams part = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,70);
togglebutton = new ToggleButton(this);
button = new Button(this);
button.setLayoutParams(par);
button.setLayoutParams(part);
layout.addView(button);
layout.addView(togglebutton);
LinearLayout lay =(LinearLayout)findViewById(R.id.lay);
try {
lay.addView(layout);
}catch(Exception e){
e.printStackTrace();
}
}
}
But didn't work I always get exception with that
What can I do?
Or what is equal to JPanel in Android?
You said nothing about "adding content from one Activity to other" in your initial post and didn't include an a logcat for your exception, but the obvious problem is not calling setContentView while using findViewById.
This code creates the activity you are trying to make.
public class MainActivity extends Activity {
ToggleButton togglebutton;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout rootView = new LinearLayout(this);
rootView.setOrientation(LinearLayout.HORIZONTAL);
rootView.setWeightSum(100);
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,30);
LinearLayout.LayoutParams part = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,70);
togglebutton = new ToggleButton(this);
button = new Button(this);
button.setText("Click Me");
button.setLayoutParams(par);
button.setLayoutParams(part);
rootView.addView(button);
rootView.addView(togglebutton);
setContentView(rootView);
}
}
The closest thing to JPanel for android would be the root view of your layout. I think cricket has the right solution and your getting an error beacuse your not setting setContentView(). This will set your root view to the xml layout file specified. For example setContentView(R.layout.main);.
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
In the below code I have created two text views and added them programmatically to a relative layout. I want to align them side by side.
The code runs fine but is not placing the new TextView to the right of previous TextView instead the new TextView is positioned at margin (0,0,0,0) i.e. upper right corner of the screen:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout= (RelativeLayout) findViewById(R.id.relative_Layout);
textView[0] = new TextView(this);//creates first textview
textView[0].setId(0);
textView[0].setText("1");
textView[0].setBackgroundResource(R.drawable.shape);//parses an image from shape.xml
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(textView[0]);//creates another textview
textView[1] = new TextView(this);
textView[1].setBackgroundResource(R.drawable.shape);
RelativeLayout.LayoutParams relativeLayoutParams=
new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView[0].getId());//to align the textview side by side
textView[1].setText("2");
relativeLayout.addView(textView[1], relativeLayoutParams);
Try the following:
Set the id of textView[0] to 1 instead of 0 (id needs to be a positive integer)
Add to the relativeLayoutParams of textView[1] a rule for RelativeLayout.ALIGN_TOP
The following worked for me:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;
TextView[] textView = new TextView[2];
// 1st TextView
textView[0] = new TextView(this);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(textView[0], relativeLayoutParams);
// 2nd TextView
textView[1] = new TextView(this);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[1].setText("2");
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
textView[0].getId()); // added top alignment rule
relativeLayout.addView(textView[1], relativeLayoutParams);
Just a guess: can you try to use a different id than 0 ?