Start a activity like a "Dialog", by writing java code - android

I want to make a activity like a "Dialog", and I know two ways so far:
Way 1) In Android ApiDemos, it is implemented by adding the attribute to the activity like
android:theme="#android:style/Theme.Dialog">
The result is: the new Activity appears on top of the existing activity, that is what I want.
Way 2) I try to invoke setTheme(android.R.style.Theme_Dialog) in the Activity.onCreate(Bundle) method, and the new activity also appears, but the background is all black. This is not what I want. Code is as below:
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Dialog);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.selecte_date);
Can anybody tell me how to implement the effect by writing Java code?

The easiest way is to add a second layout in you xml which is a overlay of existing one.
then you can set the overlay to visible in your activity. So then you got multiple views in 1 activity.
<?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:background="#android:color/transparent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#android:color/transparent">
<ImageView
android:id="#+id/icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_alignParentTop="true"
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_black"
android:visibility="false" >
</LinearLayout>
</RelativeLayout>

Related

How can I have one unique button in multiple components of a layout in android?

So I have an xml that consists of a linear layout containing a Button and a TextView like this:
<?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="100dp"
android:orientation="vertical">
<Button
android:id="#+id/btnCell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingLeft="40dp"
android:text="Button"
android:textColor="#color/blueText" />
<View
android:height="wrap_content"
android:width="wrap_content"
android:background="#drawable/Test"/>
</LinearLayout>
And I want to use this same layout inside other layouts in a different xml. I need the same button at every time, so I reuse it by including it in the two layouts (both layouts are in the same xml, but ones is hidden):
First one
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include layout="#layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Second One:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include layout="#layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
So I show the first layout and hide the second one at the beginning of the app , as the user moves within the interface, the layouts exchange so that the other one is shown and the first one hidden.
The thing is that I declare the Button in my java activity class like this:
btnCell = (Button) thirdView.findViewById(R.id.btnCell);
btnCell.setOnClickListener(this);
And implemented the listener.
#Override
public void onClick(View v) {
if (v == btnCell) {
System.out.println("entered if");
}
System.out.println("entered function");
}
The problem is that when I click the button when the first view is shown and the second hidden, the button works just fine, but when I unhide the second layout, hide the first one, and proceed to click the button, that should be the same as the first one but in a different layout, nothing happens. I searched and find out, that this happens because the id is assigned only to the button shown in the first layout because of view hierarchy, but not the one in the second layout. How can I make both buttons react to the same action, without declaring a new button in each layout but instead reusing it?
I have used this type of layout. you can create Id different for both and inflate that view and give different name so You can differentiate both thing.
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include android:id="+id/firstOne" layout="#layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android second one is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include android:id="+id/secondTwo" layout="#layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The Problem is both the layout are include in same layout file and the id of that
button are same so whenever you click on any of the button at the same time event will fire on both button like both are clicked.
So, you have to give the different id for both the button I hope it works fine..
You can add a different Id for each included layout:
<include android:id="+id/layout_a" layout="#layout/buttonLayout"/>
and
<include android:id="+id/layout_b" layout="#layout/buttonLayout"/>
and then use two findViewById to reach them:
btnCellA = (Button)thirdView.findViewById(R.id.layout_a).findViewById(R.id.btnCell);
btnCellB = (Button)thirdView.findViewById(R.id.layout_b).findViewById(R.id.btnCell);

Android : put listfragment inside xml layout

ListFragment by defaults shows a progressbar while the data get loaded. I plan on putting the listfragment in xml file and have it show progressbar till I get the data from the server.
The reason I m trying to put listfragment inside xml is because in my layout I have a linearlayout above the place where I plan to put listfragment.
Here is my xml file.
<?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"
>
<LinearLayout
android:id="#+id/filterHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#color/white"
>
<ToggleButton
android:id="#+id/filterToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:textColor="#color/black"
android:textOff="#string/filterToggleButtonText"
android:textOn="#string/filterToggleButtonText"
android:drawableLeft="#drawable/filter_small"
/>
</LinearLayout>
<fragment
andorid:name="in.coupondunia.androidapp.testListFragment"
android:id="#+id/couponsByCategoryFragment"
android:layout_below="#id/filterHolder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Have you tried using http://developer.android.com/reference/android/app/ListFragment.html#setListShown(boolean) with parameter true? From the docs it might work.

Losing EditText style after adding Views programmatically in Android

