In this Android Navigation Icon - Profile picture instead of hamburger icon stackoverfolw post
ImageView profileImage = (ImageView)
binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);
How an Where to define R.id.iv_profile_image in which xml file ?
I couldn't find the XML code in the example you linked. However, I noticed the following line in the Activity's onCreate() method. This would indicate that the XML file being used is called activity_maps.xml. Most likely this is where the ImageView was placed.
binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
Assuming this assumption is correct, the ImageView can be declared something like this. The answer to "where" within the XML depends as XML is a declarative language.
<ImageView android:id="#+id/iv_profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
...
android:contentDescription="#string/to_replace" />
Related
How would one change the android:src XML property programatically of a FloatingActionButton or Button widget? I have a button defined as following within an activity:
FloatingActionButton floatingActionButton = (FloatingActionButton) this.findViewById(R.id.start_button);
It has an initial android:src set as follows:
android:src="#android:drawable/ic_media_play"
I'm trying to utilize the setImageResource(int id) to change the android:src XML property, but how to I access a Drawable icon such as ic_media_pause after a button click that occurs for example. When I try to pass in R.drawable.ic_media_pause to the setImageResource() method I received an cannot resolve symbol error.
floatingActionButton.setImageResource(R.drawable.ic_media_pause);
How do I get at the available R.drawable resources to pass it in as an argument.
Any help would be great appreciated. Thank you!
You're trying to access a default icon included in the SDK. You need to prefix your resource identifier with "android." For example:
floatingActionButton.setImageResource(android.R.drawable.ic_media_pause);
this will allow you to reference the default icons.
:)
I am trying to set a custom view in an ActionBar. I am using the support.v7 ActionBar - min API is 9. For some odd reason, I cannot get the custom view to appear in the ActionBar. Am I missing something painfully obvious? Any help appreciated.
Here is the layout I am inflating:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flsfjkelsjek"
android:background="#color/black"
android:textColor="#color/white"
/>
...Very simple, just a TextView for testing.
Here is the code to set this view as my custom ActionBar view:
TextView customView = (TextView) myInflater.inflate(R.layout.my_test_textview, null);
myActionBar.setDisplayShowTitleEnabled(false);
myActionBar.setDisplayHomeAsUpEnabled(true);
myActionBar.setDisplayShowCustomEnabled(true);
myActionBar.setCustomView(customView, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Again, very simple and straightforward. I have tried many different varations on this, such as:
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_SHOW_TITLE);
and have even tried that line along with all of the above. I've also tried with a TextView just created on the fly, rather than inflating one.
The custom view simply doesn't appear. When I check the value of
myActionBar.getCustomView()
it returns my TextView. However, the width of that TextView is 0. Does anyone have any idea why the CustomView is not showing up?
I'm completely new to Android application programming, and I was reading through Google's tutorial: http://developer.android.com/training/basics/firstapp/starting-activity.html.
On this page, under the "Display the Message" section, they create TextView object, and use setContentView with the textView object as the argument, to display some text. I was wondering, if I'm understanding correctly, instead of creating the TextView object within the code, can you define it in XML instead? If you define it in XML, would that require you to create a new XML file besides main_activity.xml? Thanks.
You can declare all your layouts and views inside a xml. For the given example, the code would look like the following
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set your parent view
setContentView(R.layout.main_layout);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Get the reference to the TextView and update it's content
TextView textView = (TextView)findViewById(R.id.my_text_view);
textView.setText(message);
}
And your main_layout.xml would look like
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:id="#+id/my_text_view"/>
You can arbitrarily create view files (XML files) and specify the primary type of the view and the children it contains. You could create the TextView element within the main_activity.xml view and find it by the relative Id.
That being said in the article in question if you want to have a separate view for just the TextView element then you would likely need another XML file to define that view if you do not want to specify it programmatically.
In a standard application you will likely have a predefined view (XML file) that you will set as the content view and reference elements from within it (as well as possibly add new elements).
It is very flexible, in short to answer your question no, you do not need to generate a new XML view file, you could simply add a TextView to an existing view file or specify it at runtime.
I am creating an application in android. And when I try running the app after inserting an ImageView control into my app, I am getting the following error in logCat:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bdisys.android.wsaccess/com.bdisys.boinfoportal.ui.FirstAppUI}:
java.lang.ClassCastException: android.widget.TextView
The following came like a warning after I had inserted an ImageView into my layout/main.xml.
The following classes could not be found:
- ImageView (Change to android.widget.ImageView, Fix Build Path, Edit XML)
Following the xml code portion after I had inserted the ImageView.
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/favicon" />
</TableRow>
Can anyone tell why this error comes?
Am not getting any idea about this. Please help. Thanks in advance.
You are triying to cast a TextView into ImageView. Check your resource xml.
I had the same problem with the ImageView, the message The following classes... was showing up in the design tab of my main.xml
In my case the problem was that the drawable had no extension, however it appeared in #drawable/ section and was appearing in the autocomplete too as if nothing was wrong.
Cannot provide any info on the classcast exception though.
To sum up: Make sure your favicon file has the correct extension
if you have code like this
ImageView img;
img = (ImageView) findViewById(R.id.TextView1);
change to
TextView txt;
txt = (TextView) findViewById(R.id.TextView1);
Can you overlay a view on top of everything in android?
In iPhone I would get the new view set its frame.origin to (0,0) and its width and height to the width and height of self.view. Adding it to self.view would then cause it to act as an overlay, covering the content behind (or if it had a transparent background then showing the view behind).
Is there a similar technique in android? I realise that the views are slightly different (there are three types (or more...) relativelayout, linearlayout and framelayout) but is there any way to just overlay a view on top of everything indiscriminately?
Simply use RelativeLayout or FrameLayout. The last child view will overlay everything else.
Android supports a pattern which Cocoa Touch SDK doesn't: Layout management.
Layout for iPhone means to position everything absolute (besides some strech factors). Layout in android means that children will be placed in relation to eachother.
Example (second EditText will completely cover the first one):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/root_view">
<EditText
android:layout_width="fill_parent"
android:id="#+id/editText1"
android:layout_height="fill_parent">
</EditText>
<EditText
android:layout_width="fill_parent"
android:id="#+id/editText2"
android:layout_height="fill_parent">
<requestFocus></requestFocus>
</EditText>
</FrameLayout>
FrameLayout is some kind of view stack. Made for special cases.
RelativeLayout is pretty powerful. You can define rules like View A has to align parent layout bottom, View B has to align A bottom to top, etc
Update based on comment
Usually you set the content with setContentView(R.layout.your_layout) in onCreate (it will inflate the layout for you). You can do that manually and call setContentView(inflatedView), there's no difference.
The view itself might be a single view (like TextView) or a complex layout hierarchy (nested layouts, since all layouts are views themselves).
After calling setContentView your activity knows what its content looks like and you can use (FrameLayout) findViewById(R.id.root_view) to retrieve any view int this hierarchy (General pattern (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view)).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id = "#+id/Everything"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- other actual layout stuff here EVERYTHING HERE -->
</LinearLayout>
<LinearLayout
android:id="#+id/overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</LinearLayout>
Now any view you add under LinearLayout with android:id = "#+id/overlay" will appear as overlay with gravity = right on Linear Layout with android:id="#+id/Everything"
You can use bringToFront:
View view=findViewById(R.id.btnStartGame);
view.bringToFront();
The best way is ViewOverlay , You can add any drawable as overlay to any view as its overlay since Android JellyBeanMR2(Api 18).
Add mMyDrawable to mMyView as its overlay:
mMyDrawable.setBounds(0, 0, mMyView.getMeasuredWidth(), mMyView.getMeasuredHeight())
mMyView.getOverlay().add(mMyDrawable)
I have just made a solution for it. I made a library for this to do that in a reusable way that's why you don't need to recode in your XML. Here is documentation on how to use it in Java and Kotlin. First, initialize it from an activity from where you want to show the overlay-
AppWaterMarkBuilder.doConfigure()
.setAppCompatActivity(MainActivity.this)
.setWatermarkProperty(R.layout.layout_water_mark)
.showWatermarkAfterConfig();
Then you can hide and show it from anywhere in your app -
/* For hiding the watermark*/
AppWaterMarkBuilder.hideWatermark()
/* For showing the watermark*/
AppWaterMarkBuilder.showWatermark()
Gif preview -
I have tried the awnsers before but this did not work.
Now I jsut used a LinearLayout instead of a TextureView, now it is working without any problem. Hope it helps some others who have the same problem. :)
view = (LinearLayout) findViewById(R.id.view); //this is initialized in the constructor
openWindowOnButtonClick();
public void openWindowOnButtonClick()
{
view.setAlpha((float)0.5);
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// check if the Overlay should be visible. If this value is false, it is not shown -> show it.
if(view.getVisibility() == View.INVISIBLE)
{
view.setVisibility(View.VISIBLE);
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
Log.d("Overlay", "Klick");
}
else if(view.getVisibility() == View.VISIBLE)
{
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
bringToFront() is super easy for programmatic adjustments, as stated above. I had some trouble getting that to work with button z order because of stateListAnimator. If you end up needing to programmatically adjust view overlays, and those views happen to be buttons, make sure to set stateListAnimator to null in your xml layout file. stateListAnimator is android's under-the-hood process to adjust translationZ of buttons when they are clicked, so the button that is clicked ends up visible on top. This is not always what you want... for full Z order control, do this: