Why does the WebView scrollbar render as a black rectangle? It's as if the underlying scrollbar graphic isn't being loaded and the entire scrollbar area just ends up being black. I'm loading static HTML. Here's what it looks like:
Here's what I believe it should look like:
Here's the 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=".MainActivity">
<WebView
android:id="#+id/initial_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Here's the MainActivity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String html = "";
for(int i=0; i<250; i++) {
html += "text"+i+" ";
}
WebView wv = (WebView)findViewById(R.id.initial_webview);
wv.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
}
#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_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;
}
return super.onOptionsItemSelected(item);
}
}
I'm using SDK 21 and minSDK 16, as well as a Samsung Galaxy S3 profile with API 17, if that helps.
Related
I have to make a program where what I write in EditText shows up in TextView when I press a button. However when I press the button, the app shuts down. Where am I going wrong? I have tried doing it the way many similar questions' solutions said, but to no use. Please help. PS: I'm a total noob at android programming. Any help would be appreciated.
This is my main class:
public class abc extends Activity implements View.OnClickListener {
EditText editText;
TextView textView;
Button button;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abc);
EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);
button.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.menu_abc, 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;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_button:
name= editText.getText().toString();
textView.setText(name);
break;
}
}
}
This is the xml code:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:text="ABD"
android:id="#+id/text_tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
/>
<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="HERE"
android:id="#+id/btn_button"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ed_edit"/>
</LinearLayout>
You are initializing local variables using findViewById().
Replace :
EditText editText=(EditText)findViewById(R.id.ed_edit);
TextView textView=(TextView)findViewById(R.id.text_tview);
Button button=(Button)findViewById(R.id.btn_button);
With :
editText=(EditText)findViewById(R.id.ed_edit);
textView=(TextView)findViewById(R.id.text_tview);
button=(Button)findViewById(R.id.btn_button);
I am trying to make a shopping cart android app and i want to show the cart with number of items.
attaching the screenshot of app below .
When clicking on the cart icon its does not going any where.. :(.
here is my code.
MainActivity.java
public class PandaActivity extends ActionBarActivity {
TextView notifCount;
RelativeLayout count;
int mNotifCount = 0;
protected ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.topbar));
actionBar.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.backbutton));
}
#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_panda, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.badge) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_panda.xml
<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.sha.netstager.pandafoods.activity.PandaActivity">
<item
android:title="demo"
android:id="#+id/badge"
app:actionLayout="#layout/feed_update_count"
app:showAsAction="always">
</item>
</menu>
feed_update_count.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right">
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="#drawable/cart" />
<!-- Badge Count -->
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="18dp"
android:layout_marginRight="4dp"
android:layout_height="22dp"
android:layout_alignParentRight="true"
android:text="99"
android:textSize="16dp"
android:background="#f7134e"
android:textColor="#fff"/>
</RelativeLayout>
I tried many ways to solve this problem with onclick methord and some others. but dont know what is the error is happening here.
There is no messages or warnings in logcat when I click on the menu item in action bar.
Many Thanks..
Try to add onclick in your onCreateOptionsMenu, instead of current solution as given below.
#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, menu);
RelativeLayout rl_viewBag = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
txt_bagCount = (TextView)rl_viewBag.findViewById(R.id.actionbar_notifcation_textview);
txt_bagCount.setText("10");
rl_viewBag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cartIntent=new Intent(getApplicationContext(),CartShowActivity.class);
startActivity(cartIntent);
}
});
return true;
}
for better look of actionbar use extends AppCompatActivity this is the same look and feel will be provided which is been used by gmail.
I use this method to align action bar title to center
How to make android action bar title center aligned?
But my action bar has menu, and then action bar width is not same to screen
so the title is not exactly center.
Is there other way to center the title?
Code:
actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="YOUR ACTIVITY TITLE"
android:textColor="#000"
android:textSize="24sp" />
</LinearLayout>
the activity which used custom action bar
public class WriteText extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_text);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.write_tag, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Maybe you can change the actionbar xml file to make one your own style of actionbar.
I keep getting a crash when I change my set content view to an xml layout and not sure whats wrong. The program is supposed to have two fragments, a list view on the left and a webview on the right when it is in landscape and just the list in portrait. Sorry for all the code, I just can not figure out what is going on.
Here is my main activity
public class MainActivity extends Activity {
static final String LOGTAG = MainActivity.class.getSimpleName() + "_TAG";
static Resources mRes = null;
static FragmentManager mFrgmntMngr = null;
static MainActivity mThisAct = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager.enableDebugLogging(true);
setContentView(R.layout.activity_main_1view);
mRes = getResources();
mFrgmntMngr = getFragmentManager();
mThisAct = this;
}
static boolean isInLandscapeOrientation() {
return mRes.getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
}
#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);
}
public void displayTwainText(int mCurPosition) {
// TODO Auto-generated method stub
if ( isInLandscapeOrientation() ) {
// Check what fragment is shown, replace if needed.
TwainTitleWebViewFragment tTxtFrgmnt = (TwainTitleWebViewFragment)
mFrgmntMngr.findFragmentById(R.id.twain_title_list);
if (tTxtFrgmnt == null || tTxtFrgmnt.getDisplayedTwainIndex() !=mCurPosition)
// Make new fragment to show this selection.
tTxtFrgmnt = TwainTitleWebViewFragment.newInstance(mCurPosition);
// Execute a transaction, replacing any existing
// fragment inside the frame with the new one.
Log.d(LOGTAG, "about to run FragmentTransaction...");
FragmentTransaction frag_trans
= mFrgmntMngr.beginTransaction();
frag_trans.setCustomAnimations(R.animator.bounce_in_down,
R.animator.fade_out);
frag_trans.setCustomAnimations(R.animator.bounce_in_down,
R.animator.slide_out_right);
frag_trans.replace(R.id.list, tTxtFrgmnt);
frag_trans.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(mThisAct, TwainTitleViewActivity.class);
intent.putExtra(mRes.getString(R.string.twain_index_key), mCurPosition);
this.startActivity(intent);
}
}
}
This is activity_main1view.xml in the lay out folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.hw_07_final_.TitleListFragment"
android:id="#+id/twain_title_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
this is the one in layout-land
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.hw_07_final_.TitleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#00550033"/>
<FrameLayout
android:id="#+id/twain_text"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
this is the log cat
You CANNOT have numbers or uppercase letters when naming your xml files.
activity_main1view.xml should be activity_mainview.xml excluding "1" from the layout name.
Change
<fragment class="com.example.hw_07_final_.TitleListFragment"
To
<fragment android:name="com.example.hw_07_final_.TitleListFragment"
Run your app and it will inflate your fragment.
I'm trying to reproduce the Google Keep, Gmail, etc ActionBar change effects.
Like image bellow, when the user select a note or a mail, the action bar changes using its effects.
I would like to do the same.
Any help?
Looks like it's relatively new ability, called Contextual action mode.
Simply put, it's just specific 'context' menu for long-selected item. I think that the link above provides enough information. However, below is some simple example on how to use it:
main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clipChildren="false"
android:id="#+id/root">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Do Long Press"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
context_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:title="close"
android:showAsAction="always"
android:id="#+id/close_menu" />
</menu>
MyActivity.java:
public class MyActivity extends Activity implements ActionMode.Callback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
startActionMode(MyActivity.this);
return true;
}
});
}
#Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
// TODO: auto-generated block
return false;
}
#Override
public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
switch (item.getItemId()) {
case R.id.close_menu:
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(final ActionMode mode) {
// TODO: auto-generated block
}
}
It looks like the following:
Execute Long press
Please note, that V item is default one and in context_menu.xml I've provided only X item.