I'm creating childview for viewflipper at runtime based on the no of records in database,
inflating a view and adding to viewflipper. While Clicking each childview of viewflipper, should have newviewflipper with some childs. How to achieve this?
ViewFlipper.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
CustomViewSample.xml
<?xml version="1.0" encoding="utf-8"?>
<com.sample.MjpegView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullscreen_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Java Code
ViewFlipper viewflipper = (ViewFlipper)findViewById(R.id.flipper);
- **livevideocount** is an integer contains no of records from table.
for(int i=0;i<livevideocount;i++)
{
MjpegView mv = (MjpegView)inflater.inflate(R.layout.CustomViewSample,null);
mv.setVisibility(MjpegView.VISIBLE);
viewflipper.addView(mv);
}
Clicking this MjpegView object should generate another viewflipper with 3 childviews.
can anyone help me?
You should not directly inflate dynamic content into ViewFliper.
You should add dyna_layout as <LinearLayout /> or <RelativeLayout /> into your ViewFlipper.xml, consider dyna_layout as a ViewGroup:
ViewGroup parent = (ViewGroup)(viewflipper.findViewById(R.id.dyna_view));
MjpegView mv = (MjpegView)inflater.inflate(R.layout.CustomViewSample, parent);
mv.setVisibility(MjpegView.VISIBLE); //may use or not
parent.addView(mv);
Related
I have a layout as indicated below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<LinearLayout
android:layout_height="match_parent"
...
The associated activity is like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);// here is the layout
}
I'm trying to make it visible in the following way:
LinearLayout layoutActLogin = (LinearLayout) findViewById(R.layout.act_login);
layoutActLogin.setVisibility(View.VISIBLE);
But Android Studio told me that there is an error about R.layout.act_login
findViewById is for views, not layouts.
You should put an ID in your view like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<LinearLayout
android:layout_height="match_parent"
...
Then get the view reference and make it visible
View viewActLogin = findViewById(R.id.my_view);
viewActLogin.setVisibility(View.VISIBLE);
You can not change visibility of Layout. you can only change visibility of Views inside.
You can assign id to you view by using android:id tag in you layout. You can read more about this here.
In your case. Just give some Id to your View/ViewGroup, and reference that View from your Activity and using findViewById method and change its visibility.
You're misunderstanding how layouts and views work. Layouts define what is shown on-screen to a user during an activity, set by calling setContentView() within the Activity's onCreate() method. Views are individual elements within the layout, which are accessed with the R.id prefix using findViewById().
In your example you'll need to apply an ID to the root ConstraintLayout (using android:id) to be able to access it:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
Then you can access it with:
ConstraintLayout layoutActLogin = (ConstraintLayout) findViewById(R.id.parent_layout);
layoutActLogin.setVisibility(View.VISIBLE);
EDIT Looking at your code I now realise you want to control the root ConstraintLayout, which makes my answer almost identical to Eduardo Herzer's. Leaving my answer up due to the added explanation at the beginning.
I am having a Fragment, where I inflate "fragment_board.xml":
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.app.BoardView"
android:id="#+id/view_board"/>
</FrameLayout>
As you can see, fragment_board contains a custom view "BoardView", from where I want to load the following "view_board.xml":
<com.app.BoardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_vertical">
<android.widget.HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_horizontal"
android:foregroundGravity="left">
</android.widget.HorizontalScrollView>
</com.app.BoardView>
My custom view contains two scroll views (I use it for panning), and I want to be able to re-use it in other layouts. The BoardView extends the outer (vertical) scroll view like this:
public class BoardView extends ScrollView
When I use it stand-alone, it inflates fine and I can findViewById() both scroll views in the inflated layout. But when I use it in a layout tree (such as a fragment), I run into problems.
In the fragment, I inflate the layout tree like this:
View view = inflater.inflate(R.layout.fragment_board, container, false)
From the fragment, I can findViewById() the outer (vertical) scroll view, but findViewById() returns null for the inner (horizontal) scroll view.
In BoardView, I inflate like this:
View view = inflate(getContext(), R.layout.view_board, this);
As said, I can find both scroll views fine when inflated by itself, but when inflated as part of the fragment, I get StackOverflowError.
It is clear why: I inflate in the fragment first, and then I inflate the same XML in the view again, which triggers another inflation of the view and so on. I get a cyclic reference here. Problem I can't figure out how I can add the inner (horizontal) scroll view to the already existing layout. I think I need to merge somehow or inflate layouts manually, but I can't figure out how.
Here are some refs (which didn't help in my case):
Android - Writing a custom (compound) component
How to inflate XML-Layout-File correctly inside Custom ViewGroup?
Create a custom View by inflating a layout?
InvocationTargetException on inflating an xml - android
Android: StackOverFlowError with InvocationTargetException when inflating layout
Can anybody suggest what I should do. If possible, I'd want BoardView to work as a generic component, which I can plug into a layout tree where needed.
As Android [according to one of the refs above] does not officially support composite components, would my best option be to drop XML layouts for the inner views and add them in code?
Please check if something like this fixes the issue:
fragment_board.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.app.BoardView
android:id="#+id/view_board"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
view_board.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_vertical">
<android.widget.HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_horizontal"
android:foregroundGravity="left">
</android.widget.HorizontalScrollView>
</merge>
More information on using MERGE and INCLUDE
I am trying to add a Layout to a LinaerLayout. while creating my view for the first time the layout is add nicely which means that the inflate and the addView are working well. but when I click a button to do the process a second time and add the same view another time it works but the view is add on top of the first one.
The Fragment :
// onCreateView, I bind the container with his LinearLayout
mLnPricing = (ViewGroup) mRootView.findViewById(R.id.lnPricing);
//some stuff
private void createPriceBlock() {
mParent = LayoutInflater.from(getActivity()).inflate(
R.layout.block_pricing, null);
//binding some views
mIbAddPricing.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
createPriceBlock();
}
});
mLnPricing.addView(mParent,0);
}
block_pricing.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//some text fields
</LinearLayout>
main_lauyout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
//some controllers
<LinearLayout
android:id="#+id/lnPricing"
style="#style/addLabels.AppTheme"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
</ScrollView>
I don't know what's wrong with my code but for some weird reason the same code is working on another project.
What I expect: Add the new view at the position 0 and move the other views down.
what I have: The view is add on top of the previews one and a blank space is created below the view
The default orientation for a LinearLayout is horizontal. You've not specified an orientation for the LinearLayout that the new Views are being added to, so the old ones are being pushed off-screen to the right. Add android:orientation="vertical" to the lnPricing LinearLayout tag.
I want to create a layout like this:
I think the ScrollView is necessary, maybe like this:
<RelativeLayout>
<RelativeLayout id="fixed">
</RelativeLayout>
<ScrollView>
<RelativeLayout>
<Button...
<ListView....
<Button...
</RelatievLayout>
</ScrollView>
</RelativeLayout>
But it seems that add a ListView inside a ScrollView is not good idea.
Any idea to make it?
BTW, there are not only Button1 and Button2 outside the listview, there are more views, so I do not think add the views as foot or head is a good idea.
This is xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="#+id/rlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
Java code
LinearLayout llt = (LinearLayout)findViewById(R.id.rlt);
for(int i=0;i<12;i++)
{
final int k =i;
LinearLayout ll = new LinearLayout(this);
ll.setId(i);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText(" name:"+i);
ll.addView(tv);
ll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("This is the printed text no"+k);
}
});
//ll.setBackgroundColor(Color.BLACK);
llt.addView(ll);
}
You can create your own ListView like this
First idea (not recommanded): Adding a ListView inside a ScrollView is a bad idea, because both are scrollable by default. You could use a LinearLayout instead of the ListView and create a custom onClickListener for the items.
<Scrollview>
<Button.../>
<LinearLayout>
<items.../>
</LinearLayout>
</Scrollview>
Second idea (recommanded) is to go with one ListView and create a custom Adapter extending BaseAdapter returning the Views for each row and holding the data. This means adding your Buttons and Items to the ListView and create a custom OnClickListener and Adapter to hold you Model. This Is the way I have used and I would do it again this way.
I your Adapter, you will need to Override:
getViewTypeCount - number of types of different row-layouts.
getItemViewType the type of the row.
getView returning the view for a position, here you need to inflate the layout and set the data.
This is a related question how to create a ListView with different types of rows.
I have a UI, I build it dynamically. I should want to put some component in a xml resource file. So I do :
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
... in a file res/layout/titreitem.xml as I see anywhere. But I don't understand how get it to put inside my UI. So, inside activity.onCreate, I want to do something like :
RelativeLayout myBigOne = new RelativeLayout(this);
TextView thingFromXML = [what here ? ];
myBigOne.addView(thingFromXML);
setContentView(myBigOne);
Use a LayoutInflater....The Entire Layout can be inflated, without dynamically creating it....
LayoutInflater li = LayoutInflater.from(context);
View theview = li.inflate(R.layout.whatever, null);
Approach seems to be little incorrect. You should put RelativeLayout to the xml as your TextView made, and inflate the whole xml. Afterwards, you will be free to add views to your layout. So, do this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+androi:id/relLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
In your activity:
setContentView(R.layout.titreitem);
RelativeLayout layout = (RelativeLayout)findViewByid(R.id.relLayout);
layout.addView(...);