I'm creating a search widget and a searchable activity. I followed the android developer guide and have this so far.
Here is my searchableActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
As mentioned, R.layout.search is creating the error. I don't have a search xml in layouts, and I don't understand what I am supposed to define within search.xml.
You need to have a layout named search.xml in your res/layout folder where you'll show the search results.
I my case I showed the search results in a RecyclerView and here's a sample layout of mine.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_background_search">
<LinearLayout
android:id="#+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:tint="#color/color_primary_light"
android:src="#drawable/ic_action_search" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty_search_result"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/search_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
You are trying to set the current layout to search.xml and you said yourself that you do not have this file present.
setContentView is setting the layout that you will see when running the app in this instance.
Related
I am using fragments in an activity with a BottomNavigationView and out of the box I noticed the fragment content goes below the BottomNavigationView
This is the fragment in Android studio before inflating it in the activity:
The app after the fragment has been inflating:
Here is my fragment XML I read on other posts on this site that I needed to use the CoordinatorLayout as parent and add app:layout_behavior="#string/appbar_scrolling_view_behavior" to the first child but it didn't work.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_margin="16dp"
tools:context="com.brigafrica.koyako.fragments.SendFragment">
<TextView
android:id="#+id/textView"
style="#android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:text="#string/amount_to_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edtAmountToSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:digits="0123456789.,"
android:inputType="numberDecimal"
app:layout_constraintBottom_toTopOf="#+id/appCompatButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.095" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/appCompatButton"
style="#style/AppTheme.RoundedCornerMaterialButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="10dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="#string/send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
What is necessary to identify the problem is your activity XML content and how you are managing the fragments in your activity.
The simplest way to think is that the fragment is just a content that will be displayed in a piece of your activity, and you should provide the correct information about the dimensions of this piece in your activity.
Explaining with my example
An activity called MainActivity that has one ViewPager and one BottomNavigationView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="newidea.com.br.bottomnavigationexample.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/main.viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentTop="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:menu="#menu/bottom_navigation_main" />
</RelativeLayout>
The ViewPager is the piece where the fragment will be displayed in MainActivity, so I have to tell the activity that the ViewPager can not invade the BottomNavigationView space, if not the fragment content that will be displayed in the ViewPager will goes below the BottomNavigationView.
How to do that ?
In my example I configured the ViewPager with this:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentTop="true"
It means that the ViewPager width and height matchs the parent's width and height, the ViewPager top is aligned with the parent's top, and it is a layout above the BottomNavigationView, so the ViewPager can not invade the BottomNavigationView space.
What you need to understand is that doesn't matter what is in your fragment, what really matter here is the dimensions configuration of the ViewPager, because the ViewPager will display the fragment.
Full example with 3 fragments, each one with a Button in the Bottom
Fragment in Android Studio before inflating
The app after the fragment has been inflated
This activity doesn't open my images at all however the text still opens. I tried both .png and .jpg. I have also tried #drawable still no result.
Java code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
}
xml script
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:text="JUST WORK!!!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="58dp"
android:layout_marginBottom="177dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_above="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="120dp"
android:id="#+id/imageView2" />
</RelativeLayout>
Thank you in advance
Your code looks okay.
Try adding your image in your drawable folder.
And add,
android:src="#drawable/yourimage"
in your ImageView XML components.
Basically what I'm trying to do is have half the screen be a tab layout and half an image (or really anything else). It works just fine until I start adding weight.
As is, nothing is displayed. What would cause this problem?
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="left"
android:weightSum="2"
android:background="#00025a">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#android:id/tabcontent">
</FrameLayout>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/test_room_1"/>
</android.support.v4.app.FragmentTabHost>
Without the weight component the tab related stuff displays, but I'm assuming it pushes the image out as it isn't shown.
Any help would be appreciated. :)
UPDATE:
Here's a screenshot as requested: Weighted
And the calling activity:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.ImageView;
public class sandbox_mode extends FragmentActivity {
ImageView level_background;
private FragmentTabHost mTabHost;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sandbox_mode_play);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Enemy Tab").setIndicator("Enemies"),
enemy_tab.class, null);
}
}
If you look at the documentation on FragmentTabHost link, you'll see that it's not inherited from LinearLayout, so basically layout_weight is not supported by this container. I would suggest adding additional top LinearLayout for this to work correctly.
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="left"
android:weightSum="2"
android:background="#00025a">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#android:id/tabcontent">
</FrameLayout>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/test_room_1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
use childFragmentManager instead FragmentManager
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);
If you want to use nested fragments within a native Fragment. use getFragmentManager().
If you want to use nested fragments within a support library Fragment, use getChildFragmentManager().
I am trying to build a simple Android app one of whose activities will have a Google Places AutoCompleteTextView along with a Button and several other components. However, it seems to be the case that the AutoCompleteTextView component does not want to play friendly with anything else. Currently, I can only get one (the AutoCompleteTextView component) or the others rendering at a time. Here are the relevant portions of my code:
activity_main.xml
<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="horizontal" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
GooglePlacesAutocompleteActivity.java
public class GooglePlacesAutocompleteActivity extends Activity implements OnItemClickListener {
// class fields are here ...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDateOnView(); // sets up the date picker
addListenerOnButton(); // adds a listener to a Button
// next three lines configure the Google Place autocomplete
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item));
autoCompView.setOnItemClickListener(this);
}
// more methods...
}
If you need more information to render an answer here, please feel free to ask me and I will post as soon as I can.
Hi You have a problem of orientation in your LinearLayout. I guess you want your AutocompleteTextView in top of the rest. If that is the case you should probably add a nested LinearLayout.
<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" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<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="wrap_content"
android:orientation="horizontal" >
[your other items]
</LinearLayout>
</LinearLayout>
If this is not what you are looking for just change match_parent to wrap_content for thw width of your AutocompleteTextView.
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)