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);.
Related
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);
}
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
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);
I am trying to understand Android MjpegDemo code that I found. This code streams IP camera video to android app. In the original app Mjpeg view takes up an entire screen and doesn't use an activity.xml in the layout dir (which is what I am used to seeing). This is partial code for the MjpegSample.java which loads as main activity. I think I understand that setContentView(mv) and WindowManager.LayoutParams.FLAG_FULLSCREEN is the reason everything fills the screen. Is there a way to work with this type of an Activity and still add other objects, like buttons or backgrounds?
public class MjpegSample extends Activity {
private MjpegView mv;
public void onCreate(Bundle myBundle) {
super.onCreate(myBundle);
String URL = "http://someURL/mjpg/video.mjpg";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mv = new MjpegView(this);
setContentView(mv);
mv.setSource(MjpegInputStream.read(URL));
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
}
}
Here is a detailed example on how to achieve this. Here, we use a LinearLayout object as the main view content, then we add our View objects to this (I also included an example LinearLayout that is nested inside the main one, to show how you can embed View containers within other View containers):
public class MjpegSample extends Activity {
private MjpegView mv;
private LinearLayout ll1;
private LinearLayout nestedL;
private Button btn1;
private TextView txt1;
public void onCreate(Bundle myBundle) {
super.onCreate(myBundle);
//allow legacy-style network queries on UI thread (in case you compile for Android 3.0+, which do not allow
//network transactions in main UI thread by default)
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final String URL = "http://someURL/mjpg/video.mjpg";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create new button programmatically
btn1 = new Button(this);
btn1.setText("Test button 1");
//set so that content is wrapped in its parent container (which will be the nestedL object below)
btn1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//Create new TextView programmatically
txt1 = new TextView(this);
txt1.setText("Test text 1");
//set so that content is wrapped in its parent container (which will be the nestedL object below)
txt1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//Create new MjpegView programmatically
mv = new MjpegView(this);
//set so that content is wrapped in its parent container (which will be the ll1 object below)
mv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
nestedL = new LinearLayout(this);
nestedL.setOrientation(LinearLayout.HORIZONTAL);
//set so that content is wrapped in its parent container (which will be the ll1 object below)
nestedL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//we add btn1 and txt1 in nestedL LinearLayout (they will be ordered horizontally, according to nestedL orientation
//as configured above, and all those widgets will not be stretched either)
nestedL.addView(btn1);
nestedL.addView(txt1);
//create the main LinearLayout widget which will be set as the view content using setContentView(...)
ll1 = new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
//we add the other LinearLayout (horizontal one) on top
ll1.addView(nestedL);
//then we add the video player thing
ll1.addView(mv);
//ll1 is set as the main view for your activity
setContentView(ll1);
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
mv.setSource(MjpegInputStream.read(URL));
}
}
I have a problem with creating a working Layout in View class that will not crush the program. I think everything is done correctly in the code, but probably there are some setting I don't know about I hope that You guys will reveal that to me.
I am trying to do drawing View with a "Clear" button and RadioGroup.
So first od all, that works fine, but both added content Views (btnReset = "Clear", btnScale = RadioGroup) are hoovered on each other. That is why I want to add one parent Layout, so I can easily manage these Views.
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnScale, tv.paramsRadio);}
DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
I omit most of the code because I think its irrelevant, everything works fine but the layout.
Anyway, when I try to create a layout, I simply add a little code:
Changed File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView tv = new DrawView(this);
setContentView(tv);
addContentView(tv.layout, tv.params); }
Changed DrawView.java
public class DrawView extends View {
public Button btnReset;
public RelativeLayout.LayoutParams params;
public RelativeLayout.LayoutParams paramsRadio;
public RelativeLayout layout;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(btnReset, params);
layout.addView(btnScale, paramsRadio);
I assume that should work like charm, but unfortunately, when I try to do anything with newly created layout, my app crashes. What is wrong with that code?
Well, after quite long fight I managed to solve my own problem :D I did not do it, like #pskink suggested, because honestly for me it was too advanced. I found much simpler solution.
The thing is, You can create as much views as You want in DrawView.java, but sort them in Layout created in File1.java!
Just like that:
File1.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
DrawView tv = new DrawView(this);
ll.addView(tv.btnReset, tv.params);
ll.addView(tv.btnScale, tv.paramsRadio);
ll.addView(tv);
setContentView(ll);
And DrawView.java stays the same:
public class DrawView extends View {
public Button btnReset;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams paramsRadio;
public RadioGroup btnScale;
public DrawView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
btnScale = new RadioGroup(context);
btnScale.setOrientation(RadioGroup.HORIZONTAL);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsRadio = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);