Dynamic relative layout in android - android

I am trying to have two images on tp of each other. I can have it work fine with an xml file but I would like to do this dynamically. ctdeasyone is a transparent image.
So this works fine..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/bck1"
android:src="#drawable/fish2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_gravity="center">
</ImageView>
<ImageView
android:id="#+id/bck2"
android:src="#drawable/ctdeasyone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_gravity="center">
</ImageView>
</RelativeLayout>
When I do this. only the second image shows up (it is the transparent one.) Can any of the experts advice on this? Newbbie here... This is my first question. TIA.
public class TwoPicksOnEachOther extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Grabbing the Application context
final Context context = getApplication();
RelativeLayout relativeLayout = new RelativeLayout(this);
final ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.fish2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
relativeLayout.addView(iv,lp);
// Creating transparent image
final ImageView iv2 = new ImageView(this);
iv.setImageResource(R.drawable.ctdeasytwo);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
relativeLayout.addView(iv2,lp2);
setContentView(relativeLayout);
}
}

I had to put it in the emulator and play with it for a while until I saw it:
iv.setImageResource(R.drawable.fish2);
(...)
iv.setImageResource(R.drawable.ctdeasytwo);
You're never setting the image resource for iv2!
I changed that and now I see two images as expected.

This is perfect for FrameLayout . Since you want the images right on top of each other.

Related

ImageView android abort program

I am programming a android app with ImageView and when I try to change the width and height dynamically, the app stops (abort). I am using the following commands:
RelativeLayout.LayoutParams icon_horizontal = new RelativeLayout.LayoutParams(50, 50);
mec21h_icon2.setLayoutParams(icon_horizontal);
The layout is Relative, so what is wrong?
It would be nice to have a Stack trace in order to understand what the error is.
Without this, I can just guess:
you're trying to use the wrong parent layout params
the ImageView ref is null
...
I guess you are trying to change the image dimensions after it is visible (already displayed), if so you should first call for method requestLayout();
i.e.
mImageView.requestLayout();
Re-size image on Click of a button.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rel"
tools:context="com.testingpp.scannerbarcode.MainActivity">
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="button_one"
android:src="#drawable/btn_back"
/>
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
FrameLayout layout;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageview);
layout = (FrameLayout) findViewById(R.id.rel);
}
public void button_one(View view)
{
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(50,50);
layout.setLayoutParams(params);
}
}
if you are using Framents, then don't use onClick of xml. use onClickListener in java.

CropResolutionPolicy and AdMob in andEngine not working together

I try to show ads in my game.
Here is a method I use to do that:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//public void onSetContentView(){
//super.onSetContentView();
setContentView(R.layout.activity_main);
this.adView = new AdView(this);
this.adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
this.adView.setY(50);
this.adView.setAdUnitId("here goes my id");
com.google.android.gms.ads.AdRequest adrequest = new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR).build();
this.adView.loadAd(adrequest);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(this.mRenderSurfaceView);
layout.addView(this.adView, params);
}
and XML layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hsdev.salesrep.MainActivity" >
Without layout and ad cropresolution policy woked very good. But after adding layout it is stretched a bit. Also when I tried to change it to RatioResolutionPolicy it was alligned to the left (in horizontal view, the margin was only on right side, and not on both like it is normally in andEngine). So question is: How can I use layout (because I want to show ad, and still use resoltion policies from andEngine?
Ok, so answer is simple...Just need to set gravity.:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
tools:context="com.hsdev.salesrep.MainActivity" >
</RelativeLayout>

The content of a LinearLayout isn't visible

I tried to add some GUI elements like an ImageView or a TextView to a LinearLayout programmatically. But the elements aren't displayed.
To see if a element is drawn or not, I set a different background color for each element. The result was that I can only see the background color of the LinearLayout. But why?
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
setLayoutParams(params);
setBackgroundColor(Color.RED);
imageView = new ImageView(context);
params = new LinearLayout.LayoutParams(100, 100);
imageView.setLayoutParams(params);
imageView.setBackgroundColor(Color.BLUE);
addView(imageView);
}
}
The strange thing is that I can see the red background color of the LinearLayout but in the size of the ImageView. If I add some other GUI elements like a TextView, I can see how the LinearLayout grows. But I can not see the TextView.
I'm really confused, because this not the first time I do something like this. Can u tell me what I'm doing wrong?
This is a snippet of the layout.xml file:
<LinearLayout android:layout_width="match_parent"
android:layout_height="45dp"
android:id="#+id/bottom_bar"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<FrameLayout android:id="#+id/block_edit_delete_layout"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#drawable/block_edit_delete_selector">
<ImageView android:layout_height="match_parent"
android:layout_width="wrap_content"
android:src="#drawable/block_edit_delete"
android:scaleType="fitXY"
android:contentDescription="#string/delete"/>
</FrameLayout>
<LinearLayout
android:id="#+id/block_edit_progress"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"/>
<FrameLayout android:id="#+id/block_edit_random_layout"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#drawable/block_edit_delete_selector">
<ImageView android:layout_height="match_parent"
android:layout_width="wrap_content"
android:src="#drawable/block_edit_random"
android:scaleType="fitXY"
android:contentDescription="#string/random_numbers"/>
</FrameLayout>
</LinearLayout>
The LinearLayout with the ID block_edit_progress is the container layout of multiple instances of the class MyLinearLayout. The instances are added in the code:
for(int i = 0; i < numberOfMyLinearLayouts; i++) {
MyLinearLayout v = new MyLinearLayout(getContext());
addView(v);
}
I hope this helps.
If i convert your code to xml, it would be something like:
<LinearLayout layout_width=wrap_content, layout_height = wrap_content>
<LinearLayout id= MyLinearLayout>//just an idea, syntax may be wrong
<LinearLayout layout_width= 100, layout_width=100>
<ImageView color=BLUE>
</ImageView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Whenever you call setLayoutParams on a View, parameter params you give should be parent element.
Try something like if you want linearlayout to be the parent of your linearlayout, use MATCH_PARENT for width, height if you want your view to span the width, height of view's parent
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(lp);//lp is parent view
Also try this, just in case views are getting added to right of your views, and you are not able to see them on screen
yourview.setOrientation(LinearLayout.VERTICAL);
Change the width and height of linear layout to match_parent and see how it changes. wrap_content will only show the content of the linear layout, which seems to be your problem.
I solved the problem. (Or found a workaround)
I moved the complete initialization stuff out of the constructor of the MyLinearLayout. If I then adding a View after the layout has been completely generated, everything works.
Like this:
MyLinearLayout ll = new MyLinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
ll.setLayoutParams(params);
ll.setBackgroundColor(Color.RED);
ImageView v = new ImageView(getContext());
params = new LinearLayout.LayoutParams(50, 50);
v.setLayoutParams(params);
v.setBackgroundColor(Color.GREEN);
ll.addView(v);
addView(ll);
I don't know why the other way doesn't work. Thanks for the fast answers!

