setting the view dynamically from the java file - android

I am trying to make the java file set the view for the activity dinamically by using a specific number of linearlayouts and setting the source images
so how can I use java code to create a specific number of Image Views
<?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="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/menuline"
android:layout_width="match_parent"
android:layout_height="85dp" >
</ImageView>
</LinearLayout>
</ScrollView>

To create an ImageView (assuming this in an Activity):
ImageView imageView = new ImageView(this);
imageView.set(...) //configure your imageView according your needs
...
imageView.setLayoutParam(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,85); //set width and height
To add your imageView to the linearLayout :
linearLayout.addView(imageView);

Related

Many layouts in Scroll View in android

I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.

ScrollView Scrollbartrack visibility

I want to remove the shadow(scrollbartrack) shown below scrollbarthumb.
But unable to do it. I tried to set android:scrollbarTrackVertical="#null"
Please ignore the black part.
You can do it using XML or code.
In XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/yourScroll"
android:scrollbars="none" <!-- add this -->
>
or in your code:
yourScrollView.setVerticalScrollBarEnabled(false);
yourScrollView.setHorizontalScrollBarEnabled(false);
Edit
You have changed the question with your new image , for that task add drawable image as your requirement to android:scrollbarThumbVertical
<ScrollView
android:scrollbarThumbVertical="#drawable/line" <--like this-->
android:id="#+id/my_scroll"
android:layout_width="match_parent"
android:layout_height="500dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp">
</LinearLayout>
</LinearLayout>
</ScrollView>
Note : My drawble image contain transparent area(in left and right sides) ,thats why its bit right from the edge

GridLayoutManager 3 columns in RecyclerView

I'm trying to make simple GridLayout with 3 columns but although I get 3 columns I get strange gap between rows. So, I get 3 images in row than one row that is empty (which it seems has height matching height of the row abov(that has images), then row of 3 images, than strange gap...etc. I expected not to get than blank gap. How to remove this blank gap? Trying to set wrap_content to layout_height on recycler view, and fragment didn't helped.
This is my code:
final GridLayoutManager layoutManager = new GridLayoutManager(this,3);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
This is xml of the activity:
<RelativeLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.adriagate.adriagateonlineandroid.activities.ImagesActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragmentImagesHeader"
android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesHeader"
tools:layout="#layout/fragment_images_header" android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragmentImagesGrid"
android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesList"
tools:layout="#layout/fragment_images_list" android:layout_width="match_parent"
android:layout_height="wrap_content" >
</fragment>
</RelativeLayout>
Notice that this layout has an important fragment called fragmentImagesGrid which has this layout:
<FrameLayout 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"
tools:context="com.adriagate.adriagateonlineandroid.fragments.ImagesList">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_view_images_one_row"
>
</FrameLayout>
This i the layout of the one element inside recycler view:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
>
<ImageView
android:id="#+id/imageViewImagesAllOneRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
tools:src="#drawable/slika_200_200"
/>
</FrameLayout>
Looks like it happens because your images have different sizes.
Change your item to LinearLayout and set its layout height and width to match_parent. Set gravity="center" (Of the linear layout).
Another issue is android:layout_height="wrap_content". The RecyclerView doesn't work well when you set the height property to wrap_content. If you still want to use RecyclerView with wrap_content, you can ckeck this solution

Adding buttons below GraphView, all inside a Fragment

I have been trying to add Button's below a GraphView, and all these elements are part of a Fragment. Tried many approaches but none of them worked properly.
This is the layout file for the Fragment (fragment_graph.xml).
<FrameLayout 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"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical"
/>
</FrameLayout>
And this is the Java code dynamically adding a graph and button, placed in the Fragment's onViewCreated (View view, Bundle savedInstanceState).
storageGraph = new LineGraphView(getActivity(), graphLabel);
storageGraph.addSeries(graphSeries); // More config calls follow
...
LinearLayout view = (LinearLayout)getView().findViewById(R.id.graph_fragment_layout);
Button button = new Button(getActivity());
button.setText("Test");
view.addView(storageGraph);
view.addView(button);
The Button is not visible though I have set orientation to vertical for the LinearLayout containing it.
EDIT - solved!
I found that nesting the graph under its own LinearLayout and the buttons under another LinearLayout, and both of these wrapped in a LinearLayout fixed the problem! The LinearLayout containing the graph must be weighted (I chose a weight of 0.8).
Layout file looks like:
<FrameLayout 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"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_wrap"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/graph_buttons"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_navigation_previous_item"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_navigation_next_item"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I've just tried it and it works. Perhaps your graph is taking all the available space, so added button is below the screen? Try to wrap your LinearLayout into a ScrollView and see if there is a button in the bottom.

Change activity background from code

I have an activity with a background:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#drawable/background_fingerboard" />
How can I change background image from code? I actually use mono, but Java samples will also be helpful.
first add LinearLayout id as in your layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayoutid"
android:minWidth="25px"
android:minHeight="25px"
android:background="#drawable/background_fingerboard"
and in code part set background as::
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayoutid);
linearLayout.setBackgroundResource(R.drawable.background_fingerboard);
In java we do like this
Give id to LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id=#+id/parentLayout
Then in code
LinearLayout layout=(LinearLayout)findViewById(R.id.parentLayout);
layout.setBackgroundColor(Color.BLUE); Or
layout.setBackgroundDrawable(d); Or
layout.setBackgroundResource(resid);
LinearLayout vi = (LinearLayout ) findViewById(<id>)
vi.setBackgroundResource(R.drawable.<id2>);
you need to provide the id to your LinearLayout in XML as well....

Categories

Resources