Show activity's view overlay FrameLayout with Fragment - java

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).

Related

android gallery app with sliding pictures

As in the title. I want to have grid of pictures which consists of 2 columns and every picture must have the same dimensions. The user should be able to click on each picture to enter the full screen mode and then slide pictures from left to right (full sized pictures in that mode). By far, I made a grid of clickable elements but without functionality of sliding them. Here is my code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.miki.androidtwo.MainActivity">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:scrollbars="vertical"
android:gravity="center">
</GridView>
</RelativeLayout>
activity_full_image.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.miki.androidtwo.FullImageActivity">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="I want to see other doggos" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
Other classes are FullImageActivity and ImageAdapter; they are pretty obvious and simple so I won't post them.
How can I implement this sliding pictures functionality?
How can i determine dimensions of pictures in a grid that will be independent of the device?
Actually I think I hard-coded it with the line:
imageView.setLayoutParams(new GridView.LayoutParams(480,402));
If you want to slide the images while they are not full screen, you can use a HorizontalScrollView, or if you want to slide them while they are fullscreen, you can use a ViewPager, along with a PagerAdapter.
So to populate the grid imageView.setLayoutParams is fine to use for setting the size of each imageview like the way you did. When a user clicks on an image, you are going to go to a new Activity that is setup with a ViewPager and a PagerAdapter. This way the user can slide through fullscreen images.

How to add Text View on Bottom of frame Layout using Java Code

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);

Android programmatically added button has match parent width and height

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.

Inflate a layout inside other layout's LinearLayout

I've got this Layout:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
The 2 borders (left,right )are filled by icons. When I touch one of these icons I access to other activity. The top black bar is a custom title bar.
The clear grey inside space is where I need to fit all activities that I've got on my app. So this layout would be something like a menu layout that would be static in all the activities.
This is the Layout xml:
menu_view.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="com.example.MenuView" >
<!-- Show top black custom title bar-->
<include
layout="#layout/custom_tittlebar" >
</include>
<!-- Button columns -->
<LinearLayout
android:id="#+id/lefthandmenu"
android:layout_width="85dip"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:background="#drawable/border_cut" >
<ImageView
... />
...
</LinearLayout>
<LinearLayout
android:id="#+id/righthandmenu"
android:layout_width="85dip"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:background="#drawable/border_cut" >
<ImageView
... />
...
</LinearLayout>
<!-- Blank space which will contain other activities -->
<LinearLayout
android:id="#+id/activitycontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/righthandmenu"
android:layout_toRightOf="#id/lefthandmenu"
android:layout_below="#id/title"
android:orientation="vertical" >
</LinearLayout>
And this is the class where are defined all the icon's onClickListeners.
MenuView.java
public class MenuView extends RelativeLayout {
private final LayoutInflater inflater;
Context context;
public MenuViewActivity(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.menu_view, this, true);
((ImageView)this.findViewById(R.id.navButton)).setOnClickListener(launch_nav);
}
final OnClickListener launch_nav = new OnClickListener() {
#Override
public void onClick(View v) {
getContext().startActivity(new Intent(getContext(), Navigation.class));
}
};
Well, having this (I'm not sure if its all ok, maybe I'm doing something wrong with the inflate method or something like this), now the thing would be to define the other activitie's layouts to be inside this view. To do this, I write:
ExampleActivity.java
public class ExampleActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);
View this_layout = getLayoutInflater().inflate(R.layout.main, inside_menu_view, true);
inside_menu_view.addView(this_layout);
But I'm getting a NullPointerException on this last line. So, something when inflating on this snippets must be wrong.
Hey use the following structure. put. include your common layout in each and every activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Show top black custom title bar-->
<include
android:id="#+id/ll_top"
layout="#layout/custom_tittlebar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</include>
<include
android:id="#+id/ll_left"
layout="#layout/custom_left"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
</include>
<include
android:id="#+id/ll_right"
layout="#layout/custom_right"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
</include>
<Button
android:id="#+id/btn_center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="center"
android:layout_toRightOf="#+id/ll_left"
android:layout_centerHorizontal="true"
android:layout_below="#+id/ll_top"
></Button>
</RelativeLayout>
You can have one main activity like BaseActivity that is extended by FragmentActivity. Base activity extends SlidingFragmentActivity and implements basic structure like menu etc. FragmentActivity onCreate method is :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fm = getSupportFragmentManager();
//This is main content that is switchable
mContent = new MainListActivity();
//Additionally you can define those two lines as fragments and add them as default on both sides :
sideFragments = new sideFragments(this);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, mContent);
ft.replace(R.id.side_framgments, sideFragments);
ft.commit();
}
When you press some of those buttons from your menu(left and right border) you will change the fragment in middle of the screen with this function in FragmentActivity :
public void switchContent(Fragment fragment, String tag) {
mContent = fragment;
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).addToBackStack(tag).commit();
}
Note R.id.content_frame is the XML that is switchable between those two lines (let say menus on both sides) and R.id.side_framgments are those two lines in between the main layout that is switchable.

ZoomControls on Google Maps API - how can I move it above a ButtonBar?

I have enabled the built in zoom controls in my google maps application, however, it shows on my footer button panel:
I want the zoom controls to be one top of the footer button bar.
public class MinuteMapActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// the methods on the map are initialised here - onCreate
zoomControls();
satelliteDisplay();
snapToMap();
}
// method for Zoomcontrols
private void zoomControls() {
LinearLayout zoomControls = (LinearLayout) findViewById(R.id.zoomControls);
MapView zoomLayout = (MapView) findViewById(R.id.MapView);
//zoomControls.addView(zoomLayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
zoomLayout.setBuiltInZoomControls(true);
}
XML Layout:
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:id="#+id/MapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0zZsLcQS5Zeiqjbo1n8-nn2CqtxKIuTq2T6bskg"
/>
<!-- set zoom controls on map at the bottom -->
<LinearLayout
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
If I enable
//zoomControls.addView(zoomLayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
it crashes.
Some assistance on how I can move the zoomcontrols above the bottom bar.
I think the reason it crashes is because you are trying to assign LinearLayout parameters to a layout inside a RelativeLayout, so you would have to use RelativeLayout.LayoutParams instead.
Also, I am a bit confused as to why you are using a LinearLayout for your zoom controls instead of the pre-defined View called ZoomControls. Perhaps it would save you a bit of time?
If you want to place the zoom controls above your bottom view use
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/buttonbar" />
assuming you call your ButtonBar view buttonbar.
May you have missed the two line of code in your
<com.google.android.maps.MapView.
so paste this two lines in it and you will got it. its working 100 percent.
android:enabled="true"
android:clickable="true"

Categories