I am adding a Progress Bar when the user does a search query, and it will not show up in the screen. Here are the snippets:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button searchButton = (Button) findViewById(R.id.searchButton);
setContentView(R.layout.activity_main); //main layout, which has a child layout called resultsLayout
searchButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
EditText searchString = (EditText) findViewById(R.id.searchString);
search(searchString.getText().toString());
}
});
protected void search(String searchString){
LinearLayout resultLayout = ((LinearLayout) findViewById(R.id.resultsLayout));
resultLayout.addView(DisplayResults.getLoadBar(getApplicationContext()));
//do search
resultLayout.removeAllViews();
}
public static ProgressBar getLoadBar(Context context){
ProgressBar loadBar = new ProgressBar(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
loadBar.setLayoutParams(params);
loadBar.setVisibility(View.VISIBLE);
return loadBar;
}
I know some of it might not be needed, like the setVisibility right when creating, but I have tried several things and it not works. Perhaps there wasn't enough time to show up the bar?(It takes about 1.5sec for the search)
I have also debugged the application, and it shows the ProgressBar as a child of the layout, but I still cannot see it in the screen.
ProgressDialog progressDialog; //Create a prog dialog object
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button searchButton = (Button) findViewById(R.id.searchButton);
progressDialog=new ProgressDialog(this); //init it
searchButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
progressDialog.show(); //show it when needed
EditText searchString = (EditText) findViewById(R.id.searchString);
search(searchString.getText().toString());
}
});
protected void search(String searchString){
progressDialog.dismiss();//dismiss it when the search is complete
LinearLayout resultLayout = ((LinearLayout) findViewById(R.id.resultsLayout));
resultLayout.addView(DisplayResults.getLoadBar(getApplicationContext()));
//do search
resultLayout.removeAllViews();
}
You could try this ProgressDialog object implementation.
https://developer.android.com/reference/android/app/ProgressDialog.html
add setContentView(resultLayout); at the end of search() method
I realized that android does not update the screen for every instruction in the code, apparently it does so only when a function ends. That being said, I had to do my search in a separate thread in order for the loading to appear between the time the searched started and before it ended.
Related
I am trying to save data to a Deque on buttonclick and display the data in a tablelayout. My issue is that every time I trigger the onclick, the Deque loses all previous data. I tried to instantiate the Deque outside the onCreate method and it did not work. I am using a Deque as a stack because I need to display the data LIFO. Any help on this would be very much appreciated. Here is what I've tried so far:
public class MainActivity extends AppCompatActivity {
private Button buttonAdd;
private EditText editText1;
private Deque<String> input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.edit_text_1);
final Deque<String> input = new ArrayDeque<String>();
buttonAdd = (Button) findViewById(R.id.button_add);
buttonAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
String data = editText1.getText().toString();
input.addFirst(data);
Deque<String> inputData = input;
while (!inputData.isEmpty()) {
String s = inputData.removeFirst();
TableRow row = new TableRow(MainActivity.this);
TextView textViewData = new TextView(MainActivity.this);
textViewData.setText(s);
textViewData.setGravity(Gravity.CENTER);
row.addView(textViewData);
table.addView(row);
}
}
});
}
}
The problem is this line:
String s = inputData.removeFirst();
With this operation, you remove (delete) the next entry and since you do it in a loop:
while (!inputData.isEmpty()) {
it simply empties the queue.
What you want is to iterate over the queue without removing anything:
for (String element : inputData) {
textViewData.setText(s);
}
I know this questions already has been asked by some peoples but Still I am getting issue fro last 1 day. I have chat application and My emoji is sent successfully and displaying perfect in Website but In TextView its returning strange characters. I am using this Library for Emoji in TextView
This is Response from Web
data
:
"{"type":"text","message":"\ud83d\ude29","to":"264","from":"175","group_id":"0","groupFlag":0}"
but My TextView showing this Value
ðÿ˜ðÿ˜¡ðÿ˜¡ðÿ˜¡
**My ScreenShot is Here **
Please guide me How can I show Emoji in TextView. ? My complete code is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = findViewById(R.id.root_view);
emojiButton = (ImageView) findViewById(R.id.emoji_btn);
submitButton = (ImageView) findViewById(R.id.submit_btn);
emojiconEditText = (EmojiconEditText) findViewById(R.id.emojicon_edit_text);
textView = (EmojiconTextView) findViewById(R.id.textView);
emojIcon = new EmojIconActions(this, rootView, emojiconEditText, emojiButton);
emojIcon.ShowEmojIcon();
emojIcon.setKeyboardListener(new EmojIconActions.KeyboardListener() {
#Override
public void onKeyboardOpen() {
Log.e("Keyboard", "open");
}
#Override
public void onKeyboardClose() {
Log.e("Keyboard", "close");
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newText = emojiconEditText.getText().toString();
textView.setText(newText);
}
});
}
can you try this may be worked:
emojIcon.setUseSystemEmoji(true);
Perhaps you need you need to convert the Html request to string
Html.fromHtml((String) YOUR_STRING_HERE).toString()
I have LinearLayout1, LinearLayout2 and a Button in MainActivity. When I click the Button, I want it to jump from LinearLayout1 to LinearLayout2. How can I do that?
You can do like this :
LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.linearlayout_1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
}
});
Thanks to all of you specially Mike M
i get my answer with:
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
final LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.liner1);
final LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.liner2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.removeView(button);
mLinearLayout2.addView(button);
}
});
}
}
If you want one of the many different views (not LinearLayouts) to be displayed right on the main activity (may depend on a condition or a state engine or time based interval), you probably can use ViewFlipper.
You can hide and show one or the other, for example:
LinearLayout mLinearLayout1 = (LinearLayout) findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout) findViewById(R.id.linearlayout_1);
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
But If you want to show an hide some views with diifferent behavior I recommend you using Fragments.
OK so here is some code. Seems to have no errors however when run in Android Studio I am getting "Unfortunately, NameOfApplication has stopped". I have no idea what is wrong with it.
public class MainActivity extends Activity implements OnClickListener {
EditText centimeters = (EditText) findViewById(R.id.editCentimeters);
EditText inches = (EditText) findViewById(R.id.editInches);
Button btnConverter = (Button) findViewById(R.id.button);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConverter.setOnClickListener(this);
}
#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 void onClick(View view) {
switch(view.getId()){
case R.id.button:
double c = Double.valueOf(centimeters.getText().toString());
double i = c * 0.393701;
inches.setText(String.valueOf(i));
break;
default:
break;
}
}
}
I have another question as well. It is my beginning with Android and the reference api is quite big so what would you recommend as a best and quickest way to learn it? Get a basic understanding of its structure etc.?
You have to initialise UI elements after calling setContentView() in the onCreate() method like this:
EditText centimeters;
EditText inches;
Button btnConverter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
centimeters = (EditText) findViewById(R.id.editCentimeters);
inches = (EditText) findViewById(R.id.editInches);
btnConverter = (Button) findViewById(R.id.button);
btnConverter.setOnClickListener(this);
}
public class MainActivity extends Activity implements OnClickListener {
//You need the global variables to access the data from onClick, altough you could findViewById inside the onClick itself not to have global variables that exist during the whole activity life.
private EditText centimeters;
private EditText inches;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here you set the view (setContentView(R.layout.activity_main)) so after this line is when you can start looking for your views declared in the xml layout file, "activity_main.xml
setContentView(R.layout.activity_main);
//We can look for the views form here
centimeters = (EditText) findViewById(R.id.editCentimeters);
inches = (EditText) findViewById(R.id.editInches);
Button btnConverter = (Button) findViewById(R.id.button);
btnConverter.setOnClickListener(this);
}
....
The rest the same
I have two different layouts. One is which loads while start of the Activity and the other which loads after running some checks and creates a custom dialog. The Dialog has a button in it to trigger, at this point in time, onclick has a Toast message so I can confirm that the button has been clicked. Unfortunately I can't able to get any response when the button is clicked. I've been all over the web and I can't quite find what I'm missing.
public class myactivity extends Activity{
Dialog accesspopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(cListener);
}
private OnClickListener cListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
};
public void showPopup(){
accesspopup = new Dialog(myactivity.this);
accesspopup.setContentView(R.layout.pop_window);
accesspopup.setCancelable(false);
accesspopup.setTitle("Window Title");
accesspopup.show();
}
I did some more searching around and found that I need to create the OnClickListener inside the method which I am using to build and display the Dialog and not in the OnCreate.
use this way...
public class myactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
});
}
May be still your R.layout.activity_myactivity is the controllable Contentview in your activity.
So you have to define your new layout as setContentView.
or you mentioned it is a Dialog box.
So you can add a content view for a dialog like the following,
Dialog d = new Dialog (this);
d.setContentView(your inflated view);