In my main activity layout,I have an image button.
On clicking it,a popup pops up.The layout of the popup is as follows.
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100px"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/black">
<Button
android:padding="5dp"
android:id="#+id/b_help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Help"
android:textColor="#android:color/white"
android:background="#android:color/black"/>
<Button
android:padding="5dp"
android:id="#+id/b_pref"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences"
android:textColor="#android:color/white"
android:background="#android:color/black"/>
<Button
android:padding="5dp"
android:id="#+id/b_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About Us"
android:textColor="#android:color/white"
android:background="#android:color/black"/>
</LinearLayout>
The popup is displayed by the following code from MainActivity.java
btnOpenPopup = (ImageButton)findViewById(R.id.menu_button);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
popupWindow = new PopupWindow(getBaseContext());
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}});
The popup window class is as follows
PopupWindow.java
public class PopupWindow extends android.widget.PopupWindow implements View.OnClickListener{
Context ctx;
View popupView;
Button help,pref,about;
public PopupWindow(Context context)
{
super(context);
ctx = context;
popupView = LayoutInflater.from(context).inflate(R.layout.test, null);
setContentView(popupView);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
// Closes the popup window when touch outside of it - when looses focus
setOutsideTouchable(true);
setFocusable(true);
// Removes default black background
setBackgroundDrawable(new BitmapDrawable());
pref = (Button)popupView.findViewById(R.id.b_pref);
pref.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_pref:break;
case R.id.b_help:break;
case R.id.b_about:break;
}
}
}
Now on clicking the preferences button in the popup,i need to display the preference fragment.
I tried this ,for setOnClickListener for the preference button,but it is not working as the popupwindow class cannot extend fragmentactivity and getFragmentManager() is not happening.
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
Prefs mPrefsFragment = new Prefs();
//this line..
setContentView(R.layout.activity_prefs);
mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
mFragmentTransaction.addToBackStack(null);
setContentView(R.layout.activity_main);
mFragmentTransaction.commit();
How can i invoke the preffragment using the button from popup??
Any help will be much appreciated.Cause I am badly stuck at this.
I would suggest you save a reference to the fragment manager you got as a variable in another situation, such as when you first started, and use that instead. Takes a little memory, but the context you are in will not effect how you get your manager then.
Related
Is it possible to have a button in the main activity and when the user clicks
it , the fragment layout will appear? Im new to Android Studio.If its possible
do you think you can show me an example of it.
It should be similar to the description provided here in the link just add the button to the activity instead of the fragment in this example
Button Click inside Fragment to open up new fragment
You can use it directly from the main activity like this example:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Click me"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.root_layout, new NewFragment());
transaction.commit();
//button.setVisibility(View.GONE);
}
});
}
}
Where NewFragment is just a black fragment.
The problem is the LinearLayout is replaced, but the button is not:
So you can set the visibility (as a commented in the code). But I suggest to create 2 fragments, one with the button and one with your content. Then in the activity onCreate() method, you can set the first fragment and replace it with the other one later.
I defined a layout view in a .xml file called menu_list_slide_lateral.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_menu_slide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnSliBebe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bebe" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/foto_diario" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/consejos" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ajustes" />
</LinearLayout>
Im creating the SlidingMenu from code:
setBehindContentView(R.layout.menu_list_slide_lateral);
setSlidingActionBarEnabled(true);
slideMenu = getSlidingMenu();
slideMenu.setMode(SlidingMenu.LEFT);
slideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slideMenu.setShadowWidthRes(R.dimen.shadow_width);
slideMenu.setBehindOffset(100);
slideMenu.setFadeDegree(0.35f);
And mi activity extends from SlidingFragmentActivity:
public class TimelineActivity extends SlidingFragmentActivity
It shows perfectly the menu, but i want to do some actions when the user chooses an option from the menu:
For example, i want to open another activity when i choose the "Bebe" option.
I tried to set a onClick event to that button but it doesn't seem to work, it makes nothing:
inflater = getLayoutInflater();
item = inflater.inflate(R.layout.menu_list_slide_lateral, null);
btnSliBebe = (Button) item.findViewById(R.id.btnSliBebe);
btnSliBebe.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Log.e(TAG, "boton bebe");
}
});
How can i access that buttons and asign them a event?
Thanks!
What you should to is to create separate fragment for slide menu which will contain your layout R.layout.menu_list_slide_lateral and will handle actions. This is easy to do.
Now when you have this fragment you need to insert it into your slide activity.
setBehindContentView which you used is placeholder for menu. So create simple layout which will hold your menu fragment. E.g. call it R.layout.menu_frame and it should be like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then in your sliding activity set R.layout.menu_frame as behindContentView and add your menu fragment into this view.
public class BaseSlidingActivity extends SlidingFragmentActivity {
protected MenuFragment mSlidingMenuFragment;
private SlidingMenu mSlidingMenu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
//if we create new menu - create new MenuFragment and insert it into
//menu_frame
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mSlidingMenuFragment = new MenuFragment();
t.replace(R.id.menu_frame, mSlidingMenuFragment);
t.commit();
}
//if activity was restored(e.g. on orientation change) find it in fragment
//manager
else {
mSlidingMenuFragment = (MenuFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
mSlidingMenu = getSlidingMenu();
mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
mSlidingMenu.setShadowDrawable(R.drawable.shadow);
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
mSlidingMenu.setFadeDegree(0.35f);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
I hope it helps. You can browse through source code of example at github slidinfmenu example
I have a layout with a top bar container and a content container. When clicking on a button in the top bar, a vertical menu is displayed using an animation. My minSdkVersion is 9.
This works well when I start the app and I still haven't clicked a menu button (i.e. the content fragment has not changed), but as soon as I have clicked an option (and then replace the fragment in the content_container), the vertical menu behaves erratically. The click event of the menu btn is properly triggered, but the vertical menu is not always shown (but sometimes it is...). However, when I click the button and then touch the screen, the animation (show or hide the menu) starts.
I guess it has something to do with the vertical menu overlapping the content fragment, and then replacing the content fragment modify it in some way, but I can't find any solution.
Anybody can help?
top bar fragment
#Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
toggleMenu(0);
Button btn_menu = (Button) getView().findViewById(R.id.btn_menu);
btn_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mVerticalMenu.setVisibility(View.VISIBLE);
toggleMenu(1000);
}
});
}
private void toggleMenu(int duration){
if(mMenuIsOpen){
TranslateAnimation anim1 = new TranslateAnimation(0,0,0,-(mHeight-mMenuVerticalOffset));
anim1.setFillAfter(true);
anim1.setDuration(duration);
mVerticalMenu.setAnimation(anim1);
AlphaAnimation anim2 = new AlphaAnimation(0.7f, 0.0f);
anim2.setFillAfter(true);
anim2.setDuration(duration);
menu_option_01.setOnClickListener(null);
menu_option_02.setOnClickListener(null);
menu_option_03.setOnClickListener(null);
mMenuIsOpen = false;
}
else{
TranslateAnimation anim1 = new TranslateAnimation(0,0,-(mHeight-mMenuVerticalOffset),0);
anim1.setFillAfter(true);
anim1.setDuration(duration);
mVerticalMenu.setAnimation(anim1);
AlphaAnimation anim2 = new AlphaAnimation(0.0f, 0.7f);
anim2.setFillAfter(true);
anim2.setDuration(duration);
menu_option_01.setOnClickListener(mButtonClickListener);
menu_option_02.setOnClickListener(mButtonClickListener);
menu_option_03.setOnClickListener(mButtonClickListener);
mMenuIsOpen = true;
}
}
private OnClickListener mButtonClickListener = new OnClickListener()
{
public void onClick(View v)
{
toggleMenu(1000);
if(!v.isSelected()){
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
switch(v.getId()){
case R.id.menu_option_01:
// replace content_container by fragment 1
break;
case R.id.btn_02:
// replace content_container by fragment 2
break;
case R.id.btn_03:
// replace content_container by fragment 3
break;
}
}
}
};
private OnClickListener mBgClickListener = new OnClickListener()
{
public void onClick(View v)
{
toggleMenu(1000);
}
};
Main layout
<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" >
<FrameLayout
android:id="#+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="44dp" />
<FrameLayout
android:id="#+id/top_bar_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false" />
</RelativeLayout>
top bar layout
<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:background="#00000000" >
<LinearLayout
android:id="#+id/vertical_menu"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginTop="44dp"
android:background="#ffffff"
android:orientation="vertical"
android:visibility="gone" >
<!-- menu layout -->
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#ffffff" >
<Button
android:id="#+id/btn_menu"
android:layout_width="50dp"
android:layout_height="44dp"
android:background="#drawable/menubtn" />
<ImageView
android:layout_width="130dp"
android:layout_height="44dp"
android:src="#drawable/logo"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
At the end of my Toggle method, I invalidate the root view:
rootView.invalidate();
and now it works. Not quite clear why I must do that though...
I know there is already an accepted answer for this but I had a similar problem and the answer didnt help.
I had a view that I declared at the end of my layout to keep its Z index above its siblings. I had to touch the page to make the animation work.
So I set the Z index again through Java and it worked.
view.bringToFront();
I have an idea in mind but im not sure about how to implement it
first i have a dialog
final Dialog dialog = new Dialog(mContext);
i also have a layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textViewWhen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
what i want is to add this layout in the dialog, i may also want to add more of the same layout right under it inside that dialog
how can i do that?
for example how can i add two of this layout in one dialog?
something like
Dialog Title
Large Text
Small Text
Medium Text
Large Text
Small Text
Medium Text
Something like this:
LayoutInflater li = LayoutInflater.from(SomeActivity.this);
someLayout = (LinearLayout)li.inflate(R.layout.some_layout, null);
alert = new AlertDialog.Builder(SettingsActivity.this);
alert.setView(someLayout);
This is an example from my application:
public class ConfirmDialog extends DialogFragment {
public static String TAG = "Confirm Dialog";
public interface ConfirmDialogCompliant {
public void doOkConfirmClick();
public void doCancelConfirmClick();
}
private ConfirmDialogCompliant caller;
private String message;
public ConfirmDialog(ConfirmDialogCompliant caller, String message){
super();
this.caller = caller;
this.message = message;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doOkConfirmClick();
}
});
((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
caller.doCancelConfirmClick();
}
});
return view;
}
}
where the inflated layout is confirm_dialog.xml.
You inflate your layout in the onCreateView method.
In this case I used DialogFragment (which I suggest you to use...see the support library so that you don't have to worry about your target SDK) but the same applies to Dialog.
Hope it helps you!
You can check this documentation page which explain how to add a custom layout on dialog
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
The key is the setContentView method:
dialog.setContentView(R.layout.custom_dialog);
Check out this one :
how to get customized alert dialog , like the one shown in image?
Refer the answer which I've given (Aamir Shah)
Use a DialogFragment, which allows you to, just like any other Fragment, completely customize the layout. It is available in the v4 support library.
http://developer.android.com/reference/android/app/DialogFragment.html
http://developer.android.com/tools/extras/support-library.html
I have this app, that I created a custom dialog for. I must of goofed something up cause while the .show call on the dialog does indeed bring it up, it looks like a whole new fragment and it is not floating but instead replacing the ui with its contents. I did see in their help for DialogFragment:
http://hi-android.info/docs/reference/android/app/DialogFragment.html#Lifecycle
that one can embed a dialog as a regular fragment or not. Though I am not doing anything to do this so I cannot figure out why its acting like an embedded fragment and not floating. After thinking on it, is it the way I defined my XML definition? The dialogfragment example above didn't really give a definition for the xml layout, so maybe that is where my issue is? (Even added the gravity to the xml file, still no dice)
My xml definition for this Dialog is here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text = "Location:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"/>
<Spinner
android:id="#+id/location_spinner"
android:layout_width = "450sp"
android:layout_height="wrap_content"/>
<!-- fill out the data on the package total cost etc -->
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/location_dlg_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"/>
<Button android:id="#+id/location_dlg_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button android:id="#+id/location_dlg_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create new..."/>
</LinearLayout>
</LinearLayout>
Like I said displays just fine, the code for the fragment:
package com.viciousbytes.studiotab.subactivities.dialogfragments;
import ... ...
public class LocationPicker extends DialogFragment {
ArrayList<Location> mLocations;
public static LocationPicker newInstance()
{
LocationPicker loc = new LocationPicker();
return loc;
}
private void setLocations(ArrayList<Location> loc)
{
mLocations=loc;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location_dialog, container, false);
Spinner spinner = (Spinner)v.findViewById(R.id.location_spinner);
ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(v.getContext(), android.R.layout.simple_spinner_item, mLocations);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(mLocations==null)
spinner.setPrompt("No Locations");
else
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new LocationSelectedListener());
// Watch for button clicks.
Button newBtn = (Button)v.findViewById(R.id.location_dlg_new);
newBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// Cancel do nothing dismissthis
Button cancelBtn = (Button)v.findViewById(R.id.location_dlg_cancel);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// okay button means set listener with the selected location.
Button okBtn = (Button)v.findViewById(R.id.location_dlg_ok);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
return v;
}
}
It is called from a fragment itself? though does that matter? because I am calling a TimePIckerDialog and a DatePickerDialog and those work fine, but my calling code from my other fragment is:
void showLocationDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("locpicker");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = LocationPicker.newInstance();
newFragment.show(ft, "locpicker");
}
Your constructors are wrong. Try to have just one static method newInstance to instantiate the fragment for all cases and use a Bundle to store the arguments that you want to use in the fragment. Refer to Basic Dialog section here and extend it to your case.