I have a layout called wizard that has an empty ViewFlipper inside of it.
I have other views that include this 'wizard' and programmatically add children to the ViewFlipper inside the wizard. The problem I'm having is that one of these children has an EditText, and this View loses it's default style. I tried setting everything up in the xml file and the problem dissapeared, so the problem is in adding the views programmatically. I Also tried calling invalidate() on the main container (in the code below: #+id/recarga_celular_main), but nothing happens.
The Edit text can be seen in the following link: EditText weird style problem
The view is something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recarga_celular_main"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/theme_home_background"
android:gravity="center_horizontal|top" >
...
<CustomWizard
android:id="#+id/recargaCelular_wizard"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="#drawable/theme_layer_blue_bottom_black"
android:gravity="center_horizontal|center_vertical" >
</CustomWizard>
...
</RelativeLayout>
The 'wizard' is something like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/touch_wizard_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:gravity="center|top"
android:orientation="vertical" >
...
<ViewFlipper
android:id="#+id/wizard_flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/aceptar_limpiar_btns"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
...
</LinearLayout>
In the java code, first I add the view to the flipper, and then I display the desired child:
flipper.addView(step.getView(), flipper.getChildCount() - 1);
...
flipper.setDisplayedChild(index);
EDIT
Manually set the default EditText style:
<EditText
style="#style/some_style"
android:background="#android:drawable/edit_text"
android:gravity="left"
android:hint="Celular" />

Accessing Android views on nested layouts

I have trouble accessing Views from a layout that is included in another layout.
Please take a look at this picture:
http://dl.dropbox.com/u/3473245/layout_includes.png
How do I access the 4 text views programmatically?
Its probably something really simple that I'm missing.
Thank you very much!
You can do as follows:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include android:id="#+id/item_base_lang" layout="#layout/dictionary_list_item" />
<include android:id="#+id/item_learn_lang" layout="#layout/dictionary_list_item" />
</LinearLayout>
dictionary_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dictionary_list_item_text_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dictionary_list_item_text_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
To set the text programmatically:
((TextView)findViewById(R.id.item_base_lang).findViewById(R.id.dictionary_list_item_text_header)).setText("item_base_lang_header");
((TextView)findViewById(R.id.item_base_lang).findViewById(R.id.dictionary_list_item_text_content)).setText("item_base_lang_content");
((TextView)findViewById(R.id.item_learn_lang).findViewById(R.id.dictionary_list_item_text_header)).setText("item_learn_lang_header");
((TextView)findViewById(R.id.item_learn_lang).findViewById(R.id.dictionary_list_item_text_content)).setText("item_learn_lang_content");
This Android wiki page shows how to use reusable UI components with XML layouts, but it doesn't show how to access nested reusable components from code.
Although it is fairly straightforward, it might be not so clear for those who are pretty new to Android Views.
The following two lines should help you get the languageHeader of both includes. You can do the same for languageText
findViewByid(R.id.activityBaseLangView).findViewById(R.id.languageHeader)
findViewByid(R.id.activityLearnLangView).findViewById(R.id.languageHeader)

Android Layout : On Top it takes too much of space

In my 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:id="#+id/user_pswd_new_root" android:scrollbars="vertical"
android:soundEffectsEnabled="true">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/ScrollViewLogin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarStyle="outsideInset" android:scrollbars="vertical|horizontal" android:visibility="visible">
<RelativeLayout android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_height="fill_parent">
<ImageView android:background="#drawable/logo_login" android:layout_height="wrap_content" android:id="#+id/imageView1"
android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:padding="0dp" android:layout_margin="0dp"/>
...............
</RelativeLayout>
</ScrollView>
</RelativeLayout>
With the above code, I set in a Dialog and things are shown proeprly, but there is lot of unwanted space above the image which unnecessarily increases the height of the dialog. See the results :
Any idea why the top space is occupied. And how do I get rid of it. Where am I going wrong ?
It's the title of the Dialog, which is empty because you didn't specify a title (but the view is still there). You have to remove it, for example like this:
class MyDialog extends Dialog {
#Override
protected void onCreate(Bundle savedInstanceState) {
// make sure to call requestWindowFeature before setContentView
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog_layout);
// other initialization code
}
// ...
}
But that depends on whether you are using a simple Dialog or an AlertDialog. If this doesn't work for you, post your dialog-creation code (Java) and I'll update my answer to show how to remove the title in your case.

Categories

Resources