How to display TabActivity in AlertDialog?
I know that it's a little bit tricky, as we should run TabActivity as normal activity,
e.g.
Intent intent = new Intent(MyClass.this, Settings.class);
startActivity(intent);
where Settings - TabActvivity.
The only way I know it to set view for AlertDialog, but it will don't work.
View view = (View) ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.settings, null);
new AlertDialog.Builder(this).setTitle("AlertDialog").setView(view).show();
Is there any way to show TabActivity in AlertDialog?
Thanks,
You're sort of conflating the concept of Views and Activities here, but probably the easiest way to do what you want is to set your TabActivity's theme to Theme.Dialog and start it, instead of using an AlertDialog and trying to wrap an Activity inside a popup inside another Activity. For your own sanity's sake, don't go down that road into Inception-type territory.
I wont really recommend this approach but since you have requested the behavior heres a sample code:
Heres a Tab layout, which has a EditText in tab1 and a Button in tab2
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_centerHorizontal="true"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="TextBox"
/>
</LinearLayout>
<Button android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button"
/>
</FrameLayout></LinearLayout></TabHost>
Code to inflate this layout to a AlertDialog
AlertDialog.Builder builder=new AlertDialog.Builder(this);
LayoutInflater inflator=getLayoutInflater();
View view=inflator.inflate(R.layout.main, null);
TabHost tabHost=(TabHost)view.findViewById(R.id.tabhost);
tabHost.setup();
TabHost.TabSpec spec=tabHost.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Clock");
tabs.addTab(spec);
spec=tabHost.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Button");
tabs.addTab(spec);
builder.setView(view);
builder.setPositiveButton("OK", null);
builder.create().show();
As I said THIS IS NOT RECOMMENDED APPROACH create a theme instead as suggested by Yoni Samlan above.
Related
I want to implement something like what instagram does here.
Upon long pressing on a post it opens up another view on top the screen to show the post and makes the remaining part of the screen blur.
for creating something like this in android you can use alertDialog with custom layout and blur the background:
custom_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button1"
android:layout_below
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
in Activity:
//create an alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Name"); //you can add this to your custom layout only
// set the custom layout
final View view = getLayoutInflater().inflate(R.layout.custom_layout, null);
builder.setView(view);
Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
// onclick functions
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
for blur background, you can add a theme like https://stackoverflow.com/a/41373144/7364284 but, I would suggest using window attributes like https://stackoverflow.com/a/10381077/7364284 because of its less code and easy to use.
I follow tutorial found on internet, but it seems that doesn't work...
I get Tab 1 always opened, that is okay, but I don't see TABS menu up...
Here is my code:
Main2Activity:
public class Main2Activity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// create the TabHost that will contain the Tabs
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Analog Clock");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("DigitalClock");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag3");
spec.setContent(R.id.tab3);
spec.setIndicator("Button");
tabs.addTab(spec);
}
Content_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AnalogClock android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<DigitalClock android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Do Nothing"/>
</FrameLayout>
</LinearLayout>
</TabHost>
This is how it looks when i run it
I solved it with:
setContentView(R.layout.content_main2);
I was calling activity_main2, but don't know why when I call activity_main2 don't get content_main2.
I have in Activity_main2.xml:
<include layout="#layout/content_main2" />
Does anyone know why?
you can use seperate activity for each tab. as said by "intelliJ". create seperate activity and seperate xml file for each tab and then add your analog digital and button in each activity respectively. say in activity1 add analog and in activity2 add digital and so on.
I am working on a project in which I need to dynamically add TextView and Spinner as well through the program. I was able to add these two things dynamically from my program successfull.
Below is the XML layout I have used for that.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:gravity="center_vertical|center_horizontal"
android:text="Save" />
</LinearLayout>
</ScrollView>
Problem Statement:-
So what I want is- All the TextView and Spinner should get shown firstly then in the last Button should get shown.
But what is happening currently Save Button gets shown at the Top and then all the other TextView and Spinner gets shown.
How can I make sure, that Button comes at the bottom of screen.
Below is the screenshot from which you can figure out the problem. I want to show the Save button at the bottom of screen.
Or you can create a button like this in the code and added to your layout after adding all the spinners and textviews like below, you will have to remove the declaration of the button from your xml:
Button button = new Button(this) ///this is a context object
button.setWidth(100);
button.setText("Save");
layout.addView(button);
You can dynamically create the layout and add the button to that layout and then add this layout to the Super layout
Or what you can do is Declare a new xml file in the res/layout/savebutton_layout.xml
<?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="horizontal"
android:id="#+id/lLayoutBT"
>
<Button
android:id="#+id/saveBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/save" />
</LinearLayout>
Now in your code you will have to find this layout containing the button and then add this layout to your main layout (just like you were adding your button)
For example
LinearLayout buttonLayout =(LinearLayout) view.findViewById(R.id.lLayoutBT);
views.add(buttonLayout);
If you use a RelativeLayout (instead of a LinearLayout) you have access to properties like:
<Button
android:id="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
...
</Button>
If you change your LinearLayout to a RelativeLayout then add this in your Button
<Button
...
android:layout_alignParentBottom="true"/>
it should give you what you want. I use RelativeLayout instead of LinearLayout as often as possible anymore because in most instances, it is the more flexible and efficient. They take a little more time to get used to but are definitely worth taking the time to understand and implement
Edit
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
android:text="Save" />
</RelativeLayout>
</ScrollView>
Something like that should work. You may have to play with it a little to get exactly what you want but that should get you there.
I have a button inside of a LinearLayout that is oriented vertically. Not sure why the button doesn't show up?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<com.commonsware.cwac.tlv.TouchListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tlv="http://schemas.android.com/apk/res/org.stocktwits.activity"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:drawSelectorOnTop="false"
tlv:normal_height="64dip"
tlv:grabber="#+id/icon"
tlv:remove_mode="slideRight"
android:layout_height="wrap_content"/>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
android:height="wrap_content" has no meaning for vertically-scrollable widgets like ListView. Use android:layout_weight with your LinearLayout or a RelativeLayout to achieve whatever look you are trying to achieve.
Try adding this to your LinearLayout:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
I'm guessing the layout is just not showing?
I am currently trying out the Android 1.5 SDK and I have seen a couple of examples on TabHost.
What I am trying to do is to use a different button on each Tab to do its tasks.
What I tried
was using onClickListiner() and onClick(). I think this is what all the developers use, but I keep getting a null exception on the LogCat every time the button is pressed. Also I have each XML Layout so I call the Tab as : tab.add(...setContent(R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
What would be the best way to make a button/TextView work properly under the TabHost?
I'm not entirely sure where your problem is, but this is how I've set up my Tabbed activities before
layout.xml
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Tab.java
public class InitialActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
}
}
Then, you can just use findViewById() on any views within the tabs, or if there are shared names between them, you can do findViewById(R.id.tab1).findViewById(R.id.text)