Getting Tabs (Tabhost) working - android

I'm trying to turn a single page app into one with tabs. I've looked at various Googled solutions, but none work, and I can't find a 'Tabs - how they work' anywhere! One problem I have when using a 'Googled' answer is a null pointer exception on the Layout of main.xml.
Can anyone help/point me to a decent page?
My code is below.
Thanks,
Martin.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/edtICAO" android:text=" "></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Brief!" android:id="#+id/btnBrief" android:onClick="btnBriefClick"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/btnHome" android:text="Home" android:clickable="true" android:onClick="btnHomeClick"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/btnChartList" android:text="Charts List" android:onClick="btnChartClick"></Button></LinearLayout><ScrollView android:id="#+id/ScrollView02" android:layout_width="wrap_content" android:layout_height="wrap_content"><LinearLayout android:id="#+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Metar :" android:id="#+id/txtMetar"></TextView><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TAF :" android:id="#+id/txtTAF"></TextView></LinearLayout></ScrollView><ScrollView android:id="#+id/ScrollView01" android:layout_width="fill_parent" android:fillViewport="true" android:isScrollContainer="false" android:scrollbarAlwaysDrawHorizontalTrack="true" android:scrollbarAlwaysDrawVerticalTrack="true" android:scrollbars="horizontal|vertical" android:visibility="visible" android:layout_height="fill_parent">
<TextView android:id="#+id/txtNotam" android:text="NOTAM :" android:layout_height="fill_parent" android:layout_width="fill_parent" android:textColor="#color/black" android:background="#color/white"></TextView>
</ScrollView>
</LinearLayout>

Hey martin,
make sure you follow this guide:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
The tutorial is not complete, however. After completing all the steps, open you AndroidManifest.xml file and add the following:
<activity android:name=".TabName"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
for every tab (insert appropriate class name instead of TabName). I'm assuming that you will create a separate class for each tab, just like in the tutorial.

Yes, I had looked at that, but thought it was a bit complex. Having now done the project I realise that's the way it is!!
However, the project will compile and install, but won't run.
On a real phone 'Open' is disabled after installation and the app doesn't appear in the apps list.
On the emulator, :
[2010-11-12 11:49:49 - HelloTabWidget] The launch will only sync the application package on the device!
[2010-11-12 11:55:46 - HelloTabWidget] Success!
[2010-11-12 11:55:46 - HelloTabWidget] \HelloTabWidget\bin\HelloTabWidget.apk installed on device
[2010-11-12 11:55:46 - HelloTabWidget] Done!
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="martin.HelloTabWidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".HelloTabWidget" android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:name=".ArtistsActivity" android:label="ArtistsActivity"> </activity>
<activity android:name=".AlbumsActivity" android:label="AlbumsActivity"></activity>
<activity android:name=".SongsActivity" android:label="SongsActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>

Related

Android: Keyboard does not always appear on edittext focus

I have a Login view. When the user starts the app, if he already synced with the server before, the app opens on the login view and the user is prompted to enter his login. In this situation, the keyboard opens automatically when the app starts. If, however, the user never synced before, the user is redirected to the configuration view and will sync from there. After the sync is over, the configuration activity finishes and the user then sees the login view for the first time. At this moment, the focus is on the PIN edit text, as it should, but the keyboard does not open.
I tried several solutions found online, none of which worked. This is my code in its current state:
View:
#Override
public void onResume() {
super.onResume();
EditText pinEditText = (EditText)findViewById(R.id.passwordEditText);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean dbContainsUsers = controller.Dispatcher.Count(new controller.User(), false) > 0;
if (!dbContainsUsers) {
Intent i = new Intent(getApplicationContext(), Configuration.class);
startActivity(i);
}
pinEditText.clearFocus();
pinEditText.requestFocus();
imm.showSoftInput(pinEditText, InputMethodManager.SHOW_IMPLICIT);
}
Layout:
<?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"
tools:context="view.Login">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include layout="#layout/app_bar" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<EditText
android:id="#+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/lgnEditTextMarginLeft"
android:textSize="#dimen/textSizeL"
android:ems="8"
android:inputType="numberPassword"
android:selectAllOnFocus="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal">
<Button
android:id="#+id/btnSynchroniser"
style="#style/RexforetTheme.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/btn_marge_top"
android:drawableLeft="#drawable/icon_sync"
android:textSize="#dimen/textSizeM"
android:text="#string/btn_synchroniser" />
<Button
android:id="#+id/btnConfiguration"
style="#style/RexforetTheme.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/btn_marge_top"
android:drawableLeft="#drawable/icon_configuration"
android:textSize="#dimen/textSizeM"
android:text="#string/btn_configuration" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.aaa.bbb">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/RexforetTheme">
<activity android:name="view.Login">
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:windowSoftInputMode="stateVisible"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="view.MenuPrincipal" />
<activity android:name="view.Configuration" />
<service
android:name="service.WebDBService"
android:exported="false"/>
</application>
</manifest>
you have android:windowSoftInputMode="stateVisible" in this place replace
android:windowSoftInputMode="stateHidden|adjustPan"
in your menifest file
you have android:windowSoftInputMode="stateVisible" in this place replace
android:windowSoftInputMode="adjustResize"
in your manifest file.

