I have been experimenting the following issue: I have created a custom action bar:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/com_facebook_blue" >
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#ffffff"
android:text="#string/app_title"/>
</RelativeLayout>
Which is inflated by a GraphicsBuilder class in the following way:
public static void setupActionBar(String title, Activity a) {
final ViewGroup actionBarLayout = (ViewGroup) a.getLayoutInflater()
.inflate(R.layout.action_bar_layout, null);
final ActionBar actionBar = a.getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
final int actionBarColor = a.getResources().getColor(R.color.main_red);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
TextView tv = (TextView) actionBarLayout.findViewById(R.id.action_bar_title);
Typeface font = Typeface.createFromAsset(a.getAssets(), "fonts/Molle-Regular.ttf");
tv.setText(title);
tv.setTypeface(font);
}
Which leads to the following results (I have kept the title bar background blue to highlight the issue):
Basically it looks like that the custom action bar does not fill the parent width, therefore any try to align the title at the center is useless. How can I fix this?
I know this is a long time gone.
Solved it by changing the layout params once it's been set as custom view.
My logic was that when you inflate it the first time, it assumes the size of the original container.
When you call:
actionBar.setCustomView(titlebar);
It's already got its dimensions preset.
My solution was:
View titlebar = inflater.inflate(R.layout.titlebar, null);
// Do stuff to Title Bar //
ActionBar actionBar = getActionBar();
actionBar.setCustomView(titlebar);
View v = actionBar.getCustomView();
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
v.setLayoutParams(lp);
Alternatively, you can first set the action bar layout without the inflater:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.id.titlebar);
View v = actionBar.getCustomView();
// Do something to the view E.g Set button listeners blah //
// Rest of code
Related
How can I achieve a Tab/Viewpager style like this ?
So its not looking like the normal Sliding Tab Layout like here.
Are there some guides to follow out there ? It looks very custom to me
First define the slidingTabLayout with your view pager as:
<com.forthcode.androidoze.Utils.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"/>
<android.support.v4.view.ViewPager
android:id="#+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
In above code I have taken the sliding tab color as transparent, you can take any as per your need.
Now define a custom view thatrepresent your single tab.
customtab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="10dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
Now create a selector.xml inside the drawable folder that defines the text color of the tab when it will be selected and not selected.
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#ccc" />
<item android:state_focused="true" android:color="#ccc" />
<item android:state_pressed="true" android:color="#ccc" />
<item android:color="#fff" />
</selector>
In the last line of selector.xml is the default tab text color when the tab is not selected.
Now finally in your void populateTabStrip() method of SlidingTabLayout class just add the code to implement the selector with the tab as shown below:
tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector));
Remember this line should be inside the populateTabStrip() inside the for loop, just to make it easy i'm writing the complete method populateTabStrip(){} of slidingTabLayout, so your populateTabStrip() should look like this:
private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null;
if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
}
if (mDistributeEvenly) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
lp.width = 0;
lp.weight = 1;
}
tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener);
tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector));
String desc = mContentDescriptions.get(i, null);
if (desc != null) {
tabView.setContentDescription(desc);
}
mTabStrip.addView(tabView);
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
}
}
}
Now in the Activity or Fragment where you are using the tab define the viewpager,slidingtablayout and the viewpager adapter, and set the adapter with view pager and set the slidingtab with view pager as shown below(Note i have used inside fragment, if you are using Activity then modify as per your need):
SlidingTabLayout mSlidingTabLayout;
ViewPager mViewPager;
PagerAdapter mPagerAdapter;
SharedPreferences mySharedpref;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_home, container, false);
mPagerAdapter=new PagerAdapter(getChildFragmentManager());
mViewPager= (ViewPager) view.findViewById(R.id.vpPager);
mSlidingTabLayout= (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mViewPager.setAdapter(mPagerAdapter);
mSlidingTabLayout.setCustomTabView(R.layout.customtab, R.id.textView1);
mSlidingTabLayout.setSelectedIndicatorColors(R.color.tabIndicator);
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {//change the color of the tab indcator
#Override
public int getIndicatorColor(int position) {
// TODO Auto-generated method stub
return getResources().getColor(R.color.tabIndicator);
}
});
mSlidingTabLayout.setViewPager(mViewPager);
mViewPager.setCurrentItem(tabPosition);
return view;
}
This should work fine. if you find any issue then comment, i will try to reply
You can have a top layout in your activity, in top of the view pager.
In the OnPageSelected of the ViewPager you can change the text color of the top layout to simulate tab selection
I have created a popup and I have set one background of popup.
My xml file is as below. I want to change background of popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pp1" >
<WebView
android:id="#+id/webview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginTop="30dp" />
My showPopup method is as below. So how can I change my popup background? and also I load a webview in popup window.
private void showPopup(final Activity context, Point p) {
int popupWidth = 720;
int popupHeight = 380;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
if (i == 1) {
WebView wv = (WebView) layout.findViewById(R.id.webview1);
wv.loadUrl("file:///android_asset/aboutus.html");
}
/*
* Resources res = getResources(); Drawable drawable =
* res.getDrawable(R.drawable.newImage); LinearLayout linearLayout =
* (LinearLayout)findViewById(R.id.GameLayout);
* linearLayout.setBackgroundDrawable(drawable);
*/
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 130;
int OFFSET_Y = 100;
// Clear the default translucent background
// popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// Getting a reference to Close button, and close the popup when
// clicked.
}
Try this,
Change background from xml file:
in layout tag in your xml file write
android:background="#drawable/image">
and add the image in your drawable folder, make sure the image is of .png format.
Change background at runtime:
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Or
Instead this, you can write your activity name with this
btn.setBackground(MainActivity.this.getResources().getDrawable(R.drawable.yellow_button));
I want to change the action bar background color to a custom color. This is what I currently get:
How do I get this color on the full action bar width?
My action bar xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#0c555c"
android:gravity="center"
android:id="#+id/action_bar_title"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:showAsAction="ifRoom">
</RelativeLayout>
onCreate method:
RelativeLayout actionBar = (RelativeLayout)findViewById(R.id.action_bar_title);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
If you are designing your custom actionBar than write your code like this:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8B58A3")));
LayoutInflater inflater = LayoutInflater.from(this);
#SuppressLint("InflateParams")
View v = inflater.inflate(R.layout.titleview, null);
actionBar.setCustomView(v);
Hope it helps !
please add this line at the end .
Toolbar parent =(Toolbar) actionBar.getParent();
parent.setContentInsetsAbsolute(0,0);
I have a class that programmatically adds buttons to a layout, but I need the buttons that are added to be flush with the bottom of the layout and with eachother - no margins visible.
As you can see in the attached screenshot - there is a margin both below the buttons, between the buttons and to the left and right of the buttons. I need there to be no margins - the buttons should fill the entire lower part of the layout.
Here is my xml for the container layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/someclass_rootlayout"
android:orientation="vertical"
android:background="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="somepackage.someclass"
>
<LinearLayout
android:id="#+id/someclass_buttonlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
/>
</LinearLayout>
And here is my code for adding the buttons programatically in a class that receives as input an array (buttonNames) containing the String names of the buttons to be added. For testing purposes I'm always passing an array of two elements and positioning them to the left and to the right of the layout.
private Window window;
private WindowManager.LayoutParams windowParams;
private int resourceIdentifier = 0;
...
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.someclass_fragment, container, false);
ButterKnife.inject(this, view);
window = getDialog().getWindow();
windowParams = window.getAttributes();
window.requestFeature(Window.FEATURE_NO_TITLE);
someclass_buttonlayout.setWeightSum(buttonNames.size());
for (String string : buttonNames) {
addNewButton(string);
resourceIdentifier++;
}
return view;
}
private void addNewButton(String name) {
Button newbutton = new Button(context);
newbutton.setText(name);
newbutton.setId(resourceIdentifier);
newbutton.setTextColor(getResources().getColor(R.color.black));
newbutton.setMinHeight(0);
newbutton.setMinWidth(0);
newbutton.setShadowLayer(0,0,0,0);
newbutton.setBottom(0);
newbutton.setPadding(0,0,0,0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (resourceIdentifier==0) {
layoutParams.gravity = Gravity.LEFT;
} else {
layoutParams.gravity = Gravity.RIGHT;
}
layoutParams.weight = 1;
newbutton.setLayoutParams(layoutParams);
newbutton.setOnClickListener(this);
newbutton.setTag(name);
someclass_buttonlayout.addView(newbutton);
}
Does anyone have any ideas why despite everything these buttons still have a margin around them, and if there's any way of removing that margin?
You should add background color to the button or buttons while creating the buttons dynamically like this:
newbutton.setBackgroundColor(Color.parseColor("#848482")); //#848482 is dark gray
Set Background color or drawable to your button. That will remove the padding as well as Margins.
I have made a transparent custom progress bar.I want to move the progress bar to a certain location on the screen programatically.But every time the custom progress bar get drawn at the middle of the screen.
public class CustomProgressBar extends Dialog {
ProgressBar mProgressBar;
TextView mTextMessage;
public CustomProgressBar(Context context, int id) {
super(context, R.style.ProgressDialogTheme);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_progree_dialog,
null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);//the progress bar does not get aligned to left top with the defined margin space.
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);//
params.setMargins(50, 50, 0, 0);//
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, id);
this.addContentView(view, params);
}
}
And this is how I am using it inside my activity
Dialog mProgressBar = new CustomProgressBar(
MyActivity.this,
otherView.getId());
My progress bar layout file custom_progree_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ProgressBar
android:id="#+id/id_server_connection_progress_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="#drawable/somedrwablefile" />
<TextView
android:id="#+id/id_server_connection_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/padding_30px"
android:layout_marginTop="#dimen/padding_4px"
android:layout_toRightOf="#id/progress_dialog"
android:text="#string/connectingMessage"
android:textColor="#color/white"
android:textSize="#dimen/padding_40px" />
</RelativeLayout>
Please guide me and let me know where I am going wrong.I have spent two days trying to figure out the problem.
So I solved this problem by making changes to my custom_progress_dialog and aligning the progress bar and the text to the location I want.Also I am not using the custom class any more.I am just inflating the layout at runtime and adding it as a child view inside my activity.
if (mView == null)
{
mView = getLayoutInflater().inflate(R.layout.server_connection_progree_dialog,null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
MyActivity.this.addContentView(mView, layoutParams);
}
else
if (!mView.isShown())
{
mView.setVisibility(View.VISIBLE);
}