I'm having troubles trying to style my tabs in android.
I want to make them look exactly the same as whats in the open source android contacts list (see https://android.googlesource.com/platform/packages/apps/Contacts
).
Problem is that when they display on the screen it looks a bit different to the contacts app.
When it should look like this:
Notice how the background colors are a little bit different and the text colors are different.
Not sure why this is the case as its basically the same code and icons.
My tab layout code is the following:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp">
<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"
android:padding="0dp" />
</LinearLayout>
</TabHost>
Which doesn't contain anything special there.. and the TabActivity is as follows:
public class TabbedActivity extends TabActivity implements
TabHost.OnTabChangeListener {
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tab);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
setupLatestTab();
setupSavedTab();
tabHost.setCurrentTab(0);
}
private void setupLatestTab() {
Intent intent = new Intent().setClass(this, ResultsActivity.class);
tabHost.addTab(tabHost
.newTabSpec("latest")
.setIndicator("Latest",
getResources().getDrawable(R.drawable.ic_tab_recent))
.setContent(intent));
}
private void setupSavedTab() {
Intent intent = new Intent().setClass(this, ResultsActivity.class);
tabHost.addTab(tabHost
.newTabSpec("saved")
.setIndicator("Saved",
getResources().getDrawable(R.drawable.ic_tab_starred))
.setContent(intent));
}
#Override
public void onTabChanged(String tabId) {
// Because we're using Activities as our tab children, we trigger
// onWindowFocusChanged() to let them know when they're active. This may
// seem to duplicate the purpose of onResume(), but it's needed because
// onResume() can't reliably check if a keyguard is active.
Activity activity = getLocalActivityManager().getActivity(tabId);
if (activity != null) {
activity.onWindowFocusChanged(true);
}
}
}
I am using the same images from the drawable folders too.
I know i can set the background of tabs manually by doing something like this in the tabactivity
tabHost.getTabWidget().getChildAt(index).setBackgroundColor(Color.parseColor("#ff202020"));
But the contacts app isn't doing this sort of thing anywhere (most of the top tab code is in DialtactsActivity), so just want to do what the open source app is doing when displaying tabs - i'm not sure how and why the contacts application tabs look much better when im basically using the same code and resources.
I guess im just missing something trivial??
This turned out to be a problem with my minimum android version not being specified ..
Added:
<uses-sdk android:minSdkVersion="8" />
to the androidmanifest and it worked fine. I guess it was reverting to the old tabs look and feel in earlier versions of android.
Related
I have a fragment (fragment_1) which gets displayed when the main activity starts and displays a full screen layout (layout_1). Now i have added a button, clicking on which will start a new fragment (fragment_2)and replace the entire layout (layout_1) with my new layout (layout_2).
My first fragment (fragment_1) has a ScrollView as my root view.
<ScrollView
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"
android:background="#fff"
android:scrollbarSize="0dp"
android:id="#+id/scroll_main"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
....
</RelativeLayout>
</ScrollView>
Now i want to start a new fragment (fragment_2) on a button click and display another layout (layout_2) but i don't know how to do that.
My main activity had a frame layout like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root_container">
And i used to set this as my default layout using
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
After this i start my first fragment and display the layout_1.
Now from fragment_1 i want to start fragment_2 and display the layout_2. I have read online articles and documentation where i can't find and solution regarding this. Pleas Note :" I don't want to simply replace a part of my layout_1 and replace it with my layout_2 rather i want my layout_1 totally gets replaced with layout_2 on a button click"
Replacing fragments seems like a job for the fragment manager.
This answer might help you Replacing a fragment with another fragment inside activity group
I am trying to add an editText in between two already existing editTexts programmatically on the click of a button in android. I am just wondering if this is possible, as i have been unable to find any related questions?
What do you want to acomplish with runtime element adding? Whats the purpose? Maybe isn't enough to show/hide the element on specific actions?
I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml:
android:visibility="gone"
or in java code in the onCreate() method:
specificElement.setVisibility(View.GONE)
Then when you normally would add the element you rather just set the visibility to visible:
specificElement.setVisibility(View.VISIBLE)
What about that?
you can add 3rd EditText on 2nd position.
For that first you should have reference of the parent layout nad then do like this.
if you have done :
parent.addView(editText1);
parent.addView(editText2);
So now your parent have two child views.
now to add 3rd EditText i.e. editText3 then do this like:
parent.addView(editText3, 1);// addView(<childview>, <index>);
Like this your 3rd EditText will be in 2nd position.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);
yourEditBox.setLayoutParams(params);
Use above code to add and align you're editbox between another two editboxes.
Inside Activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(linearLayout);
EditText editText = new EditText(getBaseContext());
editText.setHint("Programmatically Added EditText");
linearLayout.addView(editText, 1);
}}
Layout file structure
<?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:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2" />
</LinearLayout>
Hope this helps. Cheers!
You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button.
In xml file :Set visibility to gone or invisible depending on what you actually want :
android:visibility="invisible" it won't be visible but it's going to take place in your view
android:visibility="gone" it won't be visible and it's not taking place in your view
In your code :
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setVisibility(View.VISIBLE);
});
I have put an image from my drawable folder and added it onto my action bar and would now like to move it left over beside the back arrow. Anyone any ideas on how to do this in Android Studio. Thanks
My Code is as fallows
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.mainlogo);
Try using a Toolbar, the new way to make AppBars. It is more easily customized and reused, among other advantages.
Android's docs make it seem a lot harder than it really is. They use Toolbar but connect it to the AppBar API's still. If you use a lot of ActionBar utility methods like ActionBar.hide(), then it makes sense to call
setSupportActionBar(myToolbar);
But it can actually be significantly easier.
Why you don't need to setSupportActionBar
According to the article above, if we don't use the AppBar API's we don't need the custom theme, onCreateOptionsMenu(), or any of the AppBar mess.
Here is how to do Toolbars simply:
Get v7 appcompat
I think it is neccessary still to make your Activity extend AppCompatActivity
Add the following code:
At to the top of your layout resource file, inside the top level Layout of course:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In your activity:
protected void onCreate(Bundle savedInstanceState) {
...
//Get the toolbar
Toolbar toolbar = ((Toolbar)findViewById(R.id.my_toolbar));
//Set up the toolbar's navigation icon and behavior
toolbar.setNavigationIcon(R.drawable.ic_close_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//whatever you want to happen on click
}
});
}
Optionally you can do things like:
toolbar.inflateMenu(R.menu.save_menu);
toolbar.setTitle("Hey");
Heres where the custom behavior you wanted comes in. If you want an ImageView in the toolbar, you simply add it to the layout resource:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_logo"/>
</android.support.v7.widget.Toolbar>
In fact, if you want a title with custom styles, instead of banging your head against the wall trying to figure out Toolbar styling, simply add a TextView where the ImageView is. You can modify that however you want, and make it your title!
Disclaimer: It's build with CLI only tools, so certainly some "auto-generated lines" are missing
It's a small application with a MainActivity with a EditText and a Button
It works fine, but if I extends now from ActionBarActivity now my EditText and Button are hidden behind the Action Bar
https://developer.android.com/guide/topics/ui/actionbar.html The android documentation does not state about changes needing to be made in the layout.xml nor does the Training guide from which my application is based on
My MainActivity.java
public class MainActivity extends ActionBarActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// adding these lines show that the widget are correctly added
//ActionBar actionBar = getSupportActionBar();
//actionBar.hide();
}
}
My main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="#+id/enter_verb"
android:hint="#string/enter_verb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="#string/button_conjugate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
My guess would be that I need to add something in my layout to make it aware there's a Action Bar, but I can't find anywhere what it should be.
Very strange, I've added the line coming from this link
https://developer.android.com/training/basics/actionbar/overlaying.html
Which is:
android:paddingTop="?attr/actionBarSize"
in my LinearLayout tag, and then recompiled, after that the widget were shown but with a padding of the size of the actionBar in addition to the actionBar itself.
I then removed the line, recompiled and it now works as expected
A bug because of temporary files?
I've been racking my brain for the past two days trying to get a button to work.
Basically I want my button to call 'add a password function'.
If you like to view my entire code, its at github.com/servingbaby/UPM-Epassafe .
Here is my main.xml (The button shows up succesfully however when pushing it nothing happens)
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:gravity="center"
android:text="#string/no_accounts" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center"
android:text="#string/add_account" android:id="#+id/add" android:minWidth="125dip"></Button>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Here is my AndroidManifest slimmed down to the parts I believe are where I am going wrong.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.epassafe.upm"
android:versionCode="3"
android:versionName="1.2">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="16"/>
<application android:icon="#drawable/upm"
....>
<activity android:name=".AppEntryActivity">
</activity>
<activity android:name=".FullAccountList">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResults" />
</activity>
<activity android:name=".AddEditAccount">
</activity>
</application>
</manifest>
Here is my MainActivity.Java, where I believe I should be calling the correct code.
public abstract class MainActivity extends Activity implements OnClickListener{`
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button countButton = (Button) findViewById(R.id.add);
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
MainActivity.this.startActivity(intent);
}
});
}
}
But pushing the button nothing happens like I expect. No errors or force close, so Im really at a loss at the moment. Thankyou!
**Edit
Thank you all how attempt to help me. I have tried many of the suggested solutions and still haven't figured it out just yet. I have uploaded my files to github with some of the attempted edits I have made as commented. Basically I am just trying to figure out how to add a button and get it to run the necessary code to add an account. (The original code works well, but I would like to improve upon it. Sorry my competence level is still low, but trying to learn my best :) I would really appreciate if anyone could help me with this! Thank you^^
**Edit 2
Again Thankyou all for the advice and suggestions. I figured out a simple solution. I ended up using a Menu Wrapper to create a button click event, and also it turned out I was editing the wrong Java document, when I was suppose to be trying to add the correct code to another one that seem to almost be doing the same thing. Learn something new everyday.
I don't understand why you're implementing OnClickListener to your activity. Remove the implements OnClickListener from your activity. It should work fine.
But since you're doing this for don't-know-what reason, you should make your button clickable like this.
countButton.setOnClickListener(this);
public void onClick(View view)
{
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
MainActivity.this.startActivity(intent);
}
First of all you dont need to implement OnClickListener to set a a listner in your button. But maybe you have it for another reason, if not, remove it.
You are declaring wrong the names of the id of your xml i think, take a look at this:
Difference between "#id/" and "#+id/" in Android
May be because of the #android:id instead of #+id.
use this way call Intent
Intent intent = new Intent(MainActivity.this, AddEditAccount.class);
startActivity(intent);
look at your list view
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
you have given width and height as fill parent so your ListView is on your button so on the screen when you click on the button you are not actually clicking on the button. when you click a button the button color changes for a sec that is not happening in your case see it for your self. so remove the listview or set the height below the button and then check your button click will work