How to permanently change a textView using editText from the app - text

I am trying to change the text of a text view to that of the inputted text from the edit text. i can do this easily with setText, yet when i exit and reenter the app, the textView has reset to its original value. I want to permanently change the textview value so when i exit and reenter the app, the textview is the same value i entered in the EditText.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button clicker = (Button) findViewById(R.id.button);
final TextView text = (TextView) findViewById(R.id.textView2);
final EditText input = (EditText) findViewById(R.id.input);
final Editable str = (Editable) input.getText();
clicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
text.setText(str.toString());
}} );

The android framework automatically saves the state of certain views when your activity is destroyed. Unfortunately, TextView is not one of those views (but EditText is).
See this question:
Restoring state of TextView after screen rotation?
You should learn more about the activity lifecycle, specifically about recreating an activity:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Related

How can I change Snackbar background text color in Android Studio?

My Snackbar background displays the default background I've set on the Theme.xml. So, how do I change the background color of the SnackBar?
Any help would be much appreciated.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityNewDeltioBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = binding.tabs;
tabs.setupWithViewPager(viewPager);
FloatingActionButton fab = binding.fab;
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar message = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null);
message.show();
}
});
}
Edit:
I've also tried creating a custom style, but that didn't work as expected. It changed the color of the SnackBar sides, not the background itself...
You might have to try message.setBackgroundColor(colorYouWant).
Also, if you have a colors.xml, I would say to make sure that you're correctly naming the colors.
For more information, you could also check out this answer.

Two differents button for one Animation Drawable

I have a problem with my code I develop a game on android studio and I meet the following concern.
There are two buttons on my app, a Punch button and a Kick Button and an Imageview in which I animate my images.
When I click on the punch button, I create a drawable animation of two images
when I click on the kick button, I create a drawable animation of two images as well.
The problem is that when I execute this code with two drawable animations, the application crashes !!
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private Button btnKick;
private ImageView imageView1 ;
private ImageView imageView2 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackgroundResource(
R.drawable.animationpunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
imageView2 = (ImageView)
findViewById(R.id.imageView2);
imageView2.setBackground
Resource(R.drawable.animatio
nkick);
animationDrawable[1] = (AnimationDrawable)
imageView2.getBackground();
btnPunch.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
});
btnKick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[0].stop()
animationDrawable[1].setOneShot(true);
animationDrawable[1].start();
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
</Code>
I yet use two different imageview for both animations
on the other hand when I remove an animation like the following code, it works well but suddenly I am limited to use the punch button, I would like it to work with both buttons
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private ImageView imageView1 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackground
Resource(R.drawable.animatio
npunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
btnPunch.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
}}
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
click on punch button, it animates my animation unch
I click on kick button, it animates my animation kick
a help?

one tap to display edit text and keyboard android

I'm creating an Android app and i need to add text on a picture.
I need to do a snapchat-like to add my text : I managed to display an editText when you click on the picture but then i have to tap again on the EditText to display the keyboard.
But I need only one click to display the EditText AND the Keyboard.
Thanks for your help.
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText et=(EditText)findViewById(R.id.editText1);
et.setVisibility(View.VISIBLE);
}
});
in case someone is in the same problem, here's the answer :
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText et=(EditText)findViewById(R.id.editText1);
et.setVisibility(View.VISIBLE);
et.setFocusableInTouchMode(true);
imm.showSoftInput(et, 0);
}
});
And for the XML file set the EditText like this :
android:visibility="invisible"

How add custom dynamic layout in a dialog box?

i am new to android java programming. I am adding a custom dynamic layout to a dialog box. i have 2 classes , Mainactivity.java and Getval.java
The Mainactivity.java create a dialoge and Getval generates dynamic layout at run time.
Every thing works good but when it tried to call Getval.java's method, it gives an exception. here is the code of both classes.
The g.setval(); in Mainactivity.java cause problem.
:::::Mainactivity.java:::::
public class MainActivity extends Activity {
private Button button;
public Getval g=new Getval();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.buttonShowCustomDialog);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//set up dialog
g.setval();
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.attributewin);
dialog.setTitle("Attribute Window");
dialog.setCancelable(true);
dialog.show();
}
});
::::Getval::::
public class Getval extends Activity{
String names[]={"test","chaeck","kajsdhasj","dlasdig"};
String values[]={"test","chaeck","kajsdhasj","dlasdig"};
String test[]={"Attribut","Value"};
public void setval(){
TableLayout tl = (TableLayout) findViewById(R.id.tly);
Button b=new Button(this);
for (int i = 0; i < names.length; i++) {
//Row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//TextViews
TextView tv0 = new TextView(this);
tv0.setText(names[i]);
tv0.setMaxWidth(75);
TextView tv1 = new TextView(this);
tv1.setText(values[i]);
tv1.setMaxWidth(150);
View line = new View(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
line.setBackgroundColor(Color.BLACK);
//Setting Views
tr.addView(tv0);
tr.addView(tv1);
tl.addView(tr);
tl.addView(line);
}
//Button Row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
tr.setGravity(0x11);
b.setId(100);
b.setText("Return");
b.setGravity(0x11);
tr.addView(b);
tl.addView(tr);
}
}
Looking over this without seeing your xml or complete code (or exception that is thrown), I would be willing to guess that you are trying to inflate/modify a view (R.id.tly) from an activity other than the one where it was originally inflated from (MainActivity).
Generally speaking, you can't do this without doing some tricks with cross activity handlers or other methods. Perhaps change your code to inflate the views (R.id.tly) in MainActivity (and in your dialog if it is inside of R.layout.attributewin), do your processing in Getval, return them to MainActivity so it can populate the views.
But again, this is just a guess, since I don't see your xml or exception.

SherlockFragmentActivity with SherlockListFragment

I have 3 tabs and all of them has lists inside(different ones of course). When i click on tab I see the proper result(result which i would like to get).
I know how to get the clicked item info and make request to database about that item and get result and create new intent and put that result in it as a extra. BUT i don't know how I can display my result inside the same fragment without creating new intent. Because when i create new intent and show result in new intent my tabs obviously disappears, but i need them always to be there.
My main activity:
public class MainActivity extends SherlockFragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar.newTab()
.setText("Dictionary")
.setTabListener(new DictionaryFragment())
.setIcon(R.drawable.android);
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Play")
.setTabListener(new PlayFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Manage")
.setTabListener(new ManageFragment())
.setIcon(R.drawable.apple);
actionBar.addTab(tab);
}
}
One of my fragments:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
built_in_DB_setup dbOpenHelper = new built_in_DB_setup(getActivity().getBaseContext(), DB_NAME);
database = dbOpenHelper.openDataBase();
fillFreinds();
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, friends);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
And here is the item click listener inside this class
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String a=getData(((TextView) v).getText().toString());
Intent i = new Intent("com.example.fragmenttest.VIEWWORD");
i.putExtra("result", a);
startActivity(i);
}
Ones I have String a which i have got from onListItemClick how can I replace current Tab data with this string?

Resources