I have a toolbar that i created by myself, a custom toolbar and want to place a searchview above so everytime i click the icon it replaces my toolbar.
My Custom toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/emerald">
<ImageView
android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="0"
android:src="#drawable/ic_002_keyboard_left_arrow_button"
android:onClick="previousActivity"
android:tint="#color/white"/>
<com.example.afcosta.inesctec.pt.android.Helpers.NexusBoldTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:layout_gravity="left"
android:layout_marginLeft="25dp"
android:id="#+id/toolbar_title"
android:textSize="#dimen/toolbar_title" />
<ImageView
android:id="#+id/user_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:src="#drawable/ic_user"
android:onClick="userProfile"
android:layout_marginRight="25dp"
android:tint="#color/white"/>
<ImageView
android:id="#+id/user_ic2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:src="#drawable/ic_search_black_24dp"
android:tintMode="#color/white"
android:onClick="userProfile"
android:layout_marginRight="25dp"
android:tint="#color/white"/>
</android.support.v7.widget.Toolbar>
i include it on other activities like this:
<include
layout="#layout/custom_toolbar"
android:id="#+id/my_toolbar"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
now i want to use the searchview on this custom toolbar, but i can't even add it, any help?
Create a menu with item
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
In your activity
#Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.menu_main, menu);
MenuItem search = menu.findItem( R.id.action_search);
final SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do your action
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
Related
I want to change the background of the SearchView in the Appbar which is available default in Android using Java.
The below image shows how my current app bar looks like:
The below image shows what kind of SearchView background I want to achieve (Something similar to this):
The below is the code that I have for the search bar:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
adapter.getFilter2().filter(newText);
return false;
}
});
The xml code is:
<androidx.appcompat.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#1F2D78"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#color/white">
Please help me to figure out how to achieve this
Instead of using menu search view you can use custom edittext inside toolbar.
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/white"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/btnBack"
android:layout_width="22dp"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:layout_marginStart="15dp"
android:src="#drawable/ic_back" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/searchContainer"
android:layout_weight="1"
app:endIconMode="none"
android:layout_width="0dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edtSearch"
android:drawableStart="#drawable/ic_search"
android:drawablePadding="3dp"
android:hint="Search.."
android:paddingStart="7dp"
android:paddingEnd="7dp"
android:inputType="textPersonName"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
and you can customize your TextInputLayout
I'm creating a notes app and I'm using a contextual action bar to delete notes. I have my notes displayed in a card view. I have the action bar working to the point where I can select notes and delete them. But when I select notes I want to use a checkmark as a selector if possible (otherwise any selector) which I have been unsuccessful in achieving.
contextual action bar
mListViewNotes = (ListView) findViewById(R.id.listview_notes);
mListViewNotes.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mListViewNotes.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
CardView cardView = (CardView) findViewById(R.id.cardView);
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (list_items.contains(notes.get(position))) {
count = count-1;
list_items.remove(notes.get(position));
mode.setTitle(count + " Notes Selected");
} else {
count = count+1;
list_items.add(notes.get(position));
mode.setTitle(count + " Notes Selected");
}
if (count == 0) {
mode.setTitle("No Notes Selected");
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
app_bar.setVisibility(View.GONE);
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
actionMode = mode;
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
deleteNote();
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
count = 0;
app_bar.setVisibility(View.VISIBLE);
list_items.clear();
}
});
}
item_note.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/light_sand"
android:orientation="vertical"
android:id="#+id/item_note">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginBottom="6dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="6dp">
<ImageView
android:layout_width="1000dp"
android:layout_height="150dp"
android:layout_gravity="start"
android:layout_marginLeft="-80dp"
android:layout_marginTop="-12dp"
android:background="#drawable/texture_tile_old"
android:scaleType="fitStart" />
<ImageView
android:id="#+id/clef_note"
android:layout_width="70dp"
android:layout_height="120dp"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="#drawable/treble_clef" />
<View
android:layout_width="780dp"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="43dp"
android:background="#color/black" />
<View
android:layout_width="780dp"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="70dp"
android:background="#color/black" />
<View
android:layout_width="780dp"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="97dp"
android:background="#color/black" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scaleType="center">
<TextView
android:id="#+id/list_note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="28sp" />
<TextView
android:id="#+id/list_note_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:layout_marginLeft="65dp"
android:layout_marginTop="0dp"
android:maxLines="2"
android:textColor="#color/gunmetal"
android:textSize="20sp"
android:lineSpacingExtra="4dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/brown"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Last Updated:"
android:textColor="#color/white" />
<TextView
android:id="#+id/list_note_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:gravity="end"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/white" />
</LinearLayout>
</android.support.v7.widget.CardView>
You can add a CheckBox to your item layout, and/or change the background of the layout itself to display a selected state.
With the first option, you can change the layout to the selected state just by setting the isChecked to true.
checkBox.setChecked(true);
To change the background of the layout, you can simply set the background to a new color resource.
itemLayout.setBackgroundResource(R.color.color_selected);
Or you can use a ColorStateList to do some more advanced state changes (such as a ripple effect to a solid color).
ripple_selector.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/color_highlight">
<item android:drawable="#drawable/color_highlight"/>
</ripple>
item_selector_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/color_selected"/>
<item android:drawable="#drawable/ripple_effect"/>
</selector>
To use this, you can set the background attribute in your layout xml to item_selector_background then changed the selected state in your code.
itemLayout.setSelected(true);
MaterialCardView can be a good solution. result
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:strokeWidth="4dp"
app:rippleColor="#color/colorPrimary"
app:strokeColor="#color/colorAccent"
app:cardCornerRadius="#dimen/margin_app"
app:cardBackgroundColor="#android:color/white">
<androidx.constraintlayout.widget.ConstraintLayout
android:paddingVertical="#dimen/margin_large"
android:paddingHorizontal="#dimen/margin_xlarge"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
I have an activity that correctly displays my ActionBar and it's items, however when I try to add a fragment to that activity the ActionBar disappears when that fragment is launched, or if I try to include the ActionBar it shows the bar but no title or MenuOptions.
Want I am trying to achieve is one activity, with the same ActionBar for all fragments, unless specified otherwise.
MainActivity.Java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HomeFragment homeFragment = new HomeFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.main_container, homeFragment)
.addToBackStack(null)
.commit();
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.search_mag_icon){
// Open intent here
Intent i = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
HomeFragment.java
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Book icon and text -->
<ImageView
android:id="#+id/bookIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="110dip"
android:src="#drawable/book" />
<TextView
android:id="#+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bookIcon"
android:layout_marginLeft="20dip"
android:text="#string/bookLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- Barcode icon and text -->
<ImageView
android:id="#+id/barcodeIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="120dip"
android:src="#drawable/barcode" />
<TextView
android:id="#+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/barcodeIcon"
android:layout_marginLeft="260dip"
android:paddingTop="5dip"
android:text="#string/barcodeLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- Message icon and text -->
<ImageView
android:id="#+id/messageIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="350dip"
android:src="#drawable/email" />
<TextView
android:id="#+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messageIcon"
android:layout_marginLeft="55dip"
android:text="#string/messageLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- List icon and text -->
<ImageView
android:id="#+id/listIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="350dip"
android:src="#drawable/list" />
<TextView
android:id="#+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/listIcon"
android:layout_marginLeft="273dip"
android:paddingTop="5dip"
android:text="#string/listLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/app_bar_main" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/colorPrimaryDark"
app:itemTextColor="#FFF"
app:itemIconTint="#FFF"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Book icon and text -->
<ImageView
android:id="#+id/bookIcon"
android:src="#drawable/book"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "110dip"
/>
<TextView
android:id="#+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bookLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="20dip"
android:layout_below="#+id/bookIcon" />
<!-- Barcode icon and text -->
<ImageView
android:id="#+id/barcodeIcon"
android:src="#drawable/barcode"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "120dip"
/>
<TextView
android:id="#+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/barcodeLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_below="#+id/barcodeIcon"
android:paddingTop="5dip"
android:layout_marginLeft="260dip"/>
<!-- Message icon and text -->
<ImageView
android:id="#+id/messageIcon"
android:src="#drawable/email"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="#+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/messageLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="55dip"
android:layout_below="#+id/messageIcon" />
<!-- List icon and text -->
<ImageView
android:id="#+id/listIcon"
android:src="#drawable/list"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="#+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/listLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_below="#+id/listIcon"
android:paddingTop="5dip"
android:layout_marginLeft="273dip"/>
</RelativeLayout>
Thank you for any help in advance.
modify layout
Put you ToolBar inside LinearLayout and Change Fragment with FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/app_bar_main" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
call Fragment from Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment));
fragmentTransaction.commit();
Flash Card App
I have multiple static strings stored on strings.xml and I would like to navigate between them using the prev_Clicked/next_Clicked buttons.
I also would like to be able to store strings for Questions and Answers while the app is running, and access those via prev/next buttons
Thank you for your time, follows my code:
.java
public class MainActivity extends splashActivity {
Button closeButton, addButton, prevButton, nextButton;
TextView QAText, answerText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashcard);
QAText = (TextView) findViewById(R.id.fcfront_textView);
answerText = (TextView) findViewById(R.id.fcfront_textView); //not used anywhere atm
QAText.setOnClickListener(new View.OnClickListener()
{
//function to toggle between Q/A
public void onClick(View v)
{
if(QAText.getText().toString().equals(getString(R.string.fc_front))){
QAText.setText(R.string.fc_back);
Toast.makeText(getApplicationContext(), R.string.fc_front , Toast.LENGTH_LONG).show();
}else{
QAText.setText(R.string.fc_front);
Toast.makeText(getApplicationContext(), "SWAPPING: back-to-front", Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//gets click event directly from xml
public void next_Clicked(View view)
{
//test:
Toast.makeText(getApplicationContext(), "Next button works!!", Toast.LENGTH_LONG).show();
}
public void prev_Clicked(View view)
{
//test:
Toast.makeText(getApplicationContext(), "Prev button works!!", Toast.LENGTH_LONG).show();
}
} //end of main
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity"
android:clickable="true"
android:id="#+id/background"
android:background="#ff80b5ff">
<TextView
android:text="#string/fc_front"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fcfront_textView"
android:layout_centerVertical="true"
android:textSize="30sp"
android:layout_centerHorizontal="true"
android:linksClickable="true"
android:textIsSelectable="false"
android:singleLine="true"
android:onClick="OnClick"
android:clickable="true" />
<!--<TextView-->
<!--android:text="#string/fc_back"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="#+id/fcfront_textView"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:id="#+id/fcback_textView"-->
<!--android:textSize="30sp"-->
<!--android:linksClickable="true"-->
<!--android:textIsSelectable="false"-->
<!--android:singleLine="true"-->
<!--android:clickable="true" />-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_prev"
android:id="#+id/prevbutton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom|center"
android:layout_weight="1"
android:textSize="30sp"
android:layout_alignParentStart="true"
android:onClick="prev_Clicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_next"
android:id="#+id/nextbutton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="bottom|center"
android:layout_weight="1"
android:textSize="30sp"
android:hint="Browse +"
android:onClick="next_Clicked"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:id="#+id/closebutton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:textSize="30sp"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/plusbutton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:textSize="30sp"
android:layout_alignParentStart="true" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CSCE305HW1</string>
<string name="fc_front">FLASHCARD: FRONT</string>
<string name="fc_back">FLASHCARD: BACK</string>
<string name="Q1">Question1</string>
<string name="A1">Answer1</string>
<string name="Q2">Question2</string>
<string name="A2">Answer2 </string>
<string name="action_settings">Nothing here yet</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_goodbye">goodbye</string>
<string name="main_next">Next</string>
<string name="main_prev">Previous</string>
</resources>
define your strings as an array in xml like this :
<string-array name="your_string_array">
<item>string1</item>
<item>string1</item>
</string-array>
and use them as an array in your code like this :
String[] some_array = getResources().getStringArray(R.array.your_string_array);
int index = 0 ;
...
prevButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
...
#Override
public void onClick(View v) {
switch(v.getId) {
case R.id.your_next_button_id :
index++;
break;
case R.id.your_previous_button_id :
index--;
break;
}
yourTextView.setText(some_array[index]);
}
Put the strings in an array. You will be able to use the arrays index to navigate backwards, forwards or jump straight to a given string.
I'm creating a simple paint application that calls the changeColor() method in my main activity through an image button via android:onClick="changeColor" in my layout file. I have the image buttons in my main linear layout and in a layout that is displayed via a dialog box. The 2 layouts have the same code for the image buttons but it is crashing when I try to change color via the layout XML file from the dialog. The error I get says it can not find the changeColor() method. What is the best approach to take to solve my error.
MainActivity.java
public class MainActivity extends ActionBarActivity implements OnClickListener {
private PaintView pView;
private ImageButton currentColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pView = (PaintView) findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
currentColor = (ImageButton) paintLayout.getChildAt(0);
currentColor.setImageDrawable(getResources().getDrawable(
R.drawable.paint_pressed));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.color:
Toast.makeText(getApplicationContext(), "edit selected",
Toast.LENGTH_SHORT).show();
Dialog brushColorDialog = new Dialog(this);
brushColorDialog.setTitle("Brush Color:");
brushColorDialog.setContentView(R.layout.colors);
brushColorDialog.show();
return true;
case R.id.brush:
Dialog brushSizeDialog = new Dialog(this);
brushSizeDialog.setTitle("Brush Size:");
brushSizeDialog.setContentView(R.layout.brush_chooser);
Toast.makeText(getApplicationContext(), "Brush Dialog goes here",
Toast.LENGTH_SHORT).show();
brushSizeDialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void changeColor(View view) {
// use chosen color
if (view != currentColor) {
// update color
ImageButton imgView = (ImageButton) view;
String color = view.getTag().toString();
pView.setColor(color);
imgView.setImageDrawable(getResources().getDrawable(
R.drawable.paint_pressed));
currentColor.setImageDrawable(getResources().getDrawable(
R.drawable.paint));
currentColor = (ImageButton) view;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
colors.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FFFF0000"
android:contentDescription="#string/paint"
android:onClick=""
android:src="#drawable/paint"
android:tag="#FFFF0000" />
<ImageButton
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF800000"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FF800000" />
<ImageButton
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FFFFFF00"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FFFFFF00" />
<ImageButton
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF808000"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FF808000" />
</LinearLayout>
<!-- Bottom Row -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF00FF00"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FF00FF00" />
<ImageButton
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF008000"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FF008000" />
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<net.soloq.paint4.PaintView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<!-- Top Row -->
<LinearLayout
android:id="#+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF660000"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FF660000" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFF0000"
android:contentDescription="#string/paint"
android:onClick="changeColor"
android:src="#drawable/paint"
android:tag="#FFFF0000" />
The image buttons from the activity_main.xml work just fine but it crashes on colors.xml
Thanks in advance!
Remove this line from colors.xml...
android:onClick="changeColor"
And add an id to ImageButton XML as below...
<ImageButton
android:id="#+id/colorImageButton"
android:layout_width="#dimen/large_tile"
android:layout_height="#dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF800000"
android:contentDescription="#string/paint"
android:src="#drawable/paint"
android:tag="#FF800000" />
Then, set OnClickListener as below...
Dialog brushColorDialog = new Dialog(this);
brushColorDialog.setTitle("Brush Color:");
brushColorDialog.setContentView(R.layout.colors);
ImageButton imageBtn = (ImageButton)brushColorDialog.findViewById(R.id.colorImageButton);
imageBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//add your code here
}
});
brushColorDialog.show();
I was facing the same problem, the issue was in the manifast if I'm not mistaken, a theme ref cause the app to search the on click delegate in the theme. Search for a theme ref and delete it if exist.