I wish to change my app:footercolor value defined on xml file, and I want to do this while runtime.
How can I achieve this? I haven't found any information about how to do this. I'm find very hard to learn something just about changing xml through java. I'm certain that there must be a way to make that happen and I just can't figure it out.
Here is the xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.viewpagerindicator.TitlePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="40dp"
android:layout_width="fill_parent"
android:background="#000000"
android:textColor="#1573D6"
app:footerColor="#1573D6"
app:footerLineHeight="2dp"
app:footerIndicatorStyle="none"
app:selectedColor="#FFFFFF"
app:selectedBold="true" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
Thank you
The XML definitions will get turned into Java Objects when the activity is launched. So what you need to do is to get a reference to those Java objects and change them - instead of trying to modify the XML.
I'm not very familiar with Android, but I think the following will work:
TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
int blue = 0xFF;
titleIndicator.setFooterColor(blue);
titleIndicator.invalidate(); // get the UI to repaint this View at some point
Let me know how it goes!
Related
In my XML layout I have some TextView with ids like slot0, slot1...slot15.
Is there any way to generate the corresponding Id dynamically in java like the following?
findViewById(R.id.*customStringForId*)
then access each of TextView using a for loop?
I am currently unable to use findViewById(R.id.*customStringForId*) because I can't find it in the XML.
Thats a bad practice for access component from your xml
You need set manual for id with findViewById for tell java class if in your xml there existing textview with id which already you set and give you access for do whatever like implement onclick event, settext, etc.
If you cant find your id, you need check if setContentView in your java point to your xml.
there are some ways to solve your problem but you should write your layout in the question to let us know how you design the layout.
But for example if you have a list of TextViews inside layout like the following:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/slot0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
</LinearLayout>
you can access the TextView Dynamically via the layout like the following:
public TextView getTextView(int index){
return ((LinearLayout) findViewById(R.id.layout)).getChildAt(index)
}
I'm trying to add a "You are offline" element to the bottom of each layout in my Android app. I would like to define it globally, not to paste the same element to each xml layout file.
I could probably create some ParentActivity and append it programmatically, but is it a good solution?
What is the best way?
Thanks.
Use custom BottomSheetDialog. You do not have to inclue it in layout file. Instead, you will call it programmatically.
BottomMessageDialog:
public class BottomMessageDialog extends BottomSheetDialog {
public BottomMessageDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.dialog_bottom_message);
}
}
dialog_bottom_message.xml:
<?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"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="You are offline"
android:textSize="18sp"
android:gravity="center"/>
</LinearLayout>
Call it:
BottomMessageDialog bottomMessageDialog = new BottomMessageDialog(MainActivity.this);
bottomMessageDialog.show();
Hope it will help.
best way is creating a custom xml file with any name you want and you can use it any number of times you want without any copy and paste.
step 1: creating custom layout named footer_message .
<TextView
android:layout_width="match_content"
android:layout_height="wrap_content"
android:text="You are Offline"/>
step 2: adding that custom layout in another xml which you want that msg.
<include
android:id="#+id/footer_message"
layout="#layout/footer_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
create an xml file
footer.xml
make your layout in it and write this code in the xml files where you want that footer
<include
android:layout_height="wrap_content"
android:layout_width="fill_parent"
layout="#layout/footer"
android:id="#+id/footer"/>
There is the DismissOverlayView, which you can implement to give the user an alternative way to exit the application on an android watch. This View implements something, that looks like a fullscreen FAB. Now I would like to know, how I could implement the same View with another button icon/color/behaviour. Since you can't change the DismissOverlayView which code looks btw. like this :
<android.support.wearable.view.DismissOverlayView
android:id="#+id/dismiss_overlay"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
I guess you have to implement some custom FAB, but I can't use the FAB in my watch XML either because there is an dependency missing or because it's simply not supported by the watch os. I tried following code for testing :
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"/>
Edit: For better understanding, I want the button look like this :
Edit: I just found this solution, I was not aware of that one :
https://developer.android.com/training/wearables/ui/confirm.html
But I would still be curious, if you could implement a more customisable version of this buttons.
Okay I found a solution for that problem, actually you can simply create a new layout.xml and overwrite the default code, which is the following one :
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/dismiss_overlay_explain"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="32dp"
android:layout_width="wrap_content"
android:padding="12dp"
style="#style/DismissOverlayText"
/>
<android.support.wearable.view.ActionPage
android:id="#+id/dismiss_overlay_button"
android:layout_height="match_parent"
android:background="#color/dismiss_overlay_bg"
android:layout_width="match_parent"
android:src="#drawable/ic_full_cancel"
android:color="#color/dismiss_close"
app:buttonRippleColor="#color/dismiss_close_pressed"
android:text="#string/dismiss_overlay_button_label"
/>
</merge>
Now you can use your newly created xml customise it and use this one instead of the original dimissOverlayView. But you should be careful with doing so, because this is not really intended from the design guidelines to do.
I'm working through the android tutorial at
http://developer.android.com/training/basics/firstapp/building-ui.html
I've created the project as stated in the tutorial however the code that it says is supposed to be in my activity.xml file is instead in my content.xml file its as if they have swapped?
Code in activity xml:
<?xml version="1.0" encoding="utf-8"?>
<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_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MyActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_my" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Code in content xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
The tutorial is telling me to change the relative layout block to linear layout (which it says should be in the activity xml, but for me its in content xml)
So when I replace the code in activity xml with what the tutorial has it gives me errors because I removed the code that was there previously.
OR
if I edit the code in content xml and run the app I can't see the changes because activity is not updated, and I cant move the linear layout code to activity xml because it gives me "multiple root tags" error.
Can someone show me a solution that will allow me to continue with the tutorial, because if I edit the java code or something like that I get problems with later stages in the tutorial.
Thanks
Instead of working on the activity.xml try editing the content.xml to reflect the LinearLayout change (do add the orientation value as shown).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Do also go through on the Android Getting Started Resources. They will come in handy
You just have to keep in mind that these .xml files represent the layout part, the UI.
To put it simply, a 'screen' in Android is composed of 2 files : the layout (the .xml file), and its associated .java file.
Here, we can see that your "content" is included in your "activity".
It's quite the same thing as putting directly the content of "content.xml" into "activity.xml".
What is in the "OnCreate()" method of you MyActivity.java ?
Have you tried replacing the "include" in "activity.xml" by the actual content of "content.xml", not forgetting to remove "root specific" values (xmlns:android, xmlns:tools, xmlns:app and tools:context)?
As fernaMuruthi said, try following the Android Getting Started Resources. Also, try not to use the graphic designer : it sometimes generate crappy code. instead, Type you UI pseudo-xml directly.
I was having the same issue. Never really worked with XML, and my Java is rusty. I ended up searching online a bunch and this is what I found out.
The .xml files are used to determine your layouts for your activity. They can be used in conjunction with one another or separately. This means using content.xml and main_activity.xml don't really matter. However because the tutorial is not very clear about this, it makes it hard for newcomers to figure out a work around.
The solution is to understand what the MainActivity.java file is doing with your .xml files. Mike James at i-programmer explains this fairly well...(http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html)
There is a method called onCreate() that controls which layout is being used by MainActivity.java. Here you will see the auto generated function setContentView() this is the main issue with determining what is being displayed in the app.
There is also a bunch of other generated code that is used to get more data from main_activity.xml. If you look at the code you will see what needs to be removed.
Hope this helps. Please up vote if it did :)
So, if you follow the tutorial to a tee, i.e. make your activity_my.xml file the exact same as what they are expecting, then you will see the edit text view and the button view as depicted.
It seems that quite a bit has been added to the blank activity template since the creation of this tutorial, making the tutorial quite obscure.
After looking around for a while and trying multiple solutions I finally realized that the toolbar created in activity_my.xml was obscuring the text/button created in content_my.xml.
My solution was to just pad the linear layout.
android:paddingTop="55dp"
But then again I'm on day #2 in the Android/Java world...
I am a novice learning Android development and the next step I must take is to be capable to understanding threads to be able to implement a game loop.
I have taken knowledge from google and StackOverflow and snippets from examples to come up with a lump of code that seems to not be working:
MainActivity.java
GameSetup.java
GameView.java
activity_main.xml
game_setup.xml
MainActivity.java is a simply title screen with a play button. This seems to work fine and will link to the next activity. However, I am getting a crash whenever I attempt to load up the next activity. My guestimations based on the error lead me to believe that threads have issues with RelativeActivity, but I'm not so sure on what the issue may be exactly, I am rather confused.
Here is the error I am getting thrown, I am having issues trying to decipher the issue with it.
Thanks.
R.id.layout is a RelativeLayout but you are trying to cast it to GameView, that's causing your crash.
Edit:
To add your GameView to the RelativeLayout you can do
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.your.package.GameView
android:id="#+id/my_game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
</RelativeLayout>
and retrive it with
GameView myGameView = (GameView) findViewById(R.id.my_game_view);
Hmm, from the code you have written so far, I can explain the reason for the ClassCastException.
You are declaring a RelativeLayout in both XML files.
Both RelativeLayouts use the same android:id "#+id/layout"
In GameView.java you try to find a view by specifying the id
m_game = (GameView) findViewById(R.id.layout);
IMO you are retrieving one of the RelativeLayouts and then try to cast it to a GameView. As this is not possible, Java throws the exception.