This is my onBindViewHolder method:
#Override
public void onBindViewHolder(#NonNull final mViewHolder h, int i) {
final JSON data = jdata[i];
if(data .getName() != null && data .getStatus() !=null) {
h.textcontainer.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 1.0f
);
h.textcontainer.setLayoutParams(params);
h.title.setText(feed.getName());
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 2
);
h.textcontainer.setVisibility(View.GONE);
h.playerView.setLayoutParams(params);
}
and this my XML:
<LinearLayout
android:id="#+id/videopost_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:id="#+id/video_textcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="#dimen/feed_item_padding_left_right"
android:paddingRight="#dimen/feed_item_padding_left_right"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom"
android:layout_weight="1"
>
<TextView
android:id="#+id/video_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_item_profile_name"
android:textStyle="bold"
android:textColor="#color/background"
android:paddingStart="#dimen/feed_item_profile_info_padd"
android:paddingEnd="#dimen/feed_item_profile_info_padd"
/>
</LinearLayout>
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exo_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:show_buffering="true"
android:layout_weight="1"
>
</com.google.android.exoplayer2.ui.PlayerView>
My Adapter has a textview in it, when the json data = null, I obviously want to hide that TextView without any spacing left, and when the data (title for the video) isn't null, I want to show the text.
The current codes hides the TextView, but leaves an empty space. If I delete the following two lines from the if-statemant:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 1.0f
);
h.textcontainer.setLayoutParams(params);
Then there's no empty space left, but the TextView won't display if the data isn't empty. I'm pretty sure this is some stupid error somewhere, but I'm not able to find it. SOS :-)
Firstly, you have two individual layouts LinearLayout and PlayerView sharing a weight sum of two (2) making it a 50:50 distribution. Whether the child view of the LinearLayout is gone the 50% space would still remain until it's given out.
Hence any time you need to hide textview/title completely and remove not-needed space, dynamically set the weight of PlayerView to two (2) and make the visibility of LinearLayout gone.
The LinearLayout around your TextView is redundant. You can simplify this to simply be a single LinearLayout with two children. Set the height to wrap_content and do not specify a weightSum. When the TextView's visibility is set to GONE, PlayerView will be the only visible child and your LinearLayout will match its size.
<LinearLayout
android:id="#+id/videopost_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical">
<TextView
android:id="#+id/video_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_item_profile_name"
android:textStyle="bold"
android:textColor="#color/background"
android:paddingStart="#dimen/feed_item_profile_info_padd"
android:paddingEnd="#dimen/feed_item_profile_info_padd"
/>
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exo_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:show_buffering="true"/>
</LinearLayout>
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 fragment inside a ViewPager and am trying to dynamically change the height of a ListView depending on the size of the screen.
Here is the xml code for my fragment:
Fragment.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RelativeLayout
android:id="#+id/rlDiscoveredDevice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tvSuggestBTOn">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDiscoveredDevices"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="#string/text_list_discovered_devices"
/>
<ProgressBar
android:id="#+id/pbDiscoveredDevices"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/tvDiscoveredDevices"
android:layout_marginStart="16dp"
/>
<ListView
android:id="#+id/lstDiscoveredBTDevices"
android:layout_height="250dp"
android:layout_width="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/list_view_divider_height"
android:choiceMode="singleChoice"
android:listSelector="#color/list_item_selected"
android:layout_below="#+id/tvDiscoveredDevices"
android:layout_marginTop="#dimen/list_view_margin_top"
/>
</RelativeLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pairBT"
android:background="#drawable/ic_action_down"
android:layout_marginStart="134dp"
android:layout_below="#+id/rlDiscoveredDevice"
android:layout_alignParentStart="true"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unpairBT"
android:background="#drawable/ic_action_up"
android:layout_below="#+id/rlDiscoveredDevice"
android:layout_toEndOf="#+id/pairBT"
android:layout_marginStart="73dp"
/>
<RelativeLayout
android:id="#+id/rlPairedDevice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/pairBT">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPairedDevices"
android:text="#string/text_list_paired_devices"
android:layout_alignParentStart="true"
/>
<ListView
android:id="#+id/lstPairedBTDevices"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/list_view_divider_height"
android:choiceMode="singleChoice"
android:listSelector="#color/list_item_selected"
android:layout_below="#+id/tvPairedDevices"
android:layout_alignParentStart="true"
android:layout_marginTop="#dimen/list_view_margin_top"
/>
</RelativeLayout>
</RelativeLayout>
Here is my java code that I use to dynamically change the height:
DiscoveredDevice.java
public class DiscoveredDevice extends Fragment{
final String TAG = "DiscoverDevice Fragment";
private SharedPreferences appPrefs;
private BTActions btActions;
private ArrayList<BluetoothDevice> arrDiscoveredDevicesList;
private Set<BluetoothDevice> arrPairedDevicesList;
private ArrayAdapter<String> btDiscListArrayAdapter;
private ArrayAdapter<String> btPairedListArrayAdapter;
private String strDiscoveredListItemSelected = "";
private String strPairedListItemSelected = "";
private CommonFunctions cf;
private boolean blnIsFragmentLoaded = false;
// UI Objects
private TextView tvDiscoveredDevices;
private TextView tvPairedDevices;
private ListView lvDiscoveredList;
private ListView lvPairedDevicesList;
private ImageButton ibtnPair;
private ImageButton ibtnUnPair;
private ProgressBar pbDiscDevicesSpinner;
private TextView tvSuggestBTOn;
private ProgressBar pbLoading;
public DiscoveredDevice() {
btActions = new BTActions();
cf = new CommonFunctions();
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Begin render of Discovered Device fragment...");
super.onCreate(savedInstanceState);
// Define variables
appPrefs = this.getActivity().getPreferences(Context.MODE_PRIVATE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_discovered_device, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Define the lists on DiscoveredDevice fragment
btDiscListArrayAdapter = new ArrayAdapter<>(getContext(), R.layout.simple_row, R.id.simple_row_Txt);
btPairedListArrayAdapter = new ArrayAdapter<>(getContext(), R.layout.simple_row, R.id.simple_row_Txt);
// Define UI Objects
defineUIObjects();
// Position UI objects
positionUIObjects();
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
private void defineUIObjects() {
tvDiscoveredDevices = (TextView) getView().findViewById(R.id.tvDiscoveredDevices);
tvPairedDevices = (TextView) getView().findViewById(R.id.tvPairedDevices);
lvDiscoveredList = (ListView) getView().findViewById(R.id.lstDiscoveredBTDevices);
lvPairedDevicesList = (ListView) getView().findViewById(R.id.lstPairedBTDevices);
ibtnPair = (ImageButton) getView().findViewById(R.id.pairBT);
ibtnUnPair = (ImageButton) getView().findViewById(R.id.unpairBT);
tvSuggestBTOn = (TextView) getView().findViewById(R.id.tvSuggestBTOn);
pbDiscDevicesSpinner = (ProgressBar) getView().findViewById(R.id.pbDiscoveredDevices);
pbLoading = (ProgressBar) getView().findViewById(R.id.spin_kit_progress);
pbLoading.setIndeterminateDrawable(new DoubleBounce());
}
private void positionUIObjects() {
final ViewGroup vgDiscDevice = (ViewGroup) getView().findViewById(R.id.rlDiscoveredDevice);
final AtomicInteger aiLayoutHeight = new AtomicInteger();
Rect rect = new Rect();
// Get the window
Window win = getActivity().getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Find height of AppBarLayout
AppBarLayout ablTabs = (AppBarLayout) getActivity().findViewById(R.id.ablTabs);
// Obtain the screen height & width
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int intScreenHeight = metrics.heightPixels;
int intScreenWidth = metrics.widthPixels;
Log.i(TAG, "Actual Screen Height = " + intScreenHeight + " Width = " + intScreenWidth);
// Set the height for Discovered Devices list
RelativeLayout.LayoutParams rlParams = (RelativeLayout.LayoutParams) getView().findViewById(R.id.rlDiscoveredDevice).getLayoutParams();
// Get height of Discovered Devices relative layout
int intDiscoveredDevicesRLHeight = (int)(Math.round((intScreenHeight - rect.top - ablTabs.getMeasuredHeight()) * 0.45));
Log.i(TAG, "Setting the height of Discovered Devices Relative layout as '" + intDiscoveredDevicesRLHeight + "'");
rlParams.topMargin = ablTabs.getMeasuredHeight();
rlParams.leftMargin = 50; // I DID THIS JUST TO CHECK IF THE LEFT MARGIN GETS MOVED TO THE RIGHT. THIS IS WHERE I NEED A BETTER WAY TO PROPERLY ALIGN THE LIST
rlParams.height = intDiscoveredDevicesRLHeight;
lvDiscoveredList.setLayoutParams(rlParams);
}
I want each list to occupy 45% of the screen(excluding the AppBarLayout). If you see the below screenshot, when I set the new height, the ListView goes out of alignment and part of it gets cuts to the left of the screen. I have set the Left margin to 50 to bring it into view.
[Screenshot]
I have placed 2 ListViews inside a RelativeLayouts so that they can be individually controlled as a whole. Am I doing something wrong here ?
I think you can achieve this with just xml, no dynamic resizing!
In your post you mentioned that you want the list views to each take 45% of the available height. Thus, I am assuming that the center content with the image views will take 10% of the height (though this approach will also work if they had a fixed height, I'll include that answer too at the bottom.
All you need to do is change your top level layout to a LinearLayout (with a vertical orientation), put your image views inside of a LinearLayout (with horizontal orientation), change some of the heights of your views and add layout_weight attributes to the two RelativeLayouts and the inner LinearLayout you will create. Here is an example of what your xml will look like:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RelativeLayout
android:id="#+id/rlDiscoveredDevice"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="#+id/tvSuggestBTOn"
android:layout_weight="9">
<TextView
android:id="#+id/tvDiscoveredDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/text_list_discovered_devices"
/>
<ProgressBar
android:id="#+id/pbDiscoveredDevices"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/tvDiscoveredDevices"
/>
<ListView
android:id="#+id/lstDiscoveredBTDevices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tvDiscoveredDevices"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/list_view_divider_height"
android:choiceMode="singleChoice"
android:listSelector="#color/list_item_selected"
android:background="#drawable/abc_list_selector_disabled_holo_dark"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="2">
<ImageButton
android:id="#+id/pairBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_down"
/>
<ImageButton
android:id="#+id/unpairBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_up"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/rlPairedDevice"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPairedDevices"
android:text="#string/text_list_paired_devices"
android:layout_alignParentStart="true"
/>
<ListView
android:id="#+id/lstPairedBTDevices"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/list_view_divider_height"
android:choiceMode="singleChoice"
android:listSelector="#color/list_item_selected"
android:layout_below="#+id/tvPairedDevices"
android:layout_alignParentStart="true"
android:background="#drawable/abc_list_selector_disabled_holo_dark"
/>
</RelativeLayout>
</LinearLayout>
The important thing to note here is that the RelativeLayouts now have a weight of 9 each and the center linear layout has a weight of 2. Thus the space in the top level linear layout will be divided in that ratio, the RelativeLayouts will get 45% each and the LinearLayout will get 10%.
If you wanted the linear layout in the center to be wrap content instead of taking 10% of the screen (I would recommend this) then you could go ahead and assign it a height of wrap_content and remove the layout_weight attribute from it. The top level LinearLayout will then take the leftover height after allocating space for the center LinearLayout and divide it evenly between the two Relative layouts.
PS: (Heads up, you can probably use the xml I posted here. I set backgrounds on the list views to make it easy for me to see their sizes without data, make sure to remove those).
PPS: Note that this approach allows you to remove a lot of the layout positioning attributes that you had with a top level relative layout! This not only improves the cleanliness of your code, but also makes your UI more performant (Relative Layouts are less performant than other view groups, especially when nested).
I'm currently working on a server-client android application, and in one of my views I have a HorizontalScrollView that contains a vertical LinearLayout. This LinearLayout will contain a bunch of TextViews that are customized with a background. It works fine until the TextViews get a new row because of the character length, it looks like this:
http://puu.sh/9L127/bf29a86639.jpg
The idea is that the text is supposed to fit in the background, is there any way that I can keep the TextView position like the other ones, even though it has multiple rows?
I hope I made myself clear enough, this is how the TextViews are set up in the code:
public void setupCard(GameCard card) {
final int CARD_WIDTH = 200;
final int CARD_HEIGHT = 100;
m_card = card;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(CARD_WIDTH, CARD_HEIGHT);
lp.setMargins(0, 0, 5, 0);
super.setText(m_card.getContent());
super.setBackgroundResource(R.drawable.cardbg);
super.setLayoutParams(lp);
super.setGravity(Gravity.CENTER);
}
And this is how the activity is set up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/bng"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/cardLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Thanks in advance!
Thats because the LinearLayout alignes the text baselines of the TextViews.
Try to specify
android:baselineAligned="false"
for the LinearLayout.
I have Vertical ScrollView where I am going to add some Views after a buttonClick. Here is the xml of the scroll view
<ScrollView
android:background = "#drawable/border"
android:layout_weight="31"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<LinearLayout
android:id="#+id/layoutScroll"
android:padding = "10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layoutContainer"
android:padding="5dp"
android:orientation="vertical"
android:background="#drawable/border_2"
android:layout_width="fill_parent"
android:layout_height = "wrap_content">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent">
<TextView
android:id="#+id/textWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/word"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textNumber"
android:background = "#drawable/border_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text=" 1 "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<EditText
android:id="#+id/editWord"
android:layout_marginTop="3dp"
android:layout_gravity="left"
android:inputType="text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:scrollbars="horizontal"/>
<TextView
android:id="#+id/textDefinition"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "#string/definition" />
<EditText
android:id="#+id/editDefinition"
android:gravity="top|left"
android:inputType="textMultiLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
android:maxLines="3"
android:scrollbars="vertical"/>
</LinearLayout>
<TextView
android:id="#+id/textEmpty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
In this function I create the same objects for adding them to the LinearLayout
private void initInterface (){
layoutScroll = (LinearLayout) findViewById (R.id.layoutScroll);
layoutContainer = new LinearLayout(getApplicationContext());
relativeLayout = new RelativeLayout(getApplicationContext());
textWord = new TextView(getApplicationContext());
textDefinition= new TextView(getApplicationContext());
textNumber = new TextView(getApplicationContext());
textEmpty = new TextView(getApplicationContext());
editWord = new EditText(getApplicationContext());
editDefinition = new EditText(getApplicationContext());
layoutContainer.setPadding(5, 5, 5, 5);
layoutContainer.setOrientation(LinearLayout.VERTICAL);
layoutContainer.setBackgroundResource(R.drawable.border_2);
LinearLayout.LayoutParams param1 =
new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT );
layoutContainer.setLayoutParams(param1);
RelativeLayout.LayoutParams param2 =
new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(param2);
textWord.setLayoutParams(param2);
textWord.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textWord.setText("Word:");
RelativeLayout.LayoutParams param3=
new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
param3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textNumber.setBackgroundResource(R.drawable.border_1);
textNumber.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textNumber.setLayoutParams(param3);
relativeLayout.addView(textWord);
relativeLayout.addView(textNumber);
LinearLayout.LayoutParams param4 = param1;
param4.setMargins(0, 3, 0, 0);
editWord.setGravity(Gravity.LEFT);
editWord.setTextAppearance(this,android.R.style.TextAppearance_Medium);
editWord.setLines(1);
editWord.setHorizontallyScrolling(true);
editWord.setLayoutParams(param4);
textDefinition.setLayoutParams(param1);
textDefinition.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textDefinition.setText("Definition:");
editDefinition.setGravity(Gravity.TOP | Gravity.LEFT);
editDefinition.setSingleLine(false);
editDefinition.setLayoutParams(param1);
editDefinition.setMaxLines(3);
editDefinition.setLines(3);
editDefinition.setVerticalScrollBarEnabled(true);
editDefinition.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
textEmpty.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textEmpty.setLines(1);
textEmpty.setLayoutParams(param1);
layoutContainer.addView(relativeLayout);
layoutContainer.addView(editWord);
layoutContainer.addView(textDefinition);
layoutContainer.addView(editDefinition);
}
And then I add them to the Layout like this.
public void onCLick_Add(View v){
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}
The problem is that It only workd once . When I click the button for the second time the app crashes.
Thank you in advance.
Your log says:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
You are trying to add same layoutContainer and textEmpty multiple times when clicking multiple times. This fails because they are already added and therefore already have a parent. (You cannot add same View to more then one parent)
Solution
I guess you want to create a new View every time you press the button and add this to your layoutScroll. You should call your init-method again for every click:
public void onCLick_Add(View v){
initInterface();
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}
I would also suggest to divide initInterface() into:
private View createLayoutContainer();
private View createTextEmpty();
Make sure you take initialization of scrollView outside (e.g. onCreate) and you declare views like layoutContainer localy in the createLayoutContainer() instead of globally.
Here a snippet how it would look:
private View createLayoutContainer(){
LinearLayout layoutContainer = new LinearLayout(getApplicationContext());
RelativeLayout relativeLayout = new RelativeLayout(getApplicationContext());
TextView textWord = new TextView(getApplicationContext());
...
return layoutContainer;
}
public void onCLick_Add(View v){
layoutScroll.addView(createLayoutContainer());
layoutScroll.addView(createTextEmptyView());
}
The problem occurs due to the fact that a ScrollView can only have on child View. In other words, a ScrollView can have a LinearLayout or a RelativeLayout or a TextView etc. Update your code such that new views inside are added inside LinearLayout (layoutScroll) instead of ScrollView.
As I pointed out in the comment, the bug is this : Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Checklist for these kind of bugs.
As pointed out in this post the specified child already has a parent
Double check all your addView calls.
Make sure not to add any view more then once. When a View is allready
used (e.g., you got it with findViewById, don't use addView on it.
When you want to add a view, use addView with a NEW view. You can add
several of these new views to one view, but you cannot add that one
view multiple times.
You can't re-use a view simply by changing some
stuff. You CAN re-use a variable, but you need to make a new view if
you want to re-add it using addView.