Why keyboard is overlap on the AutoCompleteTextView when inside ScrollView

I have a simple android layout and code, but the keyboard always overlap my AutoCompleteTextView when I click it.
Note : The AutoCompleteTextView when inside ScrollView.
Note : platform is 6.0.1
package com.example.jason_wu.search;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
public class Main2Activity extends AppCompatActivity {
private AutoCompleteTextView mAtv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mAtv = (AutoCompleteTextView) findViewById(R.id.location_auto_complete);
//issue 1
//mAtv.setBackground(getDrawable(R.drawable.search_input_focus));
//issue 2
mAtv.setHeight(300);
}
}
and my AndroidManifest.xml here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.jason_wu.search">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.example.jason_wu.search.Main2Activity"
android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Layout.xml :
<?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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="800dp"
android:text="Large Text"
android:id="#+id/textView"/>
<AutoCompleteTextView
android:id="#+id/location_auto_complete"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:background="#color/transparent"
android:dropDownAnchor="#+id/location_auto_complete"
android:dropDownHeight="wrap_content"
android:dropDownVerticalOffset="1dip"
android:fontFamily="sans-serif-light"
android:hint="#string/new_notification_location_hint"
android:imeOptions="actionSearch"
android:inputType="textUri"
android:dropDownSelector="#color/location_bg_select"
android:popupBackground="#color/location_bg_notselect"
android:singleLine="true"
android:textColor="#FFFFFF"
android:alpha="0.2"
android:textColorHint="#999999"
android:textCursorDrawable="#null"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_below="#+id/location_auto_complete" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
See
try this attribute to the activity in the AndroidManifest.xml
android:windowSoftInputMode="stateHidden|adjustPan"
it should look something like this.
<activity
android:name="com.myexample.TestActivity"
android:label="#string/login_screen"
android:screenOrientation="portrait" // well you can change the orientation if you want
android:windowSoftInputMode="stateHidden|adjustPan" />
the attribute you want to play with is this windowSoftInputMode try different values as you may require different output after certain time.
adjustPan
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
adjustResize
The activity's main window is always resized to make room for the soft keyboard on screen.
stateHidden
The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.
you can find more details on activity-element documentation
Update
I have written something that might help you, layout is available on the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="activities.list.first.TestLayoutActivity">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/TestLayoutActivity_autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:completionThreshold="1"
android:drawableLeft="#android:drawable/ic_menu_search"
android:text="" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
AndroidManifest.xml
<activity
android:name="activities.list.first.TestLayoutActivity"
android:label="#string/title_activity_test_layout"
android:windowSoftInputMode="stateHidden|adjustPan"
/>
Code is on the code
String[] list = {"match1", "match2", "match3",
"match12", "match11", "match8", "match7", "match4",
"match13", "match10", "match9", "match6", "match5",};
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.TestLayoutActivity_autoCompleteTextView);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, list);
autoCompleteTextView.setAdapter(adapter);
Before
After
[
Try limiting the dropdown items to show
One way is you can Limit the dropdown items to show. But there's no such proper way of it to show number of maximumSuggestions option, But the trick you can apply is Get the height of one of the rows and multiply it (number of rows to show) times and use setDropDownHeight() to set its height.
There is a method setDropDownHeight of AutoCompleteTextView class. You can see them and use.
If you are confused or not sure of height of suggestions items you can customize this. Have a look how to set custom height.
The keyboard behavior is that scroll the view to close the bottom of text area.
Finally, we have to use android:gravity="bottom" attribute to solve UI issue, but we need to consider the UX, ex: Hint.
So, The solution is to listen onClick() and For keyboard issue, we have to use dynamic method to set the gravity.

Android: EditText in ScrollView goes of the screen when kayboard appears

I am trying to create a login screen. I have used ScrollView to hold input fields. I have following xml:
<RelativeLayout ... >
<RelativeLayout
android:id="#+id/top_menu" ... >
<ImageButton ... />
<com.neopixl.pixlui.components.textview.TextView... />
</RelativeLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/top_menu"
android:overScrollMode="always">
<LinearLayout
android:id="#+id/login_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<com.neopixl.pixlui.components.edittext.EditText
android:id="#+id/user_name_email"
android:layout_width="match_parent"
android:layout_height="#dimen/input_height_size"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/edit_text_strock"
android:hint="#string/user_Name_Email_EditText"
android:maxLines="1"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:textColor="#color/White"
android:textColorHint="#color/White"
android:textSize="#dimen/register_input_text_size"
pixlui:typeface="MuseoSans.otf"/>
<com.neopixl.pixlui.components.edittext.EditText.... />
<com.neopixl.pixlui.components.button.Button... />
<com.neopixl.pixlui.components.textview.TextView... />
<com.neopixl.pixlui.components.textview.TextView... />
<com.neopixl.pixlui.components.textview.TextView... />
<com.neopixl.pixlui.components.button.Button... />
</LinearLayout>
</ScrollView>
</RelativeLayout>
You can see full xml here.
When I click on EditText user_name_email, it goes off the screen.
Before click:
After click:
I have tried scrollTo(x, y) and smootScrollTo(x, y) bu they didn't work.
How can I fix it?
UPDATE:
adjustPan works but I want it to work with adjustResize.
Is it possible?
Use the following line in the manifest file, for the activity you need the change.
android:windowSoftInputMode="adjustPan"
Use it as shown below:
<activity
android:name=".ActivityName"
android:label="title_for_activity"
android:windowSoftInputMode="adjustPan" >
</activity>
Hope this helps.
add this to manifest file:
android:windowSoftInputMode="adjustPan"
in the targeted activity tag.

Why is the Android activity weirdly zoomed in?

I am running the app on Galaxy Note, which is hdpi. And I got all the drawables (drawable-hdpi, drawable-mdpi etc.) correct. But the page (activity) is weirdly zoomed in like this:
You can notice that the cursor icon, top header and the background tiles are larger than normal.
It is supposed to be like this:
The layout file:
<?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"
android:background="#drawable/bg_app"
>
<EditText android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:inputType="textMultiLine|textCapSentences"
android:textColor="#000"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="top|left"
android:padding="10dp"
android:layout_margin="10dp"
android:lineSpacingMultiplier="1.1"
android:minLines="3"
/>
</LinearLayout>
you need to add the following to the manifest
the tag "uses-sdk" with android:minSdkVersion="8" (it might work with 7 I did not test)
:-)
Problem solved by using
<uses-sdk android:minSdkVersion="4" />
in AndroidManifest.xml

