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));
}
}
Related
I have had no trouble adding a web view to my layout on click with a button, but when it comes to adding a web view on create of my main activity it just does nothing. I need the program to create certain web views based on data stored when I run it. It just really gets me that the same exact code runs perfectly when I put it inside an onClick button, but in the main method it does absolutely nothing. No error, no anything.
tickerLinearLayout = (LinearLayout) findViewById(R.id.TickerLinearLayout);
currency = new WebView(this);
currency.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, btcUp.getMeasuredHeight()));
tickerLinearLayout.addView(currency);
currency.getSettings().setJavaScriptEnabled(true);
disableScroll(currency);
currency.loadUrl("file:///android_asset/btc.html");
To create a Webview programatically in an Activity, you will first have to add your Webview into a RelativeLayout. I am choosing a RelativeLayout because my use case needs the Webview to be positioned on different locations on the Parent View time to time.
public class CustomRelativeLayout extends RelativeLayout {
private WebView mWebView;
public CustomRelativeLayout(Context context) {
super(context);
mWebView = new WebView(context);
mWebView.setId(View.NO_ID);
mWebView.setScrollContainer(false);
mWebView.loadData("<html><body>TEST</body></html>", "text/html", "utf-8");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
context.getResources().getDisplayMetrics().widthPixels, context.getResources().getDisplayMetrics().heightPixels);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mWebView.setLayoutParams(params);
addView(mWebView);
}
public WebView getTheWebView() {
return mWebView;
}
}
You can modify the CustomRelativeLayout class to accept URL.
In your MainActivity class, you will have to add a new instance of CustomRelativeLayout and add it to the contentView as shown below:
setContentView(new CustomRelativeLayout(this));
To make adjustments to the size of the webview play around with the width and height of the LayoutParams added to the Webview.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
<width>, <height>);
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 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 want to add a new button to the view by clicking on an existing static button within the same view. And the button should stay there in the view until we manually delete it.
Browsing for help on this for a while and all I can get to is creating dynamic buttons. Hope any of you can help me out in what I am trying to achieve.
On your existing button click listener call this function.
private void addButton(){
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);//create a layout in you content view
layout.setOrientation(LinearLayout.VERTICAL);
Button newButton = new Button(this);
newButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
newButton.setText("Button Text");
newButton.setId(1);//some id
layout.addView(row); // add button in layout
}
You must access to layout with findView and add your button to that like this code which located in onCreate function in your activity :
Button staticBtn = (Button)(findViewById(R.id.staticBtnID));
LayoutParam staticBtnLayoutParam = staticBtn.getLayoutParam();
Button btn = new Button(this);
int width = 0; //you can use staticBtnLayoutParam.getWidth(); or .getX(); to get staticBtn parameters and set your param related to that
int height = 0 ;
btn.setLayoutParams(new LayoutParams(width , height));
LinearLayout layout = (LinearLayout)(findViewById(R.id.linearLayoutID));
layout.addView(button);
Best Regards