Controls overlap when trying to position programmatically using relative layout (Android)

I am trying to create a fragment. In the fragment I plan to place a text view next to it a Spinner control. I tried using this code. However Spinner always is placed over textview to the left of the screen. Can anyone suggest what could be the problem?
Code:
public class FragmentTest extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.ll2, null);
RelativeLayout lref = (RelativeLayout)view.findViewById(R.id.ll2ll);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
lp.addRule(RelativeLayout.ALIGN_LEFT);
RelativeLayout rl = new RelativeLayout(getActivity());
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setLayoutParams(lp);
tv.setText("My ");
rl.addView(tv, lp);
Spinner s = new Spinner(getActivity());
lp.addRule(RelativeLayout.RIGHT_OF, tv.getId());
s.setId(2);
String currencyData[] = {"USD",
"EUR","INR"
};
ArrayAdapter<Object> currencyAdapter; currencyAdapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_spinner_item, currencyData);
currencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(currencyAdapter);
rl.addView(s,lp);
return rl;
}
}
XML file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:isScrollContainer="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/ll2ll"
android:layout_width="140dp"
android:layout_height="50dp"
android:textSize="#dimen/font_size" />
</RelativeLayout>
When a view is inflated from a XML you will not get the LayoutParams properly. Try using new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
your problem is this, you can't use the same layout parameters for adding all the layouts and modifying the lp.
because the lp is used after you have initialized all the layouts, that means you are using same parameters for all the layouts spinner and textview both.
try using different layout parameters for all the layouts
RelativeLayout.LayoutParams lp
RelativeLayout.LayoutParams lptextview
RelativeLayout.LayoutParams lpspinner
for all of them
if your problem is solved mark my answser as correct

OpenGL and Android relative layout

I already tried asking this question at Relative layout Coding, but that first attempt was not very clear.
I would like to create a relative layout over GLSurfaceView but the layout must look like this:
<RelativeLayout
android:id="#+id/View1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#raw/gi"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="64px"
android:layout_height="64px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/b1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="64px"
android:layout_height="64px"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:src="#drawable/b2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="64px"
android:layout_height="64px"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView2"
android:src="#drawable/b3" />
</RelativeLayout>
However, if I code it like they say in the answers it works but the images are still put on top of each other. (I used the getId functions to addrules)
So I'm thinking of adding the whole xml file and work that way but any time I load an xml the app stops. Here's most of the code:
public class HelloWorld extends Activity {
...
protected void onCreate(Bundle savedInstanceState) {
....
opengl = new GLSurfaceView(getApplication());
opengl.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE};
EGLConfig[] configs = new EGLConfig[1];
int[] result = new int[1];
egl.eglChooseConfig(display, attributes, configs, 1, result);
return configs[0];
}
});
renderer = new MyRenderer();
mGLView.setRenderer(renderer);
setContentView(opengl);
addContentView(findViewById(R.id.View1), new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
Bear in mind that without the last call to addContentView the app works.
It looks like I'm gonna answer my own question.
instead of addcontentview i added the following
gi=new RelativeLayout(this);
View view;
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.gi, null);
hud.addView(view);
addContentView(gi, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
and as such I succeded in adding a whole xml file above the glsurface view.
But I am wondering now on how to get an id of each child in r.layout.gi. Let's say I want to change a picture of R.id.imageView1 ??????????????
Does Anybody know how to do this with the current code?
I know this worked for me with the normal FrameLayout.
TextView counter = new TextView(getApplicationContext());
counter.setBackgroundResource(R.drawable.counter);
counter.setTextSize(50);
counter.setTypeface(null, Typeface.BOLD);
counter.setText(" 000");
addContentView(counter, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Categories

Resources