How do I programmatically remove an existing rule that was defined in XML?

You can’t remove a rule because all rules are always stored in a fixed-size java array. But you can set a rule to 0. For example layoutParams.addRule(RelativeLayout.RIGHT_OF, 0); layoutParams.addRule(RelativeLayout.BELOW, R.id.new_ref_LinearLayout); EDIT (thanks to Roger Rapid): As of API level 17, the class RelativeLayout.LayoutParams has the following method: public void removeRule(int verb) So you can remove … Read more

Moving from one activity to another Activity in Android

First You have to use this code in MainActivity.java class @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),NextActivity.class); startActivity(i); } You can pass intent this way. Second add proper entry into manifest.xml file. <activity android:name=”.NextActivity” /> Now see what happens.

Extending RelativeLayout, and overriding dispatchDraw() to create a zoomable ViewGroup

If you are applying a scale factor to the drawing of your children, you also need to apply the appropriate scale factor to all of the other interactions with them — dispatching touch events, invalidates, etc. So in addition to dispatchDraw(), you will need to override and appropriate adjust the behavior of at least these … Read more

Circular dependencies cannot exist in RelativeLayout, android?

The problem is caused because there is a circular reference in the layout parameters. For example, when view B is layout_below view A, view A can’t reference view B anymore in it’s below, alignRight etc. This can also exist between multiple views: A references B references C. In that scenario C can’t reference A because … Read more

Can I Set “android:layout_below” at Runtime Programmatically?

Yes: RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.BELOW, R.id.below_id); viewToLayout.setLayoutParams(params); First, the code creates a new layout params by specifying the height and width. The addRule method adds the equivalent of the xml properly android:layout_below. Then you just call View#setLayoutParams on the view you want to have those params.

How to create a RelativeLayout programmatically with two buttons one on top of the other?

I have written a quick example to demonstrate how to create a layout programmatically. public class CodeLayout extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Creating a new RelativeLayout RelativeLayout relativeLayout = new RelativeLayout(this); // Defining the RelativeLayout layout parameters. // In this case I want to fill its parent RelativeLayout.LayoutParams rlp … Read more

What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?

LinearLayout means you can align views one by one (vertically/ horizontally). RelativeLayout means based on relation of views from its parents and other views. ConstraintLayout is similar to a RelativeLayout in that it uses relations to position and size widgets, but has additional flexibility and is easier to use in the Layout Editor. WebView to … Read more

Overlapping Views in Android

Android handles transparency across views and drawables (including PNG images) natively, so the scenario you describe (a partially transparent ImageView in front of a Gallery) is certainly possible. If you’re having problems it may be related to either the layout or your image. I’ve replicated the layout you describe and successfully achieved the effect you’re … Read more

Create a new TextView programmatically then display it below another TextView

If it’s not important to use a RelativeLayout, you could use a LinearLayout, and do this: LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); Doing this allows you to avoid the addRule method you’ve tried. You can simply use addView() to add new TextViews. Complete code: String[] textArray = {“One”, “Two”, “Three”, “Four”}; LinearLayout linearLayout = new … Read more

tech