I'm trying to add this button
Button dalsi_akce = new Button(this);
dalsi_akce.setGravity(Gravity.CENTER);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
dalsi_akce.setLayoutParams(p);
setContentView(dalsi_akce);
dalsi_akce.setText("test");
button appears but is full match parent. I have this button over whole display. How to set width and height of button?
You are setting the activity's content to be a button. That's why it spans over the whole activity and is simply wrong.
Instead create your activity's layout (an xml file) and set it with setContentView. Then you can programatically add a button to the content.
Example:
your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.myLayout);
Button dalsi_akce = new Button(this);
dalsi_akce.setGravity(Gravity.CENTER);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
dalsi_akce.setLayoutParams(p);
dalsi_akce.setText("test");
viewGroup.addView(dalsi_akce);
}
main.xml:
<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"
android:id="#+id/myLayout"
tools:context=".MyActivity">
</RelativeLayout>
First of all you should define your content view as a RelativeLayout or LinearLayout, then add your button to this layout. Also You can another constructor of RelativeLayout.LayoutParams class:
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(200, 70);
actually you use this constructor :
public LayoutParams(int w, int h) {
super(w, h);
}
It would be easier if you just created your layout in layout.xml, and then customize your buttons as you wish in code. For example you could do the following:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id"#+id/left_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Left"/>
</RelativeLayout>
This would give you one button that is only as big as its contents in the top right corner.
Related
I want to create a scroll view list which would show "boxes" or more accurately custom made layouts (I'm using a custom class that extends a RelativeLayout - basically shows different pieces of information, some are static like places' working hours and some change dynamically and will be pulled from a server).
Though I encountered a problem - I (for the sake of seeing if my solution works) created 5 boxes and added them to the scroll view list but it seems like they are stacked upon each other. What's the proper way to make them appear one under another without manually tweaking their position coordinates? I was using addView() for that purpose but it doesn't work as intended for me or I use it poorly. If you know the answer, please briefly describe how this should be done.
Thanks in advance!
EDIT, here goes the code:
public class RestaurantBox extends RelativeLayout {
RestaurantBox (Context context){
super(context);
this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
TextView restaurantName = new TextView(context);
restaurantName.setText("Test Restaurant");
RelativeLayout.LayoutParams restNameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.addView(restaurantName, restNameParams);
restaurantName.setTypeface(null, Typeface.BOLD);
TextView freeSpots = new TextView(context);
freeSpots.setText("15/20");
RelativeLayout.LayoutParams freeSpotParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
freeSpotParams.topMargin = restNameParams.bottomMargin + 50;
this.addView(freeSpots, freeSpotParams);
TextView book = new TextView(this.getContext());
RelativeLayout.LayoutParams bookParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
book.setText("Book");
bookParams.setMargins(0,freeSpotParams.bottomMargin + 100,0,0);
this.addView(book, bookParams);
}
}
public class BrowseRestaurants extends AppCompatActivity {
int restaurantCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_restaurants);
Intent intent = getIntent();
String login = intent.getStringExtra("LOGIN");
TextView text = (TextView) findViewById(R.id.txtWelcomeUser);
text.setText("Welcome, " + login);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
RestaurantBox initRBox = new RestaurantBox(this);
initRBox.setTop(0);
initRBox.setBottom(300);
relativeLayout.addView(initRBox);
for(int i=1;i<5;i++){
final View view = relativeLayout.getChildAt(i-1);
RestaurantBox restaurantBox = new RestaurantBox(this);
restaurantBox.setTop(view.getBottom() + 50);
restaurantBox.setBottom(restaurantBox.getTop() + 300);
relativeLayout.addView(restaurantBox);
}
}
}
<!-- activity_browse_restaurants.xml-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context="com.konrad.rezerwacje1.BrowseRestaurants">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<RelativeLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/txtLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Logout"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/txtWelcomeUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/holo_blue_dark"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp" />
</RelativeLayout
>
you are adding view in RelativeLayout so view stacking on each other so change it to LinearLayout in activity_browse_restaurants.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
now make changes according to in BrowseRestaurants.class
replace
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
with
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants);
else will be fine if you want to change variable name its up to u, let me know if any problem
I have a custom dialog set up and I have the layout's width set to wrap_content but it is still taking the entire screen width. I even tried setting the width to 100dp and it still uses the whole screen Why does this happen?
I have set up the dialog like this:
public class DialogGoToLine extends Dialog
{
//The root view of the dialog
private static View rootView;
public DialogGoToLine(Context context, int themeResId){
super(context, themeResId);
}
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Allow the dialog to be canceled by tapping out of its bounds
setCancelable(true);
//Inflate the custom layout
LayoutInflater layoutInflater = LayoutInflater.from(HotspotHelper.getAppContext());
rootView = layoutInflater.inflate(R.layout.dialog_go_to_line, null);
//Set up the layout
//setTitle(HotspotHelper.getAppContext().getString(R.string.clientDetails));
//setTitle("Go to line");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(rootView);
final EditText editText = (EditText) rootView.findViewById(R.id.editTextGoToLine);
Button buttonGoToLine = (Button) rootView.findViewById(R.id.buttonGoToLine);
buttonGoToLine.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View p1)
{
int line = Integer.valueOf(editText.getText().toString()) - 1;
((LinearLayoutManager)LogActivity.recyclerViewLog.getLayoutManager()).scrollToPositionWithOffset(line, (int)HotspotHelper.dpToPx(30));
dismiss();
}
});
}
}
And the layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewGoToLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:labelFor="#+id/editTextGoToLine"
android:text="Go to line: "
android:background="#color/accent"
android:textColor="#color/primaryText"
android:textSize="18sp"/>
<EditText
android:id="#id/editTextGoToLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_margin="4dp"
android:layout_centerVertical="true"
android:inputType="number"
android:gravity="center"
android:textSize="18sp"/>
<Button
android:id="#+id/buttonGoToLine"
android:layout_width="wrap_content"
android:text="Go"
android:layout_margin="4dp"
android:textSize="18sp"
android:layout_height="wrap_content"/>
Edit 1
If it makes a difference, this dialog has a style in which I declared the background to this drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/colorPrimary"/>
<corners
android:radius="2dp" />
</shape>
Use have to use this method setContentView(View view, ViewGroup.LayoutParams params) instead of setContentView(rootView);
ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
// 300,
// 300);
//for coustom size
params.height = thumb_size;
params.width = thumb_size;
// set padding ,margins ,gravity as your desired
Use this setContentView(rootView, params);
I faced same issue, and i came across a solution. If you give fixed width or wrap_content to your root of your layout, it doesn't consider that. I don't know the reason behind this. Put one more parent layout inside your root and give required width and height to the inner parent, put all views inside this parent. I hope it helps.
<?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:background="#color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
//put all your views inside this linear layout
<TextView......./>
<TextView......./>
<TextView......./>
</LinearLayout>
</LinearLayout>
Try this before showing the dialog:
dialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.dialog_width), WindowManager.LayoutParams.WRAP_CONTENT)
dialog_width is 100dp.
For some reason, removing the theme that I applied to the dialog fixed the wrap_content issue. The only thing the custom theme did was set the background of the dialog, but removing it has solved the issue.
Try changing the android:orientation="vertical" in your main Linear Layout because You are using linear layout with horizontal orientation so all the widgets are aligned horizontally and they are acquiring their assigned space horizontally that's why it cover the whole screen.
I have a ViewFlipper under which i have a Linear Layout and Frame Layout.
I am adding imageViews and TextViews in Frame Layout dynamically from Java Class
Right now its displaying Text View at the top of my Activity and image View after it.
I want to it first display image View and then TextView
I am sharing an excerpt from my MainActivity.class
FrameLayout newsFrameLayout;
newsFrameLayout = (FrameLayout) findViewById(R.id.newsFrameLayout);
FrameLayout n1= newsFrameLayout;
ImageView imageView;
imageView = new ImageView(this);
n1.addView(imageView);
titleTextView= new TextView(this);
RelativeLayout.LayoutParams textViewParam = new RelativeLayout.LayoutParams(SlidingPaneLayout.LayoutParams.WRAP_CONTENT, SlidingPaneLayout.LayoutParams.WRAP_CONTENT);
// textViewParam.gravity= Gravity.BOTTOM;
n1.addView(titleTextView,textViewParam);
Layout File
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/newsSliderView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/newsLinearLayout">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:id="#+id/newsFrameLayout">
</FrameLayout>
</LinearLayout>
</ViewFlipper>
Your views are added with the default gravity which is top. You need to specify the LayoutParams with which your want the views to be added. Tο get the textview to the bottom you do this:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
titleTextView.setLayoutParams(params);
n1.addView(titleTextView);
I want to set the buttons one after another vertically. I was trying something like below, but it doesn't work. Need help please.
RelativeLayout body=(RelativeLayout) findViewById(R.id.body);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btn;
List<Button> allEds = new ArrayList<Button>();
for(int i=0;i<totalSID;i++){
btn = new Button(FaqList.this);
btn.setId(i);
allEds.add(btn);
if(i==0){}
else
buttonParams.addRule(RelativeLayout.BELOW,allEds.get(i-1).getId());
body.addView(btn,buttonParams);
}
Adding the xml file. For reference you can go through it.
<?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">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:animateLayoutChanges="true">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout
I am not sure where is the problem actually
If you want to add buttons vertically then just use LinearLayout instead of RelativeLayout in your body.
int iNumberOfButtons = productTypeList.size();
Button[] dynamicButtons = new Button[iNumberOfButtons];
LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < iNumberOfButtons; i++) {
ProductType productType = productTypeList.get(i);
dynamicButtons[i] = new Button(getActivity());
dynamicButtons[i].setText(productType.getTitleString());
dynamicButtons[i].setId(i);
dynamicButtons[i].setTextSize(15.0f);
dynamicButtons[i].setOnClickListener(this);
dynamicButtons[i].setLayoutParams(paramsButton);
dynamicButtonsLinearLayout.addView(dynamicButtons[i]); // dynamicButtonsLinearLayout is the container of the buttons
}
Use linear layout instead of Relative layout. If you need to add multiple button dynamically use RecyclerView based on Item position you can do whatever you need
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button bt=new Button(this);
bt.setLayoutParams(lparams);
this.m_vwJokeLayout.addView(bt);
you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this :
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
I have the following 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"
tools:context="es.xxx.xxx.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#CCFF0000"
android:id="#+id/lyNetworkError">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No hay conexión a internet"
android:textAlignment="center"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"/>
</RelativeLayout>
In its FrameLayout the app will load other fragments.
This is the onCreate code of activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Constants.setAppContext(this);
setContentView(R.layout.activity_main);
Log.d("LoadFragment", "1 "+ loadFragment);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit();
}
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
fragmentManager = getSupportFragmentManager();
lyNetworkError = (LinearLayout) findViewById(R.id.lyNetworkError);
}
The problem is that LinearLayout (That contains TextView) doesn't show (is posible that fragment render over LinearLayout, because if I remove getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit(); the LinearLayout appears)
So, how can I show the LinarLayout over fragment (loaded inside FrameLayout)?
If the LinearLayout and your Fragments are displaying in the correct location on screen when each is shown individually, then you can simply reverse the order of of the FrameLayout and LinearLayout in your XML.
The problem is that RelativeLayout allows its children to overlap. The last item in the RelativeLayout will appear "above" or "on top" of other items in the layout. Since you haven't specified any layout constraints for your views, the RelativeLayout puts them both in the default position, which is the top left corner. Since your FrameLayout is set to fill the parent view's width and height, it will overlay everything else.
If you actually want the LinearLayout to appear above the FrameLayout, then you can use RelativeLayout's positioning properties (explained very well here) to position your views.
Specifically, you would be looking for something like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/lyNetworkError"
android:id="#+id/container"/>
The android:layout_below attribute tells the FrameLayout that you want it to always be below the view with ID lyNetworkError (below as with text on a piece of paper, not in 3-dimensional space).