Action Button Without Notification - Wear - android

I want to know if it is possible to create this kind of buttons on android wear inside my app (without a notification like on the link).
http://developer.android.com/training/wearables/notifications/creating.html

You can use this view : ActionPage. You can set the text below, the icon inside, the action to perform on click, etc. Check the documentation for more.
Here is an example :
<android.support.wearable.view.ActionPage
android:id="#+id/action_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="1"
android:textColor="#color/st_grey" />
I used it in a GridViewPager : it was in a fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_action, container, false);
ActionPage actionPage = (ActionPage) view.findViewById(R.id.action_page);
actionPage.setText(actionText);
actionPage.setImageResource(iconResId);
actionPage.setOnClickListener(onClickListener);
return view;
}

Related

How to respond BottomSheetDialogFragment's outside touch events in Android

I want to prevent dialog dismissing and response touch event behide the dialog when I touch outside of the BottomSheetDialogFragment, so I do like this in my BottomSheetDialogFragment class:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.poi_result_bottom_dialog, container, false);
this.getDialog().setCanceledOnTouchOutside(false);
return dialogView;
}
However, I find if I set 'setCanceledOnTouchOutside(false)', my activity under the dialog can't respond touch event when I touch outside of the dialog.
You should not be using a BottomSheetDialog. What you want is referred as a Persistent Bottom Sheet. It is well described with instructions at androidhive.info by using an embedded View, and setting up a BottomSheetBehavior.

set textview to other xml in fragment

I am doing an android project which uses a Tabhost to link to 3 fragments and need some communication between them. Each tab will include another xml file. I linked different element in different sml file to the .java file. But it doesn't seem to work. Here is the code:
In Main.xml
<TabHost>
<LinearLayout>
<include
android:id="#+id/fragmentreview"
layout="#layout/fragentreview"/>
</LinearLayout>`
In fragentreview.xml
<LinearLayout>
<TextView
android:id="#+id/textViewinfragR"
android:text="Text in new xml reviews" />
</LinearLayout>`
In fragment.class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragentreview, container, false);
txtMsgR = (TextView)rootView.findViewById(R.id.textViewinfragR);
txtMsgR.setText("Main Car integer = ");
return rootView;
}
When I launch the app textViewinfragR it should show Main Car integer = but it is still showing Text in new xml reviews. Is there a problem in my code?
You are setting the text in the wrong place. Try setting it in onActivityCreated
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtMsgR = (TextView) view.findViewById(R.id.textViewinfragR);
txtMsgR.setText("Main Car integer = ");
}
This should work. The onViewCreated method is called after the inflation is completed.
onCreateView -> onViewCreated -> onActivityCreated

Customize a xml layout in a fragment - Android

I am new in Android programming.
I created the main Activity of my app style google shop ussing ActionBarSherlock and a NavigationTabs, with fragments, each referencing another activity (Fragment 1 Fragment 2, etc) and each fragment inflating a layout.
However, I'm used to create layouts in xml and then customize them in java. To put a different text depending on the time of day, or according to some data in a database, giving function to buttons, etc.. But in a Fragment Class, I can not even use setContentView to work with each text or button, and set the context for using my database is giving me problems.
How I can customize a xml layout in a fragment?
Or what would be the right thing to do?
Here my Fragment:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.menu, container, false);
}
This is more simple then you think. onCreateView instanciate au returns the view for your Fragment. As you said, in a simple Activity you set (and instanciate) the view with setContentView() and then you get your Views with findViewById().
findViewById() asks for the view to return the view item that you want, you can call it from your view before returning it. Like this:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.menu, container, false);
// For example, getting a TextView
TextView tv = (TextView) v.findViewById(R.id.myTextView);
// do your job
return v;
}
so far so good, you just need to use the view you are inflating to get everything.
here is an example
View v = inflater.inflate(R.layout.menu, container, false);
Button b = (Button)v.findViewById(r.id.button1);
return v;
inside onActivityCreated you could use:
View mView = getView();
TextView textView = (TextView) view.findViewById(R.id.theIdOfTextView);
where theIdOfTextView is declared inside R.layout.menu.
getView() returns the View you inflated inside onCreateView. You use it only after onCreateView has been executed

Android changing the background color for preference fragment

I have set a theme for my application, which includes a blue background...
However, this made my preference fragment blue too. I want to keep the original "android:Theme.Holo.Light.DarkActionBar".
How to do so? thanks!
Add the following code to your PreferenceFragment implementation:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.your_color));
return view;
}
you can use a style file or in your PreferenceActivity add this
getListView().setBackgroundColor(Color.rgb(255, 0, 0)); // or whatever color value you want

How to display text in a TextView inside a Fragment

Seems like a simple question to me, but I can't figure it out. I have this code inside my Fragment:
myTextView.setText("Test");
However, this code brakes my app and I am unsure why. Perhaps this is a conflict with Views, but I still don't understand why it wont work. Any suggestions?
How did you set the content?
Edited my answer :) try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xmlName,
container, false);
TextView tv= (TextView) **view**.findViewById(R.id.textViewName);
tv.setText("yourText");
return view;
}
here is a tutorial for fragments : fragments tutorial

Categories

Resources