I am doing application with TabHost formate, The tab is displaying with Default color, is there any possibility to change default color to our own color.? I got some ideas from google,
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(R.color.transparent); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#000011")); // selected
}
whenever i doing this i am getting force close error. if anyone having idea of changing background color, pls guide me.
You need first to change the default appearence
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if (i == 0) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));
else tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
}
before dealing with the onTabChanged event
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parsecolor("#7392B5")); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#FFFFFF")); // selected
}
});
Create tabs with the view:
final TabHost tabs = getTabHost();
tabs.setup();
final TabHost.TabSpec spec = tabs.newTabSpec("tag");
spec.setIndicator(View.inflate(this, R.layout.tab_view, null));
tabs.addTab(spec);
Define your selector
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" >
<shape>
<gradient
android:endColor="#bbb"
android:centerColor="#999"
android:startColor="#ddd"
android:angle="270" />
<stroke
android:width="3dp" />
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="0.1dp"
android:topRightRadius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_selected="true" >
<shape>
<gradient
android:endColor="#888"
android:centerColor="#777"
android:startColor="#999"
android:angle="270" />
<stroke
android:width="3dp" />
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="0.1dp"
android:topRightRadius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#eee"
android:centerColor="#aaa"
android:endColor="#ccc"
android:angle="270" />
<stroke
android:width="3dp" />
<corners
android:topLeftRadius="10dp"
android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="0.1dp"
android:topRightRadius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
Add your selector as a background drawable to your tab view
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="#dimen/tab_width"
android:layout_height="fill_parent"
android:background="#xml/tabs_background"/>
"#dimen/tab_width" is your tabs width in dp, pixels or smthn.
Related
I am not able to show the shadow of the indicator above the indicatior height within the tabs. i am using the karim library for material tabs,where i apply elevation but that does not help.How can i accheive shadow within the tab?
layout
<io.karim.MaterialTabs
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#android:color/transparent"
android:textColor="#color/white"
app:mtIndicatorColor="#color/colorPrimaryRed"
app:mtIndicatorHeight="3dp"
app:mtPaddingMiddle="false"
app:mtSameWeightTabs="true"
app:mtTabPaddingLeftRight="10dp" />
test drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:left="-11dp" android:right="-11dp" android:top="-11dp">
<shape>
<stroke android:width="10dp" android:color="#color/colorPrimaryRed" />
<gradient android:angle="90" android:endColor="#00fafafa" android:startColor="#88d66f67" android:type="linear" />
<corners android:radius="2dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
java
class StarterMenuAdapter extends FragmentPagerAdapter {
public StarterMenuAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 1) {
LoginFragment loginFragment = new LoginFragment();
return loginFragment;
} else {
RegisterFragment registerFragment = new RegisterFragment();
return registerFragment;
}
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
#Override
public int getCount() {
return CONTENT.length;
}
}
want to achieve
what i got
how can shadow within the tab be achieved?
It is similar as you want,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:left="-8dp" android:right="-8dp" android:top="-8dp">
<shape>
<stroke android:width="7dp" android:color="#color/red" />
<gradient android:angle="90" android:endColor="#00fafafa" android:startColor="#88d66f67" android:type="linear" />
<corners android:radius="2dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
Output
How to create this type of progress bar in android without image? I need to show the gradient effect also. http://goldesel.to/img/loading.gif
Progress Dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.
Create asyncTask class: ShowCustomProgressBarAsyncTask
public class ShowCustomProgressBarAsyncTask extends AsyncTask < Void,Integer,Void > {
int myProgress;
#Override
protected void onPostExecute(Void result) {
textview.setText("Finish work with custom ProgressBar");
button1.setEnabled(true);
progressBar2.setVisibility(View.INVISIBLE);
}
#Override
protected void onPreExecute() {
button1.setEnabled(false);
textview.setText("Start work with custom ProgressBar");
myProgress = 0;
progressBar.setSecondaryProgress(0);
}
#Override
protected Void doInBackground(Void...params) {
while (myProgress < 100) {
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(100);
}
return null;
}#Override
protected void onProgressUpdate(Integer...values) {
progressBar.setProgress(values[0]);
progressBar.setSecondaryProgress(values[0] + 5);
}
}
And in file res/drawable/custom_progress_bar_horizontal.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#ffdddddd"
android:centerY="0.50"
android:endColor="#ffffffff"
android:startColor="#ffffffff" />
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="90"
android:endColor="#771997e1"
android:startColor="#770e75af" />
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="90"
android:endColor="#ff1997e1"
android:startColor="#ff0e75af" />
</shape>
</clip>
</item>
</layer-list>
OUTPUT:
For more Information refer this
I make a circle button with states and I want to add shadow to the button like the default buttons how I can do this? Here is my code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#color/yellow_pressed" />
<stroke android:width="1dp" android:color="#fff" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="oval">
<solid android:color="#color/yellow_pressed" />
<stroke android:width="1dp" android:color="#fff" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/yellow_default" />
<stroke android:width="1dp" android:color="#fff" />
</shape>
</item>
</selector>
Starting from Lollipop, you can use:
public static void setOvalElevationToView(final View view) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
view.setOutlineProvider(new ViewOutlineProvider() {
#TargetApi(VERSION_CODES.LOLLIPOP)
#Override
public void getOutline(View view, Outline outline) {
final int size = view.getWidth();
outline.setOval(0, 0, size, size);
}
});
}
You might also need to disable clipping on the parent of the view, so that the shadows won't be clipped
How to change text position for different button states?
On image you can see how its look like now:
drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="4dp">
<shape android:shape="rectangle">
<solid android:color="#DCDBDB" />
<corners android:radius="7dp" />
<stroke android:width="1dp" android:color="#B1B1B1"/>
</shape>
</item>
<item android:bottom="4.5dp" android:left="1.5dp" android:right="1.5dp" android:top="5.5dp">
<shape android:shape="rectangle" android:gravity="bottom">
<solid android:color="#F2F1F1" />
<corners android:radius="6dp" />
<stroke android:width="1dp" android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#DCDBDB" />
<corners android:radius="7dp" />
<stroke android:width="1dp" android:color="#B1B1B1"/>
</shape>
</item>
<item android:bottom="8.5dp" android:left="1.5dp" android:right="1.5dp" android:top="1.5dp">
<shape android:shape="rectangle">
<solid android:color="#F2F1F1" />
<corners android:radius="6dp" />
<stroke android:width="1dp" android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Few days ago I asked similar question - Android 3D button src padding, but for text I cant use this trick :/ (or dont know how).
You will need to create a custom Button and override the methods for changing it's state in order to customize it's gravity based on the current state.
For example here is the code for a custom Button whose text will jump to the bottom when it is pressed:
public class VariableGravityButton extends Button {
public VariableGravityButton(Context context) {
super(context);
}
public VariableGravityButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VariableGravityButton(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setPressed(boolean pressed) {
if (pressed != isPressed()) {
setGravity(pressed ? Gravity.CENTER_HORIZONTAL |
Gravity.BOTTOM : Gravity.CENTER);
}
super.setPressed(pressed);
}
}
I have a ListActivity that consists of many rows and I'm trying to customize the divider in between rows, so I can give the divider: a corners radius, change its color and apply a border, and so on.
I tried to refer it to a drawable shape layout but it doesn't work; it force closes.
listactivity code :
public class Rows extends ListActivity {
String classes[] = { "row1", "row2", "row3", "row4", "row5"};
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ListView lv = getListView();
lv.setCacheColorHint(0);
lv.setSelector(android.R.color.transparent);
lv.setDivider(new ColorDrawable(this.getResources().getColor(R.drawable.divider)));
lv.setPadding(20, 0, 20, 0);
lv.setVerticalScrollBarEnabled(false);
lv.setBackgroundResource(R.drawable.list_background);
setListAdapter(new MyArrayAdapter(this, classes));
}
divider.xml code :
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="5dp" android:height="5dp" android:color="#B22222" />
<solid android:color="#FFFFFF" />
<padding android:left="10dp" android:right="10dp" />
<corners android:radius="10dp" android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp" android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
What am I doing wrong?
You should not use ColorDrawable in first place. And if your list params are non-conditional why not setting it all in XML directly:
android:divider="#drawable/divider"