How can i show a button as it is pressed whenever the activity is created and UI is shown to user in Android?
This should be okay:
#Override
public void onStart(){
super.onStart();
Button button = (Button) findViewById(R.id.test_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Clicked");
}
});
// This will NOT trigger the onClickListener!
button.setPressed(true);
}
You should use the button selector state like the following code i.e(res/drawable/button.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_add_to_favorites_active">
</item>
<item android:state_focused="true" android:drawable="#drawable/button_add_to_favorites_active">
</item>
<item android:drawable="#drawable/button_add_to_favorites_active"> //Here set the drawable looks like pressed one.
</item>
</selector>
Related
I'm trying to display SplashScreen in my App. The code is the same that all tutorials or documentations but my problem is with icon, i put the default icon from mipmap but it looks like this
when i use any pic or icon is the same, how can i control the size?, because the default icon has the correct size.
The code
xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimaryDark" /><!-- color de fondo -->
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher" /> <!-- imagen centrada -->
</item>
style.xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#mipmap/ic_launcher</item>
</style>
Activity
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
}
Okay, so i got a small problem that i really don't know how to fix this.
So i have an application where i use my own style for an Action Bar
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="android:textColorSecondary">#color/action_bar_bg</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="height">30dp</item>
<item name="background">#color/action_bar_bg</item>
</style>
</resources>
The menu that is displayed
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/sortByStatus"
android:title="#string/sort_by_status"
app:showAsAction="never" />
<item android:id="#+id/sortByName"
android:title="#string/sort_by_name"
app:showAsAction="never" />
<item android:id="#+id/sortByCompany"
android:title="#string/sort_by_company"
app:showAsAction="never" />
</menu>
This works great for almost all places, except when i try to display my options menu. I have added the options menu and the handler for it, but when i click on the options menu then the default top icons (from the notification bar) also shows inside my options menu, and i don't want those to show there. It also starts showing the default bottom navigation buttons for Sony phones.
Enabling the menu from onResume
#Override
public void onResume() {
super.onResume();
AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
if (appCompatActivity != null && appCompatActivity.getSupportActionBar() != null)
appCompatActivity.getSupportActionBar().setTitle(getResources().getString(R.string.presense_status));
setHasOptionsMenu(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and then the menu onCreate
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_presense_status, menu);
}
and then the handler for selected item
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().getFragmentManager().popBackStackImmediate();
break;
case R.id.sortByStatus:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
case R.id.sortByName:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
case R.id.sortByCompany:
if (null != mDataSource) {
// update data list
reloadData();
}
break;
default:
super.onOptionsItemSelected(item);
}
return true;
}
I really can't see where the problem is coming from or how to remove it so that this does not happen. This happens inside a Fragment that is started by my main Activity, meaning i only have one Activity that spawns different fragments for the different in-app tasks.
Any help or thought would be greatly appreciated.
I still don't really know why the notification bar was affected by what i did, but forcing the application to fullscreen in this view stops the problem, so i guess that's an acceptable workaround.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Im trying to do a menu with only one icon, but instead of that icon, i am getting the typican three dots, and inside my option. So, i only want one icon. That is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context="com.bartek.gestionpea.MainActivity">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
app:showAsAction="ifRoom"/>
</menu>
Here goes my class:
public class MainActivity extends Activity {
PeniaSQLiteHelper usdbh;
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setTitle("GESTION");
Button btnCrearPena = (Button) findViewById(R.id.btncrearpena);
btnCrearPena.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CrearNueva.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_principal, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.ayuda:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
You are using some things for the native action bar (e.g., inheriting from Activity) and some things for the appcompat-v7 action bar backport (e.g., app:showAsAction). Choose one and stick with it:
If you wish to use the native action bar, use android:showAsAction
If you wish to use the appcompat-v7 action bar backport, inherit from AppCompatActivity (in the latest appcompat-v7) or ActionBarActivity (for prior versions of appcompat-v7)
Since you use the standard Activity, try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
android:showAsAction="always" />
</menu>
You can modify the app theme (in themes.xml):
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/your_icon_here</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"options"</item>
</style>
More info here
Note that when using appcompat themes, you sometimes need to set xml attibutes twice:
once with and once without the android namespace
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
I am trying to change the background color of a layout on button press and but change back to the original drawable on release.
The following code changes the background resource onclick button which changes the background but on release it stays with the new drawable resource:
btnOne = (Button) findViewById(R.id.btnIcon1);
btnOne.setOnClickListener(oneClick);
btnOne.setOnTouchListener(oneC);
View.OnClickListener oneClick = new View.OnClickListener() {
#SuppressLint("NewApi")
public void onClick(View v) {
editor.putString("AndroidInfo", "1");
editor.commit();
Intent myIntent = new Intent(getApplicationContext(), VersionDetail.class);
startActivityForResult(myIntent, 0);
overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
}
};
View.OnTouchListener oneC = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
rlOne.setBackgroundResource(R.drawable.dateborderclick);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
rlOne.setBackgroundResource(R.drawable.dateborder);
return true; // if you want to handle the touch event
}
return false;
}
};
dateborder xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#CCE5E5E5" />
</shape>
dateborderclick xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#CCD1FFFE" />
</shape>
dateborder is the default background. When the user interacts with the button I want the dateborder to be dateborderclick on press and back to dateborder on release. Based on the code above it should work but now the pressing works but the click doesn't.
You're making harder on yourself than it needs to be. Android has a State List Drawable that takes care of this for you. Here's what one looks like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:dither="true"
android:variablePadding="false">
<item
android:state_selected="true"
android:state_enabled="true"
android:drawable="#drawable/tab_on" />
<item android:drawable="#android:color/transparent" />
</selector>
Basically you define items that are in various states and give them a drawable. The last one if your default state. If you follow the link above you will see all of the different flags you can set to tune it to be exactly what you want. Once you define this drawable just set it as the background of your button.
i want to change icon when click on tabhost
below is the source code
private void setTabs() {
addTab("Home", TabHome.class, R.drawable.home);
addTab("Performers", TabPerformers.class, R.drawable.performers);
addTab("Tickets", TabTickets.class, R.drawable.tickets);
addTab("Info", TabInfo.class, R.drawable.info);
}
private void addTab(String labelId, Class<?> c, int drawableId) {
tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
i want to change tab's icon when user press tab for that i have used below tabhost event
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equals("tabHome")) {
but could not success
there is also a tab_indicater.xml file but from that only background will be change not icon
below is the xml code
<?xml version="1.0" encoding="utf-8"?>
<!-- Non focused states -->
<item android:drawable="#drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_bg_selector" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Focused states -->
<item android:drawable="#drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_bg_selector" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
<!-- Pressed -->
<item android:drawable="#drawable/tab_bg_selector" android:state_pressed="true" android:state_selected="true"/>
<item android:drawable="#drawable/tab_press" android:state_pressed="true"/>
below is the screen shot
when we click on any one tab it's icon should be change like here it must become orange..
can any body help me...
There's no straightforward way to change the TabSpec icon. You have to put your icons in the selector drawables.