pretty easy question here, but I am not a java superuser quite yet.
I am using a ViewFlipper to provide a number of images, and a sliding-drawer to contain text specific to each image. I would like to have the text of the sliding drawer change to a specific string dependent on which child view is currently displayed.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/image1"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/image2"></ImageView>
</ViewFlipper>
</LinearLayout>
here is the java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)findViewById(R.id.flipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}
if (v == previous) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
}
I'm sure the answer is really obvious, but that's why I need your help....THANKS!
When you call showNext() and showPrevious(), update your TextView to match.
Related
My java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
final TextView textView = (TextView) findViewById(R.id.text1);
final Button button = (Button) findViewById(R.id.button1);
button.post(new Runnable() {
#Override
public void run() {
int buttonWidth = button.getWidth();
int textWidth = textView.getWidth();
button.setWidth(buttonWidth-textWidth);
}
});
My xml views:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Timer"
android:textSize="16sp"/>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="+5"/>
I'm using the second way as described in this answer.
What I want to do is have the button fill the entire width with just enough space for the textView. I've been learning Android for the last few months only so it'll be helpful if you could explain in a lucid manner.
you can use a linearlayout with your button having a weight of 1 and your textview to wrap content.
try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text" />
</LinearLayout>
Use the addOnGlobalLayoutListener instead from the solution you linked, that usually works for me when I see this issue.
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT > 16) {
myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
myView.getWidth();
}
});
I have a Toolbar created in Linear Layout associated with a relative layout webview. How to hide this toolbar for specific urls for eg : 'index.php'
My Toolbar code is given below
I have tried many times and cant make it work properly with the given answers so updating the code with the xml layout code.
if (TextUtils.isEmpty(getString(R.string.toolbar))) {
showToolBar = false;
}
if (showToolBar) {
mSearch = (ImageView) findViewById(R.id.search);
mAdd = (ImageView) findViewById(R.id.add);
mProfile= (ImageView) findViewById(R.id.profile);
mHome= (ImageView) findViewById(R.id.home);
mSettings= (ImageView) findViewById(R.id.settings);
// ImageView mRefresh = (ImageView) findViewById(R.id.refresh);
mSettings.setOnClickListener(this);
mHome.setOnClickListener(this);
mProfile.setOnClickListener(this);
mSearch.setOnClickListener(this);
mAdd.setOnClickListener(this);
// mRefresh.setOnClickListener(this);
}
else {
LinearLayout llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
if (llToolbarContainer != null) {
llToolbarContainer.setVisibility(View.GONE);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mAdView.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
}
XML Layout code , please do help
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/webview_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
tools:context=".universalwebview.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</FrameLayout>
<LinearLayout
android:id="#+id/toolbar_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#fff"
android:orientation="horizontal">
<ImageView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_home"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_search"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_add"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_profile"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_settings"
android:tint="#color/tintcolor" />
</LinearLayout>
<ImageView
android:id="#+id/image_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="visible"
android:src="#drawable/splash" />
</RelativeLayout>
In Your MainActivity Code you can't make Your Showtoolbar bool variable false by default.As that is the place where your bottom navigation is actually populating.So it may gives a run time error.Instead of that, make your Linear Layout hide when url contains "index.php".
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(url.contains("index.php")
{
findViewById(R.id.toolbar_footer).setVisibility(View.GONE);
}
else
{
findViewById(R.id.toolbar_footer).setVisibility(View.VISIBLE);
}
}
Call this under your WebViewClient .
You need to create a function , that will hide an tool bar. And create custom WebViewClient for WebView
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (url.contains("index.php")) {
hideToolBar();
}
}
};
Something like this. Not sure that you need onPageFinished, but you can find more info WebViewClient
Get url
Use url.contains("index.php") to judge
set showToolBar = false; or showToolBar = true;
Use if (showToolBar) {} else {}
Try this .
private boolean showToolBar;
private LinearLayout llToolbarContainer;
public void initWebView() {
llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
String url = "your_url";
if (url.contains("index.php")) {
showToolBar = false;
llToolbarContainer.setVisibility(View.GONE);
} else {
showToolBar = true;
llToolbarContainer.setVisibility(View.VISIBLE);
}
mSearch = (ImageView) findViewById(R.id.search);
mAdd = (ImageView) findViewById(R.id.add);
mProfile = (ImageView) findViewById(R.id.profile);
mHome = (ImageView) findViewById(R.id.home);
mSettings = (ImageView) findViewById(R.id.settings);
// ImageView mRefresh = (ImageView) findViewById(R.id.refresh);
mSettings.setOnClickListener(this);
mHome.setOnClickListener(this);
mProfile.setOnClickListener(this);
mSearch.setOnClickListener(this);
mAdd.setOnClickListener(this);
// mRefresh.setOnClickListener(this);
}
I am making a quiz app hence I wanted my MenuButton and NextButton view Invisible at the beginning but after satisfying a particular condition I wanted the View to be visible again hence I used findViewById(R.id.MenuButton).setVisibility(View.VISIBLE); and same for the NextButton but still these view were Invisible.What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<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:layout_gravity="center">
<TextView
android:id="#+id/Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="TextView"
android:gravity="center"
android:textSize="25dp"
android:textColor="#000000"/>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:text="RadioButton1"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="54dp"
android:layout_below="#+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="On_RadioButton1_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="horizontal"
android:layout_marginTop="55dp"
android:layout_below="#+id/radioButton4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/MenuButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:text="menu" />
<Button
android:id="#+id/NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="125dp"
android:text="next" />
</LinearLayout>
</RelativeLayout>
//Java file
private int Question_no=0;
private Boolean Boolean_Var=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
String[] Question_Array = getResources().getStringArray(R.array.Question1);
TextView Questions = (TextView) findViewById(R.id.Question);
Questions.setText(Question_Array[Question_no]);
String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
Radio_Button1.setText(Radio_Button1_Array[Question_no]);
findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);
}
public void On_RadioButton1_Click(View view)
{
if(Boolean_Var==false)
{
String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
String CorrectAns = CorrectAns_Array[Question_no];
String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
String Answer = Answer_Array[Question_no];
if(Answer.equals(CorrectAns))
{
RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
Right_Ans.setTextColor(Color.GREEN);
AnswerSubmitted();
}
else
{
RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
Wrong_Ans.setTextColor(Color.RED);
GreenTick();
AnswerSubmitted();
}
}
Boolean_Var=true;
}
public void AnswerSubmitted()
{
findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}
You should get a reference to your buttons and call setVisibility on the reference. What you're currently doing is setting visibility INVISIBLE on one reference and setting it VISIBLE on another.
Button nextButton;
//in onCreate
nextButton = (Button)findViewById(R.id.nextButton);
nextButton.setVisibility(View.INVISIBLE);
//in AnswerSubmitted
nextButton.setVisibility(View.VISIBLE);
I think you should tell android that you want to execute that method when your radiobutton is clicked. by adding :
Radio_Button1.setOnClickListener(new OnClickListener (){
public void onClick(View v) {
On_RadioButton1_Click(v);
}
});
So you can get that method executed everytime you click.
Add it at the end of OnCreate
I'm trying to make the content of a textview to automatically scroll to the bottom at the most recent text appended. I tried many ways from many post but seems there's no way to make it work. I trie also different layout_wheight and width parameters and so on. The silly thing is that I already made it in the past and it worked, but now using the same set up it is not.
Here's my latest xml code:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.5"
android:weightSum="2">
<ScrollView
android:id="#+id/askScv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<TextView
android:id="#+id/askTxv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:maxLines="99999"
android:scrollbars="vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
<ScrollView
android:id="#+id/answerScv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<TextView
android:id="#+id/answerTxv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:maxLines="99999"
android:scrollbars="vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
</TableRow>
and the Java one:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionEdt = (EditText) findViewById(R.id.questionEdt);
mAnswerTxv = (TextView) findViewById(R.id.answerTxv);
mAnswerTxv.setMovementMethod(new ScrollingMovementMethod());
mAnswerScv=(ScrollView)findViewById(R.id.answerScv);
mAnswerScv.fullScroll(View.FOCUS_DOWN);
mAskTxv=(TextView) findViewById(R.id.askTxv);
mAskTxv.setMovementMethod(new ScrollingMovementMethod());
mAskScv=(ScrollView)findViewById(R.id.askScv);
mAskScv.fullScroll(View.FOCUS_DOWN);
mAskBtn = (ImageButton) findViewById(R.id.askBtn);
mAskBtn.setOnClickListener(this);
mStartBtn = (ImageButton) findViewById(R.id.startBtn);
mStartBtn.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Bundle result;
if (view.equals(mAskBtn)) {
result=mAnsweringMachine.answer(mQuestionEdt.getText().toString());
mAskTxv.append("\n" + mQuestionEdt.getText().toString());
mAskScv.smoothScrollTo(0,mAskTxv.getBottom());
mAnswerTxv.append("\n" + result.getString(AnsweringMachine.DIALOGUE_RESULT));
mAnswerScv.smoothScrollTo(0,mAnswerTxv.getBottom());
} else if (view.equals(mStartBtn)) {
result = mAnsweringMachine.runStartingPrompt();
mAnswerTxv.append(result.getString(AnsweringMachine.DIALOGUE_RESULT));
//mAnswerScv.fullScroll(View.FOCUS_DOWN);
}
}
And here's my old working code, xml:
<ScrollView
android:id="#+id/terminalScv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/terminalTxv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
and Java one:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terminal);
terminalTxv=(TextView) findViewById(R.id.terminalTxv);
terminalScv=(ScrollView) findViewById(R.id.terminalScv);
terminalScv.fullScroll(View.FOCUS_DOWN);
}
#Override
protected void onBtDataReceived(int[] data) {
super.onBtDataReceived(data);
String out=String.valueOf(terminalTxv.getLineCount())+" - ";
for (int x=0;x<data.length-1;x++){
out+=data[x];
out+=" ";
}
out+="\n";
terminalTxv.append(out);
terminalScv.smoothScrollTo(0, terminalTxv.getBottom());
}
Hope somebody can make me understand this enigma....
Thanks!
In the XML for the textView element I would try to add:
android:gravity="bottom"
Also are you sure you need to specify android:scrollbars="vertical" in both scrollview and textview? In addition, I noticed the difference between old working version is that it doesn't uses setMovementMethod() - personally I would use this method without scrollview, but since you already have scroollview I don't see a reason why would you need it...
So I have an application that dynamically adds a table row every time the "Add Row" button is pressed. The code attached is the code that runs when "Add Row" is pressed. What I am trying to do is have 3 EditTexts next to each other on the same row. In order to do that, I need to change the Layout_Width of each EditText so the first EditText doesn't cut the other two off the screen. I can't seem to figure out a way to properly do this and was wondering if anyone would help me out. After I figure out how to do that, the next step is to adjust the layout_width according to the screen size but thats later down the road. It needs to be done programmatically because a user can theoretically have as many rows as they want to.
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
tablerow = new TableRow(getActivity());
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed2 = new EditText(getActivity());
ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
ed3 = new EditText(getActivity());
ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
tablerow.addView(ed1);
tablerow.addView(ed2);
tablerow.addView(ed3);
table1.addView(tablerow);
}
};
I believe what you are looking for is LayoutParams and its "weight" value.
Try:
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed1.setLayoutParams(params);
The third value (1.0f) in the LayoutParams constructor is a weight...all EditTexts in that TableRow should end up the same width.
You can try next approach, it is based on xml. You will be able to play around with layout at table_row.xml.
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.a_mn_button_add_row:
onClickButtonAddRow();
break;
}
}
private void onClickButtonAddRow() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table);
tableLayout.addView(View.inflate(this, R.layout.table_row, null));
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/a_mn_button_add_row"
android:onClick="onClick"
android:text="Add row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/a_mn_table"
android:stretchColumns="0,1,2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
table_row.xml for use case with 3-1-2 proportions.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:layout_weight="1"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="2"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>