How to hide Title Bar of a pop-up activity - java

I created an activity which is shown as pop up/ Dialog by following answers given in this question.
I did it by adding these lines in AndroidManifest
<activity
android:name=".package.Activity"
android:theme="#style/Theme.AppCompat.Light.Dialog.Alert" />
It works well as intended but the problem it shows Appname as title. I want to make it appear as AlertDialog. How to fix this?
I also cant use setTitle("") or label="" because empty space is left where appname was before.

Add this code to the top of the onCreate() method of the Activity:
setTheme(R.style.{style}
The {style} should be ended with NoActionBar. i.e. R.style.Theme_AppCompat_NoActionBar
And most importantly, add the code before these two lines of code (add it to the top of the method):
super.onCreate(savedInstanceState);
setContentView(R.layout.{your layout});
Hope this answer helped!

You can simply call the setTitle("") function from within your Activity.

Related

To remove the back arrow button from Action Bar

In my Android Project, whenever I go from First Activity to Second, I get an left arrow button followed by the some text in the action bar. That left arrow only comes when I set my First Activity as the hierarchical parent of the Second Activity.
I want to remove that back button and replace it with an icon. I have read other posts for the same question, and so I have already tried the following code in my onCreate() method.
getActionBar().setDisplayHomeAsUpEnabled(false);
But after reaching to the second activity, the app gets crashed.
Please help me out.
Use getSupportActionBar() bar:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);

Android - One button changing to next screen created. How to do next one?

I created very simple button directing to new screen from this tutorial:
www.mkyong.com/android/android-activity-from-one-screen-to-another-screen
and it works fine.
The question is how to make a button returning from main2.xml to main.xml (and every next one button). I am not very good at it yet and logically i tried to create button with #id:button2 and a class App3Activity.java the same as App2Activity.java but changed button1 to button2 and main to main2. However, it crashed. What is wrong ?
Crash log: http://pastebin.com/bmcVMmJ0
Code: http://pastebin.com/8p6GwG3u
there are many reason regarding your scenario
Activity not defined in Manifest file
Calling button in activity may not getting its view to trigger its functionality
using 2 different XML in same Activity
Intent not instantiated properly
Context Switching is not proper
there are many reasons behind your its an open ended Question unless you provide stacktace (Logcat) or code that you are working with
App3Activity is not declared in manifest file.
AppActivity is starting App2Activity, but App2Activity doesn't initialize button and click listener.

How to stop soft keyboard appear by default in android

I am writing an android application with two EditText on the main activity. My problem is that once the main activity starts the soft keyboad automatically appears. I want the soft keyboard to appear only when the user clicks on the EditText component. How can I accomplish this?
In your activity, in your onCreate method, put the following
edittext.setFocused(false);
And then afterwards (in a different method, maybe an onClickListener for your edittext) put
edittext.setFocused(true);
Also you could put the below line in you manifest, in the the activity section, just below the android:name attribute
android:windowSoftInputMode="stateUnchanged"
Try this,
<EditText android:id="#+id/edit_text_id"
..
android:focusable="false" />
if you want to manage the inpt events , take a look on this part :
==> http://developer.android.com/guide/topics/ui/ui-events.html

Changing the Action bar icon

I'm currently implementing theme support for my application and a part of it is changing the action bar app icon. I want to use a dark icon when Holo Light is selected. Everything is done in the method except for the part where the action bar app icon is set. The code that I'm trying to use is:
getActionBar();
ActionBar.setIcon(R.drawable.my_icon);
"There is no such reference available here" is the error that I'm getting. How should this be done correctly?
BTW my minSdkVersion is 14 so no action bar Sherlock stuff.
getActionBar();
You're throwing the action bar away right there. getActionBar() returns an instance of ActionBar, which you then need to call setIcon() on. Like so:
ActionBar actionBar = getActionBar();
actionBar.setIcon(R.drawable.my_icon);
Though its a bit late answer but i thought it might be useful.
From inside an activity:
For API level 14 or higher:
getActionBar().setIcon(R.drawable.my_icon);
For lower API level we have to extend ActionBarActivity and then:
getSupportActionBar().setIcon(R.drawable.my_icon);
From inside a Fragment:
For API level 14 or higher:
getActivity().getActionBar().setIcon(R.drawable.my_icon);
For lower API level we can use (activity must extend ActionBarActivity):
((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);
And in both cases we have to call setDisplayShowHomeEnabled(true) before setting the icon or logo.
((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);
I am using this for my use , and it's working for me. Hope this help all
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.icon);
You need to add the drawable that you want to reference into your drawable/ folder under res/.
edit: In your Android installation folder there are a lot of stock images to use. You can probably find it there.
The existing answer is very correct. There is, however, also an alternative.
A minimal approach would be to use
getActionBar().setIcon(R.drawable.my_icon);
Gets your job done right away. :)
Technical Details: Since getActionBar() by default returns an object, you can directly manipulate it without having to receive it in an in-scope object explicitly.
Calling to setIcon wasn't enough for me.
Before that, I had to switch the display from activity logo to activity icon:
actionBar.setDisplayUseLogoEnabled(false);
For the differences between activity icon and logo see Android icon vs logo.
Try this
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setIcon(R.drawable.your_icon);
Kotlin answer:
In your activity just paste this code.
You need to enable the option first:
supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setIcon(R.drawable.your_icon)

Android ListView - onListItemClick does not work properly

I created a ListView in Android, and a corresponding ListActivity. Each individual item in the ListView has just one TextView (I plan to add an image and a CheckBox later).The ListActivity overrides the onListItemClick to perform certain tasks on click of any item on the list.
Heres whats happening -
When I first tried clicking on any item, nothing happened.
I then tried setting the properties "Focusable" and "Focusable in Touch Mode" to false for the TextView, as mentioned here, here and here. The List items started recognizing clicks, but only when I clicked somewhere away from the TextView. Whenever I tried clicking on the TextView or anywhere near it, it did not work.
I also tried changing various attributes like Clickable, but nothing has worked so far.
Any idea what I could be doing wrong ?
Thanks
After playing around with virtually every attribute in my TextView, I finally found the reason why it was not working. It was because of the attribute android:inputType="text" in my TextView. I'm not sure why I added that piece of code (I probably copied the TextView from one of my other applications), but removing it solves my problem.
Class which will listen clicks on ListView should implement interface AdapterView.OnItemClickListener

Categories