The Button over SupportMapFragment inside a viewpager programmatically - java

I'm struggling with something really simple. ( or at least this was my idea.. )
I would need to put a simple button on top of a map inside a viewpager.
Actually I'm creating the supportmapfragment programmatically and adding it to the crated viewpager adapter .
ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager());
mapFrag = new SupportMapFragment();
mapFrag.getMapAsync(this);
adapter.addFragment(mapFrag, getString(R.string.maptab));
now... to put a button on top I would need the view from supportmapfragment so I can push up "my button_" via addview.
I cannot understand how to retrieve the view without extending the class for supportmapfragment but this would need me to create a custom supportmapfragment class.
any idea?
after suggestion: I made a test with a relative layout :
<fragment
android:id="#+id/rlMap"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button 1"/>
this make button appear for a second (before map load) then being uncovered by map . even putting button inside the fragment sort same effect

Even if you try and put the button over the fragment's view, you will still need to set a click listener and do some logic when it's pressed. I think that would make the existing code a bit messy, passing a responsibility of the fragment (registering the button click listener) to the activity.
What I would do if I were you would be create a new Fragment with some ConstraintLayout that contains the underlying map and the button (but does not extend SupportMapFragment). It would be a wrapper fragment where you can also handle the button with its events, and whatever would be needed more.

Related

How to add an element between two elements android

I am trying to add an editText in between two already existing editTexts programmatically on the click of a button in android. I am just wondering if this is possible, as i have been unable to find any related questions?
What do you want to acomplish with runtime element adding? Whats the purpose? Maybe isn't enough to show/hide the element on specific actions?
I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml:
android:visibility="gone"
or in java code in the onCreate() method:
specificElement.setVisibility(View.GONE)
Then when you normally would add the element you rather just set the visibility to visible:
specificElement.setVisibility(View.VISIBLE)
What about that?
you can add 3rd EditText on 2nd position.
For that first you should have reference of the parent layout nad then do like this.
if you have done :
parent.addView(editText1);
parent.addView(editText2);
So now your parent have two child views.
now to add 3rd EditText i.e. editText3 then do this like:
parent.addView(editText3, 1);// addView(<childview>, <index>);
Like this your 3rd EditText will be in 2nd position.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);
yourEditBox.setLayoutParams(params);
Use above code to add and align you're editbox between another two editboxes.
Inside Activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(linearLayout);
EditText editText = new EditText(getBaseContext());
editText.setHint("Programmatically Added EditText");
linearLayout.addView(editText, 1);
}}
Layout file structure
<?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:orientation="vertical"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2" />
</LinearLayout>
Hope this helps. Cheers!
You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button.
In xml file :Set visibility to gone or invisible depending on what you actually want :
android:visibility="invisible" it won't be visible but it's going to take place in your view
android:visibility="gone" it won't be visible and it's not taking place in your view
In your code :
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setVisibility(View.VISIBLE);
});

How to set a temporary fragment to my "home screen" Android studio

I have started coding and I really like it, I have been on/off on diffrent project (to learn diffrent things) But now i got a problem, I "app"reciate all the help i can get.
I am using the navigation drawer and got three fragments installed. I can navigate through the navigation drawer and select a fragment and it loads on my screen. But when i start my app the screen is just white and the navigation drawer is on the left and i can pick a fragment. Is it possible to have a fragment on my "home screen" so when I start the app there is a fragment already placed on the screen BUT! when i chosse another fragment in the navigation drawer i would like to have my fragment who loaded at the beginning to disappear. Otherwise both fragments will show on eachother (Text on text). Maybe to have the temporary fragment to "finish" itself somehow. Please look at my imgur image to get my idea.Imgur image press here to view
Here is a generic solution on how to use the FragmentManager. It should give a good idea on how to display a Fragment. Besides that you could include a static Fragment in your layout. It's up to you how to approach it.
So the FragmentManager solution looks like this:
YourActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = AnyFragment.instantiate(this, AnyFragment.class.getName());
fragmentManager.beginTransaction().add(R.id.fragment_placeholder, fragment).addToBackStack(null).commit();
}
The target id is defined in your activity layout.xml
<LinearLayout 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:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
Later if you want to replace the current Fragment you can use the remove and add method of the FragmentManager (the replace method is kinda buggy).
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_placeholder))
fragmentManager.beginTransaction().remove(currentFragment).add(R.id.fragment_placeholder, yourNewFragment).addToBackStack(null).commit();

I want my Android Fragment to cover the MainActivity screen (I don't want to see any MainActivity text, buttons, etc.)

This seems to be such a fundamental question that I'm embarrassed to ask it, but I'm so frustrated by my Fragment learning curve that I'll expose my ignorance.
An example in a textbook that cuts a lot of corners that make expanding them difficult, if they even work, had no button to click; MainActivity simply loaded FragmentA. OK, keep it basic. I get that.
So I added a button to MainActivity to click to load FragmentA, but the button shows on the FragmentA screen, sort of like this (not an actual screen shot, but close):
How do I prevent that? Should I use a second Activity instead of a Fragment? Since this endeavor is to be utilized in a much larger project, I don't want to do anything considered not best practice. I realize that the main use of Fragment is to enable side-by-side "screens" on devices that are large enough. That's not what I want to do, but it IS possible to accomplish what I want with a Fragment, isn't it?
MainActivity.java
public class MainActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnLoadFragmentAByClick(View view)
{
FragmentA fragmentA;
fragmentA = new FragmentA();
FragmentTransaction ft ;
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.layout_container, fragmentA);
ft.addToBackStack("example");
ft.commit();
}
}
FragmentA.java
public class FragmentA extends Fragment
{
#Override
public View onCreateView(LayoutInflater _inflater,
ViewGroup _container,
Bundle _savedInstanceState)
{
return _inflater.inflate(R.layout.fragment_a,
_container,
false);
}
}
activity_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"
tools:context =".MainActivity" >
<LinearLayout
android:id ="#+id/layout_container"
android:orientation ="vertical"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
<Button
android:id ="#+id/btnLoadFragmentA"
android:text ="Load Fragment A"
android:onClick="btnLoadFragmentAByClick"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
fragment_a.xml
<RelativeLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="Layout for fragment A"
android:textAppearance ="?android:attr/textAppearanceLarge"
>
</TextView>
</RelativeLayout>
EDIT
I realize that I could hide the MainActiviy button (and any other objects) before loading FragmentA and show them after returning, but I was hoping for a one-or-two-line "fix".
How do I prevent that?
Well, to some extent, you don't, insofar as this has nothing to do with fragments.
Your activity_main.xml has the Button floating over top of the LinearLayout (???) that you are using for your fragment container. If you do not want the Button floating over top of the fragment container, then fix the layout file to not have the Button floating over top of the fragment container.
I realize that I could hide the MainActiviy button (and any other objects) before loading FragmentA and show them after returning, but I was hoping for a one-or-two-line "fix".
The typical solution for full-UI replacement using fragments is to have everything in fragments. Your replace() would replace your original fragment with a replacement. So, in this case, your Button would be managed by one fragment, and clicking the Button would replace() that fragment with another fragment. Given that your FragmentTransaction has addToBackStack(), pressing BACK would get rid of the replacement fragment and return you to your Button fragment.

Another Fragment on top op a Mapview (SupportMapFragment)

Im adding another Fragment to the screen. This screen already has en MapView in a SupportMapFragment so the added Fragment should be on top of the MapView. This new view cover like 2/3 of the screen and a big part of the Map but when I scroll on the View it scrolls the MapView underneath. This is not what i expected because the new View is added on top of the MapView. The new view exist of a relative layout containing a ImageView which wraps it contents. So when scrolling on the imageview (which has totally no functionality) it scrolls the MapView. Can anyone explain why this happens and if possible, provide me with a solution?
EDIT:
basically i have a MapView which fills the whole screen. This code sets up my mapview:
public void setUpMapIfNeeded(int satelliteMode, LatLng startPoint, float zoomLevel) {
// Do a null check to confirm that we have not already instantiated the map.
if (mapView == null) {
// Try to obtain the map from the SupportMapFragment.
mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview))
.getMap();
// Check if we were successful in obtaining the map.
if (mapView != null) {
setUpMap(satelliteMode, startPoint, zoomLevel);
}
}
}
private void setUpMap(int satelliteMode, LatLng startPoint, float zoomLevel) {
// more settings
}
Then when a user clicks a marker a fragment must be shown on top of the mapview.
public void createDetailView() {
detailView = new DetailViewFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.viewLayout, detailView).commit();
fragmentManager.executePendingTransactions();
}
This all works fine. The view is shown (which is about 2/3 of the whole screen) but user can scroll the map when swiping the detailview. The detailview consist of a relative layout with a lot of text and buttons:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="45sp">
<!-- a lot of text and buttons here -->
</RelativeLayout>
So I still havent found a proper way to deal with this. Instead I just disabled all touches and clicks on the mapview when another view is on top of the mapview which btw works ok.
Just in case it might still be relevant, the way I've handled this kinds of layouts (one view appearing "on top of" another) is to use LinearLayout instead of a RelativeLayout in the following way:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SomeView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<PopOutView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:visibility="gone" />
</LinearLayout>
In your case the map would be that SomeView and the relative layout the PopOutView. Then in the code you would just call popOutView.setVisibility = View.VISIBLE whenever you would like it to show up. It shrinks the first view instead of being drawn on top (thus having a bit different visual effect) but at least this way the touch events should go to the right view.
Set the fragment's layout clickable property to true. It will stop the touch events from propagating down.
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:clickable="true">
</FrameLayout>

Android: Context menu doesn't show up for ListView with members defined by LinearLayout?

I've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/namecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can long-presses work on more complex elements? I'm building on 2.1.
(edit)
Registering with this on the ListActivity:
registerForContextMenu(getListView());
The code I posted is the item template for the list.
Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.
Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.
set checkbox property
focusable = false;
and run project again..
Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html
Setting the checkbox to not be focusable fixes the problem.
Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)
Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.
Anyways why not just register the TextView of each list item? Who would long press a checkbox...
This should from a regular ListView as well. But if you're starting from scratch on a new list I would consider using the CheckedTextView:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// return false to let list's context menu show
return false;
}
});

Categories