I am trying to call a dialog fragment from an FragmanActivity.
My dialog fragment include two edittext view and two buttons.
I read the android developer userguide but I can not resolve my problem.
(Fragments)
My problem is: the DialogFragment window appears like only a small window.
My Dialog XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="790dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/ePostionDate"
android:inputType="date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/lblDate"
android:focusableInTouchMode="false"
android:paddingLeft="5dip" />
<EditText
android:id="#+id/eWaterPosition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="#string/lblWaterPostion"
android:paddingLeft="5dip" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0" >
<TableRow>
<Button
android:id="#+id/btnAddWaterposition"
android:layout_width="0dp"
android:layout_weight=".50"
android:layout_height="wrap_content"
android:text="#string/lblBtnFixData"
android:layout_gravity="left" />
<Button
android:id="#+id/btnCancelWaterposition"
android:layout_width="0dp"
android:layout_weight=".50"
android:layout_height="wrap_content"
android:text="#string/lblBtnCancel"
android:layout_gravity="right" />
</TableRow>
</TableLayout>
Part of my FragmentActivity where I try to call the DialogFragment:
DialogFragment wpDialog = new WaterPositionEditActivity(getApplicationContext());
wpDialog.show(getSupportFragmentManager(), "waterPositionSetter");
Finally my DialogFragment class:
enter code here
#SuppressLint("ValidFragment")
public class WaterPositionEditActivity extends DialogFragment {
private Context context;
public WaterPositionEditActivity(Context context) {
super();
this.context = context;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.water_position, container, false);
}
}
I create my app for Android 2.2 but I want it could run on Android4 or upper.
The very small window look like this:
As you can see on the img only one edittext field show but there are another editview and two buttons. So this small window should bi bigger than now and show all layout elements!
Anyone can help what is the solution?
I found the solution. I use RelativeLayout I forgot to add in XML layout for the elements the right position.
When I add the right position for the elements the Dialog wondow appear as want.
Related
I am creating my first Android app using a Navigation Drawer, and such most of my pages are fragments. Each page will contain several ImageButtons.
My goal is for the user to click an ImageButton, and then be displayed a "popup" list of images with their respective names. When the user selects an image, the ImageButton's src should become that image.
Any tips on how I could achieve this? Thanks!
Here is one of my fragments:
public class FragmentAnubis extends Fragment {
View myView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.god_anubis, container, false);
return myView;
}
And here is its corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="100">
<ImageButton
android:id="#+id/imageStarterItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="7dp"
android:background="#null"
android:scaleType="fitCenter"
app:srcCompat="#drawable/ah_puch" />
<ImageButton
android:id="#+id/imageRelic1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="7dp"
android:background="#null"
android:scaleType="fitCenter"
app:srcCompat="#drawable/ah_muzen_cab" />
<ImageButton
android:id="#+id/imageRelic2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="7dp"
android:background="#null"
android:scaleType="fitCenter"
app:srcCompat="#drawable/agni" />
</LinearLayout>
</LinearLayout>
First of all, remove unnecessary root LinearLayout.
I guess you use DialogFragment to show list of images. In onclick handler get image source and send it throw event bus - Rx event bus or GreenRobots Event Bus https://github.com/greenrobot/EventBus
In source activity you can catch all events and set source to first button
I have extended my class with a dialog in which I have set content view and other button actionlisteners etc... what is happening is that when I create my dialog it shows background properly.
but opening it again and again is crating problems with content view, things are added haphazardly and most of the UI elements are repeating and background image is disappeared.
I know that dialog is created once and is used as a cache, I don't want that I know there is a method in Android Activity removeDialog(int dialogID) but I don't know who to use this, I don't give any id to my dialog I don't know how to give id to dialog.
#dialog code
public class OptionsDailog extends Dialog implements OnClickListener {
public OptionsDailog(Activity pContext) {
super(pContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
mContext = pContext;
setContentView(R.layout.option_menu);
setBasicContents();
}
#options_menu.xml
<?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:id="#+id/rl_root_option_menu">
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#drawable/bg_episode" android:id="#+id/rl_option_screen">
<LinearLayout android:id="#+id/ll_options" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true">
<Button android:text="Achievements" android:textSize="18sp" android:id="#+id/btn_achievements" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Tutorial" android:textSize="18sp" android:id="#+id/btn_tutorial" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Leaderboard" android:textSize="18sp" android:id="#+id/btn_leaderboard" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_music" android:text="Music" android:textSize="18sp" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_sound" android:textSize="18sp" android:text="Sound" android:layout_marginBottom="10dip" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
</LinearLayout>
</RelativeLayout>
<ImageButton android:background="#drawable/btn_back" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/ib_back_options_menu"></ImageButton>
</RelativeLayout>
#Dialog showing code
final OptionsDailog mDailog = new OptionsDailog(JungleCrashLand.this);
mDailog.show();
The Dialog class works with its own set of identifiers. If you create a dialog with createDialog(int x) you can pass this x as your own identifier. removing the dialog follows the same structure: removeDialog(int x) where x is, again, the identifier of your dialog type. It is done this way to be able to distinguish between different "types" of dialogs, defined by your own, without the need to extend the dialog class.
so basically:
static final int OPTIONS_DIALOG = 0;
[...]
createDialog(OPTIONS_DIALOG);
[...]
removeDialog(OPTIONS_DIALOG);
as you can have only one dialog per activity, this will remove only this dialog and every(!) reference to it. Read http://developer.android.com/guide/topics/ui/dialogs.html to fully understand what is going on here.
I'm trying to create a music player that will be visible in all my activities. To do this, I'm going to use a Fragment as some of you advised me earlier.
Since I have no experience with Fragments whatsoever, I decided to implement a fragment in a "filler" activity first.
I created a simple fragment containing only a button and some text, and try to inflate that in my filler activity.
However, after inflating this fragment does not show up. A message does get printed to LogCat telling me that the fragment has been inflating.
I'm pretty sure I'm missing something, but since I have no experience with this and I couldn't find a tutorial that quite explained how to do this, I don't know what it is that I'm missing.
Music player Fragment
public class AppMusicPlayer extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
System.out.println("fragment added");
return inflater.inflate(R.layout.musicplayer_main, container, false);
}
}
Music Player 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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the fragment you're looking for" />
</LinearLayout>
Filler Activity
public class Filler extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filler);
Button b = (Button) findViewById(R.id.terug);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Filler layout
<?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" >
<!-- Title bar -->
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/title_bar" >
<TextView
android:id="#+id/fillertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Work In Progress"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" >
</TextView>
<Button
android:id="#+id/terug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="#drawable/button_back"
android:text="Back"
android:textColor="#FFFFFFFF"
android:textStyle="bold" >
</Button>
</RelativeLayout>
<!-- End of title bar -->
<TextView
android:id="#+id/wip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This section of the app has not been made yet. Work in progress." >
</TextView>
<fragment
android:id="#+id/musicPlayerFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:name="com.mobowski.app.player.AppMusicPlayer" />
</LinearLayout>
Obviously I'm doing something wrong, but what?
Note: I'm using Android 2.3 with the Compatibility library.
Any help is appreciated
The problem has been fixed with the help of Yashwanth Kumar and blessenm. One gave an XML solution, the other a programmatic solution. Now I'm just wondering which solution is the most desirable one. Are there any concessions to be made with either of the solutions, or does it all boil down to the programmer's preferred solution?
I haven't tried adding fragments directly to xml. But what I normally do is add a framelayout or linearlayout to the filler layout.
Suppose its id is 'fragment_holder'. I use the following code in the fragment activity right after setContentView. Try this
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new AppMusicPlayer(),"musicplayer");
ft.commit();
you should mention layout_height as 0dip, not the width in vertical linear layout.
change that it should work.
<fragment
android:id="#+id/musicPlayerFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.mobowski.app.player.AppMusicPlayer" />
i'm having some problems with autofocus in a tabhost . I need to display softinput and focus in the editText when i open that tab (with that edittext) , better explained with the next picture :
In one tab i have that layout . But when i open it , the editText doesn't gain focus. I have done it badly opening program the editText when the tab shows , but it behavior is erratic and it only runs second or third times it does .
What i want is to do it automatically , like often it does android on editText . I put code now :
tab_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/AbsoluteLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/search_bar"
android:layout_alignParentTop="true"
android:background="#FF000000"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:paddingTop="6dip"
android:paddingBottom="6dip"
android:weightSum="4.0">
<EditText android:id="#+id/search_field"
android:layout_height="45dip"
android:maxHeight="45dip"
android:layout_width="0dip"
android:layout_weight="3.1"
android:maxLines="1"
android:lines="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="flagNoExtractUi"
android:textSize="18dip"
android:hint="#string/search_hint_text"
android:textColorHint="#color/edit_text_hint_search"
android:background="#drawable/et_search_field"
android:textColor="#color/edit_text"
android:layout_marginLeft="2dip"
android:layout_marginRight="0dip"
android:textColorHighlight="#color/edit_text_highlight" />
<ImageButton android:text="Search" android:src="#drawable/ic_search_normal" android:id="#+id/search_button"
android:layout_width="0dip"
android:layout_height="45dip"
android:layout_weight="0.9"
android:scaleType="fitCenter"
android:layout_alignTop="#+id/search_field"
android:layout_alignBottom="#+id/search_field"
android:background="#drawable/button_generic"
android:layout_marginLeft="6dip"
android:layout_marginRight="2dip"
android:padding="10dip"/>
</LinearLayout>
</RelativeLayout>
Then i have the activity into an activity group , but i think it musn't be a problem for autofocus , basically this is what i do with that layout:
public class SearchListActivity extends ListActivity implements OnScrollListener
{
...
#Override
public void onCreate(Bundle savedInstanceState)
{
...
View v = View.inflate(this, R.layout.tab_header, null);
getListView().addHeaderView(v);
...
}
...
}
Thanks for reading , all help will be gratefully read.
Add the following in the OnResume override:
EditText ET=(EditText) findViewById (R.id.search_field);
ET.requestfocus();
ET.SetText("Your soft input");
I am developing an app with tabs. On one of the tabs,
I have two EditText fields and a couple of buttons. I want to add an ExpandableListView
to the tab, below the rest of the elements.
My problem is that the current class extends Activity. All the example I've found extend ExpandableListActivity, but I can't extend both for the same class.
How can I add this list to the tab? I've added an ExpandableListView element in the XML file, but whenever I run it, it doesn't show up. I understand that I need to bind the view to some data, but how do I go about doing this?
Thank you.
Here is my class code:
public class DirectionsTab extends Activity
{
private Button clear_btn;
private Button go_btn;
private EditText origin_txt;
private EditText dest_txt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.directions_tab);
// initialize views and onClick event handler
origin_txt = (EditText)findViewById(R.id.origin_txt);
dest_txt = (EditText)findViewById(R.id.dest_txt);
clear_btn = (Button)findViewById(R.id.clear_btn);
// clear all text fields and return focus to dest_txt
clear_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
origin_txt.setText("");
dest_txt.setText("");
origin_txt.requestFocus();
}
});
} // end public void onCreate(Bundle savedInstanceState)
} // end public class DirectionsTab extends Activity
and XML:
<?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">
<TextView
android:id="#+id/origin_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Origin"/>
<EditText
android:id="#+id/origin_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/origin_lbl"/>
<TextView
android:id="#+id/dest_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/origin_txt"
android:text="Destination" />
<EditText
android:id="#+id/dest_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/dest_lbl" />
<ExpandableListView
android:id="#+id/listView"
android:layout_below="#+id/dest_txt"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /><!--android:id="#android:id/list"-->
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dest_txt"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="Clear" />
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/clear_btn"
android:layout_alignTop="#id/clear_btn"
android:text="Go!" />
</RelativeLayout>
I got this!, If anyone is having problems with this, make sure you follow these steps:
Copy and paste ExpandableList3 example (located in ApiDemos project from Samples folder in your Android SDK). This is just a good example of how to set up a list.
Make your class inherit from ExpandableListActivity
Create the following in your XML layout file:
<ExpandableListView
android:id="#android:id/list"
android:layout_below="#+id/go_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
It is VERY important that the ID property matches what you see above, otherwise, nothing will work. This little detail wasted plenty of hours for me and was discovered seating neatly as a small detail in the android docs.