Transparent activity is there in my app and I want to give left, right, top margin to this activity. For this I am using WindowManager.LayoutParams. But there I am not able to give Gravity top, right, left at a time. There is only two parameter x and y. If I want to set left, right and top then what I need to do.
Code:-
public class SelectedRecipient extends Activity {
private Context context;
private WindowManager.LayoutParams wmParams=null;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("*** SelectedRecipient ***");
context = SelectedRecipient.this;
WindowManager.LayoutParams wmParams = this.getWindow().getAttributes();
wmParams.gravity = Gravity.TOP | Gravity.LEFT;
wmParams.x = 50; // x position
wmParams.y = 40; // y position
ArrayList<String> aListNoOfSelection = new ArrayList<String>();
aListNoOfSelection.add("Vinit");
aListNoOfSelection.add("Vikash");
aListNoOfSelection.add("Jonson");
aListNoOfSelection.add("Nikolesh");
ArrayAdapter<String> aAdapterNoOfSelection = new NoOfSelectionAdapter(context, 0, aListNoOfSelection);
listView = new ListView(context);
listView.setAdapter(aAdapterNoOfSelection);
setContentView(listView);
}
}
I also tried this(which not wwork):-
LayoutParams layoutParams = new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 0, 50, 0);
listView.setLayoutParams(layoutParams);
You probably want to use a dialog theme like answered above, if so...
put android:theme="#android:style/Theme.Dialog" inside your AndroidManifest.xml
or by calling: setTheme(android.R.style.Theme_Dialog); before calling setContentView(); in your onCreate()
if not you can use something like:
ViewGroup.MarginLayoutParams wmParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wmParams.setMargins((int left),(int top),(int right),(int bottom));
or wmParams.bottomMargin , wmParams.leftMargin , wmParams.rightMargin , wmParams.topMargin
Related
I'm trying to create a view that contains TextView (on the left) and Button (on the right) and a SeekBar at the Bottom, something like this.
|-----------------------------------|
| <TextView> <Button> |
| < SeekBar > |
|-----------------------------------|
Note that angle brackets represents the width, just used for demonstration.
I can do that by creating a CompoundView but I wanted to keep things flat.
I'm going to create others similar to this one and there will be a lot of these.
Please ask for any further clarifications (if needed).
Thank you.
What about using a Framelayout and adding your views on the go?
public class Cell extends FrameLayout {
private TextView tv;
private Button btn;
private ProgressBar progressBar;
public Cell (Context context, int width, int height) {
super(context);
getLayoutParams().width = width;
getLayoutParams().height = height;
tv = new TextView(context);
FrameLayout.LayoutParams tvParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP|Gravity.LEFT);
tv.setLayoutParams(tvParams);
btn = new Button(context);
FrameLayout.LayoutParams btnParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP|Gravity.RIGHT);
btn.setLayoutParams(btnParams);
progressBar = new ProgressBar(context);
FrameLayout.LayoutParams progressParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
progressBar.setLayoutParams(progressParams);
addView(tv);
addView(btn);
addView(progressBar);
}
public void setText (String text) {
tv.setText(text);
}
public void setProgress (int progress) {
progressBar.setProgress(progress);
}
}
Note that you might need to use other constructors as well.
I have tried to create a dialog that occupies the full screen width from the old AlertDialog builder to the new DialogFragment approach in the onCreateView() and onViewCreated() to get the displayed dialog to occupy the full width of the screen. I can certainly get the width and height values of the screen but regardless of how I try to force the dialog to use these values, they are ignored. The displayed dialog is always the same width regardless of orientation.
In my latest attempt I have an xml layout that I inflate. I need to use a custom view so I cannot define that view in xml. So I add it.
Here is the most current attempt I have in my DialogFragment code. Of course this is just one of many attempts I have made trying to follow hints from posts and Slidenerd videos.
public class PopupDialog extends DialogFragment implements View.OnClickListener
{
private static final String TAG = PopupDialog.class.getName();
Button cancel = null;
Button focus = null;
View viewInput = null;
int width;
int height;
int id;
public PopupDialog()
{
}
public PopupDialog(View v, int id, int width, int height)
{
viewInput = v;
this.id = id;
this.width = width;
this.height = height;
}
#Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstance)
{
Log.d(TAG, "onCreateView of DialogFragment called.");
View viewDialog = inflator.inflate(R.layout.popup_dialog, null);
// RelativeLayout relativeLayout = (RelativeLayout)viewDialog;
// LayoutParams params = new LayoutParams(width, height);
// relativeLayout.setLayoutParams(params);
// Point point = new Point();
// Activity activity = getActivity();
// activity.getWindowManager().getDefaultDisplay().getSize(point);
// if(point.x > point.y)
if(width > height)
{
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
ViewParent parent = viewInput.getParent();
if(parent != null)
{
Log.d(TAG, "View already present. Removing.");
((ViewGroup)parent).removeView(viewInput);
}
LayoutParams params = new LayoutParams(width, height);
viewInput.setLayoutParams(params);
((ViewGroup)viewDialog).addView(viewInput, 0);
cancel = (Button)viewDialog.findViewById(R.id.btn_cancel);
focus = (Button)viewDialog.findViewById(R.id.btn_focus);
cancel.setOnClickListener(this);
focus.setOnClickListener(this);
setCancelable(false);
return viewDialog;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated of DialogFragment called.");
//getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, height);
getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 10; //x position
wmlp.y = 450 * (id) + 10;
// wmlp.width = width;
// wmlp.height = height;
}
I am plotting a sine wave. The view has the correct size as the sine wave has a range of 0 to 12 but in the landscape orientation the displayed dialog box only gets a little more than half way, so 0 to 6 + is seen and then one has to wait for the wave to recycle as it plots from 6 to 12 before it becomes visible again when it goes back to 0. I AM able to place the dialog box upper left hand corner.
Does anyone know how to solve this problem? I went to the fragment because I was led to believe that the canned AlertDialog approach was fixed in width and there was nothing one could do. I am facing the same limitation with the DialogFragment.
try adding this code in on create() method after setContentView
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I gave up and created my graph in a ListView in a ViewFlipper. Not want I wanted but I got more real estate for the graph.
I currently add images through xml with the whole R.id.x method with the following function:
public void Image(int ID, int x, int y){
ImageView iv = (ImageView) findViewById(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
}
I've written a new function to get these images on screen programmatically instead of parsing them in XML, with help from afore-mentioned topics/questions I searched and came up with this:
public void ImageRAW(int ID, int x, int y){
ImageView iv = new ImageView(c);
iv.setImageResource(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
rl.addView(iv);
}
But it did not work. I also tried adding the following line, to no avail: iv.setVisibility(View.VISIBLE);
And in regards to the variables rl and c:
private Context c;
private RelativeLayout rl;
public void SetUtilContext(Context context){
c = context;
rl = new RelativeLayout(context);
}
The above function is called in every Activity's onCreate() function and sets the UtilLib's current Context and RelativeLayout for drawing accordingly.
The function ImageRAW() is something I would like to use to replace the old Image() function, to make things easier for me. How would/could I get this working?
Try add this before your SetUtilContext():
RelativeLayout menu = findViewById(R.layout.menu);
And this at the end of your ImageRAW() method:
menu.addChild(rl);
As per Incredible's request, my onCreate function where I'm testing the functions:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
SetUtilContext(this);
Image(R.id.logo, 0, 0);
GetScreenSize();
ImageButton(R.id.start_btn
, (screenWidth/2)-(GetImageWidth(R.id.start_btn)/2)
, 48+GetImageHeight(R.id.logo));
ImageButton(R.id.options_btn
, (screenWidth/2)-(GetImageWidth(R.id.options_btn)/2)
, 96+GetImageHeight(R.id.logo)+GetImageHeight(R.id.start_btn));
ImageButton(R.id.about_btn
, (screenWidth/2)-(GetImageWidth(R.id.about_btn)/2)
, 144+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*2));
ImageButton(R.id.exit_btn
, (screenWidth/2)-(GetImageWidth(R.id.exit_btn)/2)
, 192+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*3));
PlayAudio(R.raw.theme, true);
ImageRAW(R.drawable.head, 0, GetImageHeight(R.id.logo));
}
I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);
Alright. So I start my activity in Main, grab the FragmentManager and instantiate a Fragment which needs to return a View. OK. So I extended a LinearLayout in order to have something to return. My Activity and Fragment are happy but I am not.
Three LinearLayouts which I create in the parent ViewGroup are there (code below). I have verified this by counting children and by setting the background colors to contrast one another. The parent also changes size depending on how tall I make the children (when I don't declare any LayoutParams on the parent).
public class Mainmenu extends LinearLayout {
private ArrayList<LinearLayout> panes = new ArrayList<LinearLayout>();
private Context context;
private final int
LEFT = 0, CENTER = 1, RIGHT = 2;
public Mainmenu(Context c) {
super(c);
context = c;
setBackgroundColor(Color.WHITE);
setOrientation(HORIZONTAL);
setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
for(int i=0;i<=RIGHT;i++){ //Create the (3) Panes
LinearLayout ll = new LinearLayout(context);
ll.setLayoutParams(
new LinearLayout.LayoutParams(300,
LinearLayout.LayoutParams.FILL_PARENT));
switch(i){
case LEFT | RIGHT:
ll.setBackgroundColor(Color.DKGRAY);
default:
ll.setBackgroundColor(Color.BLACK);
}
ll.setOrientation(LinearLayout.VERTICAL);
ll.setVisibility(VISIBLE);
ll.setWillNotDraw(false);
panes.add(i, ll);
addView(ll);
}
LinearLayout.LayoutParams buttons =
new LinearLayout.LayoutParams(100, 50);
buttons.setMargins(15, 5, 5, 0);
TextView tv1 = new TextView(context);
tv1.setText("hello");
tv1.setTextColor(Color.RED);
panes.get(LEFT).addView(tv1, buttons);
Button button = new Button(context);
button.setText("Launch Editor");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
panes.get(CENTER).addView(button);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
Nothing is null, all my elements (3 ViewGroups and 2 Views) are present in the tree but not visible. I've tried bringing children to the front through the parent and the children, creating them in different super.methods and invalidating the view in a similarly shotgunned fashion. What's going on? Is it as simple as not having any idea what I'm doing?
The problem is simply because you are overriding onLayout and doing nothing with it. You only need to override this if you want to layout the children yourself (ie, you were designing some unique custom layout). In this case just remove that method, or call super.onLayout.