I am currently building a popup window which should show 2 rows of labels and buttons allowing the user to view two videos.
I am having difficulties with the layout. Here is the popup layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/layout_background_start">
<TextView
android:id="#+id/spacer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:textSize="5dp"
android:textStyle="bold"
android:text=""/>
<TextView
android:id="#+id/general_video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spacer1"
android:textSize="20dp"
android:textStyle="bold"
android:text="Single-Player Tutorial"/>
<Button
android:id="#+id/general_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/general_video_title"
android:text="See Video"/>
<TextView
android:id="#+id/spacer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_below="#id/general_video_button"
android:textSize="5dp"
android:textStyle="bold"
android:text=""/>
<TextView
android:id="#+id/multiplayer_video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/spacer2"
android:text="Multiplayer Tutorial"/>
<Button
android:id="#+id/multiplayer_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/multiplayer_video_title"
android:layout_below="#+id/general_video_button"
android:text="See Video"/>
</RelativeLayout>
This is the code displaying the popup in the main activity:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.videos_popup, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
popupWindow.showAtLocation(m_PlanesLayout, Gravity.CENTER, 0, 0);
I am using the emulator to test the display of the popup with different phones or tablets. The problem is that on tablets the "See Video" buttons become very big and do not respect the "wrap_content" setting in the layout. It seems that the popup window somehow respects a minimum size relative to the width of the screen and that the button size is adjusted in order to obtain this minimum size.
Can anyone please help ?
Yes, it's not respected wrap_content because it's stretched between TextView and right edge of the screen:
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/multiplayer_video_title"
Related
Android version 11.0.4
I am trying to use a ListView to display a series of elements with different types of information to display in the same list using a custom ListAdapter.
To make the same ListAdapter able to display different types of information I'm creating a custom XML template with all possible elements that I want to display and hiding the ones I don't want to show programmatically.
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
When the extra elements are shown
When the extra elements are gone
The code that hides the elements:
TextView catagory = (TextView) convertView.findViewById(R.id.title);
TextView time1 = (TextView) convertView.findViewById(R.id.time1);
TextView time2 = (TextView) convertView.findViewById(R.id.time2);
TextView location = (TextView) convertView.findViewById(R.id.location);
TextView description = (TextView) convertView.findViewById(R.id.description);
View separator1 = (View) convertView.findViewById(R.id.separator_vertical1);
View separator2 = (View) convertView.findViewById(R.id.separator_horizontal1);
View separator3 = (View) convertView.findViewById(R.id.separator_vertical2);
View separator4 = (View) convertView.findViewById(R.id.separator_horizontal2);
catagory.setText(item.name);
Class type = item.getType();
if (EventItem.class.equals(type)) {
time1.setText(String.valueOf(((EventItem)item).timeStart));
time2.setText(String.valueOf(((EventItem)item).timeEnd));
location.setText(((EventItem)item).location);
description.setText(((EventItem)item).description);
} else if (TaskItem.class.equals(type)) {
time1.setText(String.valueOf(((TaskItem)item).timeDue));
description.setText(((TaskItem)item).description);
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ReminderItem.class.equals(type)) {
time1.setText(String.valueOf(((ReminderItem)item).time));
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
separator4.setVisibility(View.GONE);
description.setVisibility(View.GONE);
} else if (SubjectItem.class.equals(type)) {
description.setText(((SubjectItem)item).description);
} else if (NoteItem.class.equals(type)) {
description.setText(((TaskItem)item).description);
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ContactItem.class.equals(type)) {
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
}
The XML template:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:id="#+id/title"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical1"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/time1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time1"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/title"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/title"
android:background="#android:color/white"/>
<TextView
android:id="#+id/location"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/separator_horizontal1"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical2"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time2"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/location"
android:background="#android:color/white"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator_horizontal2"
android:textAlignment="textStart"
android:textSize="20sp"
android:textColor="#ffffffff"/>
The ListView the elements are displayed in:
<ListView
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:id="#+id/text_overview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="#ffffffff"
android:divider="#android:color/transparent"
android:dividerHeight="8dp"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
The answer to your question is to use View.INVISIBLE
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
View.GONE: This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
Here's the link of Documentation.
Use View.INVISIBLE which takes up space and invisible the view.
while you are using View.GONE which doesn't takes up space for the view and invisible the view.
For more description, you can go with:https://developer.android.com/reference/android/transition/Visibility
I eventually got it working, not sure how but setting each element as visible before going into the if statements stoped the shrinking of the view window. It also solved the issue of some list items missing elements that shouldn't have been hidden. I can only assume some resources were being reused somewhere and arriving pre-hidden.
I'm making a quiz game. If the correct button if pressed (4 options) i want to show another picture. First picture is a questionmark, next picture is a picture of a person.
I have these two ImageViews
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView"
android:src="#drawable/et"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView2"
android:src="#drawable/questionmark"
android:layout_alignTop="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false" />
Both in my RelativeLayout. ImageView2 is overlapping ImageView.
What is the best way to either switch between the picture or remove ImageView2?
If private button knapEt is pressed, ImageView2 should be removed or switched in layering, so ImageView is shown.
Using a simple logic, at first only one of the view is shown the other is not visible using the android:visibility="gone" property.
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView"
android:src="#drawable/et"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:visibility="gone"/>
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView2"
android:src="#drawable/questionmark"
android:layout_alignTop="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false" />
then use the codes to toggle the image. each time it is clicked, the first one becomes invisible and the second one becomes visible and vice versa.
boolean flag = true;
public void toggleImage()
{
if (flag == true)
{
image.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}
else
{
//make the background visible
image.setVisibility(View.VISIBLE);
imageView2.setVisibility(View.GONE);
}
}
I am designing a pop up AlertDialog that sets the view of of the alert dialog to the following XML file. For some reason the file will not add margins and I cannot figure out what I am doing wrong. Margins and Padding do not work in the activity_priority_level_values LinearLayout, the LinearLayout that holds all of the TextViews, or any of the TextViews. How can I add margins (or padding) for the dialogs borders and margins between all of the text views?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_priority_level_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/large_space">
<TextView android:id="#+id/level_1_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_header"/>
<TextView android:id="#+id/level_1_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_description"/>
<TextView android:id="#+id/level_2_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_2_header"/>
<TextView android:id="#+id/level_2_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_2_description"/>
<TextView android:id="#+id/level_3_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_3_header"/>
<TextView android:id="#+id/level_3_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_3_description"/>
<TextView android:id="#+id/level_4_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_4_header"/>
<TextView android:id="#+id/level_4_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_4_description"/>
<TextView android:id="#+id/level_5_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_header"/>
<TextView android:id="#+id/level_5_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_description"/>
</LinearLayout>
</LinearLayout>
Activity
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.activity_priority_level_values);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if(hiddenLayout == null){
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
builder.setView(hiddenInfo);
}
builder.create();
builder.show();
The problem is the inflating of your view.
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
You should never inflate with null. Instead change the code to:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, myLayout, false);
If you are inflating with null the layout params will be ignored.
I'm working on my first Android project, which means that I'm not that familiar with Android and Java, and I want to move scroll of ScrollView after a user presses the button for next action (IME_ACTION_SEND). My codes are as follows.
activity_add.xml
It basically consists of TextView, EditText, NumberPicker, Spinner, Button. I want to move the scroll after IME_ACTION_SEND on EditText, so that the NumberPicker is centered on the screen.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/addActivity_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<LinearLayout
android:id="#+id/layout_activity_add"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_addItem"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="40sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout"
android:layout_marginTop="50dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine = "true"
android:maxLength="20" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout3"
android:layout_marginTop="50dp">
<TextView
android:id="#+id/period_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_period"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/period_number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/period_textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</NumberPicker>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/formLayout4"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_category"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_toStartOf="#+id/background_spinner"
android:layout_toLeftOf="#+id/background_spinner" />
<Spinner
android:id="#+id/background_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical">
</Spinner>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_submit"
android:layout_gravity="center_horizontal"
android:text="#string/label_submit" />
</LinearLayout>
</ScrollView>
AddItemActivity.java
I've just copied the part which I think is relevant.
public class AddSinceItemActivity extends ActionBarActivity {
EditText title;
Spinner spinner;
NumberPicker numberPicker;
String title_string;
ViewGroup linearLayout;
ScrollView addActivity_scrollview;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
addActivity_scrollview = (ScrollView) findViewById(R.id.addActivity_scrollview);
linearLayout = (ViewGroup) findViewById(R.id.layout_activity_add);
title = (EditText) findViewById(R.id.title_editText);
numberPicker = (NumberPicker) findViewById(R.id.period_number_picker);
spinner = (Spinner) findViewById(R.id.background_spinner);
/* For title */
title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
// I think here is the most important part.
addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());
}
return false;
}
});
}
}
For addActivity_scrollview.smoothScrollTo(0, numberPicker.getBottom());,
I've tried many possible codes, such as
addActivity_scrollview.smoothScrollTo(0, spinner.getTop());
addActivity_scrollview.smoothScrollTo(0, 300); (300 is just a random number)
addActivity_scrollview.smoothScrollBy(0, 300);
but the scroll is always stuck (it moves a little but it's always the same position with above codes) and the screen barely shows the selected number of NumberPicker. How can I achieve the goal to set scroll so that the screen shows the entire NumberPicker?
just add this line to your edittext code in xml :android:imeOptions="actionSend" and then just try this into your activityaddActivity_scrollview.smoothScrollTo(0, 480);
I should've added android:imeOptions="actionSend" to <EditText> in order to properly listen on onEditorAction(). I thought it'd be okay with android:singLine="true" because that option enabled "next" action on keyboard.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_editText"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/hint_title"
android:singleLine="true"
android:maxLength="20"
android:imeOptions="actionSend" />
Consider the widget hello:
When clicked, it opens the area at the bottom with additional widgets (text, buttons, etcetera):
What is this kind of expand/collapse widget called?
One way to do this is to build your whole layout with the text and the buttons, but hide them initially by setting their visibility to gone or invisible. Then in code in the onClick listener of the "hello_text" text view, you can change their visibility to visible.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/hello_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_text" />
<TextView
android:id="#+id/the_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_text"
android:visibility="invisible" />
<LinearLayout android:id="#+id/btn_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible">
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text1" />
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text2" />
</LinearLayout>
</LinearLayout>
Now in your activity or fragment you can do:
TextView the_text_view = (TextView) v.findViewById(R.id.the_text);
LinearLayout ll_btn_holder = (LinearLayout) v.findViewById(R.id.btn_holder);
TextView hello_text_view = (TextView) v.findViewById(R.id.hello_text);
hello_text_view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
the_text_view.setVisibility(View.VISIBLE);
ll_btn_holder.setVisibility(View.VISIBLE);
}
});