I wanted to set margin to View programmatically. Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<ListView
android:id="#+id/attendeelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#F6CECE" >
</ListView>
</LinearLayout>
And the method when my button onClick to call out the popup Window view:
private void openPopUp() {
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.event_attendee_pop,
null);
llAttendeeList = (LinearLayout) popupView
.findViewById(R.id.llAttendeeList);
attendeeListView = (ListView) popupView.findViewById(R.id.attendeelistview);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10, 0);
popupView.setLayoutParams(params);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, 350);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popupWindow.dismiss();
return true;
}
return false;
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
mAdapter = new ListAdapter(getActivity());
attendeeListView.setAdapter(mAdapter);
}
I tried to set margin to the popupView but no luck. The margin is not there. I wonder which part override it.
Manually set width and height of PopupWindow :
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
popupWindow.setWidth(width-10);
popupWindow.setHeight(height-10);
try to use layout it will work
LinearLayout.LayoutParams params = new LayoutParams
(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
follow this tutorial http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams
Related
I'm really new to android and I'm trying to do very basic stuff ..
I would like to have a layout which contains 2 textFields
The first one should be 100 dp height
The second one should fill the remaining height of the layout
This is what I have done for now :
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
But I don't how to say that the second one should be juste after the first one and until the the bottom of the layout.
What should I do ?
Thanks
Don't use relative layout for such task. It's slower than LinearLayout and can be easily done with LinearLayout. Use layoutWeight, like in the example below:
<?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="match_parent">
<TextView
android:layout_height="100dp" android:layout_width="match_parent"
/>
<TextView
android:layout_height="0dp" android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
In order to set layout_weight from code, do the following:
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(100));
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(Color.BLUE);
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
setContentView(linearLayout);
}
First of all you should use linear layout for this task.But if you want to put it in RelativeLayout then the solution is below
1-You should set you second textView's height to match_parent
2-RelativeLayout.END_OF will align you layout to right side of textview1.So use RelativeLayout.BOTTOM
3-RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,100); will set your textview's heght to 100 pixels (Pixel size varies with devices).So set height in dp like this
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
TRY this-
//density of device
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
// RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
RelativeLayout.LayoutParams lyp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
lyp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
RelativeLayout.LayoutParams lyp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lyp2.addRule(RelativeLayout.BOTTOM, textView1.getId());
relativeLayout.addView(imageView1, lyp2);
mainLinearLayout.addView(relativeLayout);
Hy.... I'm making a project on eclipse. In this project I made an xml layout like this :
<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"
tools:context=".AddRemoveMainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/main_layout"
>
<!-- I will add some view programatically in this line -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/b_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="addItems"
/>
<Button
android:id="#+id/b_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
android:onClick="deleteItems"
/>
</TableRow>
</LinearLayout>
</ScrollView>
And on my mainactivity :
public class AddRemoveMainActivity extends Activity {
Button add,del;
LinearLayout ll;
private String[] spinnerContent = {"Ikan","Udang","Kepiting","Kerang"};
private int count = 0;
private int line = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_remove_main);
ll = (LinearLayout) findViewById(R.id.main_layout);
}
public void addItems(View view)
{
count++;
final LinearLayout frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerContent);
final TableRow row1 = new TableRow(this);
Spinner spin = new Spinner(this);
TextView jenis = new TextView(this);
TextView keterangan = new TextView(this);
TextView total = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 13, 17, 9);
spin.setAdapter(adapter);
jenis.setText("Jenis : "+Integer.toString(count));
//jenis.setLayoutParams(params);
keterangan.setText("Keterangan : ");
total.setText("Jumlah(kg) :");
row1.addView(jenis);
row1.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin);
frame.addView(row1);
ll.addView(frame, line);
line++;
}
public void deleteItems(View view)
{
if(count > 0)
{
final LinearLayout temp = (LinearLayout)ll.findViewById(count);
temp.removeAllViews();
ll.removeView(temp);
count--;
line--;
}
}
As you can see from my mainActivity, I have added textview(jenis) and spinner(spin) to a tableRow, then I added that tableRow to a linearLayout(frame) then added that frame to the linearLayout that came from in xml file above the two buttons (Add & Del button).......
My problem here is that I just want to add a margin between my textview (jenis) and (Spinner) spin. As you can see again from my mainActivity, I have tried using :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 13, 17, 9);
...
...
...
jenis.setLayoutParams(params);
But this one is not work... And I have tried another way but it's not work too :
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
So Any idea or another way for adding margin to my textview...???
Thanks in advance... :-)
Try the below code and let me know if it works
android.widget.TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 13, 17, 9);
row1.addView(jenis, layoutParams);
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin, layoutParams);
That works:
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
// NJ forgot the last important step, set LayoutParams on the TextView:
v.setLayoutParams(mlp);
}
Please add this method and check . it worked for me
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
}
I have a gridview where each view is a videoview that show rtsp strems.
I want a circle progress bar that run until video starts.
I have a problem, video starts but than disappear, black screen. This only using relative layout, If I use Linear layout the progress bar logic works fine, but that is not what I want.
this is my adapter code for gridview
public class VideoAdapter extends BaseAdapter {
private static final String TAG = VideoAdapter.class.getName();
private Context mContext;
private GridView mGv;
private int ROW_NUMBER = 3;
private int COL_NUMBER = 3;
private String[] mvideoSrc;
public VideoAdapter(Context c, GridView gv,int col,int row,String[] videosrc) {
mContext = c;
mGv=gv;
ROW_NUMBER=row;
COL_NUMBER=col;
mvideoSrc=videosrc;
}
public int getCount() {
return ROW_NUMBER*COL_NUMBER;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new VideoView for each item referenced by the Adapter
#SuppressLint("NewApi")
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) { // if it's not recycled, initialize some attributes
gridView = new View(mContext);
gridView = inflater.inflate(it.nexera.visiamobile.R.layout.videoview_layout, null);
final ProgressBar progressBar = (ProgressBar) gridView.findViewById(it.nexera.visiamobile.R.id.grid_progress);
VideoView videoView = (VideoView) gridView.findViewById(it.nexera.visiamobile.R.id.grid_videoview);
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width=0;
int height=0;
if (android.os.Build.VERSION.SDK_INT >= 13){
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
else{
width = display.getWidth();
height = display.getHeight();
}
// Calculate ActionBar height
TypedValue tv = new TypedValue();
int actionBarHeight=0;
if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,mContext.getResources().getDisplayMetrics());
}
TextView textview = (TextView) ((Activity) mContext).findViewById (it.nexera.visiamobile.R.id.txt_grid);
int textview_height=textview.getHeight();
height=height-actionBarHeight-textview_height;
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
(width/COL_NUMBER),
(height/ROW_NUMBER));
// LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
// (width/COL_NUMBER),
// (height/ROW_NUMBER));
videoView.setLayoutParams(param);
videoView.setFocusable(false);
videoView.setFocusableInTouchMode(false);
Uri video = Uri.parse(mvideoSrc[position]);
videoView.setVideoURI(video);
progressBar.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//ProgressBar progressBar = (ProgressBar) gridView.findViewById(it.nexera.visiamobile.R.id.grid_progress);
progressBar.setVisibility(View.GONE);
mp.start();
}
});
// videoView.start();
} else {
gridView = (View) convertView;
}
return gridView;
}
//
}
and this is my layout for each item of gridview, you can ignore comment line for linear layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ProgressBar
android:id="#+id/grid_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#android:style/Widget.ProgressBar.Large"
android:layout_marginRight="5dp"
android:visibility="gone"
android:layout_centerInParent="true"
android:layout_gravity="center"
/>
<VideoView
android:id="#+id/grid_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</VideoView>
</RelativeLayout>
<!--
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ProgressBar
android:id="#+id/grid_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/Widget.ProgressBar.Large"
android:layout_marginRight="5dp"
android:visibility="gone"
android:layout_gravity="center"
/>
<VideoView
android:id="#+id/grid_videoview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</VideoView>
</LinearLayout>
-->
I would like to do this vertical line programmatically
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
This is how I create the view:
View view = new View(getActivity());
How can I add the width, height and background parameters?
Try this
View v = new View(activityContext);
v.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT), GetPixel(1, activityContext));
v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
and
// To convert pixels to dp units
public int GetPixel(float f , Context context)
{
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int)((f * displayMetrics.density) + 0.5f);
}
you can also use this:
View mView = new View(Context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
myLInearLayout.setOrientation(LinearLayout.VERTICAL);
mView .setLayoutParams(params);
mView.setBackgroundResource(android.R.color.white);
myLInearLayout.addView(mView);
Heres an example :
View mView = new View(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mView.setBackgroundResource(android.R.color.black);
mRelativeLayout.addView(mView, params);
Let me explain it with example.
Here's xml file which contains LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll" android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
Your JAVA file.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
float dpi = 0;
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dpi = (float) (dm.densityDpi / 160.0);
LinearLayout linear = new LinearLayout(
StackOverflowAnswerDemoActivity.this);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
(int) (1 * dpi));
linear.setBackgroundColor(Color.WHITE);
linear.setLayoutParams(params);
ll.addView(linear);
}
The best option if you already have an XML file is not to make changes in code but to use LayoutInflater
I have made a popup, which works fine, but I'd like to write to the TextView on it. Using the standard method doesn't work (just crashes it), despite Eclipse finding the TextView's id and not showing any problems. The popup is in another XML layout.
Generates the popup
public PopupWindow pw;
public void popUpShow() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pw = new PopupWindow(
inflater.inflate(R.layout.popup, null, false),
400,
600,
true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_layout"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAA"
>
<TextView
android:id="#+id/popupOut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
/>
<Button
android:id="#+id/popupclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close"
android:onClick="popUpHide" />
</LinearLayout>
Before creating your PopupWindow, keep a reference to the view Inflated
ViewGroup v = (ViewGroup)inflater.inflate(R.layout.popup, null, false);
pw = new PopupWindow(
v,
400,
600,
true);
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
TextView view = (TextView)v.findViewById(R.id.textView1);
If the textView is in a dialog, then the view id should be retrieved by
Dialog dialog = new Dialog(this);
TextView view = (TextView)dialog.findViewById(R.id.textView1);
Try
public void popUpShow() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout t = (LinearLayout) inflater.inflate(R.layout.item1, null, false);
PopupWindow pw = new PopupWindow(
t,
400,
600,
true);
pw.showAtLocation(t.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}