Overlay an activity on another activity OR overlay a view over another

I have 2 classes, FirstActivity and SecondActivity.
First Activity
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
Is it possible for SecondActivity to overlay on FirstActivity? ie. FirstActivity gets dimmed, SecondActivity gets displayed on top of FirstActivity.
If it is not possible for 2 different activities, is it possible to do an overlay for 2 views in the same activity? I hope using dialog is not the only option.
I suggest you set your second activity up as a dialog -- which will dim the background. Here is a tutorial that could be helpful:
http://developer.android.com/guide/topics/ui/dialogs.html
http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
Or you can simply set the theme in the manifest as a dialog for your SecondActivity.
If you don't want to do a dialog, you can overlay views using a relative 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" >
<LinearLayout android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="some content"
android:textSize="70dp"/>
</LinearLayout>
<LinearLayout android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99000000"
android:clickable="true"
android:visibility="gone">
<EditText android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="50dp" />
</LinearLayout>
</RelativeLayout>
The first LinearLayout (id/content) is your base layout where your normal content would go.
The second LinearLayout (id/overlay) is your overlay layout which you'd want to show over top of the base layout. The background color will give give you that faded out background, and you can add whatever you want to that layout to make your overlay. To show the overlay, just change its visibility from gone to visible.
In manifest file declare the secondactivity activity like this. android:theme="#android:style/Theme.Dialog".then simply call the secondactivity from firstactivity from your code.
<activity
android:name=".FirstActivity"
android:label="#string/title_activity_first" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second"
android:theme="#android:style/Theme.Dialog"
>
<intent-filter>
<action android:name="transparent.text.SECONDACTIVITY"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Second Activity xml file.you can design as your wish but for reference i have posted this.the key concept is in manifestfile (ie) how to define your secondactivity in manifest
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="192dp"
android:background="#aabbcc"
android:text="Sybrant has provided Takoma with a great team which helped us from the beginning to the final stage of our product, to our fullest satisfaction. We have been able to deliver a high quality of eLearning products to our corporate customers like Nissan with Sybrant’s support”"
tools:context=".FirstActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="43dp"
android:layout_marginLeft="80dp"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button1"
android:layout_below="#+id/textView1"
android:layout_marginRight="42dp"
android:layout_marginTop="80dp"
android:text="TextView" />
</RelativeLayout>
1-Take a screenshot of the first activity.
2-(Optional) Darken, tint or blur the screenshot.
3-Then call the second activity and use the first activity screenshot as background for the second activity.

Categories

Resources